Change the way the database counters are displayed to easiliy accomodate for more tables

This commit is contained in:
Gregoo 2025-01-20 16:36:31 +01:00
parent a857a43288
commit c6e5a6a2d9
4 changed files with 42 additions and 28 deletions

View File

@ -1,7 +1,7 @@
import logging import logging
import os import os
import sqlite3 import sqlite3
from typing import Any, Tuple from typing import Any, Final, Tuple
from .sql_stats import BrickSQLStats from .sql_stats import BrickSQLStats
@ -9,8 +9,17 @@ from flask import current_app, g
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from werkzeug.datastructures import FileStorage from werkzeug.datastructures import FileStorage
from .sql_counter import BrickCounter
logger = logging.getLogger(__name__) 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 # SQLite3 client with our extra features
class BrickSQL(object): class BrickSQL(object):
@ -72,6 +81,16 @@ class BrickSQL(object):
logger.debug('SQLite3: commit') logger.debug('SQLite3: commit')
return self.connection.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 # Defer a call to execute
def defer(self, query: str, parameters: dict[str, Any], /): def defer(self, query: str, parameters: dict[str, Any], /):
defer = self.get_defer() defer = self.get_defer()
@ -262,20 +281,6 @@ class BrickSQL(object):
# Info # Info
logger.info('The database has been dropped') 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 # Initialize the database
@staticmethod @staticmethod
def initialize() -> None: def initialize() -> None:

View File

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

View File

@ -23,6 +23,7 @@ from ..part import BrickPart
from ..rebrickable_image import RebrickableImage from ..rebrickable_image import RebrickableImage
from ..retired_list import BrickRetiredList from ..retired_list import BrickRetiredList
from ..set import BrickSet from ..set import BrickSet
from ..sql_counter import BrickCounter
from ..sql import BrickSQL from ..sql import BrickSQL
from ..theme_list import BrickThemeList from ..theme_list import BrickThemeList
from .upload import upload_helper from .upload import upload_helper
@ -51,7 +52,7 @@ def admin() -> str:
is_init = BrickSQL.is_init() is_init = BrickSQL.is_init()
if is_init: if is_init:
counters = BrickSQL.count_records() counters = BrickSQL().count_records()
record = BrickSQL().fetchone('missing/count_none') record = BrickSQL().fetchone('missing/count_none')
if record is not None: if record is not None:

View File

@ -32,18 +32,15 @@
<h5 class="border-bottom">Records</h5> <h5 class="border-bottom">Records</h5>
<div class="d-flex justify-content-start"> <div class="d-flex justify-content-start">
<ul class="list-group"> <ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-start"> {% for counter in counters %}
<span><i class="ri-hashtag"></i> Sets</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counters['sets'] }}</span> {% if not (loop.index % 5) %}
</li> </ul>
<li class="list-group-item d-flex justify-content-between align-items-start"> <ul class="list-group">
<span><i class="ri-group-line"></i> Minifigures</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counters['minifigures'] }}</span> {% endif %}
</li> <li class="list-group-item d-flex justify-content-between align-items-start">
<li class="list-group-item d-flex justify-content-between align-items-start"> <span><i class="ri-{{ counter.icon }}"></i> {{ counter.name }}</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counter.count }}</span>
<span><i class="ri-shapes-line"></i> Parts</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counters['inventory'] }}</span> </li>
</li> {% endfor %}
<li class="list-group-item d-flex justify-content-between align-items-start">
<span><i class="ri-error-warning-line"></i> Missing</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counters['missing'] }}</span>
</li>
</ul> </ul>
</div> </div>
{% endif %} {% endif %}