Als Nutzer möchte ich Datenänderungen nach verfolgen können #246 #248

Merged
Jonas merged 17 commits from #246 into 1.0.0 2023-03-14 18:31:37 +01:00
17 changed files with 255 additions and 31 deletions
Showing only changes of commit 325a17b5a8 - Show all commits

View File

@ -0,0 +1,27 @@
from abc import ABC, abstractmethod
from datetime import datetime
class HistoryTableABC(ABC):
@abstractmethod
def __init__(self):
self._id = 0
self._deleted = False
self._date_from = datetime.now().isoformat()
self._date_to = datetime.now().isoformat()
@property
def id(self) -> int:
return self._id
@property
def deleted(self) -> bool:
return self._deleted
@property
def date_from(self) -> str:
return self._date_from
@property
def date_to(self) -> str:
return self._date_to

View File

@ -0,0 +1,15 @@
from abc import abstractmethod
from cpl_core.database import TableABC
class TableWithIdABC(TableABC):
@abstractmethod
def __init__(self):
self.__init__()
self._id = 0
@property
def id(self) -> int:
return self._id

View File

@ -9,7 +9,7 @@ ALTER TABLE `AutoRoleRules`
CREATE TABLE IF NOT EXISTS `AutoRoleRulesHistory`
(
`AutoRoleRuleId` BIGINT(20) NOT NULL,
`Id` BIGINT(20) NOT NULL,
`AutoRoleId` BIGINT(20) DEFAULT NULL,
`DiscordEmojiName` VARCHAR(64) DEFAULT NULL,
`DiscordRoleId` BIGINT(20) NOT NULL,
@ -26,7 +26,7 @@ CREATE TRIGGER `TR_AutoRoleRulesUpdate`
FOR EACH ROW
BEGIN
INSERT INTO `AutoRoleRulesHistory` (
`AutoRoleRuleId`, `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `DateFrom`, `DateTo`
`Id`, `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.AutoRoleRuleId, OLD.AutoRoleId, OLD.DiscordEmojiName, OLD.DiscordRoleId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
@ -41,7 +41,7 @@ CREATE TRIGGER `TR_AutoRoleRulesDelete`
FOR EACH ROW
BEGIN
INSERT INTO `AutoRoleRulesHistory` (
`AutoRoleRuleId`, `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `Deleted`, `DateFrom`, `DateTo`
`Id`, `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.AutoRoleRuleId, OLD.AutoRoleId, OLD.DiscordEmojiName, OLD.DiscordRoleId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)

View File

@ -9,7 +9,7 @@ ALTER TABLE `AutoRoles`
CREATE TABLE IF NOT EXISTS `AutoRolesHistory`
(
`AutoRoleId` BIGINT(20) NOT NULL,
`Id` BIGINT(20) NOT NULL,
`ServerId` BIGINT(20) DEFAULT NULL,
`DiscordChannelId` BIGINT(20) NOT NULL,
`DiscordMessageId` BIGINT(20) NOT NULL,
@ -26,7 +26,7 @@ CREATE TRIGGER `TR_AutoRolesUpdate`
FOR EACH ROW
BEGIN
INSERT INTO `AutoRolesHistory` (
`AutoRoleId`, `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `DateFrom`, `DateTo`
`Id`, `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.AutoRoleId, OLD.ServerId, OLD.DiscordChannelId, OLD.DiscordMessageId, OLD.LastModifiedAt,
@ -42,7 +42,7 @@ CREATE TRIGGER `TR_AutoRolesDelete`
FOR EACH ROW
BEGIN
INSERT INTO `AutoRolesHistory` (
`AutoRoleId`, `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `Deleted`, `DateFrom`, `DateTo`
`Id`, `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.AutoRoleId, OLD.ServerId, OLD.DiscordChannelId, OLD.DiscordMessageId, TRUE, OLD.LastModifiedAt,

View File

@ -9,8 +9,8 @@ ALTER TABLE `Clients`
CREATE TABLE IF NOT EXISTS `ClientsHistory`
(
`ClientId` BIGINT(20) NOT NULL,
`DiscordClientId` BIGINT(20) NOT NULL,
`Id` BIGINT(20) NOT NULL,
`DiscordId` BIGINT(20) NOT NULL,
`SentMessageCount` BIGINT(20) NOT NULL DEFAULT 0,
`ReceivedMessageCount` BIGINT(20) NOT NULL DEFAULT 0,
`DeletedMessageCount` BIGINT(20) NOT NULL DEFAULT 0,
@ -30,7 +30,7 @@ CREATE TRIGGER `TR_ClientsUpdate`
FOR EACH ROW
BEGIN
INSERT INTO `ClientsHistory` (
`ClientId`, `DiscordClientId`, `SentMessageCount`, `ReceivedMessageCount`, `DeletedMessageCount`,
`Id`, `DiscordId`, `SentMessageCount`, `ReceivedMessageCount`, `DeletedMessageCount`,
`ReceivedCommandsCount`, `MovedUsersCount`, `ServerId`, `DateFrom`, `DateTo`
)
VALUES (
@ -47,7 +47,7 @@ CREATE TRIGGER `TR_ClientsDelete`
FOR EACH ROW
BEGIN
INSERT INTO `ClientsHistory` (
`ClientId`, `DiscordClientId`, `SentMessageCount`, `ReceivedMessageCount`, `DeletedMessageCount`,
`Id`, `DiscordId`, `SentMessageCount`, `ReceivedMessageCount`, `DeletedMessageCount`,
`ReceivedCommandsCount`, `MovedUsersCount`, `ServerId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (

View File

@ -9,7 +9,7 @@ ALTER TABLE `KnownUsers`
CREATE TABLE IF NOT EXISTS `KnownUsersHistory`
(
`KnownUserId` BIGINT(20) NOT NULL,
`Id` BIGINT(20) NOT NULL,
`DiscordId` BIGINT(20) NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
@ -24,7 +24,7 @@ CREATE TRIGGER `TR_KnownUsersUpdate`
FOR EACH ROW
BEGIN
INSERT INTO `KnownUsersHistory` (
`KnownUserId`, `DiscordId`, `DateFrom`, `DateTo`
`Id`, `DiscordId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.KnownUserId, OLD.DiscordId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
@ -39,7 +39,7 @@ CREATE TRIGGER `TR_KnownUsersDelete`
FOR EACH ROW
BEGIN
INSERT INTO `KnownUsersHistory` (
`KnownUserId`, `DiscordId`, `Deleted`, `DateFrom`, `DateTo`
`Id`, `DiscordId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.KnownUserId, OLD.DiscordId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)

View File

@ -9,8 +9,8 @@ ALTER TABLE `Servers`
CREATE TABLE IF NOT EXISTS `ServersHistory`
(
`ServerId` BIGINT(20) NOT NULL,
`DiscordServerId` BIGINT(20) NOT NULL,
`Id` BIGINT(20) NOT NULL,
`DiscordId` BIGINT(20) NOT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
@ -24,7 +24,7 @@ CREATE TRIGGER `TR_ServersUpdate`
FOR EACH ROW
BEGIN
INSERT INTO `ServersHistory` (
`ServerId`, `DiscordServerId`, `DateFrom`, `DateTo`
`Id`, `DiscordId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.ServerId, OLD.DiscordServerId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
@ -39,7 +39,7 @@ CREATE TRIGGER `TR_ServersDelete`
FOR EACH ROW
BEGIN
INSERT INTO `ServersHistory` (
`ServerId`, `DiscordServerId`, `Deleted`, `DateFrom`, `DateTo`
`Id`, `DiscordId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.ServerId, OLD.DiscordServerId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)

View File

@ -9,7 +9,7 @@ ALTER TABLE `UserJoinedServers`
CREATE TABLE IF NOT EXISTS `UserJoinedServersHistory`
(
`JoinId` BIGINT(20) NOT NULL,
`Id` BIGINT(20) NOT NULL,
`UserId` BIGINT(20) NOT NULL,
`JoinedOn` DATETIME(6) NOT NULL,
`LeavedOn` DATETIME(6) DEFAULT NULL,
@ -26,7 +26,7 @@ CREATE TRIGGER `TR_UserJoinedServersUpdate`
FOR EACH ROW
BEGIN
INSERT INTO `UserJoinedServersHistory` (
`JoinId`, `UserId`, `JoinedOn`, `LeavedOn`, `DateFrom`, `DateTo`
`Id`, `UserId`, `JoinedOn`, `LeavedOn`, `DateFrom`, `DateTo`
)
VALUES (
OLD.JoinId, OLD.UserId, OLD.JoinedOn, OLD.LeavedOn, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
@ -41,7 +41,7 @@ CREATE TRIGGER `TR_UserJoinedServersDelete`
FOR EACH ROW
BEGIN
INSERT INTO `UserJoinedServersHistory` (
`JoinId`, `UserId`, `JoinedOn`, `LeavedOn`, `Deleted`, `DateFrom`, `DateTo`
`Id`, `UserId`, `JoinedOn`, `LeavedOn`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.JoinId, OLD.UserId, OLD.JoinedOn, OLD.LeavedOn, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)

View File

@ -9,7 +9,7 @@ ALTER TABLE `UserJoinedVoiceChannel`
CREATE TABLE IF NOT EXISTS `UserJoinedVoiceChannelHistory`
(
`JoinId` BIGINT(20) NOT NULL,
`Id` BIGINT(20) NOT NULL,
`UserId` BIGINT(20) NOT NULL,
`DiscordChannelId` BIGINT(20) NOT NULL,
`JoinedOn` DATETIME(6) NOT NULL,
@ -27,7 +27,7 @@ CREATE TRIGGER `TR_UserJoinedVoiceChannelUpdate`
FOR EACH ROW
BEGIN
INSERT INTO `UserJoinedVoiceChannelHistory` (
`JoinId`, `UserId`, `DiscordChannelId`, `JoinedOn`, `LeavedOn`, `DateFrom`, `DateTo`
`Id`, `UserId`, `DiscordChannelId`, `JoinedOn`, `LeavedOn`, `DateFrom`, `DateTo`
)
VALUES (
OLD.JoinId, OLD.UserId, OLD.DiscordChannelId, OLD.JoinedOn, OLD.LeavedOn, OLD.LastModifiedAt,
@ -43,7 +43,7 @@ CREATE TRIGGER `TR_UserJoinedVoiceChannelDelete`
FOR EACH ROW
BEGIN
INSERT INTO `UserJoinedVoiceChannelHistory` (
`JoinId`, `UserId`, `DiscordChannelId`, `JoinedOn`, `LeavedOn`, `Deleted`, `DateFrom`, `DateTo`
`Id`, `UserId`, `DiscordChannelId`, `JoinedOn`, `LeavedOn`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.JoinId, OLD.UserId, OLD.DiscordChannelId, OLD.JoinedOn, OLD.LeavedOn, TRUE, OLD.LastModifiedAt,

View File

@ -9,7 +9,7 @@ ALTER TABLE `Users`
CREATE TABLE IF NOT EXISTS `UsersHistory`
(
`UserId` BIGINT(20) NOT NULL,
`Id` BIGINT(20) NOT NULL,
`DiscordId` BIGINT(20) NOT NULL,
`XP` BIGINT(20) NOT NULL DEFAULT 0,
`ServerId` BIGINT(20) DEFAULT NULL,
@ -26,7 +26,7 @@ CREATE TRIGGER `TR_UsersUpdate`
FOR EACH ROW
BEGIN
INSERT INTO `UsersHistory` (
`UserId`, `DiscordId`, `XP`, `ServerId`, `DateFrom`, `DateTo`
`Id`, `DiscordId`, `XP`, `ServerId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ServerId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
@ -41,7 +41,7 @@ CREATE TRIGGER `TR_UsersDelete`
FOR EACH ROW
BEGIN
INSERT INTO `UsersHistory` (
`UserId`, `DiscordId`, `XP`, `ServerId`, `Deleted`, `DateFrom`, `DateTo`
`Id`, `DiscordId`, `XP`, `ServerId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ServerId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)

View File

@ -0,0 +1,54 @@
from typing import Optional
from bot_data.abc.history_table_abc import HistoryTableABC
from bot_data.model.server import Server
class LevelHistory(HistoryTableABC):
def __init__(
self,
name: str,
color: str,
min_xp: int,
permissions: int,
server: Optional[Server],
deleted: bool,
date_from: str,
date_to: str,
id=0,
):
HistoryTableABC.__init__(self)
self._id = id
self._name = name
self._color = color
self._min_xp = min_xp
self._permissions = permissions
self._server = server
self._deleted = deleted
self._date_from = date_from
self._date_to = date_to
@property
def id(self) -> int:
return self._id
@property
def name(self) -> str:
return self._name
@property
def color(self) -> str:
return self._color
@property
def min_xp(self) -> int:
return self._min_xp
@property
def permissions(self) -> int:
return self._permissions
@property
def server(self) -> Server:
return self._server

View File

@ -0,0 +1,41 @@
from typing import Type
from cpl_core.database.context import DatabaseContextABC
from cpl_query.extension import List
from bot_data.abc.history_table_abc import HistoryTableABC
from bot_data.abc.table_with_id_abc import TableWithIdABC
from bot_graphql.abc.data_query_abc import DataQueryABC
class DataQueryWithHistoryABC(DataQueryABC):
def __init__(
self,
name: str,
table_name: str,
history_type: Type[HistoryTableABC],
db: DatabaseContextABC,
):
self._table_name = table_name
self._history_type = history_type
self._db = db
DataQueryABC.__init__(self, name)
self.set_field("history", self.resolve_history)
def resolve_history(self, entry: TableWithIdABC, *_):
history = List(self._history_type)
results = self._db.select(
f"""
SELECT *
FROM {self._table_name}
WHERE Id = {entry.id};
"""
)
for result in results:
history.add(self._history_type(*result[1:], result[0]))
return history

View File

@ -28,6 +28,7 @@ from bot_graphql.queries.auto_role_query import AutoRoleQuery
from bot_graphql.queries.auto_role_rule_query import AutoRoleRuleQuery
from bot_graphql.queries.client_query import ClientQuery
from bot_graphql.queries.known_user_query import KnownUserQuery
from bot_graphql.queries.level_history_query import LevelHistoryQuery
from bot_graphql.queries.level_query import LevelQuery
from bot_graphql.queries.server_query import ServerQuery
from bot_graphql.queries.user_joined_game_server_query import UserJoinedGameServerQuery
@ -57,6 +58,7 @@ class GraphQLModule(ModuleABC):
services.add_transient(QueryABC, ClientQuery)
services.add_transient(QueryABC, KnownUserQuery)
services.add_transient(QueryABC, LevelQuery)
services.add_transient(QueryABC, LevelHistoryQuery)
services.add_transient(QueryABC, ServerQuery)
services.add_transient(QueryABC, UserQuery)
services.add_transient(QueryABC, UserJoinedServerQuery)

View File

@ -3,6 +3,18 @@ interface TableQuery {
modifiedAt: String
}
interface TableWithHistoryQuery {
createdAt: String
modifiedAt: String
history: [HistoryTableQuery]
}
interface HistoryTableQuery {
deleted: Boolean
dateFrom: String
dateTo: String
}
input Page {
pageIndex: Int
pageSize: Int

View File

@ -1,4 +1,4 @@
type Level implements TableQuery {
type Level implements TableWithHistoryQuery {
id: ID
name: String
color: String
@ -9,6 +9,22 @@ type Level implements TableQuery {
createdAt: String
modifiedAt: String
history: [LevelHistory]
}
type LevelHistory implements HistoryTableQuery {
id: ID
name: String
color: String
minXp: Int
permissions: String
server: Server
deleted: Boolean
dateFrom: String
dateTo: String
}
input LevelFilter {

View File

@ -0,0 +1,54 @@
from bot_data.abc.history_table_abc import HistoryTableABC
from bot_data.model.level import Level
from bot_graphql.abc.query_abc import QueryABC
class LevelHistoryQuery(QueryABC):
def __init__(self):
QueryABC.__init__(self, "LevelHistory")
self.set_field("id", self.resolve_id)
self.set_field("name", self.resolve_name)
self.set_field("color", self.resolve_color)
self.set_field("minXp", self.resolve_min_xp)
self.set_field("permissions", self.resolve_permissions)
self.set_field("server", self.resolve_server)
self.set_field("deleted", self.resolve_deleted)
self.set_field("dateFrom", self.resolve_date_from)
self.set_field("dateTo", self.resolve_date_to)
@staticmethod
def resolve_id(level: Level, *_):
return level.id
@staticmethod
def resolve_name(level: Level, *_):
return level.name
@staticmethod
def resolve_color(level: Level, *_):
return level.color
@staticmethod
def resolve_min_xp(level: Level, *_):
return level.min_xp
@staticmethod
def resolve_permissions(level: Level, *_):
return level.permissions
@staticmethod
def resolve_server(level: Level, *_):
return level.server
@staticmethod
def resolve_deleted(entry: HistoryTableABC, *_):
return entry.deleted
@staticmethod
def resolve_date_from(entry: HistoryTableABC, *_):
return entry.date_from
@staticmethod
def resolve_date_to(entry: HistoryTableABC, *_):
return entry.date_to

View File

@ -1,10 +1,13 @@
from cpl_core.database.context import DatabaseContextABC
from bot_data.model.level import Level
from bot_graphql.abc.data_query_abc import DataQueryABC
from bot_data.model.level_history import LevelHistory
from bot_graphql.abc.data_query_with_history_abc import DataQueryWithHistoryABC
class LevelQuery(DataQueryABC):
def __init__(self):
DataQueryABC.__init__(self, "Level")
class LevelQuery(DataQueryWithHistoryABC):
def __init__(self, db: DatabaseContextABC):
DataQueryWithHistoryABC.__init__(self, "Level", "LevelsHistory", LevelHistory, db)
self.set_field("id", self.resolve_id)
self.set_field("name", self.resolve_name)