From 24e49048073e0b63cb989037004e56655b0630fc Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Sat, 23 Dec 2017 18:49:09 +0400 Subject: [PATCH] /find_user moved to custom SQL query Now it's much, MUCH faster. --- lib/dbmapping/profiles.go | 23 ++++++++++++++++++++++- lib/users/responders.go | 18 ++++++++---------- lib/users/users.go | 21 ++++++--------------- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/lib/dbmapping/profiles.go b/lib/dbmapping/profiles.go index fcba4b1..80cc99f 100644 --- a/lib/dbmapping/profiles.go +++ b/lib/dbmapping/profiles.go @@ -7,7 +7,7 @@ import ( "time" ) -// Profile is a struct, which represents `profiles` table item in databse. +// Profile is a struct, which represents `profiles` table item in database. type Profile struct { ID int `db:"id"` PlayerID int `db:"player_id"` @@ -24,3 +24,24 @@ type Profile struct { Crystalls int `db:"crystalls"` CreatedAt time.Time `db:"created_at"` } + +// ProfileWithAddons is a struct, which represents `profiles` table item in database and some good external fields. +type ProfileWithAddons struct { + ID int `db:"id"` + PlayerID int `db:"player_id"` + TelegramID int `db:"telegram_id"` + LeagueID int `db:"league_id"` + LeagueSymbol string `db:"league_symbol"` + Nickname string `db:"nickname"` + TelegramNickname string `db:"telegram_nickname"` + LevelID int `db:"level_id"` + Pokeballs int `db:"pokeballs"` + Wealth int `db:"wealth"` + PokememesWealth int `db:"pokememes_wealth"` + Exp int `db:"exp"` + EggExp int `db:"egg_exp"` + Power int `db:"power"` + WeaponID int `db:"weapon_id"` + Crystalls int `db:"crystalls"` + CreatedAt time.Time `db:"created_at"` +} diff --git a/lib/users/responders.go b/lib/users/responders.go index 6c35cd4..4636629 100644 --- a/lib/users/responders.go +++ b/lib/users/responders.go @@ -31,19 +31,17 @@ func (u *Users) FindByName(update *tgbotapi.Update) string { message := "*Найденные игроки:*\n" for i := range usersArray { - message += "#" + strconv.Itoa(usersArray[i].Player.ID) - message += " " + usersArray[i].League.Symbol - message += " " + usersArray[i].Profile.Nickname - if usersArray[i].Profile.TelegramNickname != "" { - message += " (@" + u.FormatUsername(usersArray[i].Profile.TelegramNickname) + ")" + message += "#" + strconv.Itoa(usersArray[i].PlayerID) + message += " " + usersArray[i].LeagueSymbol + message += " " + usersArray[i].Nickname + if usersArray[i].TelegramNickname != "" { + message += " (@" + u.FormatUsername(usersArray[i].TelegramNickname) + ")" } - message += " /profile" + strconv.Itoa(usersArray[i].Player.ID) + "\n" - message += "Telegram ID: " + strconv.Itoa(usersArray[i].Player.TelegramID) + "\n" - message += "Последнее обновление: " + usersArray[i].Profile.CreatedAt.Format("02.01.2006 15:04:05") + "\n" + message += " /profile" + strconv.Itoa(usersArray[i].PlayerID) + "\n" + message += "Telegram ID: " + strconv.Itoa(usersArray[i].TelegramID) + "\n" + message += "Последнее обновление: " + usersArray[i].CreatedAt.Format("02.01.2006 15:04:05") + "\n" } - c.Log.Debug(message) - msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) msg.ParseMode = "Markdown" diff --git a/lib/users/users.go b/lib/users/users.go index 1557d43..64ec4e0 100644 --- a/lib/users/users.go +++ b/lib/users/users.go @@ -7,7 +7,6 @@ import ( "github.com/go-telegram-bot-api/telegram-bot-api" "lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping" "strconv" - "strings" ) // Internal functions for Users package @@ -48,21 +47,13 @@ func (u *Users) getUsersWithProfiles() ([]dbmapping.PlayerProfile, bool) { return usersArray, true } -func (u *Users) findUserByName(pattern string) ([]dbmapping.PlayerProfile, bool) { - allUsers, ok := u.getUsersWithProfiles() - if !ok { - return allUsers, ok - } +func (u *Users) findUserByName(pattern string) ([]dbmapping.ProfileWithAddons, bool) { + selectedUsers := []dbmapping.ProfileWithAddons{} - selectedUsers := []dbmapping.PlayerProfile{} - - for i := range allUsers { - user := allUsers[i] - if user.HaveProfile { - if strings.Contains(user.Profile.TelegramNickname, pattern) || strings.Contains(user.Profile.Nickname, pattern) { - selectedUsers = append(selectedUsers, user) - } - } + err := c.Db.Select(&selectedUsers, c.Db.Rebind("SELECT * FROM (SELECT p.*, l.symbol AS league_symbol, l.id AS league_id, pl.telegram_id FROM players pl, profiles p, leagues l WHERE p.player_id = pl.id AND l.id = pl.league_id AND (p.nickname LIKE ? OR p.telegram_nickname LIKE ?) ORDER BY p.id desc LIMIT 100000) AS find_users_table GROUP BY player_id"), "%"+pattern+"%", "%"+pattern+"%") + if err != nil { + c.Log.Error(err.Error()) + return selectedUsers, false } return selectedUsers, true