This commit is contained in:
2025-01-11 20:59:10 -05:00
parent 85a0d6bc62
commit 3c57f13557
18 changed files with 464 additions and 365 deletions

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env python3.12
# pylint: disable=bare-except, broad-exception-raised
"""Global State Storage/Counters"""
@@ -11,7 +12,9 @@ from fastapi_utils.tasks import repeat_every
class State(FastAPI):
def __init__(self, app: FastAPI, util, constants):
"""Global State for API"""
def __init__(self, app: FastAPI, util, constants): # pylint: disable=unused-argument
super().__init__()
self.counter_db_path = os.path.join("/", "var", "lib", "singerdbs", "stats.db")
self.counters = {
str(counter): 0 for counter in constants.AVAILABLE_COUNTERS
@@ -53,7 +56,7 @@ class State(FastAPI):
@app.on_event("startup")
@repeat_every(seconds=10)
async def update_db():
if self.counters_initialized == False:
if not self.counters_initialized:
logging.debug("[State] TICK: Counters not yet initialized")
return
@@ -92,21 +95,20 @@ class State(FastAPI):
async def increment_counter(self, counter: str):
if not(counter in self.counters.keys()):
raise BaseException("[State] Counter %s does not exist", counter)
"""Increment Counter"""
if not counter in self.counters.keys():
raise BaseException(f"[State] Counter {counter} does not exist")
self.counters[counter] += 1
return True
async def get_counter(self, counter: str):
if not(counter in self.counters.keys()):
raise BaseException("[State] Counter %s does not exist", counter)
"""Get Counter"""
if not counter in self.counters.keys():
raise BaseException(f"[State] Counter {counter} does not exist")
return self.counters[counter]
async def get_all_counters(self):
return self.counters
"""Get All Counters"""
return self.counters