This repository was archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathhandler.js
80 lines (75 loc) · 1.99 KB
/
handler.js
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
const hdr = require("hdr-histogram-js");
const Redis = require("ioredis");
if (typeof client === 'undefined') {
var client = new Redis(fixUrl(process.env.REDIS_URL));
}
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
};
const SIZE = 10000;
module.exports.get = async (event) => {
if (!event.queryStringParameters || !event.queryStringParameters.name) {
return {
statusCode: 400,
headers: headers,
body: JSON.stringify(
{
message: 'Invalid parameters. Name is needed.',
}
),
};
}
const name = event.queryStringParameters.name;
const data = await client.lrange(name, 0, SIZE);
const histogram = hdr.build();
data.forEach(item => {
histogram.recordValue(item);
})
return {
statusCode: 200,
body: JSON.stringify(
{
histogram: histogram
}
),
};
};
module.exports.record = async (event) => {
let body = JSON.parse(event.body)
if (!body || !body.name || !body.values) {
return {
statusCode: 400,
headers: headers,
body: JSON.stringify(
{
message: 'Invalid parameters. Name and values are needed.',
}
),
};
}
const name = body.name;
const values = body.values;
await client.lpush(name, values)
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Success',
name: name
}
),
};
};
function fixUrl(url) {
if (!url) {
return ''
}
if (url.startsWith('redis://') && !url.startsWith('redis://:')) {
return url.replace('redis://', 'redis://:')
}
if (url.startsWith('rediss://') && !url.startsWith('rediss://:')) {
return url.replace('rediss://', 'rediss://:')
}
return url
}