-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
99 lines (86 loc) · 3.71 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3
import logging # модуль для сбора логов
# подтягиваем константы из config-файла
from config import LOGS, DB_FILE
# настраиваем запись логов в файл
logging.basicConfig(filename=LOGS, level=logging.ERROR,
format="%(asctime)s FILE: %(filename)s IN: %(funcName)s MESSAGE: %(message)s", filemode="w")
path_to_db = DB_FILE # файл базы данных
def create_database():
try:
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY,
user_id INTEGER,
message TEXT,
role TEXT,
total_gpt_tokens INTEGER,
tts_symbols INTEGER,
stt_blocks INTEGER)
''')
logging.info("DATABASE: База данных создана")
except Exception as e:
logging.debug(e)
return None
def add_message(user_id, full_message):
try:
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
message, role, total_gpt_tokens, tts_symbols, stt_blocks = full_message
cursor.execute('''
INSERT INTO messages (user_id, message, role, total_gpt_tokens, tts_symbols, stt_blocks)
VALUES (?, ?, ?, ?, ?, ?)''',
(user_id, message, role, total_gpt_tokens, tts_symbols, stt_blocks)
)
conn.commit() # сохраняем изменения
logging.info(f"DATABASE: INSERT INTO messages "
f"VALUES ({user_id}, {message}, {role}, {total_gpt_tokens}, {tts_symbols}, {stt_blocks})")
except Exception as e:
logging.debug(e)
return None
def count_users(user_id):
try:
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute('''SELECT COUNT(DISTINCT user_id) FROM messages WHERE user_id <> ?''', (user_id,))
count = cursor.fetchone()[0]
return count
except Exception as e:
logging.debug(e)
return None
def select_n_last_messages(user_id, n_last_messages=4):
messages = []
total_spent_tokens = 0
try:
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT message, role, total_gpt_tokens FROM messages WHERE user_id=? ORDER BY id DESC LIMIT ?''',
(user_id, n_last_messages))
data = cursor.fetchall()
if data and data[0]:
for message in reversed(data):
messages.append({'text': message[0], 'role': message[1]})
total_spent_tokens = max(total_spent_tokens, message[2])
return messages, total_spent_tokens
except Exception as e:
logging.debug(e)
return messages, total_spent_tokens
def count_all_limits(user_id, limit_type):
try:
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute(f'''SELECT SUM({limit_type}) FROM messages WHERE user_id=?''', (user_id,))
data = cursor.fetchone()
if data and data[0]:
logging.info(f"DATABASE: У user_id={user_id} использовано {data[0]} {limit_type}")
return data[0]
else:
return 0
except Exception as e:
logging.debug(e)
return 0