完成日志界面的制作
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
import sqlite3
 | 
			
		||||
import time
 | 
			
		||||
from inspect import currentframe
 | 
			
		||||
from functools import singledispatch
 | 
			
		||||
 | 
			
		||||
from codes.common import clibs
 | 
			
		||||
 | 
			
		||||
@@ -9,15 +10,15 @@ def db_init():
 | 
			
		||||
    if clibs.db_file.exists():
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    clibs.conn = sqlite3.connect(clibs.db_file, isolation_level=None, check_same_thread=False, cached_statements=2048, timeout=10.0)
 | 
			
		||||
    clibs.cursor = clibs.conn.cursor()
 | 
			
		||||
    clibs.cursor.execute("PRAGMA journal_mode=wal")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA wal_checkpoint=TRUNCATE")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA synchronous=normal")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA temp_store=memory")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA mmap_size=30000000000")
 | 
			
		||||
    clibs.cursor.execute("PRAGMA cache_size=200000")
 | 
			
		||||
    clibs.cursor.execute(
 | 
			
		||||
    conn = sqlite3.connect(clibs.db_file, isolation_level=None, check_same_thread=False, cached_statements=2048, timeout=10.0)
 | 
			
		||||
    cursor = conn.cursor()
 | 
			
		||||
    cursor.execute("PRAGMA journal_mode=wal")
 | 
			
		||||
    cursor.execute("PRAGMA wal_checkpoint=TRUNCATE")
 | 
			
		||||
    cursor.execute("PRAGMA synchronous=normal")
 | 
			
		||||
    cursor.execute("PRAGMA temp_store=memory")
 | 
			
		||||
    cursor.execute("PRAGMA mmap_size=30000000000")
 | 
			
		||||
    cursor.execute("PRAGMA cache_size=200000")
 | 
			
		||||
    cursor.execute(
 | 
			
		||||
        """
 | 
			
		||||
        create table if not exists logs(
 | 
			
		||||
            id integer primary key autoincrement,
 | 
			
		||||
@@ -28,7 +29,7 @@ def db_init():
 | 
			
		||||
        )
 | 
			
		||||
        """
 | 
			
		||||
    )
 | 
			
		||||
    clibs.cursor.execute(
 | 
			
		||||
    cursor.execute(
 | 
			
		||||
        """
 | 
			
		||||
        create table if not exists users(
 | 
			
		||||
            id integer primary key autoincrement,
 | 
			
		||||
@@ -39,14 +40,18 @@ def db_init():
 | 
			
		||||
        )
 | 
			
		||||
        """
 | 
			
		||||
    )
 | 
			
		||||
    db_write_logs("数据库初始化成功!", "login_ui")
 | 
			
		||||
    db_close()
 | 
			
		||||
    cursor.execute(f"INSERT INTO logs (level, module, content) VALUES (?, ?, ?)", ("info", "login_ui", "数据库初始化成功!"))
 | 
			
		||||
    cursor.close()
 | 
			
		||||
    conn.close()
 | 
			
		||||
 | 
			
		||||
def db_lock(func):
 | 
			
		||||
    def wrapper(*args, **kwargs):
 | 
			
		||||
        try:
 | 
			
		||||
            clibs.lock.acquire(True)
 | 
			
		||||
            ret = func(*args, **kwargs)
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            print(f"db operation error: {e}")
 | 
			
		||||
            ret = None
 | 
			
		||||
        finally:
 | 
			
		||||
            clibs.lock.release()
 | 
			
		||||
        return ret
 | 
			
		||||
@@ -62,6 +67,13 @@ def db_backup():
 | 
			
		||||
        db.unlink()
 | 
			
		||||
 | 
			
		||||
def db_conn():
 | 
			
		||||
    # import traceback, inspect
 | 
			
		||||
    # print("[Conn] 被调用", traceback.format_stack()[-2])
 | 
			
		||||
    # print("[Conn] conn=", clibs.conn, "cursor=", clibs.cursor)
 | 
			
		||||
 | 
			
		||||
    if clibs.conn is not None:
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    clibs.conn = sqlite3.connect(clibs.db_file, isolation_level=None, check_same_thread=False, cached_statements=2048, timeout=3.0)
 | 
			
		||||
    clibs.cursor = clibs.conn.cursor()
 | 
			
		||||
    clibs.cursor.execute("PRAGMA journal_mode=wal")
 | 
			
		||||
@@ -92,9 +104,30 @@ def db_write_logs(content, module="", level="info"):
 | 
			
		||||
 | 
			
		||||
    clibs.cursor.execute(f"INSERT INTO logs (level, module, content) VALUES (?, ?, ?)", (level, module, content))
 | 
			
		||||
 | 
			
		||||
@singledispatch
 | 
			
		||||
@db_lock
 | 
			
		||||
def db_query_logs():
 | 
			
		||||
    ...
 | 
			
		||||
def db_query_logs(dummy: bool = True):
 | 
			
		||||
    clibs.cursor.execute(f"SELECT * FROM logs")
 | 
			
		||||
    records = clibs.cursor.fetchall()
 | 
			
		||||
    len_records = len(records)
 | 
			
		||||
    return records, len_records
 | 
			
		||||
 | 
			
		||||
@db_query_logs.register
 | 
			
		||||
def _(levels: list):
 | 
			
		||||
    placeholders = ",".join("?" * len(levels))
 | 
			
		||||
    clibs.cursor.execute(f"SELECT * FROM logs WHERE level IN ({placeholders})", (*levels, ))
 | 
			
		||||
    records = clibs.cursor.fetchall()
 | 
			
		||||
    len_records = len(records)
 | 
			
		||||
    return records, len_records
 | 
			
		||||
 | 
			
		||||
@db_query_logs.register
 | 
			
		||||
def _(search_text: str, records: list):
 | 
			
		||||
    ids = [_[0] for _ in records]
 | 
			
		||||
    placeholder = ",".join("?" * len(ids))
 | 
			
		||||
    clibs.cursor.execute(f"SELECT * FROM logs WHERE id IN ({placeholder}) and content like ?", (ids + [f"%{search_text}%", ]))
 | 
			
		||||
    records = clibs.cursor.fetchall()
 | 
			
		||||
    len_records = len(records)
 | 
			
		||||
    return records, len_records
 | 
			
		||||
 | 
			
		||||
@db_lock
 | 
			
		||||
def db_write_users(username, password_encrypted, salt):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user