chore: enforce strict typing and internal naming conventions across web components
This commit is contained in:
@@ -10,9 +10,9 @@ class SQLiteRepository(BaseRepository):
|
||||
self.db_path: str = db_path
|
||||
|
||||
async def initialize(self) -> None:
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
# Logs table
|
||||
await db.execute("""
|
||||
await _db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS logs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
@@ -26,15 +26,15 @@ class SQLiteRepository(BaseRepository):
|
||||
)
|
||||
""")
|
||||
try:
|
||||
await db.execute("ALTER TABLE logs ADD COLUMN fields TEXT")
|
||||
await _db.execute("ALTER TABLE logs ADD COLUMN fields TEXT")
|
||||
except aiosqlite.OperationalError:
|
||||
pass
|
||||
try:
|
||||
await db.execute("ALTER TABLE logs ADD COLUMN msg TEXT")
|
||||
await _db.execute("ALTER TABLE logs ADD COLUMN msg TEXT")
|
||||
except aiosqlite.OperationalError:
|
||||
pass
|
||||
# Users table (internal RBAC)
|
||||
await db.execute("""
|
||||
await _db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
uuid TEXT PRIMARY KEY,
|
||||
username TEXT UNIQUE,
|
||||
@@ -44,19 +44,19 @@ class SQLiteRepository(BaseRepository):
|
||||
)
|
||||
""")
|
||||
try:
|
||||
await db.execute("ALTER TABLE users ADD COLUMN must_change_password BOOLEAN DEFAULT 0")
|
||||
await _db.execute("ALTER TABLE users ADD COLUMN must_change_password BOOLEAN DEFAULT 0")
|
||||
except aiosqlite.OperationalError:
|
||||
pass # Column already exists
|
||||
await db.commit()
|
||||
await _db.commit()
|
||||
|
||||
async def add_log(self, log_data: dict[str, Any]) -> None:
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
timestamp = log_data.get("timestamp")
|
||||
if timestamp:
|
||||
await db.execute(
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
_timestamp: Any = log_data.get("timestamp")
|
||||
if _timestamp:
|
||||
await _db.execute(
|
||||
"INSERT INTO logs (timestamp, decky, service, event_type, attacker_ip, raw_line, fields, msg) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
timestamp,
|
||||
_timestamp,
|
||||
log_data.get("decky"),
|
||||
log_data.get("service"),
|
||||
log_data.get("event_type"),
|
||||
@@ -67,7 +67,7 @@ class SQLiteRepository(BaseRepository):
|
||||
)
|
||||
)
|
||||
else:
|
||||
await db.execute(
|
||||
await _db.execute(
|
||||
"INSERT INTO logs (decky, service, event_type, attacker_ip, raw_line, fields, msg) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
log_data.get("decky"),
|
||||
@@ -79,7 +79,7 @@ class SQLiteRepository(BaseRepository):
|
||||
log_data.get("msg")
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
await _db.commit()
|
||||
|
||||
async def get_logs(
|
||||
self,
|
||||
@@ -87,74 +87,74 @@ class SQLiteRepository(BaseRepository):
|
||||
offset: int = 0,
|
||||
search: Optional[str] = None
|
||||
) -> list[dict[str, Any]]:
|
||||
query: str = "SELECT * FROM logs"
|
||||
params: list[Any] = []
|
||||
_query: str = "SELECT * FROM logs"
|
||||
_params: list[Any] = []
|
||||
if search:
|
||||
query += " WHERE raw_line LIKE ? OR decky LIKE ? OR service LIKE ? OR attacker_ip LIKE ?"
|
||||
like_val = f"%{search}%"
|
||||
params.extend([like_val, like_val, like_val, like_val])
|
||||
_query += " WHERE raw_line LIKE ? OR decky LIKE ? OR service LIKE ? OR attacker_ip LIKE ?"
|
||||
_like_val: str = f"%{search}%"
|
||||
_params.extend([_like_val, _like_val, _like_val, _like_val])
|
||||
|
||||
query += " ORDER BY timestamp DESC LIMIT ? OFFSET ?"
|
||||
params.extend([limit, offset])
|
||||
_query += " ORDER BY timestamp DESC LIMIT ? OFFSET ?"
|
||||
_params.extend([limit, offset])
|
||||
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
db.row_factory = aiosqlite.Row
|
||||
async with db.execute(query, params) as cursor:
|
||||
rows = await cursor.fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
_db.row_factory = aiosqlite.Row
|
||||
async with _db.execute(_query, _params) as _cursor:
|
||||
_rows: list[aiosqlite.Row] = await _cursor.fetchall()
|
||||
return [dict(_row) for _row in _rows]
|
||||
|
||||
async def get_total_logs(self, search: Optional[str] = None) -> int:
|
||||
query: str = "SELECT COUNT(*) as total FROM logs"
|
||||
params: list[Any] = []
|
||||
_query: str = "SELECT COUNT(*) as total FROM logs"
|
||||
_params: list[Any] = []
|
||||
if search:
|
||||
query += " WHERE raw_line LIKE ? OR decky LIKE ? OR service LIKE ? OR attacker_ip LIKE ?"
|
||||
like_val = f"%{search}%"
|
||||
params.extend([like_val, like_val, like_val, like_val])
|
||||
_query += " WHERE raw_line LIKE ? OR decky LIKE ? OR service LIKE ? OR attacker_ip LIKE ?"
|
||||
_like_val: str = f"%{search}%"
|
||||
_params.extend([_like_val, _like_val, _like_val, _like_val])
|
||||
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
db.row_factory = aiosqlite.Row
|
||||
async with db.execute(query, params) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return row["total"] if row else 0
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
_db.row_factory = aiosqlite.Row
|
||||
async with _db.execute(_query, _params) as _cursor:
|
||||
_row: Optional[aiosqlite.Row] = await _cursor.fetchone()
|
||||
return _row["total"] if _row else 0
|
||||
|
||||
async def get_stats_summary(self) -> dict[str, Any]:
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
db.row_factory = aiosqlite.Row
|
||||
async with db.execute("SELECT COUNT(*) as total_logs FROM logs") as cursor:
|
||||
row = await cursor.fetchone()
|
||||
total_logs: int = row["total_logs"] if row else 0
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
_db.row_factory = aiosqlite.Row
|
||||
async with _db.execute("SELECT COUNT(*) as total_logs FROM logs") as _cursor:
|
||||
_row: Optional[aiosqlite.Row] = await _cursor.fetchone()
|
||||
_total_logs: int = _row["total_logs"] if _row else 0
|
||||
|
||||
async with db.execute("SELECT COUNT(DISTINCT attacker_ip) as unique_attackers FROM logs") as cursor:
|
||||
row = await cursor.fetchone()
|
||||
unique_attackers: int = row["unique_attackers"] if row else 0
|
||||
async with _db.execute("SELECT COUNT(DISTINCT attacker_ip) as unique_attackers FROM logs") as _cursor:
|
||||
_row = await _cursor.fetchone()
|
||||
_unique_attackers: int = _row["unique_attackers"] if _row else 0
|
||||
|
||||
async with db.execute("SELECT COUNT(DISTINCT decky) as active_deckies FROM logs") as cursor:
|
||||
row = await cursor.fetchone()
|
||||
active_deckies: int = row["active_deckies"] if row else 0
|
||||
async with _db.execute("SELECT COUNT(DISTINCT decky) as active_deckies FROM logs") as _cursor:
|
||||
_row = await _cursor.fetchone()
|
||||
_active_deckies: int = _row["active_deckies"] if _row else 0
|
||||
|
||||
return {
|
||||
"total_logs": total_logs,
|
||||
"unique_attackers": unique_attackers,
|
||||
"active_deckies": active_deckies
|
||||
"total_logs": _total_logs,
|
||||
"unique_attackers": _unique_attackers,
|
||||
"active_deckies": _active_deckies
|
||||
}
|
||||
|
||||
async def get_user_by_username(self, username: str) -> Optional[dict[str, Any]]:
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
db.row_factory = aiosqlite.Row
|
||||
async with db.execute("SELECT * FROM users WHERE username = ?", (username,)) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return dict(row) if row else None
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
_db.row_factory = aiosqlite.Row
|
||||
async with _db.execute("SELECT * FROM users WHERE username = ?", (username,)) as _cursor:
|
||||
_row: Optional[aiosqlite.Row] = await _cursor.fetchone()
|
||||
return dict(_row) if _row else None
|
||||
|
||||
async def get_user_by_uuid(self, uuid: str) -> Optional[dict[str, Any]]:
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
db.row_factory = aiosqlite.Row
|
||||
async with db.execute("SELECT * FROM users WHERE uuid = ?", (uuid,)) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return dict(row) if row else None
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
_db.row_factory = aiosqlite.Row
|
||||
async with _db.execute("SELECT * FROM users WHERE uuid = ?", (uuid,)) as _cursor:
|
||||
_row: Optional[aiosqlite.Row] = await _cursor.fetchone()
|
||||
return dict(_row) if _row else None
|
||||
|
||||
async def create_user(self, user_data: dict[str, Any]) -> None:
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
await db.execute(
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
await _db.execute(
|
||||
"INSERT INTO users (uuid, username, password_hash, role, must_change_password) VALUES (?, ?, ?, ?, ?)",
|
||||
(
|
||||
user_data["uuid"],
|
||||
@@ -164,12 +164,12 @@ class SQLiteRepository(BaseRepository):
|
||||
user_data.get("must_change_password", False)
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
await _db.commit()
|
||||
|
||||
async def update_user_password(self, uuid: str, password_hash: str, must_change_password: bool = False) -> None:
|
||||
async with aiosqlite.connect(self.db_path) as db:
|
||||
await db.execute(
|
||||
async with aiosqlite.connect(self.db_path) as _db:
|
||||
await _db.execute(
|
||||
"UPDATE users SET password_hash = ?, must_change_password = ? WHERE uuid = ?",
|
||||
(password_hash, must_change_password, uuid)
|
||||
)
|
||||
await db.commit()
|
||||
await _db.commit()
|
||||
|
||||
Reference in New Issue
Block a user