From f5a556d06e8390ec9b0fdfa8e9680d984d9adfd1 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 5 May 2021 18:01:13 +0100 Subject: [PATCH] Implement repo error retrieval --- src/borgmanager/database/borgdatabase.py | 10 ++++++++- .../database/connection/errorconn.py | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/borgmanager/database/borgdatabase.py b/src/borgmanager/database/borgdatabase.py index 0764ade..9b3618a 100644 --- a/src/borgmanager/database/borgdatabase.py +++ b/src/borgmanager/database/borgdatabase.py @@ -1,5 +1,5 @@ from .connection import RepoConn, ArchiveConn, ErrorConn, LabelConn, CacheConn -from .object import Repo, Archive, Label +from .object import Repo, Archive, Label, Error from .object.label import Label from pathlib import Path @@ -51,4 +51,12 @@ class BorgDatabase(object): archive = self.archive_conn.get_latest(repo.primary_key) return self.cache_conn.get(archive.primary_key) + def get_repo_errors(self, repo_id): + label = self.label_conn.get_label_id(repo_id) + return self.error_conn.get_repo_errors(label) + + def get_recent_repo_errors(self, repo_id, days=7): + label = self.label_conn.get_label_id(repo_id) + return self.error_conn.get_recent_repo_errors(label, days) + # endregion diff --git a/src/borgmanager/database/connection/errorconn.py b/src/borgmanager/database/connection/errorconn.py index 1e72ed5..3e8d29a 100644 --- a/src/borgmanager/database/connection/errorconn.py +++ b/src/borgmanager/database/connection/errorconn.py @@ -8,6 +8,8 @@ class ErrorConn(DatabaseConnection): self.label_table = label_table super().__init__(db_path, Error, table_name) + # region INIT + def _create_table(self): create_statement = f"create table if not exists {self._sql_table}(" \ f"id INTEGER PRIMARY KEY," \ @@ -18,6 +20,10 @@ class ErrorConn(DatabaseConnection): f" {self.label_table} (id));" self.sql_execute(create_statement) + # endregion + + # region INSERT + def _exists(self, record, repo_id=None, archive_id=None, label_id=None): return None, None @@ -33,3 +39,18 @@ class ErrorConn(DatabaseConnection): cursor.execute(statement, args) self.sql_commit() return cursor.lastrowid + + # endregion + + # region QUERIES + + def get_repo_errors(self, label_id): + result = self.sql_execute_all(f"SELECT * FROM {self._sql_table} WHERE label_id = ?;", (label_id,)) + return [Error.from_sql(row) for row in result] + + def get_recent_repo_errors(self, label_id, days): + result = self.sql_execute_all(f"SELECT * FROM {self._sql_table} WHERE label_id = ?" + f" AND date(time) > date('now', '-{days} days');", (label_id,)) + return [Error.from_sql(row) for row in result] + + # endregion