Store stats in separate table
This commit is contained in:
parent
bb2e997705
commit
0adca142c0
|
@ -1,3 +1,4 @@
|
||||||
from .databaseconnection import DatabaseConnection
|
from .databaseconnection import DatabaseConnection
|
||||||
from .repo import Repo
|
from .repo import Repo
|
||||||
from .archive import Archive
|
from .archive import Archive
|
||||||
|
from .stats import Stats
|
||||||
|
|
|
@ -9,7 +9,6 @@ class Archive(DatabaseConnection):
|
||||||
self.uuid = archive_json['id']
|
self.uuid = archive_json['id']
|
||||||
self.repo_id = repo.repo_id
|
self.repo_id = repo.repo_id
|
||||||
self.name = archive_json['name']
|
self.name = archive_json['name']
|
||||||
print(archive_json['start'])
|
|
||||||
self.start = datetime.fromisoformat(archive_json['start'])
|
self.start = datetime.fromisoformat(archive_json['start'])
|
||||||
self.end = datetime.fromisoformat(archive_json['end'])
|
self.end = datetime.fromisoformat(archive_json['end'])
|
||||||
|
|
||||||
|
|
45
src/database/stats.py
Normal file
45
src/database/stats.py
Normal 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
|
|
@ -1,7 +1,7 @@
|
||||||
from sys import stdin
|
from sys import stdin
|
||||||
from os.path import realpath
|
from os.path import realpath
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from database import Repo, Archive
|
from database import Repo, Archive, Stats
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ def main(input_json: dict, path: Path):
|
||||||
db_path = path / 'borg.sqlite'
|
db_path = path / 'borg.sqlite'
|
||||||
|
|
||||||
repo = Repo(db_path, input_json['repository'])
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user