-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #422 from MetOffice/fix-unified-model-oserror-table
Fix unified model oserror table
- Loading branch information
Showing
4 changed files
with
91 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
S3 object health status | ||
""" | ||
import sqlite3 | ||
|
||
|
||
class HealthDB: | ||
"""Maintain meta-data related to S3 objects""" | ||
def __init__(self, connection): | ||
self.connection = connection | ||
self.cursor = self.connection.cursor() | ||
self.cursor.execute(""" | ||
CREATE TABLE | ||
IF NOT EXISTS health ( | ||
id INTEGER PRIMARY KEY, | ||
name TEXT NOT NULL, | ||
errno INTEGER, | ||
strerror TEXT, | ||
time TEXT, | ||
UNIQUE(name)) | ||
""") | ||
|
||
@classmethod | ||
def connect(cls, path_or_memory): | ||
"""Connect to sqlite3 database""" | ||
return cls(sqlite3.connect(path_or_memory)) | ||
|
||
def checked_files(self, pattern): | ||
"""Files that are in the database | ||
:returns files: either successfully processed or marked as OSError | ||
""" | ||
return sorted(set(self.files(pattern)) | | ||
set(self.error_files(pattern))) | ||
|
||
def files(self, pattern): | ||
query = "SELECT name FROM file WHERE name GLOB :pattern;" | ||
params = {"pattern": pattern} | ||
return [path for path, in self.cursor.execute(query, params)] | ||
|
||
def error_files(self, pattern): | ||
query = "SELECT name FROM health WHERE name GLOB :pattern;" | ||
params = {"pattern": pattern} | ||
return [path for path, in self.cursor.execute(query, params)] | ||
|
||
def insert_error(self, path, error, check_time): | ||
"""Insert OSError into table""" | ||
query = """ | ||
INSERT OR IGNORE | ||
INTO health (name, errno, strerror, time) | ||
VALUES (:path, :errno, :strerror, :time); | ||
""" | ||
params = { | ||
"path": path, | ||
"errno": error.errno, | ||
"strerror": error.strerror, | ||
"time": check_time.isoformat() | ||
} | ||
self.cursor.execute(query, params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import sqlite3 | ||
import datetime as dt | ||
import forest.db | ||
import forest.db.health | ||
|
||
|
||
def test_db_health_check(): | ||
"""Database tables to monitor S3 object availability""" | ||
database = forest.db.Database.connect(":memory:") | ||
database.insert_file_name("file.nc") | ||
pattern = "*.nc" | ||
health_db = forest.db.health.HealthDB(database.connection) | ||
assert health_db.checked_files(pattern) == ["file.nc"] | ||
|
||
|
||
def test_db_health_check_mark_oserror(): | ||
"""Database tables to monitor S3 object availability""" | ||
database = forest.db.Database.connect(":memory:") | ||
database.insert_file_name("file-0.nc") | ||
health_db = forest.db.health.HealthDB(database.connection) | ||
health_db.insert_error("file-1.nc", | ||
OSError("Error message"), | ||
dt.datetime(2020, 1, 1)) | ||
pattern = "*.nc" | ||
assert health_db.checked_files(pattern) == ["file-0.nc", "file-1.nc"] |