0
0
mirror of https://gitflic.ru/project/maks1ms/ocab.git synced 2024-12-23 16:23:01 +03:00

Добавлены новые методы и изменены старые в API модуля DataBase.

This commit is contained in:
Armatik 2024-01-02 21:26:09 +03:00
parent 7918620b99
commit 649e780109
5 changed files with 186 additions and 28 deletions

View File

@ -1,10 +1,13 @@
from .models.chats import Chats
from .models.messages import Messages
from .models.users import Users
from service import paths
from .models.user_stats import UserStats
from .models.chat_stats import ChatStats
from ....service import paths
from ..exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
import peewee as pw
def connect_database(is_test: bool = False, module: str | None = None):
if is_test:
if not module:
@ -22,69 +25,125 @@ def connect_database(is_test: bool = False, module: str | None = None):
return _database, f"{db_path}/OCAB.db"
def create_tables(db: pw.SqliteDatabase):
"""Создание таблиц"""
for table in Chats, Messages, Users:
for table in Chats, Messages, Users, UserStats, ChatStats:
if not table.table_exists():
db.create_tables([table])
def add_chat(chat_id, chat_role, chat_stats=0, chat_federation=0):
def add_chat(chat_id, chat_name, chat_type=10, chat_stats=0):
chat, created = Chats.get_or_create(id=chat_id, defaults={
'chat_role': chat_role,
'chat_name': chat_name,
'chat_type': chat_type,
'chat_stats': chat_stats,
'chat_federation': chat_federation
})
if not created:
# Обновить существующий чат, если он уже существует
chat.chat_role = chat_role
chat.chat_name = chat_name
chat.chat_type = chat_type
chat.chat_stats = chat_stats
chat.chat_federation = chat_federation
chat.save()
def add_user(user_id, user_name, user_tag=None, user_role=0, user_stats=0, user_rep=0):
user, created = Users.get_or_create(id=user_id, defaults={
'user_name': user_name,
'user_tag': user_tag,
'user_name': user_name,
'user_role': user_role,
'user_stats': user_stats,
'user_rep': user_rep
})
if not created:
# Обновить существующего пользователя, если он уже существует
user.user_name = user_name
user.user_tag = user_tag
user.user_name = user_name
user.user_role = user_role
user.user_stats = user_stats
user.user_rep = user_rep
user.save()
def add_message(message_id, message_text, message_sender, answer_id):
def add_message(message_chat_id, message_id, message_sender_id, message_text, answer_to_message_id=None,
message_ai_model=None):
Messages.create(
id=message_id,
message_text=message_text,
message_id_sender=message_sender,
answer_to_id=answer_id
message_chat_id=message_chat_id,
message_id=message_id,
message_sender_id=message_sender_id,
answer_to_message_id=answer_to_message_id,
message_ai_model=message_ai_model,
message_text=message_text
)
def add_chat_stats(chat_id, date, messages_count):
ChatStats.create(
chat_id=chat_id,
date=date,
messages_count=messages_count
)
def add_user_stats(chat_id, user_id, date, messages_count):
UserStats.create(
chat_id=chat_id,
user_id=user_id,
date=date,
messages_count=messages_count
)
# Работа с таблицей чатов
def get_chat(chat_id):
return Chats.get_or_none(Chats.id == chat_id)
def change_chat_name(chat_id, new_chat_name):
query = Chats.update(chat_name=new_chat_name).where(Chats.id == chat_id)
query.execute()
def change_chat_type(chat_id, new_chat_type):
query = Chats.update(chat_type=new_chat_type).where(Chats.id == chat_id)
query.execute()
def get_chat_all_stat(chat_id):
chat = Chats.get_or_none(Chats.id == chat_id)
return chat.chat_all_stat if chat else None
# Работа с таблицей пользователей
def get_user(user_id):
return Users.get_or_none(Users.id == user_id)
def get_user_tag(user_id):
user = Users.get_or_none(Users.id == user_id)
return user.user_tag if user else None
def get_user_name(user_id):
user = Users.get_or_none(Users.id == user_id)
return user.user_name if user else None
def get_user_role(user_id):
user = Users.get_or_none(Users.id == user_id)
return user.user_role if user else None
def get_message(message_id):
return Messages.get_or_none(Messages.id == message_id)
def get_user_all_stats(user_id):
user = Users.get_or_none(Users.id == user_id)
return user.user_stats if user else None
def get_user_rep(user_id):
user = Users.get_or_none(Users.id == user_id)
return user.user_rep if user else None
def change_user_name(user_id, new_user_name):
@ -92,18 +151,117 @@ def change_user_name(user_id, new_user_name):
query.execute()
# Аналогично для других функций обновления...
def update_user_stats():
query = Users.update(user_stats=Users.user_stats + 1)
def change_user_tag(user_id, new_user_tag):
query = Users.update(user_tag=new_user_tag).where(Users.id == user_id)
query.execute()
def update_user_rep():
query = Users.update(user_rep=Users.user_rep + 1)
def change_user_role(user_id, new_user_role):
query = Users.update(user_role=new_user_role).where(Users.id == user_id)
query.execute()
# Работа с таблицей сообщений
def get_message(message_chat_id, message_id):
return Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
def get_message_sender_id(message_chat_id, message_id):
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
return message.message_sender_id if message else None
def get_message_text(message_chat_id, message_id):
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
return message.message_text if message else None
def get_message_ai_model(message_chat_id, message_id):
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
return message.message_ai_model if message else None
def get_answer_to_message_id(message_chat_id, message_id):
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
return message.answer_to_message_id if message else None
# Работа с таблицей статистики чатов
def get_chat_stats(chat_id):
chat_stats = {}
for chat_stat in ChatStats.select().where(ChatStats.chat_id == chat_id):
chat_stats[chat_stat.date] = chat_stat.messages_count
return chat_stats
# Работа с таблицей статистики пользователей
def get_user_stats(user_id):
user_stats = {}
for user_stat in UserStats.select().where(UserStats.user_id == user_id):
user_stats[user_stat.date] = user_stat.messages_count
return user_stats
# Функции обновления
def update_chat_all_stat(chat_id):
query = Chats.update(chat_all_stat=Chats.chat_all_stat + 1).where(Chats.id == chat_id)
query.execute()
def update_chat_stats():
query = Chats.update(chat_stats=Chats.chat_stats + 1)
def update_chat_stats(chat_id, date):
chat_stats = ChatStats.get_or_none(ChatStats.chat_id == chat_id, ChatStats.date == date)
if chat_stats:
query = ChatStats.update(messages_count=ChatStats.messages_count + 1).where(ChatStats.chat_id == chat_id,
ChatStats.date == date)
query.execute()
else:
ChatStats.create(
chat_id=chat_id,
date=date,
messages_count=1
)
def update_user_all_stat(user_id):
user = Users.get_or_none(Users.id == user_id)
if user:
query = Users.update(user_stats=Users.user_stats + 1).where(Users.id == user_id)
query.execute()
else:
Users.create(
id=user_id,
user_stats=1
)
def update_user_rep(user_id):
user = Users.get_or_none(Users.id == user_id)
if user:
query = Users.update(user_rep=Users.user_rep + 1).where(Users.id == user_id)
query.execute()
else:
Users.create(
id=user_id,
user_rep=1
)
def update_user_stats(chat_id, user_id, date):
user_stats = UserStats.get_or_none(UserStats.chat_id == chat_id, UserStats.user_id == user_id,
UserStats.date == date)
if user_stats:
query = UserStats.update(messages_count=UserStats.messages_count + 1).where(UserStats.chat_id == chat_id,
UserStats.user_id == user_id,
UserStats.date == date)
query.execute()
else:
UserStats.create(
chat_id=chat_id,
user_id=user_id,
date=date,
messages_count=1
)

View File

@ -2,5 +2,5 @@
"name": "Database",
"description": "Модуль для работы с БД",
"author": "OCAB Team",
"version": "0.1"
"version": "1.0"
}

View File

@ -4,7 +4,6 @@ import peewee as pw
class Chats(pw.Model):
class Meta:
...
chat_id = pw.IntegerField(null=False)
chat_name = pw.CharField(null=False)
chat_type = pw.IntegerField(null=False, default=10)
chat_all_stat = pw.IntegerField(null=False)

View File

@ -4,7 +4,6 @@ import peewee as pw
class Users(pw.Model):
class Meta:
...
user_id = pw.IntegerField(null=False)
user_tag = pw.CharField(null=True)
user_name = pw.CharField(null=False) # до 255 символов
user_role = pw.IntegerField(null=True, default=3)

View File

@ -2,6 +2,7 @@ import os.path
from dataclasses import dataclass
from json import loads
@dataclass
class Path:
core: str
@ -17,6 +18,7 @@ def _get_paths(path_to_json: str):
modules_custom=paths["modules custom"]
)
cwd = os.getcwd()
cwd = cwd[:cwd.index('/src')]