forked from FrederikBaerentsen/BrickTracker
Change the way the database counters are displayed to easiliy accomodate for more tables
This commit is contained in:
parent
a857a43288
commit
c6e5a6a2d9
@ -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:
|
||||
|
11
bricktracker/sql_counter.py
Normal file
11
bricktracker/sql_counter.py
Normal 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
|
@ -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:
|
||||
|
@ -32,18 +32,15 @@
|
||||
<h5 class="border-bottom">Records</h5>
|
||||
<div class="d-flex justify-content-start">
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item d-flex justify-content-between align-items-start">
|
||||
<span><i class="ri-hashtag"></i> Sets</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counters['sets'] }}</span>
|
||||
</li>
|
||||
<li class="list-group-item d-flex justify-content-between align-items-start">
|
||||
<span><i class="ri-group-line"></i> Minifigures</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counters['minifigures'] }}</span>
|
||||
</li>
|
||||
<li class="list-group-item d-flex justify-content-between align-items-start">
|
||||
<span><i class="ri-shapes-line"></i> Parts</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counters['inventory'] }}</span>
|
||||
</li>
|
||||
<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>
|
||||
{% for counter in counters %}
|
||||
{% if not (loop.index % 5) %}
|
||||
</ul>
|
||||
<ul class="list-group">
|
||||
{% endif %}
|
||||
<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>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
Loading…
Reference in New Issue
Block a user