-
Notifications
You must be signed in to change notification settings - Fork 103
/
stats.js
82 lines (67 loc) · 2.06 KB
/
stats.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
81
82
'use strict';
const _get = require('lodash.get');
const moment = require('moment');
const log = require('@cumulus/common/log');
const handle = require('../lib/response').handle;
const Stats = require('../es/stats');
function getType(event) {
let index;
const supportedTypes = {
granules: 'granule',
pdrs: 'pdr',
collections: 'collection',
logs: 'logs',
providers: 'provider',
executions: 'execution'
};
const typeRequested = _get(event, 'queryStringParameters.type', null);
const type = _get(supportedTypes, typeRequested);
return { type, index };
}
function summary(event, cb) {
let params = _get(event, 'queryStringParameters', {});
if (!params) {
params = {};
}
params.timestamp__from = _get(
params,
'timestamp__from',
moment().subtract(1, 'day').unix()
);
params.timestamp__to = _get(params, 'timestamp__to', Date.now());
const stats = new Stats({ queryStringParameters: params });
stats.query().then((r) => cb(null, r)).catch((e) => cb(e));
}
function histogram(event, cb) {
const type = getType(event);
const stats = new Stats(event, type.type, type.index);
stats.histogram().then((r) => cb(null, r)).catch((e) => cb(e));
}
function count(event, cb) {
const type = getType(event);
const stats = new Stats(event, type.type, type.index);
stats.count().then((r) => cb(null, r)).catch((e) => cb(e));
}
function average(event, cb) {
const type = getType(event);
const stats = new Stats(event, type.type, type.index);
stats.avg().then((r) => cb(null, r)).catch((e) => cb(e));
}
function handler(event, context) {
log.debug(event);
handle(event, context, true, (cb) => {
if (event.httpMethod === 'GET' && event.resource.includes('/stats/histogram')) {
histogram(event, cb);
}
else if (event.httpMethod === 'GET' && event.resource.includes('/stats/aggregate')) {
count(event, cb);
}
else if (event.httpMethod === 'GET' && event.resource.includes('/stats/average')) {
average(event, cb);
}
else {
summary(event, cb);
}
});
}
module.exports = handler;