From 0adca142c0683708b4ce29fe599b5db62f78daed Mon Sep 17 00:00:00 2001 From: George Lacey Date: Tue, 4 May 2021 04:36:32 +0100 Subject: [PATCH] Store stats in separate table --- src/database/__init__.py | 1 + src/database/archive.py | 1 - src/database/stats.py | 45 ++++++++++++++++++++++++++++++++++++++++ src/main.py | 5 +++-- 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/database/stats.py diff --git a/src/database/__init__.py b/src/database/__init__.py index 8982607..011f947 100644 --- a/src/database/__init__.py +++ b/src/database/__init__.py @@ -1,3 +1,4 @@ from .databaseconnection import DatabaseConnection from .repo import Repo from .archive import Archive +from .stats import Stats diff --git a/src/database/archive.py b/src/database/archive.py index 73a3686..2d9cd3d 100644 --- a/src/database/archive.py +++ b/src/database/archive.py @@ -9,7 +9,6 @@ class Archive(DatabaseConnection): self.uuid = archive_json['id'] self.repo_id = repo.repo_id self.name = archive_json['name'] - print(archive_json['start']) self.start = datetime.fromisoformat(archive_json['start']) self.end = datetime.fromisoformat(archive_json['end']) diff --git a/src/database/stats.py b/src/database/stats.py new file mode 100644 index 0000000..2eb8532 --- /dev/null +++ b/src/database/stats.py @@ -0,0 +1,45 @@ +from . import DatabaseConnection, Repo, Archive + + +class Stats(DatabaseConnection): + def __init__(self, db_path, repo: Repo, archive: Archive, stats_json: dict, table_name: str = "stats"): + super().__init__(db_path, table_name) + + self.stat_id = None + self.repo_id = repo.repo_id + self.archive_id = archive.archive_id + self.file_count = stats_json['nfiles'] + self.original_size = stats_json['original_size'] + self.compressed_size = stats_json['compressed_size'] + self.deduplicated_size = stats_json['deduplicated_size'] + + self.stat_id = self._insert() + + def _create_table(self): + create_statement = f"create table if not exists {self._sql_table}(" \ + f"stat_id INTEGER PRIMARY KEY," \ + f"repo_id INTEGER NOT NULL," \ + f"archive_id INTEGER NOT NULL," \ + f"file_count INTEGER NOT NULL UNIQUE," \ + f"original_size INTEGER NOT NULL UNIQUE," \ + f"compressed_size INTEGER NOT NULL UNIQUE," \ + f"deduplicated_size INTEGER NOT NULL UNIQUE," \ + f"FOREIGN KEY (repo_id) REFERENCES repo (repo_id)," \ + f"FOREIGN KEY (archive_id) REFERENCES archive (archive_id))" + self.sql_execute(create_statement) + + def _exists(self): + return False + + def _insert(self) -> int: + with self.sql_lock: + cursor = self.sql_cursor + statement = f"INSERT INTO {self._sql_table}"\ + f" ('repo_id', 'archive_id', 'file_count', 'original_size'," \ + f"'compressed_size', 'deduplicated_size')"\ + f" VALUES (?, ?, ?, ?, ?, ?);" + args = (self.repo_id, self.archive_id, self.file_count, self.original_size, + self.compressed_size, self.deduplicated_size) + cursor.execute(statement, args) + self.sql_commit() + return cursor.lastrowid diff --git a/src/main.py b/src/main.py index 4b7206a..04e5289 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,7 @@ from sys import stdin from os.path import realpath from pathlib import Path -from database import Repo, Archive +from database import Repo, Archive, Stats import json @@ -9,7 +9,8 @@ def main(input_json: dict, path: Path): db_path = path / 'borg.sqlite' repo = Repo(db_path, input_json['repository']) - log_entry = Archive(db_path, repo, input_json['archive']) + archive = Archive(db_path, repo, input_json['archive']) + stats = Stats(db_path, repo, archive, input_json['archive']['stats']) if __name__ == "__main__":