Skip to content

Commit d0a4c2a

Browse files
committed
Refactor version counts into stats repository
1 parent 8100540 commit d0a4c2a

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

app/main.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
ScheduleRepository,
4141
ScheduleViewRepository,
4242
SettingsRepository,
43+
StatsRepository,
4344
TagRepository,
4445
UpdateStatusRepository,
4546
WebuiUrlRepository,
@@ -543,6 +544,7 @@ def decorated_function(*args, **kwargs):
543544
schedule_view_repo = ScheduleViewRepository(get_db)
544545
host_metrics_repo = HostMetricsRepository(get_db)
545546
login_repo = LoginRepository(get_db)
547+
stats_repo = StatsRepository(get_db)
546548

547549
# Container update management functions
548550
def check_for_update(container, client) -> Tuple[bool, Optional[str], Optional[str], Optional[str]]:
@@ -3768,14 +3770,8 @@ def get_version():
37683770

37693771
# Count schedules and hosts
37703772
try:
3771-
conn = get_db()
3772-
cursor = conn.cursor()
3773-
cursor.execute('SELECT COUNT(*) FROM schedules WHERE enabled = 1')
3774-
active_schedules = cursor.fetchone()[0]
3775-
cursor.execute('SELECT COUNT(*) FROM hosts WHERE enabled = 1')
3776-
active_hosts = cursor.fetchone()[0]
3777-
conn.close()
3778-
except:
3773+
active_schedules, active_hosts = stats_repo.get_active_counts()
3774+
except Exception:
37793775
active_schedules = 0
37803776
active_hosts = 0
37813777

app/repositories/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .schedules import ScheduleRepository
1010
from .schedules_view import ScheduleViewRepository
1111
from .settings import SettingsRepository
12+
from .stats import StatsRepository
1213
from .tags import TagRepository
1314
from .update_status import UpdateStatusRepository
1415
from .users import UserRepository
@@ -26,6 +27,7 @@
2627
'ScheduleRepository',
2728
'ScheduleViewRepository',
2829
'SettingsRepository',
30+
'StatsRepository',
2931
'TagRepository',
3032
'UpdateStatusRepository',
3133
'UserRepository',

app/repositories/stats.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from __future__ import annotations
2+
3+
import sqlite3
4+
from typing import Callable, Tuple
5+
6+
7+
class StatsRepository:
8+
"""Repository for application statistics queries."""
9+
10+
def __init__(self, db_factory: Callable[[], sqlite3.Connection]):
11+
self._db_factory = db_factory
12+
13+
def get_active_counts(self) -> Tuple[int, int]:
14+
conn = self._db_factory()
15+
try:
16+
cursor = conn.cursor()
17+
cursor.execute('SELECT COUNT(*) FROM schedules WHERE enabled = 1')
18+
active_schedules = cursor.fetchone()[0]
19+
cursor.execute('SELECT COUNT(*) FROM hosts WHERE enabled = 1')
20+
active_hosts = cursor.fetchone()[0]
21+
return active_schedules, active_hosts
22+
finally:
23+
conn.close()

0 commit comments

Comments
 (0)