mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-30 06:07:14 +00:00
debug: 输出每个SQL
This commit is contained in:
+19
-17
@@ -1,4 +1,6 @@
|
|||||||
|
import logging
|
||||||
import time
|
import time
|
||||||
|
from sqlite3 import Cursor
|
||||||
|
|
||||||
from pymysql.converters import escape_string
|
from pymysql.converters import escape_string
|
||||||
|
|
||||||
@@ -27,9 +29,15 @@ class DatabaseManager:
|
|||||||
# self.conn.isolation_level = None
|
# self.conn.isolation_level = None
|
||||||
self.cursor = self.conn.cursor()
|
self.cursor = self.conn.cursor()
|
||||||
|
|
||||||
|
def execute(self, sql: str) -> Cursor:
|
||||||
|
c = self.cursor.execute(sql)
|
||||||
|
logging.debug('SQL: {}'.format(sql))
|
||||||
|
self.conn.commit()
|
||||||
|
return c
|
||||||
|
|
||||||
# 初始化数据库的函数
|
# 初始化数据库的函数
|
||||||
def initialize_database(self):
|
def initialize_database(self):
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
create table if not exists `sessions` (
|
create table if not exists `sessions` (
|
||||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
`name` varchar(255) not null,
|
`name` varchar(255) not null,
|
||||||
@@ -41,7 +49,6 @@ class DatabaseManager:
|
|||||||
`prompt` text not null
|
`prompt` text not null
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
self.conn.commit()
|
|
||||||
print('Database initialized.')
|
print('Database initialized.')
|
||||||
|
|
||||||
# session持久化
|
# session持久化
|
||||||
@@ -50,49 +57,44 @@ class DatabaseManager:
|
|||||||
# 检查是否已经有了此name和create_timestamp的session
|
# 检查是否已经有了此name和create_timestamp的session
|
||||||
# 如果有,就更新prompt和last_interact_timestamp
|
# 如果有,就更新prompt和last_interact_timestamp
|
||||||
# 如果没有,就插入一条新的记录
|
# 如果没有,就插入一条新的记录
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
select count(*) from `sessions` where `type` = '{}' and `number` = {} and `create_timestamp` = {}
|
select count(*) from `sessions` where `type` = '{}' and `number` = {} and `create_timestamp` = {}
|
||||||
""".format(subject_type, subject_number, create_timestamp))
|
""".format(subject_type, subject_number, create_timestamp))
|
||||||
count = self.cursor.fetchone()[0]
|
count = self.cursor.fetchone()[0]
|
||||||
if count == 0:
|
if count == 0:
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
insert into `sessions` (`name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`)
|
insert into `sessions` (`name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`)
|
||||||
values ('{}', '{}', {}, {}, {}, '{}')
|
values ('{}', '{}', {}, {}, {}, '{}')
|
||||||
""".format("{}_{}".format(subject_type, subject_number), subject_type, subject_number, create_timestamp,
|
""".format("{}_{}".format(subject_type, subject_number), subject_type, subject_number, create_timestamp,
|
||||||
last_interact_timestamp, escape_string(prompt)))
|
last_interact_timestamp, escape_string(prompt)))
|
||||||
self.conn.commit()
|
|
||||||
else:
|
else:
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
update `sessions` set `last_interact_timestamp` = {}, `prompt` = '{}'
|
update `sessions` set `last_interact_timestamp` = {}, `prompt` = '{}'
|
||||||
where `type` = '{}' and `number` = {} and `create_timestamp` = {}
|
where `type` = '{}' and `number` = {} and `create_timestamp` = {}
|
||||||
""".format(last_interact_timestamp, escape_string(prompt), subject_type,
|
""".format(last_interact_timestamp, escape_string(prompt), subject_type,
|
||||||
subject_number, create_timestamp))
|
subject_number, create_timestamp))
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
# 显式关闭一个session
|
# 显式关闭一个session
|
||||||
def explicit_close_session(self, session_name: str, create_timestamp: int):
|
def explicit_close_session(self, session_name: str, create_timestamp: int):
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
update `sessions` set `status` = 'explicitly_closed' where `name` = '{}' and `create_timestamp` = {}
|
update `sessions` set `status` = 'explicitly_closed' where `name` = '{}' and `create_timestamp` = {}
|
||||||
""".format(session_name, create_timestamp))
|
""".format(session_name, create_timestamp))
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def set_session_ongoing(self, session_name: str, create_timestamp: int):
|
def set_session_ongoing(self, session_name: str, create_timestamp: int):
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
update `sessions` set `status` = 'on_going' where `name` = '{}' and `create_timestamp` = {}
|
update `sessions` set `status` = 'on_going' where `name` = '{}' and `create_timestamp` = {}
|
||||||
""".format(session_name, create_timestamp))
|
""".format(session_name, create_timestamp))
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
# 设置session为过期
|
# 设置session为过期
|
||||||
def set_session_expired(self, session_name: str, create_timestamp: int):
|
def set_session_expired(self, session_name: str, create_timestamp: int):
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
update `sessions` set `status` = 'expired' where `name` = '{}' and `create_timestamp` = {}
|
update `sessions` set `status` = 'expired' where `name` = '{}' and `create_timestamp` = {}
|
||||||
""".format(session_name, create_timestamp))
|
""".format(session_name, create_timestamp))
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
# 从数据库加载还没过期的session数据
|
# 从数据库加载还没过期的session数据
|
||||||
def load_valid_sessions(self) -> dict:
|
def load_valid_sessions(self) -> dict:
|
||||||
# 从数据库中加载所有还没过期的session
|
# 从数据库中加载所有还没过期的session
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
select `name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`, `status`
|
select `name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`, `status`
|
||||||
from `sessions` where `last_interact_timestamp` > {}
|
from `sessions` where `last_interact_timestamp` > {}
|
||||||
""".format(int(time.time()) - config.session_expire_time))
|
""".format(int(time.time()) - config.session_expire_time))
|
||||||
@@ -125,7 +127,7 @@ class DatabaseManager:
|
|||||||
# 获取此session_name前一个session的数据
|
# 获取此session_name前一个session的数据
|
||||||
def last_session(self, session_name: str, cursor_timestamp: int):
|
def last_session(self, session_name: str, cursor_timestamp: int):
|
||||||
|
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
select `name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`, `status`
|
select `name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`, `status`
|
||||||
from `sessions` where `name` = '{}' and `last_interact_timestamp` < {} order by `last_interact_timestamp` desc
|
from `sessions` where `name` = '{}' and `last_interact_timestamp` < {} order by `last_interact_timestamp` desc
|
||||||
limit 1
|
limit 1
|
||||||
@@ -154,7 +156,7 @@ class DatabaseManager:
|
|||||||
# 获取此session_name后一个session的数据
|
# 获取此session_name后一个session的数据
|
||||||
def next_session(self, session_name: str, cursor_timestamp: int):
|
def next_session(self, session_name: str, cursor_timestamp: int):
|
||||||
|
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
select `name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`, `status`
|
select `name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`, `status`
|
||||||
from `sessions` where `name` = '{}' and `last_interact_timestamp` > {} order by `last_interact_timestamp` asc
|
from `sessions` where `name` = '{}' and `last_interact_timestamp` > {} order by `last_interact_timestamp` asc
|
||||||
limit 1
|
limit 1
|
||||||
@@ -182,7 +184,7 @@ class DatabaseManager:
|
|||||||
|
|
||||||
# 列出与某个对象的所有对话session
|
# 列出与某个对象的所有对话session
|
||||||
def list_history(self, session_name: str, capacity: int, page: int):
|
def list_history(self, session_name: str, capacity: int, page: int):
|
||||||
self.cursor.execute("""
|
self.execute("""
|
||||||
select `name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`, `status`
|
select `name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`, `status`
|
||||||
from `sessions` where `name` = '{}' order by `last_interact_timestamp` desc limit {} offset {}
|
from `sessions` where `name` = '{}' order by `last_interact_timestamp` desc limit {} offset {}
|
||||||
""".format(session_name, capacity, capacity * page))
|
""".format(session_name, capacity, capacity * page))
|
||||||
|
|||||||
Reference in New Issue
Block a user