-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmatches.ts
More file actions
272 lines (245 loc) · 7.18 KB
/
matches.ts
File metadata and controls
272 lines (245 loc) · 7.18 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* eslint-disable no-console */
import mongoose from "mongoose";
export interface Match {
updated: Date;
created: Date;
uuid: string;
id: number;
name: string;
type: string;
subType: string;
templateName: string;
/* match_date string as-is e.g. "2024-01-01" */
date: string;
fetched?: Date;
uploaded?: Date;
}
interface AlgoliaMatchNumericFilters {
timestamp_utc_updated: number;
}
export interface MatchDef {
match_id: string;
match_name: string;
match_type: string;
match_subtype: string;
match_creationdate: string;
match_modifieddate: string;
match_date: string; // 2024-12-31 format
templateName: string;
}
const MatchesSchema = new mongoose.Schema<Match>(
{
updated: Date,
created: Date,
id: { type: Number, required: true, unique: true },
name: String,
type: String,
subType: String,
templateName: String,
uuid: { type: String, required: true, unique: true },
date: String,
fetched: Date,
uploaded: Date,
},
{ strict: false },
);
MatchesSchema.index({ id: -1 });
MatchesSchema.index({ fetched: 1, uploaded: 1 });
MatchesSchema.index({ updated: 1, uploaded: 1 });
MatchesSchema.index({ updated: 1 });
MatchesSchema.index({ fetched: 1 });
export const Matches = mongoose.model("Matches", MatchesSchema);
const MATCHES_PER_FETCH = 1000;
const _idRange = fromId =>
encodeURIComponent(`id: ${fromId + 1} TO ${fromId + MATCHES_PER_FETCH + 1}`);
const filtersForTemplate = template => {
if (!template) {
return "";
}
return `"templateName:${template}"`;
};
const fetchMatchesRange = async (
fromId,
template = "USPSA",
): Promise<(Match & AlgoliaMatchNumericFilters)[]> => {
console.log(`fetching from ${fromId}`);
const {
results: [{ hits }],
} = await (
await fetch(process.env.ALGOLIA_URL!, {
body: JSON.stringify({
requests: [
{
indexName: "postmatches",
params: `hitsPerPage=${MATCHES_PER_FETCH}&query=&numericFilters=${_idRange(
fromId,
)}&facetFilters=${filtersForTemplate(template)}`,
},
],
}),
method: "POST",
})
).json();
return hits.map(h => ({
updated: new Date(`${h.updated}Z`),
created: new Date(`${h.created}Z`),
id: h.id,
name: h.match_name,
uuid: h.match_id,
date: h.match_date,
timestamp_utc_updated: h.timestamp_utc_updated,
type: h.match_type,
subType: h.match_subtype,
templateName: h.templateName,
}));
};
export const matchFromMatchDef = (
h: MatchDef,
forcedTemplateName?: string,
): Match & AlgoliaMatchNumericFilters => {
if (!h) {
return h;
}
const updated = new Date(`${h.match_modifieddate}Z`);
return {
updated,
created: new Date(`${h.match_creationdate}Z`),
id: Number.parseInt(h.match_id.split("-").reverse()[0], 16),
name: h.match_name,
uuid: h.match_id,
date: h.match_date,
timestamp_utc_updated: updated.getTime(),
type: h.match_type,
subType: h.match_subtype,
templateName: forcedTemplateName || h.templateName,
};
};
const fetchMatchesRangeByTimestamp = async (
latestTimestamp: number,
template = "USPSA",
): Promise<(Match & AlgoliaMatchNumericFilters)[]> => {
console.log(`fetching up until ${latestTimestamp}`);
const {
results: [{ hits }],
} = await (
await fetch(process.env.ALGOLIA_URL!, {
body: JSON.stringify({
requests: [
{
indexName: "postmatches",
params: `hitsPerPage=${MATCHES_PER_FETCH}&query=&numericFilters=${encodeURIComponent(
`timestamp_utc_updated < ${latestTimestamp}`,
)}&facetFilters=${filtersForTemplate(template)}`,
},
],
}),
method: "POST",
})
).json();
return hits.map(h => ({
updated: new Date(`${h.updated}Z`),
created: new Date(`${h.created}Z`),
id: h.id,
name: h.match_name,
uuid: h.match_id,
date: h.match_date,
timestamp_utc_updated: h.timestamp_utc_updated,
}));
};
/**
* @param startId match id to start with, defaults to 220k, somewhere around May 2024,
* which was before the USPSA Import Loss.
*/
const fetchMoreMatches = async (startId = 220000, template, onPageCallback) => {
let resultsCount = 0;
let lastResults: Match[] = [];
let curId = startId;
do {
lastResults = (await fetchMatchesRange(curId, template)).sort((a, b) => b.id - a.id);
process.stdout.write(".");
curId = lastResults[0]?.id || Number.MAX_SAFE_INTEGER;
resultsCount += lastResults.length;
await onPageCallback(lastResults);
} while (lastResults.length > 0);
return resultsCount;
};
/**
* @param startDate match updated date to start with
*/
export const fetchMoreMatchesByTimestamp = async (
startTimestamp,
template,
onPageCallback,
) => {
let resultsCount = 0;
let lastFetchResults: (Match & AlgoliaMatchNumericFilters)[] = [];
let curTimestamp = 8640_000_000_000_000 / 1000; // max valid js date / 1000 (for algolia, which uses seconds timestamps)
do {
lastFetchResults = (await fetchMatchesRangeByTimestamp(curTimestamp, template)).sort(
(a, b) => a.timestamp_utc_updated - b.timestamp_utc_updated,
);
process.stdout.write(".");
const earliestFetchedTimestamp = lastFetchResults[0]?.timestamp_utc_updated;
curTimestamp = earliestFetchedTimestamp || startTimestamp + 1;
const lastInTheTimeWindowResults = lastFetchResults.filter(
c => c.timestamp_utc_updated >= startTimestamp,
);
resultsCount += lastInTheTimeWindowResults.length;
await onPageCallback(lastInTheTimeWindowResults);
} while (curTimestamp > startTimestamp && lastFetchResults.length > 0);
return resultsCount;
};
/**
* Fetch loop by id, won't produce updates, since id stays the same, and we're fetching
* only ids that are higher than the highest one we already have in the database.
*
* Saves matches into Matches collecion, which is later used by upload loop.
*
* Use for initial fetch of matches, for day-to-day use fetchAndSaveMoreMatchesByUpdateDate
*/
export const fetchAndSaveMoreMatchesById = async () => {
const lastMatch = await Matches.findOne().sort({ id: -1 });
console.log(`lastMatchId = ${lastMatch?.id}`);
return fetchMoreMatches(lastMatch?.id, "", async matches =>
Matches.bulkWrite(
matches.map(m => ({
updateOne: {
filter: {
uuid: m.uuid,
},
update: { $set: m },
upsert: true,
},
})),
),
);
};
/**
* Same as fetchAndSaveMoreMatchesById, but uses updated date.
*
* Should overwrite some matches if they were updated after previous fetch.
*/
export const fetchAndSaveMoreMatchesByUpdatedDate = async () => {
const lastMatch = await Matches.findOne().sort({ updated: -1 });
console.log(
`lastUpdatedMatch= ${lastMatch?.updated?.toLocaleDateString?.("en-us", {
timeZone: "UTC",
})}`,
);
return fetchMoreMatchesByTimestamp(
Math.floor((lastMatch?.updated || new Date()).getTime() / 1000 + 1),
"",
async matches =>
Matches.bulkWrite(
matches.map(m => ({
updateOne: {
filter: {
uuid: m.uuid,
},
update: { $set: m },
upsert: true,
},
})),
),
);
};