Take a more generic approach at counting all the tables in the database file

This commit is contained in:
Gregoo 2025-01-24 10:03:53 +01:00
parent 5ea9240f34
commit e2bcd61ace
4 changed files with 53 additions and 18 deletions

View File

@ -20,14 +20,6 @@ G_ENVIRONMENT: Final[str] = 'database_environment'
G_DEFER: Final[str] = 'database_defer' G_DEFER: Final[str] = 'database_defer'
G_STATS: Final[str] = 'database_stats' G_STATS: Final[str] = 'database_stats'
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'),
BrickCounter('Wishlist', 'wishlist', icon='gift-line'),
]
# SQLite3 client with our extra features # SQLite3 client with our extra features
class BrickSQL(object): class BrickSQL(object):
@ -123,7 +115,12 @@ class BrickSQL(object):
# Count the database records # Count the database records
def count_records(self) -> list[BrickCounter]: def count_records(self) -> list[BrickCounter]:
for counter in COUNTERS: counters: list[BrickCounter] = []
# Get all tables
for table in self.fetchall('schema/tables'):
counter = BrickCounter(table['name'])
# Failsafe this one # Failsafe this one
try: try:
record = self.fetchone('schema/count', table=counter.table) record = self.fetchone('schema/count', table=counter.table)
@ -133,7 +130,9 @@ class BrickSQL(object):
except Exception: except Exception:
pass pass
return COUNTERS counters.append(counter)
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], /):

View File

@ -0,0 +1 @@
SELECT "name" FROM "sqlite_master" WHERE type='table' ORDER BY "name" ASC

View File

@ -1,11 +1,46 @@
from typing import Tuple
# Some table aliases to make it look cleaner (id: (name, icon))
ALIASES: dict[str, Tuple[str, str]] = {
'bricktracker_set_checkboxes': ('Checkboxes', 'checkbox-line'),
'bricktracker_set_statuses': ('Bricktracker sets status', 'checkbox-line'),
'bricktracker_sets': ('Bricktracker sets', 'hashtag'),
'bricktracker_wishes': ('Bricktracker wishes', 'gift-line'),
'inventory': ('Parts', 'shapes-line'),
'minifigures': ('Minifigures', 'group-line'),
'missing': ('Missing', 'error-warning-line'),
'rebrickable_sets': ('Rebrickable sets', 'hashtag'),
'sets': ('Sets', 'hashtag'),
'sets_old': ('Sets (legacy)', 'hashtag'),
'wishlist': ('Wishlist', 'gift-line'),
'wishlist_old': ('Wishlist (legacy)', 'gift-line'),
}
class BrickCounter(object): class BrickCounter(object):
name: str name: str
table: str table: str
icon: str icon: str
count: int count: int
def __init__(self, name: str, table: str, /, *, icon: str = ''): def __init__(
self.name = name self,
table: str,
/,
*,
name: str | None = None,
icon: str = 'question-line'
):
self.table = table self.table = table
self.icon = icon
self.count = 0 # Check if there is an alias
if table in ALIASES:
self.name = ALIASES[table][0]
self.icon = ALIASES[table][1]
else:
if name is None:
self.name = table
else:
self.name = name
self.icon = icon

View File

@ -22,13 +22,13 @@
<div class="d-flex justify-content-start"> <div class="d-flex justify-content-start">
<ul class="list-group me-2"> <ul class="list-group me-2">
{% for counter in database_counters %} {% for counter in database_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"> <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-{{ counter.icon }}"></i> {{ counter.name }}</span> <span class="badge text-bg-primary rounded-pill ms-2">{{ counter.count }}</span>
</li> </li>
{% if not (loop.index % 5) %}
</ul>
<ul class="list-group me-2">
{% endif %}
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>