added endpoint for sidebar metastats

This commit is contained in:
2021-10-23 21:53:06 +02:00
parent cd6cd6bf58
commit 9ebebe18af
3 changed files with 287 additions and 14 deletions

74
main.go
View File

@@ -46,6 +46,7 @@ var (
configFlag = flag.String("config", "config.yaml", "Set config to use")
authCodeFlag = flag.String("authcode", "", "Provide Steam AuthCode to login")
journalLogFlag = flag.Bool("journal", false, "Log to systemd journal instead of stdout")
sqlDebugFlag = flag.Bool("sqldebug", false, "Debug SQL queries")
)
func housekeeping() {
@@ -146,6 +147,64 @@ func housekeeping() {
}
}
func getPlayerMeta(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains)
id := mux.Vars(r)["id"]
l := mux.Vars(r)["limit"]
var limit int
var err error
if l != "" {
limit, err = strconv.Atoi(l)
if err != nil {
log.Infof("[GPM] limit not an int: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
} else {
limit = 4
}
tPlayer, err := utils.GetPlayer(db, id, conf.Steam.APIKey, nil)
if err != nil {
log.Infof("[GP] Player not found: %+v", err)
w.WriteHeader(http.StatusNotFound)
return
}
metaStats := new(utils.MetaStatsResponse)
err = rdc.Get(context.Background(), fmt.Sprintf(utils.SideMetaCacheKey, tPlayer.ID), &metaStats)
if err != nil {
metaStats, err = utils.GetMetaStats(tPlayer, db.Lock, limit)
if err != nil {
log.Infof("[GPM] Unable to get MetaStats: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = rdc.Set(&cache.Item{
Ctx: context.Background(),
Key: fmt.Sprintf(utils.SideMetaCacheKey, tPlayer.ID),
Value: metaStats,
TTL: time.Hour * 24 * 30,
})
if err != nil {
log.Errorf("[GPM] Failure saving to cache: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Debugf("[GPM] SideMetaStats for %d saved to cache", tPlayer.ID)
} else {
log.Debugf("[GPM] SideMetaStats for %d from cache", tPlayer.ID)
}
err = utils.SendJSON(metaStats, w)
if err != nil {
log.Errorf("[GPM] Unable to marshal JSON: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func getPlayer(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains)
id := mux.Vars(r)["id"]
@@ -205,7 +264,7 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
}
metaStats := new(utils.MatchStats)
err = rdc.Get(context.Background(), fmt.Sprintf("csgowtfd_meta_%d", tPlayer.ID), &metaStats)
err = rdc.Get(context.Background(), fmt.Sprintf(utils.MatchMetaCacheKey, tPlayer.ID), &metaStats)
if err != nil {
wins, ties, losses, err := utils.GetMatchStats(tPlayer, db.Lock)
if err != nil {
@@ -222,9 +281,9 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
err = rdc.Set(&cache.Item{
Ctx: context.Background(),
Key: fmt.Sprintf("csgowtfd_meta_%d", tPlayer.ID),
Key: fmt.Sprintf(utils.MatchMetaCacheKey, tPlayer.ID),
Value: response.MatchStats,
TTL: time.Hour * 12,
TTL: time.Hour * 24 * 30,
})
if err != nil {
log.Errorf("[GP] Failure saving to cache: %v", err)
@@ -233,7 +292,7 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
}
log.Debugf("[GP] Metastats for %d saved to cache", tPlayer.ID)
} else {
log.Debugf("[GP] Metastats for %d from redis", tPlayer.ID)
log.Debugf("[GP] Metastats for %d from cache", tPlayer.ID)
response.MatchStats = &utils.MatchStats{
Win: metaStats.Win,
@@ -734,6 +793,10 @@ func main() {
}(db.Client)
}
if *sqlDebugFlag {
db.Client = db.Client.Debug()
}
if err := db.Client.Schema.Create(
context.Background(),
migrate.WithDropIndex(true),
@@ -779,7 +842,8 @@ func main() {
// routes
router = mux.NewRouter().StrictSlash(true)
router.HandleFunc("/player/{id}", getPlayer).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc(`/player/{id}/after/{time:\d+}`, getPlayer).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc(`/player/{id}/next/{time:\d+}`, getPlayer).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc(`/player/{id}/meta/{limit:\d*}`, getPlayerMeta).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/player/{id}/track", postPlayerTrack).Methods(http.MethodPost, http.MethodOptions)
router.HandleFunc("/player/{id}/track", deletePlayerTrack).Methods(http.MethodOptions, http.MethodDelete)
router.HandleFunc("/match/parse/{sharecode}", getMatchParse).Methods(http.MethodGet, http.MethodOptions)