-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathprocessor.cjs
More file actions
78 lines (61 loc) · 2.27 KB
/
Copy pathprocessor.cjs
File metadata and controls
78 lines (61 loc) · 2.27 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
'use strict';
const defaultTopkValues = [1, 5, 10, 20, 30, 50];
const topkValues = parseTopkValues(process.env.TOPK_VALUES);
function parseTopkValues(raw) {
if (!raw || typeof raw !== 'string') {
return defaultTopkValues;
}
const parsed = raw
.split(',')
.map((x) => Number.parseInt(x.trim(), 10))
.filter((x) => Number.isInteger(x) && x > 0);
return parsed.length > 0 ? parsed : defaultTopkValues;
}
function pickTopK(context, events, done) {
const idx = Math.floor(Math.random() * topkValues.length);
context.vars.topk = topkValues[idx];
done();
}
function validateTopKResponse(requestParams, response, context, events, done) {
if (response.statusCode !== 200) {
return done(new Error(`Expected status 200, got ${response.statusCode}`));
}
let body;
try {
body = typeof response.body === 'string' ? JSON.parse(response.body) : response.body;
} catch (err) {
return done(new Error(`Response is not valid JSON: ${err.message}`));
}
if (!Array.isArray(body)) {
return done(new Error('Response body should be a JSON array of stories'));
}
const expectedTopK = Number.parseInt(context.vars.topk || '30', 10);
if (body.length > expectedTopK) {
return done(new Error(`Response returned ${body.length} items, expected at most ${expectedTopK}`));
}
for (let i = 0; i < body.length; i++) {
const item = body[i];
if (typeof item !== 'object' || item === null) {
return done(new Error(`Item at index ${i} is not an object`));
}
if (typeof item.id !== 'number' || typeof item.score !== 'number' || typeof item.title !== 'string') {
return done(new Error(`Item at index ${i} is missing required fields (id, score, title)`));
}
if (i > 0 && body[i - 1].score < item.score) {
return done(new Error('Response is not sorted by score in descending order'));
}
}
context.vars.storyCount = body.length;
done();
}
function recordStoryCount(requestParams, response, context, events, done) {
const count = Number.isInteger(context.vars.storyCount) ? context.vars.storyCount : 0;
events.emit('histogram', 'stories.returned_per_request', count);
events.emit('counter', 'stories.items_returned_total', count);
done();
}
module.exports = {
pickTopK,
validateTopKResponse,
recordStoryCount
};