forked from yuhanfang/riot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_aggregation.go
228 lines (212 loc) · 5.8 KB
/
data_aggregation.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
// Package data_aggregation pulls data from a Riot API client and stores it in
// a centralized table.
package data_aggregation
import (
"context"
"log"
"sync"
"time"
"github.com/Tilo-K/riot/apiclient"
"github.com/Tilo-K/riot/constants/queue"
"github.com/Tilo-K/riot/constants/region"
)
// Match is the serialized format of match data. Match or Timeline may be nil
// if the data is unavailable from the API. Matches are unique by ID and
// Region.
type Match struct {
ID int64
Region region.Region
Match *apiclient.Match
Timeline *apiclient.MatchTimeline
}
// Sink stores match data in an aggregate source.
type Sink interface {
// MatchExists returns true if the match ID for the given region is already
// stored in the sink.
MatchExists(ctx context.Context, r region.Region, id int64) (bool, error)
// SaveMatches saves the matches into the sink. It is not an error to save a
// match that has already been saved. Sink must not duplicate the match, but
// may either ignore or update the saved match.
SaveMatches(ctx context.Context, m []Match) error
}
// Aggregator aggregates match data from a client into a sink. It is illegal to
// construct an instance directly. Use NewAggregator to return a valid
// instance.
type Aggregator struct {
client apiclient.Client
sink Sink
}
// NewAggregator returns an Aggregator configured to query the Riot API with
// the given client, and aggregate results into the given Sink.
func NewAggregator(c apiclient.Client, s Sink) *Aggregator {
return &Aggregator{
client: c,
sink: s,
}
}
// AggregateChallengerLeagueMatches aggregates all games from accounts
// currently in challenger league in the given region in queue. Only games
// since the given begin time are considered. The zero time value indicates
// that all games should be considered.
func (a Aggregator) AggregateChallengerLeagueMatches(ctx context.Context, r region.Region, q queue.Queue, since time.Time) error {
league, err := a.client.GetChallengerLeague(ctx, r, q)
if err != nil {
return err
}
accountIDs := a.getAccountIDsInLeague(ctx, r, league)
matches := a.GetMatchIDsForAccounts(ctx, r, q, since, accountIDs)
a.UploadMatches(ctx, r, matches)
return nil
}
func (a Aggregator) GetMatchIDsForAccounts(ctx context.Context, r region.Region, q queue.Queue, since time.Time, accountIDs []string) map[int64]struct{} {
// Query recent matches for each account.
opts := apiclient.GetMatchlistOptions{
Queue: []queue.Queue{q},
BeginTime: &since,
}
matches := make(map[int64]struct{})
matchIDs := make(chan int64)
wg := sync.WaitGroup{}
done := make(chan bool)
for _, account := range accountIDs {
wg.Add(1)
account := account
// Process each account concurrently.
go func() {
defer wg.Done()
matchlist, err := a.client.GetMatchlist(ctx, r, account, &opts)
if err == nil {
var (
subwg sync.WaitGroup
subdone = make(chan bool)
)
// Process each account match concurrently.
for _, m := range matchlist.Matches {
subwg.Add(1)
m := m
go func() {
defer subwg.Done()
exists, err := a.sink.MatchExists(ctx, r, m.GameID)
if err != nil {
log.Printf("MatchExists failed for region %s game %d: %v", r, m.GameID, err)
}
if err == nil && !exists {
select {
case matchIDs <- m.GameID:
case <-ctx.Done():
return
}
}
}()
// Block until cancelled or all matches processed.
go func() {
subwg.Wait()
subdone <- true
}()
select {
case <-ctx.Done():
case <-subdone:
}
}
} else {
log.Printf("GetMatchlist failed for region %s account %s: %v", r, account, err)
}
}()
}
// Block until cancelled or all matches processed.
go func() {
wg.Wait()
done <- true
}()
more := true
for more {
select {
case <-ctx.Done():
more = false
case match := <-matchIDs:
matches[match] = struct{}{}
case <-done:
more = false
}
}
return matches
}
func (a Aggregator) UploadMatches(ctx context.Context, r region.Region, matches map[int64]struct{}) {
// Retrieve match and timeline data for each match.
wg := sync.WaitGroup{}
for m := range matches {
match, err := a.client.GetMatch(ctx, r, m)
if err != nil && err != apiclient.ErrDataNotFound {
log.Printf("GetMatch failed for region %s game %d: %v", r, m, err)
continue
}
timeline, err := a.client.GetMatchTimeline(ctx, r, m)
if err != nil && err != apiclient.ErrDataNotFound {
log.Printf("GetMatchTimeline failed for region %s game %d: %v", r, m, err)
continue
}
upload := Match{
ID: m,
Region: r,
Match: match,
Timeline: timeline,
}
wg.Add(1)
go func() {
defer wg.Done()
err := a.sink.SaveMatches(ctx, []Match{upload})
if err != nil {
log.Printf("SaveMatches failed for region %s game %d: %v", upload.Region, upload.ID, err)
}
}()
}
done := make(chan bool)
go func() {
wg.Wait()
done <- true
}()
select {
case <-ctx.Done():
case <-done:
}
}
func (a Aggregator) getAccountIDsInLeague(ctx context.Context, r region.Region, league *apiclient.LeagueList) []string {
var (
accounts = make(chan string)
accountIDs []string
wg sync.WaitGroup
done = make(chan bool)
)
for _, entry := range league.Entries {
entry := entry
wg.Add(1)
go func() {
defer wg.Done()
summoner, err := a.client.GetBySummonerID(ctx, r, entry.SummonerID)
if err != nil {
log.Printf("GetBySummonerID failed for region %s summoner %s: %v", r, entry.SummonerID, err)
return
}
select {
case <-ctx.Done():
case accounts <- summoner.AccountID:
}
}()
}
go func() {
wg.Wait()
done <- true
}()
more := true
for more {
select {
case <-ctx.Done():
more = false
case id := <-accounts:
accountIDs = append(accountIDs, id)
case <-done:
more = false
}
}
return accountIDs
}