Store stats in separate table

This commit is contained in:
George Lacey 2021-05-04 04:36:32 +01:00
parent bb2e997705
commit 0adca142c0
4 changed files with 49 additions and 3 deletions

View File

@ -1,3 +1,4 @@
from .databaseconnection import DatabaseConnection
from .repo import Repo
from .archive import Archive
from .stats import Stats

View File

@ -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'])

45
src/database/stats.py Normal file
View File

@ -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

View File

@ -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__":