added caching to chat translation
This commit is contained in:
82
main.go
82
main.go
@@ -667,6 +667,7 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
lang, _ := tag[0].Base()
|
||||
|
||||
var translate bool
|
||||
if trans != "" {
|
||||
@@ -684,50 +685,63 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background())
|
||||
if err != nil {
|
||||
log.Infof("[GMC] match %d not found: %+v", matchId, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
resp := map[string][]*utils.ChatResponse{}
|
||||
|
||||
for _, stat := range tStats {
|
||||
steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10)
|
||||
if _, ok := resp[steamid]; !ok {
|
||||
resp[steamid] = make([]*utils.ChatResponse, 0)
|
||||
err = rdc.Get(context.Background(), fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang), &resp)
|
||||
if err != nil {
|
||||
tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background())
|
||||
if err != nil {
|
||||
log.Infof("[GMC] match %d not found: %+v", matchId, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if translate {
|
||||
lang, _ := tag[0].Base()
|
||||
translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(),
|
||||
conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout)
|
||||
if err != nil {
|
||||
log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err)
|
||||
goto sendNormalResp
|
||||
for _, stat := range tStats {
|
||||
steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10)
|
||||
if _, ok := resp[steamid]; !ok {
|
||||
resp[steamid] = make([]*utils.ChatResponse, 0)
|
||||
}
|
||||
|
||||
if srcLang == lang.String() {
|
||||
goto sendNormalResp
|
||||
}
|
||||
if translate {
|
||||
translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(),
|
||||
conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout)
|
||||
if err != nil {
|
||||
log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err)
|
||||
goto sendNormalResp
|
||||
}
|
||||
|
||||
if srcLang == lang.String() {
|
||||
goto sendNormalResp
|
||||
}
|
||||
|
||||
resp[steamid] = append(resp[steamid], &utils.ChatResponse{
|
||||
Message: translated,
|
||||
AllChat: stat.AllChat,
|
||||
Tick: stat.Tick,
|
||||
TranslatedFrom: srcLang,
|
||||
TranslatedTo: lang.String(),
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
sendNormalResp:
|
||||
resp[steamid] = append(resp[steamid], &utils.ChatResponse{
|
||||
Message: translated,
|
||||
AllChat: stat.AllChat,
|
||||
Tick: stat.Tick,
|
||||
TranslatedFrom: srcLang,
|
||||
TranslatedTo: lang.String(),
|
||||
Message: stat.Message,
|
||||
AllChat: stat.AllChat,
|
||||
Tick: stat.Tick,
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
sendNormalResp:
|
||||
resp[steamid] = append(resp[steamid], &utils.ChatResponse{
|
||||
Message: stat.Message,
|
||||
AllChat: stat.AllChat,
|
||||
Tick: stat.Tick,
|
||||
|
||||
err = rdc.Set(&cache.Item{
|
||||
Ctx: context.Background(),
|
||||
Key: fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang),
|
||||
Value: resp,
|
||||
TTL: time.Hour * 24 * 30,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("[GMC] Failure saving to cache: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = utils.SendJSON(resp, w)
|
||||
|
@@ -233,6 +233,7 @@ const (
|
||||
shareCodeURLEntry = "https://api.steampowered.com/ICSGOPlayers_730/GetNextMatchSharingCode/v1?key=%s&steamid=%d&steamidkey=%s&knowncode=%s"
|
||||
CachePrefix = "csgowtfd_"
|
||||
SideMetaCacheKey = CachePrefix + "side_meta_%d"
|
||||
MatchChatCacheKey = CachePrefix + "chat_%d_%s"
|
||||
)
|
||||
|
||||
//goland:noinspection SpellCheckingInspection
|
||||
|
Reference in New Issue
Block a user