-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphGenerator.js
81 lines (73 loc) · 1.91 KB
/
graphGenerator.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
import ESSearch from './search';
import SVGBuilder from './svgbuilder';
import S3Output from './s3-output';
const buildRequestJSON = (paramJSON) => {
let dateRange;
if (Object.prototype.hasOwnProperty.call(paramJSON, 'search')) {
dateRange = paramJSON.search.find(x => x.dateRange);
} else {
dateRange = [Date.now() - 1, Date.now()];
}
const startDate = new Date(dateRange[0]);
const endDate = new Date(dateRange[1]);
return {
index: 'posts',
size: 0,
body: {
aggs: {
time_split: {
date_histogram: {
field: 'DateCreated',
interval: '1h'
}
}
},
query: {
bool: {
filter: [
{
range: {
DateCreated: {
gte: `${startDate.toISOString()}`
}
}
},
{
range: {
DateCreated: {
lte: `${endDate.toISOString()}`
}
}
}
]
}
}
}
};
};
class graphGenerator {
constructor(ConnectionOptions, bucketName) {
this._search = new ESSearch(ConnectionOptions);
this._build = new SVGBuilder();
this._store = new S3Output(bucketName);
}
// Entry Point.
async generateGraph(paramJSON) {
const reportData = await this.getReportData(paramJSON);
const reportSVG = await this.buildGraph(reportData);
return this.saveToBucket(reportSVG, paramJSON.download);
}
// Gets the search results data from ES
async getReportData(paramJSON) {
return this._search.search(buildRequestJSON(paramJSON));
}
async buildGraph(searchResults) {
return this._build.build(searchResults);
}
// Saves said graph to the S3 Bucket
async saveToBucket(svgString, downloadMode) {
this._store.append(svgString);
return this._store.close(downloadMode);
}
}
export default graphGenerator;