From 3a01a1334e3bd9ff81ee406bc9a894f51e8ac4c5 Mon Sep 17 00:00:00 2001 From: durmusrasit <12167194+durmusrasit@users.noreply.github.com> Date: Thu, 31 Aug 2023 23:21:46 +0300 Subject: [PATCH 1/5] feat: add wip codes --- apps/twitch-bot/internal/command/command.go | 5 +++++ apps/twitch-bot/internal/service/service.go | 6 ++++++ db/database.go | 2 ++ db/mysql/mysql.go | 21 +++++++++++++++++++ packages/gosenchabot/models/models.go | 10 +++++++++ .../senchabot-prisma/prisma/schema.prisma | 16 ++++++++++++++ 6 files changed, 60 insertions(+) diff --git a/apps/twitch-bot/internal/command/command.go b/apps/twitch-bot/internal/command/command.go index 2ef6282d..28e08008 100644 --- a/apps/twitch-bot/internal/command/command.go +++ b/apps/twitch-bot/internal/command/command.go @@ -3,6 +3,7 @@ package command import ( "context" "fmt" + "log" "strings" "time" @@ -71,6 +72,10 @@ func (c *commands) Say(ctx context.Context, message twitch.PrivateMessage, cmdNa c.client.Twitch.Say(message.Channel, messageContent) c.setCommandCooldown(message.User.Name) c.service.SaveTwitchBotCommandActivity(ctx, cmdName, message.RoomID, message.User.DisplayName, message.User.ID) + err := c.service.AddBotCommandStatistic(ctx, cmdName) + if err != nil { + log.Println(err) + } } func (c *commands) RunCommand(context context.Context, cmdName string, params []string, message twitch.PrivateMessage) { diff --git a/apps/twitch-bot/internal/service/service.go b/apps/twitch-bot/internal/service/service.go index 510cb2c5..157a5993 100644 --- a/apps/twitch-bot/internal/service/service.go +++ b/apps/twitch-bot/internal/service/service.go @@ -36,6 +36,8 @@ type Service interface { CreateCommandAliases(ctx context.Context, commandName string, aliases []string, twitchChannelId string, createdBy string) (*string, error) CheckCommandAliasExist(ctx context.Context, commandAlias string, twitchChannelId string) (*string, error) DeleteCommandAlias(ctx context.Context, commandAlias string, twitchChannelId string) (*string, error) + + AddBotCommandStatistic(ctx context.Context, commandName string) error } type services struct { @@ -220,3 +222,7 @@ func (s *services) DeleteCommandAlias(ctx context.Context, commandAlias string, return infoText, nil } + +func (s *services) AddBotCommandStatistic(ctx context.Context, commandName string) error { + return s.AddBotCommandStatistic(ctx, commandName) +} diff --git a/db/database.go b/db/database.go index 78e038e2..22df7bf9 100644 --- a/db/database.go +++ b/db/database.go @@ -73,4 +73,6 @@ type Database interface { DeleteServerFromDB(ctx context.Context, serverId string) error // DISCORD + + AddBotCommandStatistic(ctx context.Context, commandName string) error } diff --git a/db/mysql/mysql.go b/db/mysql/mysql.go index 4deba7b1..8b485496 100644 --- a/db/mysql/mysql.go +++ b/db/mysql/mysql.go @@ -4,12 +4,14 @@ import ( "context" "errors" "fmt" + "log" "os" "github.com/senchabot-opensource/monorepo/db" "github.com/senchabot-opensource/monorepo/packages/gosenchabot/models" "gorm.io/driver/mysql" "gorm.io/gorm" + "gorm.io/gorm/clause" "gorm.io/gorm/logger" ) @@ -413,3 +415,22 @@ func (m *MySQL) DeleteCommandAlias(ctx context.Context, commandAlias string, twi return nil, nil } + +func (m *MySQL) AddBotCommandStatistic(ctx context.Context, commandName string) error { + botCommandStatistic := models.BotCommandStatistic{ + CommandName: commandName, + Count: 1, + } + + result := m.DB.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Table: "bot_command_statistics", Name: "id"}}, + DoUpdates: clause.Assignments(map[string]interface{}{"count": gorm.Expr("GREATEST(count, VALUES(count))")}), + }).Create(&botCommandStatistic) + + if result.Error != nil { + log.Println("(AddBotcommandStatistic): db.Clauses.Create Error: ", result.Error.Error()) + return result.Error + } + + return nil +} diff --git a/packages/gosenchabot/models/models.go b/packages/gosenchabot/models/models.go index 873097b7..5afd2ad8 100644 --- a/packages/gosenchabot/models/models.go +++ b/packages/gosenchabot/models/models.go @@ -106,3 +106,13 @@ type TwitchStreamerData struct { StreamGame string `json:"game_name"` StartedAt string `json:"started_at"` } + +type BotCommandStatistic struct { + CommandName string `gorm:"column:command_name"` + Count int `gorm:"column:count"` +} + +type MessageProcessStatistic struct { + Content string `gorm:"column:content"` + Count int `gorm:"column:count"` +} diff --git a/packages/senchabot-prisma/prisma/schema.prisma b/packages/senchabot-prisma/prisma/schema.prisma index 61d815c7..8b2c8e38 100644 --- a/packages/senchabot-prisma/prisma/schema.prisma +++ b/packages/senchabot-prisma/prisma/schema.prisma @@ -105,6 +105,22 @@ model BotCommandAliases { @@map("bot_command_aliases") } +model BotCommandStatistic { + id Int @id @default(autoincrement()) + commandName String @unique @map("command_name") + count Int @default(0) @map("count") + + @@map("bot_command_statistics") +} + +model MessageProcessStatistic { + id Int @id @default(autoincrement()) + content String @unique @map("content") + count Int @default(0) @map("count") + + @@map("message_process_statistics") +} + model DiscordServer { id Int @id @default(autoincrement()) serverId String @unique @map("server_id") From 7364fb4b62e0f3ee4ac23eb982eb4e816e1ffc36 Mon Sep 17 00:00:00 2001 From: durmusrasit <12167194+durmusrasit@users.noreply.github.com> Date: Tue, 5 Sep 2023 02:20:58 +0300 Subject: [PATCH 2/5] fix: add missing db method --- db/mysql/common.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/db/mysql/common.go b/db/mysql/common.go index ecc829b3..0bb758e1 100644 --- a/db/mysql/common.go +++ b/db/mysql/common.go @@ -3,10 +3,12 @@ package mysql import ( "context" "errors" + "log" "github.com/senchabot-opensource/monorepo/packages/gosenchabot/models" "github.com/senchabot-opensource/monorepo/packages/gosenchabot/platform" "gorm.io/gorm" + "gorm.io/gorm/clause" ) func (m *MySQL) CreateBotCommand(ctx context.Context, botPlatform platform.Platform, commandName string, commandContent string, botPlatformId string, createdBy string) (*string, error) { @@ -402,3 +404,22 @@ func (m *MySQL) GetCommandList(ctx context.Context, botPlatform platform.Platfor return botCommandList, nil } + +func (m *MySQL) AddBotCommandStatistic(ctx context.Context, commandName string) error { + botCommandStatistic := models.BotCommandStatistic{ + CommandName: commandName, + Count: 1, + } + + result := m.DB.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Table: "bot_command_statistics", Name: "id"}}, + DoUpdates: clause.Assignments(map[string]interface{}{"count": gorm.Expr("GREATEST(count, VALUES(count))")}), + }).Create(&botCommandStatistic) + + if result.Error != nil { + log.Println("(AddBotcommandStatistic): db.Clauses.Create Error: ", result.Error.Error()) + return result.Error + } + + return nil +} From 77a504713339af6e4b2e5728daee6885932a64ed Mon Sep 17 00:00:00 2001 From: durmusrasit <12167194+durmusrasit@users.noreply.github.com> Date: Wed, 6 Sep 2023 13:10:34 +0300 Subject: [PATCH 3/5] fix: update AddBotCommandStatistic method --- apps/twitch-bot/internal/command/command.go | 6 +----- apps/twitch-bot/internal/service/service.go | 8 +++++--- db/mysql/common.go | 13 +++++-------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/apps/twitch-bot/internal/command/command.go b/apps/twitch-bot/internal/command/command.go index 83e8d174..d7cdd4e1 100644 --- a/apps/twitch-bot/internal/command/command.go +++ b/apps/twitch-bot/internal/command/command.go @@ -3,7 +3,6 @@ package command import ( "context" "fmt" - "log" "strings" "time" @@ -70,10 +69,7 @@ func (c *commands) IsSystemCommand(commandName string) bool { func (c *commands) Respond(ctx context.Context, message twitch.PrivateMessage, cmdName string, messageContent string) { c.client.Twitch.Say(message.Channel, messageContent) c.setCommandCooldown(message.User.Name) - err := c.service.AddBotCommandStatistic(ctx, cmdName) - if err != nil { - log.Println(err) - } + c.service.AddBotCommandStatistic(ctx, cmdName) c.service.SaveCommandActivity(ctx, cmdName, message.RoomID, message.User.DisplayName, message.User.ID) } diff --git a/apps/twitch-bot/internal/service/service.go b/apps/twitch-bot/internal/service/service.go index 37ff6eab..5a9dd475 100644 --- a/apps/twitch-bot/internal/service/service.go +++ b/apps/twitch-bot/internal/service/service.go @@ -37,7 +37,7 @@ type Service interface { CheckCommandAliasExist(ctx context.Context, commandAlias string, twitchChannelId string) (*string, error) DeleteCommandAlias(ctx context.Context, commandAlias string, twitchChannelId string) (*string, error) - AddBotCommandStatistic(ctx context.Context, commandName string) error + AddBotCommandStatistic(ctx context.Context, commandName string) } type services struct { @@ -213,6 +213,8 @@ func (s *services) DeleteCommandAlias(ctx context.Context, commandAlias string, return infoText, nil } -func (s *services) AddBotCommandStatistic(ctx context.Context, commandName string) error { - return s.AddBotCommandStatistic(ctx, commandName) +func (s *services) AddBotCommandStatistic(ctx context.Context, commandName string) { + if err := s.DB.AddBotCommandStatistic(ctx, commandName); err != nil { + fmt.Println(err.Error()) + } } diff --git a/db/mysql/common.go b/db/mysql/common.go index 0bb758e1..8a8052f5 100644 --- a/db/mysql/common.go +++ b/db/mysql/common.go @@ -406,18 +406,15 @@ func (m *MySQL) GetCommandList(ctx context.Context, botPlatform platform.Platfor } func (m *MySQL) AddBotCommandStatistic(ctx context.Context, commandName string) error { - botCommandStatistic := models.BotCommandStatistic{ - CommandName: commandName, - Count: 1, - } + botCommandStatistic := models.BotCommandStatistic{CommandName: commandName, Count: 1} result := m.DB.Clauses(clause.OnConflict{ - Columns: []clause.Column{{Table: "bot_command_statistics", Name: "id"}}, - DoUpdates: clause.Assignments(map[string]interface{}{"count": gorm.Expr("GREATEST(count, VALUES(count))")}), - }).Create(&botCommandStatistic) + Columns: []clause.Column{{Name: "id"}}, + DoUpdates: clause.Assignments(map[string]interface{}{"count": gorm.Expr("count + 1")}), + }).Where("command_name = ?", commandName).Create(&botCommandStatistic) if result.Error != nil { - log.Println("(AddBotcommandStatistic): db.Clauses.Create Error: ", result.Error.Error()) + log.Println("(AddBotcommandStatistic): db.Update Error: ", result.Error.Error()) return result.Error } From 8dce3d32006c657a44da7281f12f019c33553b93 Mon Sep 17 00:00:00 2001 From: durmusrasit <12167194+durmusrasit@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:33:16 +0300 Subject: [PATCH 4/5] refactor: add BotPlatformType field to BotCommandStatistic --- apps/twitch-bot/internal/service/service.go | 2 +- db/database.go | 2 +- db/mysql/common.go | 6 +++--- packages/gosenchabot/models/models.go | 10 +++------- packages/senchabot-prisma/prisma/schema.prisma | 16 +++++----------- 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/apps/twitch-bot/internal/service/service.go b/apps/twitch-bot/internal/service/service.go index 5a9dd475..b3ddfa0c 100644 --- a/apps/twitch-bot/internal/service/service.go +++ b/apps/twitch-bot/internal/service/service.go @@ -214,7 +214,7 @@ func (s *services) DeleteCommandAlias(ctx context.Context, commandAlias string, } func (s *services) AddBotCommandStatistic(ctx context.Context, commandName string) { - if err := s.DB.AddBotCommandStatistic(ctx, commandName); err != nil { + if err := s.DB.AddBotCommandStatistic(ctx, platform.TWITCH, commandName); err != nil { fmt.Println(err.Error()) } } diff --git a/db/database.go b/db/database.go index 8b78376d..df8d5a9a 100644 --- a/db/database.go +++ b/db/database.go @@ -62,5 +62,5 @@ type Database interface { // DISCORD - AddBotCommandStatistic(ctx context.Context, commandName string) error + AddBotCommandStatistic(ctx context.Context, botPlatform platform.Platform, commandName string) error } diff --git a/db/mysql/common.go b/db/mysql/common.go index 8a8052f5..8f608883 100644 --- a/db/mysql/common.go +++ b/db/mysql/common.go @@ -405,13 +405,13 @@ func (m *MySQL) GetCommandList(ctx context.Context, botPlatform platform.Platfor return botCommandList, nil } -func (m *MySQL) AddBotCommandStatistic(ctx context.Context, commandName string) error { - botCommandStatistic := models.BotCommandStatistic{CommandName: commandName, Count: 1} +func (m *MySQL) AddBotCommandStatistic(ctx context.Context, botPlatform platform.Platform, commandName string) error { + botCommandStatistic := models.BotCommandStatistic{CommandName: commandName, BotPlatformType: botPlatform, Count: 1} result := m.DB.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "id"}}, DoUpdates: clause.Assignments(map[string]interface{}{"count": gorm.Expr("count + 1")}), - }).Where("command_name = ?", commandName).Create(&botCommandStatistic) + }).Where("bot_platform_type = ?", botPlatform).Where("command_name = ?", commandName).Create(&botCommandStatistic) if result.Error != nil { log.Println("(AddBotcommandStatistic): db.Update Error: ", result.Error.Error()) diff --git a/packages/gosenchabot/models/models.go b/packages/gosenchabot/models/models.go index 0182468b..54f62998 100644 --- a/packages/gosenchabot/models/models.go +++ b/packages/gosenchabot/models/models.go @@ -111,11 +111,7 @@ type TwitchStreamerData struct { } type BotCommandStatistic struct { - CommandName string `gorm:"column:command_name"` - Count int `gorm:"column:count"` -} - -type MessageProcessStatistic struct { - Content string `gorm:"column:content"` - Count int `gorm:"column:count"` + BotPlatformType platform.Platform `gorm:"column:bot_platform_type"` + CommandName string `gorm:"column:command_name"` + Count int `gorm:"column:count"` } diff --git a/packages/senchabot-prisma/prisma/schema.prisma b/packages/senchabot-prisma/prisma/schema.prisma index 4c2d2246..7490b5dc 100644 --- a/packages/senchabot-prisma/prisma/schema.prisma +++ b/packages/senchabot-prisma/prisma/schema.prisma @@ -105,21 +105,15 @@ model BotCommandAliases { } model BotCommandStatistic { - id Int @id @default(autoincrement()) - commandName String @unique @map("command_name") - count Int @default(0) @map("count") + id Int @id @default(autoincrement()) + botPlatformType String? @map("bot_platform_type") + commandName String @map("command_name") + count Int @default(0) @map("count") + @@unique([botPlatformType, commandName]) @@map("bot_command_statistics") } -model MessageProcessStatistic { - id Int @id @default(autoincrement()) - content String @unique @map("content") - count Int @default(0) @map("count") - - @@map("message_process_statistics") -} - model DiscordServer { id Int @id @default(autoincrement()) serverId String @unique @map("server_id") From bf0d4fae2ea65bde7ee3dbf6c0bcb1c590baca89 Mon Sep 17 00:00:00 2001 From: durmusrasit <12167194+durmusrasit@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:33:48 +0300 Subject: [PATCH 5/5] feat: implement for discord bot --- apps/discord-bot/internal/command/command.go | 1 + apps/discord-bot/internal/service/service.go | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/apps/discord-bot/internal/command/command.go b/apps/discord-bot/internal/command/command.go index a21a8b70..6ddddbe2 100644 --- a/apps/discord-bot/internal/command/command.go +++ b/apps/discord-bot/internal/command/command.go @@ -65,6 +65,7 @@ func (c *commands) IsSystemCommand(commandName string) bool { func (c *commands) Respond(ctx context.Context, m *discordgo.MessageCreate, cmdName string, messageContent string) { c.dS.ChannelMessageSend(m.ChannelID, messageContent) c.setCommandCooldown(m.Author.Username) + c.service.AddBotCommandStatistic(ctx, cmdName) c.service.SaveCommandActivity(ctx, cmdName, m.GuildID, m.Author.Username, m.Author.ID) } diff --git a/apps/discord-bot/internal/service/service.go b/apps/discord-bot/internal/service/service.go index 77f61baa..1963d307 100644 --- a/apps/discord-bot/internal/service/service.go +++ b/apps/discord-bot/internal/service/service.go @@ -50,6 +50,8 @@ type Service interface { DeleteDiscordBotConfig(ctx context.Context, serverId, key string) (bool, error) GetDiscordBotConfig(ctx context.Context, discordServerId string, configKey string) (*models.DiscordBotConfigs, error) CheckDiscordBotConfig(ctx context.Context, discordServerId string, configKey string, configValue string) bool + + AddBotCommandStatistic(ctx context.Context, commandName string) } type service struct { @@ -223,3 +225,9 @@ func (s *service) CheckDiscordBotConfig(ctx context.Context, discordServerId str } // DISCORD BOT CONFIG + +func (s *service) AddBotCommandStatistic(ctx context.Context, commandName string) { + if err := s.DB.AddBotCommandStatistic(ctx, platform.DISCORD, commandName); err != nil { + fmt.Println(err.Error()) + } +}