-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
stats.go
93 lines (78 loc) · 2.52 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package server
import (
"fmt"
"net/http"
"github.com/Pigmice2733/peregrine-backend/internal/analysis"
ihttp "github.com/Pigmice2733/peregrine-backend/internal/http"
"github.com/Pigmice2733/peregrine-backend/internal/store"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
// eventStats analyzes the event-wide statistics of every team at an event with submitted reports
func (s *Server) eventStats() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventKey := vars["eventKey"]
event, err := s.Store.GetEvent(r.Context(), eventKey)
if _, ok := err.(store.ErrNoResults); ok {
ihttp.Error(w, http.StatusNotFound)
return
} else if err != nil {
ihttp.Error(w, http.StatusInternalServerError)
go s.Logger.WithError(err).Error("retrieving event")
return
}
if !s.checkEventAccess(event.RealmID, r) {
ihttp.Error(w, http.StatusForbidden)
return
}
if event.SchemaID == nil {
ihttp.Respond(w, fmt.Errorf("no schema found"), http.StatusBadRequest)
return
}
// Get new team data from TBA
if err := s.updateTeamKeys(r.Context(), eventKey); err != nil {
// 404 if eventKey isn't a real event
if _, ok := errors.Cause(err).(store.ErrNoResults); ok {
ihttp.Error(w, http.StatusNotFound)
return
}
ihttp.Error(w, http.StatusInternalServerError)
go s.Logger.WithError(err).Error("updating team key data")
return
}
var reports []store.Report
realmID, err := ihttp.GetRealmID(r)
if err != nil {
reports, err = s.Store.GetEventReports(r.Context(), eventKey, nil)
} else {
reports, err = s.Store.GetEventReports(r.Context(), eventKey, &realmID)
}
if _, ok := err.(store.ErrNoResults); ok {
ihttp.Error(w, http.StatusNotFound)
return
}
schema, err := s.Store.GetSchemaByID(r.Context(), *event.SchemaID)
if _, ok := err.(store.ErrNoResults); ok {
ihttp.Error(w, http.StatusNotFound)
return
} else if err != nil {
ihttp.Error(w, http.StatusInternalServerError)
go s.Logger.WithError(err).Error("retrieving event schema")
return
}
teamKeys, err := s.Store.GetTeamKeys(r.Context(), eventKey)
if err != nil {
ihttp.Error(w, http.StatusInternalServerError)
go s.Logger.WithError(err).Error("retrieving team key data")
return
}
analyzedStats, err := analysis.AnalyzeReports(schema, reports, teamKeys)
if err != nil {
ihttp.Error(w, http.StatusInternalServerError)
go s.Logger.WithError(err).Error("analyzing event data")
return
}
ihttp.Respond(w, analyzedStats, http.StatusOK)
}
}