more AI optimizations, reworked logger
This commit is contained in:
@@ -3,7 +3,6 @@ import json
|
||||
import os
|
||||
import re
|
||||
import sqlite3 as sql
|
||||
import sys
|
||||
import tempfile
|
||||
from dataclasses import asdict
|
||||
from pathlib import Path
|
||||
@@ -13,7 +12,7 @@ from typing import Any, List, Optional, Tuple, Union
|
||||
|
||||
import loguru
|
||||
|
||||
from src import DATABASE_DIR, LOG_DIR, settings
|
||||
from src import DATABASE_DIR, settings
|
||||
from src.backend.db import (
|
||||
CREATE_ELSA_FILES_TABLE,
|
||||
CREATE_ELSA_MEDIA_TABLE,
|
||||
@@ -34,9 +33,6 @@ from src.logic.semester import Semester
|
||||
from src.utils.blob import create_blob
|
||||
|
||||
log = loguru.logger
|
||||
log.remove()
|
||||
log.add(sys.stdout, level="INFO")
|
||||
log.add(f"{LOG_DIR}/application.log", rotation="1 MB", retention="10 days")
|
||||
|
||||
|
||||
ascii_lowercase = lower + digits + punctuation
|
||||
@@ -186,7 +182,13 @@ class Database:
|
||||
Returns:
|
||||
sql.Connection: The active connection to the database
|
||||
"""
|
||||
return sql.connect(self.db_path)
|
||||
conn = sql.connect(self.db_path)
|
||||
# Fast pragmas suitable for a desktop app DB
|
||||
conn.execute("PRAGMA journal_mode=WAL;")
|
||||
conn.execute("PRAGMA synchronous=NORMAL;")
|
||||
conn.execute("PRAGMA temp_store=MEMORY;")
|
||||
conn.execute("PRAGMA mmap_size=134217728;") # 128MB
|
||||
return conn
|
||||
|
||||
def close_connection(self, conn: sql.Connection):
|
||||
"""
|
||||
@@ -214,6 +216,25 @@ class Database:
|
||||
cursor.execute(CREATE_ELSA_TABLE)
|
||||
cursor.execute(CREATE_ELSA_FILES_TABLE)
|
||||
cursor.execute(CREATE_ELSA_MEDIA_TABLE)
|
||||
# Helpful indices to speed up frequent lookups and joins
|
||||
cursor.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_media_app_prof ON media(app_id, prof_id);"
|
||||
)
|
||||
cursor.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_media_deleted ON media(deleted);"
|
||||
)
|
||||
cursor.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_media_available ON media(available);"
|
||||
)
|
||||
cursor.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_messages_remind_at ON messages(remind_at);"
|
||||
)
|
||||
cursor.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_semesterapparat_prof ON semesterapparat(prof_id);"
|
||||
)
|
||||
cursor.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_semesterapparat_appnr ON semesterapparat(appnr);"
|
||||
)
|
||||
conn.commit()
|
||||
self.close_connection(conn)
|
||||
|
||||
@@ -227,7 +248,7 @@ class Database:
|
||||
"""
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
log.debug(f"Inserting {params} into database with query {query}")
|
||||
log.debug(f"Inserting into DB: {query}")
|
||||
cursor.execute(query, params)
|
||||
conn.commit()
|
||||
self.close_connection(conn)
|
||||
@@ -1650,7 +1671,7 @@ class Database:
|
||||
tempdir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
file = tempfile.NamedTemporaryFile(
|
||||
delete=False, dir=tempdir_path, mode="wb", suffix=f".{filetype}"
|
||||
delete=False, dir=tempdir, mode="wb", suffix=f".{filetype}"
|
||||
)
|
||||
file.write(blob)
|
||||
# log.debug("file created")
|
||||
@@ -1713,9 +1734,9 @@ class Database:
|
||||
telnr = profdata.telnr
|
||||
title = profdata.title
|
||||
|
||||
query = f"INSERT INTO prof (fname, lname, fullname, mail, telnr,titel) VALUES ('{fname}','{lname}','{fullname}','{mail}','{telnr}','{title}')"
|
||||
query = "INSERT INTO prof (fname, lname, fullname, mail, telnr, titel) VALUES (?,?,?,?,?,?)"
|
||||
log.debug(query)
|
||||
cursor.execute(query)
|
||||
cursor.execute(query, (fname, lname, fullname, mail, telnr, title))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@@ -1758,10 +1779,10 @@ class Database:
|
||||
fullname = profdata["profname"]
|
||||
else:
|
||||
fullname = profdata.name()
|
||||
query = f"SELECT id FROM prof WHERE fullname = '{fullname}'"
|
||||
query = "SELECT id FROM prof WHERE fullname = ?"
|
||||
log.debug(query)
|
||||
|
||||
cursor.execute(query)
|
||||
cursor.execute(query, (fullname,))
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
return result[0]
|
||||
@@ -1776,10 +1797,10 @@ class Database:
|
||||
"""
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
query = f"SELECT * FROM prof WHERE fullname = '{fullname}'"
|
||||
query = "SELECT * FROM prof WHERE fullname = ?"
|
||||
log.debug(query)
|
||||
|
||||
result = cursor.execute(query).fetchone()
|
||||
result = cursor.execute(query, (fullname,)).fetchone()
|
||||
if result:
|
||||
return Prof().from_tuple(result)
|
||||
else:
|
||||
@@ -1795,8 +1816,8 @@ class Database:
|
||||
|
||||
int | None: The id of the prof or None if not found
|
||||
"""
|
||||
query = f"SELECT prof_id from semesterapparat WHERE appnr = '{apprarat_id}' and deletion_status = 0"
|
||||
data = self.query_db(query)
|
||||
query = "SELECT prof_id from semesterapparat WHERE appnr = ? and deletion_status = 0"
|
||||
data = self.query_db(query, (apprarat_id,))
|
||||
if data:
|
||||
log.info("Prof ID: " + str(data[0][0]))
|
||||
return data[0][0]
|
||||
@@ -1807,20 +1828,13 @@ class Database:
|
||||
# get book data
|
||||
new_apparat_id = apparat
|
||||
new_prof_id = self.getProfIDByApparat(new_apparat_id)
|
||||
query = f"""
|
||||
INSERT INTO media (bookdata, app_id, prof_id, deleted, available, reservation)
|
||||
SELECT
|
||||
bookdata,
|
||||
'{new_apparat_id}',
|
||||
'{new_prof_id}',
|
||||
0,
|
||||
available,
|
||||
reservation
|
||||
FROM media
|
||||
where id = '{book_id}'"""
|
||||
query = (
|
||||
"INSERT INTO media (bookdata, app_id, prof_id, deleted, available, reservation) "
|
||||
"SELECT bookdata, ?, ?, 0, available, reservation FROM media WHERE id = ?"
|
||||
)
|
||||
connection = self.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(query)
|
||||
cursor.execute(query, (new_apparat_id, new_prof_id, book_id))
|
||||
connection.commit()
|
||||
connection.close()
|
||||
|
||||
@@ -1832,16 +1846,18 @@ class Database:
|
||||
appratat (int): the ID of the new apparat
|
||||
"""
|
||||
# get book data
|
||||
query = f"UPDATE media SET app_id = '{appratat}' WHERE id = '{book_id}'"
|
||||
query = "UPDATE media SET app_id = ? WHERE id = ?"
|
||||
connection = self.connect()
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(query)
|
||||
cursor.execute(query, (appratat, book_id))
|
||||
connection.commit()
|
||||
connection.close()
|
||||
|
||||
def getApparatNameByAppNr(self, appnr: int):
|
||||
query = f"SELECT name FROM semesterapparat WHERE appnr = '{appnr}' and deletion_status = 0"
|
||||
data = self.query_db(query)
|
||||
query = (
|
||||
"SELECT name FROM semesterapparat WHERE appnr = ? and deletion_status = 0"
|
||||
)
|
||||
data = self.query_db(query, (appnr,))
|
||||
if data:
|
||||
return data[0][0]
|
||||
else:
|
||||
@@ -1856,8 +1872,8 @@ class Database:
|
||||
return result
|
||||
|
||||
def getBookIdByPPN(self, ppn: str) -> int:
|
||||
query = f"SELECT id FROM media WHERE bookdata LIKE '%{ppn}%'"
|
||||
data = self.query_db(query)
|
||||
query = "SELECT id FROM media WHERE bookdata LIKE ?"
|
||||
data = self.query_db(query, (f"%{ppn}%",))
|
||||
if data:
|
||||
return data[0][0]
|
||||
else:
|
||||
@@ -1876,9 +1892,7 @@ class Database:
|
||||
results = self.query_db(query, (apparat_id,))
|
||||
res = []
|
||||
for result in results:
|
||||
old_edition_edition = self.query_db(
|
||||
"SELECT bookdata FROM media WHERE id=?", (result[2],), one=True
|
||||
)
|
||||
# keep only new edition payload; old edition can be reconstructed if needed
|
||||
res.append(BookData().from_string(result[1]))
|
||||
return res
|
||||
|
||||
|
||||
Reference in New Issue
Block a user