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