-
Notifications
You must be signed in to change notification settings - Fork 3
/
ranklist.go
257 lines (225 loc) · 7.19 KB
/
ranklist.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/zhzxdev/azukiiro/storage"
)
type ranklistContextKey int
const injectionKey ranklistContextKey = iota
type ranklistContext struct {
TaskId string
ContestId string
}
func WithRanklistTask(ctx context.Context, taskId, contestId string) context.Context {
ctx = context.WithValue(ctx, injectionKey, ranklistContext{
TaskId: taskId,
ContestId: contestId,
})
return ctx
}
func LoadRanklistCtx(ctx context.Context) (string, string) {
inj := ctx.Value(injectionKey).(ranklistContext)
return inj.TaskId, inj.ContestId
}
type RanklistSettings struct {
ShowAfter int `json:"showAfter"`
ShowBefore int `json:"showBefore"`
}
type RanklistDTO struct {
Key string `json:"key"`
Name string `json:"name"`
Settings RanklistSettings `json:"settings"`
}
type PollRanklistRequest struct {
}
type PollRanklistResponse struct {
TaskId string `json:"taskId"`
ContestId string `json:"contestId"`
Ranklists []RanklistDTO `json:"ranklists"`
}
func PollRanklist(ctx context.Context, req *PollRanklistRequest) (*PollRanklistResponse, error) {
res := &PollRanklistResponse{}
raw, err := http.R().
SetContext(ctx).
SetBody(req).
SetResult(res).
Post("/api/runner/ranklist/poll")
err = loadError(raw, err)
if err != nil {
return nil, err
}
return res, nil
}
type RanklistTopstarItemMutation struct {
Score float64 `json:"score"`
Ts int `json:"ts"`
}
type RanklistTopstarItem struct {
UserId string `json:"userId"`
Mutations []*RanklistTopstarItemMutation `json:"mutations"`
}
type RanklistTopstar struct {
List []*RanklistTopstarItem `json:"list"`
}
type RanklistParticipantColumn struct {
Name string `json:"name"`
Description string `json:"description"`
}
type RanklistParticipantItemColumn struct {
Content string `json:"content"`
}
type RanklistParticipantItem struct {
Rank int `json:"rank"`
UserId string `json:"userId"`
Tags []string `json:"tags"`
Columns []*RanklistParticipantItemColumn `json:"columns"`
}
type RanklistParticipant struct {
Columns []*RanklistParticipantColumn `json:"columns"`
List []*RanklistParticipantItem `json:"list"`
}
type RanklistMetadata struct {
GeneratedAt int `json:"generatedAt"`
Description string `json:"description"`
}
type Ranklist struct {
Topstar *RanklistTopstar `json:"topstar,omitempty"`
Participant *RanklistParticipant `json:"participant"`
Metadata *RanklistMetadata `json:"metadata"`
}
type GetRanklistUploadUrlsResponse = []struct {
Key string `json:"key"`
Url string `json:"url"`
}
func GetRanklistUploadUrls(ctx context.Context) (*GetRanklistUploadUrlsResponse, error) {
taskId, contestId := LoadRanklistCtx(ctx)
res := &GetRanklistUploadUrlsResponse{}
raw, err := http.R().
SetContext(ctx).
SetResult(res).
Get("/api/runner/ranklist/task/" + contestId + "/" + taskId + "/uploadUrls")
err = loadError(raw, err)
if err != nil {
return nil, err
}
return res, nil
}
func SaveRanklist(ctx context.Context, ranklist map[string]*Ranklist) error {
res, err := GetRanklistUploadUrls(ctx)
if err != nil {
return err
}
urls := make(map[string]string)
for _, value := range *res {
urls[value.Key] = value.Url
}
for key, value := range ranklist {
str, err := json.Marshal(value)
if err != nil {
return err
}
err = storage.Upload(ctx, urls[key], str)
if err != nil {
return err
}
}
return nil
}
type CompleteRanklistTaskRequest struct {
RanklistLastSolutionId string `json:"ranklistLastSolutionId"`
}
func CompleteRanklistTask(ctx context.Context, req *CompleteRanklistTaskRequest) error {
taskId, contestId := LoadRanklistCtx(ctx)
raw, err := http.R().
SetContext(ctx).
SetBody(req).
Post("/api/runner/ranklist/task/" + contestId + "/" + taskId + "/complete")
return loadError(raw, err)
}
type GetRanklistSolutionsResponse []struct {
Id string `json:"_id"`
OrgId string `json:"orgId"`
ProblemId string `json:"problemId"`
ContestId string `json:"contestId"`
UserId string `json:"userId"`
ProblemDataHash string `json:"problemDataHash"`
State int `json:"state"`
SolutionDataHash string `json:"solutionDataHash"`
Score float64 `json:"score"`
Metrics map[string]float64 `json:"metrics"`
Status string `json:"status"`
Message string `json:"message"`
RunnerId string `json:"runnerId"`
CreatedAt int `json:"createdAt"`
SubmittedAt int `json:"submittedAt"`
CompletedAt int `json:"completedAt"`
}
func GetRanklistSolutions(ctx context.Context, since int) (*GetRanklistSolutionsResponse, error) {
taskId, contestId := LoadRanklistCtx(ctx)
res := &GetRanklistSolutionsResponse{}
raw, err := http.R().
SetContext(ctx).
SetQueryParam("since", fmt.Sprint(since)).
SetResult(res).
Get("/api/runner/ranklist/task/" + contestId + "/" + taskId + "/solutions")
err = loadError(raw, err)
if err != nil {
return nil, err
}
return res, nil
}
type GetRanklistParticipantsResponse []struct {
Id string `json:"_id" bson:"_id"`
UserId string `json:"userId" bson:"userId"`
ContestId string `json:"contestId" bson:"contestId"`
Tags []string `json:"tags" bson:"tags"`
Results map[string]struct {
SolutionCount int `json:"solutionCount" bson:"solutionCount"`
LastSolutionId string `json:"lastSolutionId" bson:"lastSolutionId"`
LastSolution struct {
Score float64 `json:"score" bson:"score"`
Status string `json:"status" bson:"status"`
CompletedAt int `json:"completedAt" bson:"completedAt"`
} `json:"lastSolution" bson:"lastSolution"`
} `json:"results" bson:"results"`
UpdatedAt int `json:"updatedAt" bson:"updatedAt"`
}
func GetRanklistParticipants(ctx context.Context, since int) (*GetRanklistParticipantsResponse, error) {
taskId, contestId := LoadRanklistCtx(ctx)
res := &GetRanklistParticipantsResponse{}
raw, err := http.R().
SetContext(ctx).
SetQueryParam("since", fmt.Sprint(since)).
SetResult(res).
Get("/api/runner/ranklist/task/" + contestId + "/" + taskId + "/participants")
err = loadError(raw, err)
if err != nil {
return nil, err
}
return res, nil
}
type GetRanklistProblemsResponse []struct {
Id string `json:"_id"`
Title string `json:"title"`
Tags []string `json:"tags"`
Settings struct {
Score float64 `json:"score"`
Slug string `json:"slug"`
SolutionCountLimit int `json:"solutionCountLimit"`
ShowAfter int `json:"showAfter"`
} `json:"settings"`
}
func GetRanklistProblems(ctx context.Context) (*GetRanklistProblemsResponse, error) {
taskId, contestId := LoadRanklistCtx(ctx)
res := &GetRanklistProblemsResponse{}
raw, err := http.R().
SetContext(ctx).
SetResult(res).
Get("/api/runner/ranklist/task/" + contestId + "/" + taskId + "/problems")
err = loadError(raw, err)
if err != nil {
return nil, err
}
return res, nil
}