-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
262 lines (207 loc) · 5.57 KB
/
index.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
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
/* Simple Json Grafana Plugin requires to implement the following endpoints
/ should return 200 ok. Used for "Test connection" on the datasource config page.
/search used by the find metric options on the query tab in panels.
/query should return metrics based on input.
/annotations should return annotations.
/tag-keys should return tag keys for ad hoc filters.
/tag-values should return tag values for ad hoc filters.
*/
var log = console.log;
console.log = function () {
log.apply(console, [Date.now()].concat(arguments));
};
var express = require('express');
var bodyParser = require('body-parser');
var _ = require('lodash');
var auth = require('./lib/auth.js');
var app = express();
const config = require('config');
if (config.has('AUTH') && config.has('AUTH.enabled') && config.has('AUTH.secret')) {
if (config.get('AUTH.enabled') == "true") {
console.log("enabling basic authentication");
app.use(auth);
} else {
console.log("authentication disabled in config file.. disabling basic authentication");
}
} else {
console.log("auth key not present.. disabling basic authentication")
}
var dataStore = require('./lib/dataStore.js');
/* //not working
var searchOptions = [
{ "text" :"Year to Date Aggregated", "value": 1},
{ "text" :"Year to Date by Tag", "value": 2},
{ "text" :"Month to Date Aggregated", "value": 3},
{ "text" :"Month to Date by Tag", "value": 4}
];
*/
var searchOptions = [
"Year to Date Aggregated",
"Year to Date by Tag",
"Month to Date Aggregated",
"Month to Date by Tag",
"Last Month Aggregated",
"Last Month By Tag"
];
app.use(bodyParser.json());
function setCORSHeaders(res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST");
res.setHeader("Access-Control-Allow-Headers", "accept, content-type", "X-Auth-Token");
}
function callServiceApi(target, callback) {
let opts = {
granularity: 'DAILY'
};
let dataStoreKey = "";
if (target.target == searchOptions[1] ||
target.target == searchOptions[3] ||
target.target == searchOptions[5]) {
opts.groupBy = [{
'Type': 'TAG',
'Key': 'Aplicacion'
}]
}
//FIX this code
if (target.target == searchOptions[0] || target.target == searchOptions[1]) {
dataStoreKey = "ytd";
}
if (target.target == searchOptions[2] || target.target == searchOptions[3]) {
dataStoreKey = "mtd";
}
if (target.target == searchOptions[4] || target.target == searchOptions[5]) {
dataStoreKey = "lmt";
}
dataStore.getResults(dataStoreKey, opts, function (error, data) {
return callback(error, data);
});
}
function buildResultTable(data, target) {
let table = {
columns: [{
text: 'Start',
type: 'date'
},
{
text: 'End',
type: 'date'
},
{
text: 'Cost',
type: 'number'
}
],
rows: [],
type: "table"
};
// add Rows
Object.keys(data.ResultsByTime).forEach(function (key) {
let row = [
data.ResultsByTime[key].TimePeriod.Start,
data.ResultsByTime[key].TimePeriod.End,
parseFloat(data.ResultsByTime[key].Total.BlendedCost.Amount)
];
table.rows.push(row);
});
return table;
}
function buildResultTimeSeries(data, target) {
let result = {
"target": target.target,
"datapoints": []
};
Object.keys(data.ResultsByTime).forEach(function (key) {
let dataPoint = [
parseFloat(data.ResultsByTime[key].Total.BlendedCost.Amount),
new Date(data.ResultsByTime[key].TimePeriod.End).getTime()
];
result.datapoints.push(dataPoint);
});
return result;
}
app.all('/', function (req, res) {
setCORSHeaders(res);
//console.log(req.url);
//console.log(req.body);
res.send('It works!!');
res.end();
});
app.all('/search', function (req, res) {
setCORSHeaders(res);
//console.log(req.url);
//console.log(req.body);
var result = searchOptions;
res.json(result);
res.end();
});
app.all('/query', function (req, res) {
setCORSHeaders(res);
//console.log(req.url);
//console.log(req.body);
var tsResult = [];
if (req.body.adhocFilters && req.body.adhocFilters.length > 0) {
console.log('filters data request');
}
_.each(req.body.targets, function (target) {
callServiceApi(target, function (err, data) {
if (err)
return;
if (target.type === 'table') {
tsResult.push(buildResultTable(data, target));
} else {
tsResult.push(buildResultTimeSeries(data, target));
}
});
});
//console.dir(tsResult, {depth : null});
res.json(tsResult);
res.end();
});
app.all('/annotations', function (req, res) {
setCORSHeaders(res);
//console.log(req.url);
//console.log(req.body);
var annotations = [{
annotation: annotation,
"title": "When is the next ",
"time": 1450754160000,
text: "teeext",
tags: "taaags"
}];
res.json(annotations);
res.end();
});
app.all('/tag[\-]keys', function (req, res) {
setCORSHeaders(res);
//console.log(req.url);
//console.log(req.body);
var tagKeys = [{
"type": "string",
"text": "Country"
}];
res.json(tagKeys);
res.end();
});
app.all('/tag[\-]values', function (req, res) {
setCORSHeaders(res);
//console.log(req.url);
//console.log(req.body);
var countryTagValues = [{
'text': 'SE'
},
{
'text': 'DE'
},
{
'text': 'US'
}
];
if (req.body.key == 'City') {
res.json(cityTagValues);
} else if (req.body.key == 'Country') {
res.json(countryTagValues);
}
res.end();
});
app.listen(8090);
console.log("Server is listening to port 8090");