diff --git a/bricktracker/sql.py b/bricktracker/sql.py index f6f478d..e594191 100644 --- a/bricktracker/sql.py +++ b/bricktracker/sql.py @@ -1,7 +1,7 @@ import logging import os import sqlite3 -from typing import Any, Tuple +from typing import Any, Final, Tuple from .sql_stats import BrickSQLStats @@ -9,8 +9,17 @@ from flask import current_app, g from jinja2 import Environment, FileSystemLoader from werkzeug.datastructures import FileStorage +from .sql_counter import BrickCounter + logger = logging.getLogger(__name__) +COUNTERS: Final[list[BrickCounter]] = [ + BrickCounter('Sets', 'sets', icon='hashtag'), + BrickCounter('Minifigures', 'minifigures', icon='group-line'), + BrickCounter('Parts', 'inventory', icon='shapes-line'), + BrickCounter('Missing', 'missing', icon='error-warning-line'), +] + # SQLite3 client with our extra features class BrickSQL(object): @@ -72,6 +81,16 @@ class BrickSQL(object): logger.debug('SQLite3: commit') return self.connection.commit() + # Count the database records + def count_records(self) -> list[BrickCounter]: + for counter in COUNTERS: + record = self.fetchone('schema/count', table=counter.table) + + if record is not None: + counter.count = record['count'] + + return COUNTERS + # Defer a call to execute def defer(self, query: str, parameters: dict[str, Any], /): defer = self.get_defer() @@ -262,20 +281,6 @@ class BrickSQL(object): # Info logger.info('The database has been dropped') - # Count the database records - @staticmethod - def count_records() -> dict[str, int]: - database = BrickSQL() - - counters: dict[str, int] = {} - for table in ['sets', 'minifigures', 'inventory', 'missing']: - record = database.fetchone('schema/count', table=table) - - if record is not None: - counters[table] = record['count'] - - return counters - # Initialize the database @staticmethod def initialize() -> None: diff --git a/bricktracker/sql_counter.py b/bricktracker/sql_counter.py new file mode 100644 index 0000000..77c5dd8 --- /dev/null +++ b/bricktracker/sql_counter.py @@ -0,0 +1,11 @@ +class BrickCounter(object): + name: str + table: str + icon: str + count: int + + def __init__(self, name: str, table: str, /, icon: str = ''): + self.name = name + self.table = table + self.icon = icon + self.count = 0 diff --git a/bricktracker/views/admin.py b/bricktracker/views/admin.py index 5dcb157..86a126b 100644 --- a/bricktracker/views/admin.py +++ b/bricktracker/views/admin.py @@ -23,6 +23,7 @@ from ..part import BrickPart from ..rebrickable_image import RebrickableImage from ..retired_list import BrickRetiredList from ..set import BrickSet +from ..sql_counter import BrickCounter from ..sql import BrickSQL from ..theme_list import BrickThemeList from .upload import upload_helper @@ -51,7 +52,7 @@ def admin() -> str: is_init = BrickSQL.is_init() if is_init: - counters = BrickSQL.count_records() + counters = BrickSQL().count_records() record = BrickSQL().fetchone('missing/count_none') if record is not None: diff --git a/templates/admin/database.html b/templates/admin/database.html index 70f767c..ec64cae 100644 --- a/templates/admin/database.html +++ b/templates/admin/database.html @@ -32,18 +32,15 @@
Records
+
{% endif %}