-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoutesController.js
418 lines (382 loc) · 10.1 KB
/
RoutesController.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
const request = require("request");
const fs = require("fs");
const _ = require("lodash");
const childProcess = require("child_process");
const config = require("../config/config");
var generatorProcess = { process: null, perc: 0 }; //Global reference to generator child process
let options = {
host: "http://127.0.0.1:8001",
redfish_defs: "/redfish/v1/TelemetryService/MetricReportDefinitions"
};
let def_path = `${options.host}${options.redfish_defs}`;
// Render Static Panels in Grafana
exports.getPanels = function(req, res) {
res.render("index.hbs", {
pageTitle: "Redfish Insight",
currentYear: new Date().getFullYear()
});
};
exports.getDefinitionCollection = function(req, res) {
request(
{
url: def_path,
json: true
},
(error, response, body) => {
if (!body) {
res.status(404);
res.json({
error: "Could not retrieve metrics"
});
} else if (error) {
console.log(error);
} else {
let metrics = [];
for (var i = 0; i < body.Members.length; i++) {
let uri = body.Members[i]["@odata.id"];
metrics.push(uri.split("/")[uri.split("/").length - 1]);
}
res.json(metrics);
}
}
);
};
// Currently being used for the landing page.
exports.getAvailableMetrics = function(req, res) {
request(
{
url: def_path,
json: true
},
(error, response, body) => {
if (!body) {
res.status(404);
res.json({
error: "Could not retrieve metrics"
});
} else if (error) {
console.log(error);
} else {
let metrics = [];
for (var i = 0; i < body.Members.length; i++) {
let uri = body.Members[i]["@odata.id"];
metrics.push(uri.substr(53, uri.length));
}
// console.log(metrics);
res.render("landing.hbs", {
pageTitle: "Redfish Insight",
metrics: metrics
});
}
}
);
};
// Route handler for /metrics/:metric
// Note: the mockup server isn't sending proper HTTP responses for
// bad requests. This is why !body is checked after the request is made.
exports.getMetric = function(req, res) {
let metric = req.params.metric;
// console.log(typeof metric);
request(
{
url: `${def_path}/${metric}`,
json: true
},
(error, response, body) => {
if (!body) {
res.status(404);
res.json({
error: "Metric does not exist",
metric: metric
});
} else if (error) {
console.log(error);
} else {
res.json(body);
}
}
);
};
//Route handler for /dataGenerator
exports.getDataGenerator = function(req, res) {
res.render("rfModeller.hbs", {
configPath:
"Config files located at: " +
fs.realpathSync("./resources/js/dataGenerator"),
currentYear: new Date().getFullYear(),
pageTitle: "Redfish Modeler"
});
};
exports.generateMockData = function(req, res) {
var q = [];
//for ajax call to get percentage complete
if (req.query.perc) {
res.writeHead(200, { "Content-Type": "application/json" });
if (generatorProcess.process != null) {
res.end("" + generatorProcess.perc);
} else {
res.end("100");
}
return;
}
//if not a percentage call, start the generator
if (req.query.time) q.push("-t", req.query.time);
if (req.query.config) q.push("-c", req.query.config);
if (req.query.interval) q.push("-i", req.query.interval);
function generate(path, callback) {
generatorProcess.process = childProcess.fork("rfmockdatacreator.js", q, {
cwd: path
});
generatorProcess.perc = 0;
var invoked = false;
// listen for errors
generatorProcess.process.on("error", function(err) {
if (invoked) return;
invoked = true;
callback(err);
});
//update perc variable when received from child process
generatorProcess.process.on("message", msg => {
generatorProcess.perc = parseInt(msg);
});
// execute the callback
generatorProcess.process.on("exit", function(code) {
generatorProcess.process = null;
if (invoked) return;
invoked = true;
var err = code === 0 ? null : new Error("exit code " + code);
callback(err);
});
}
generate("./resources/js/dataGenerator/", function(err) {
if (!err) {
res.download("./resources/js/dataGenerator/output.csv");
} else {
console.log(err);
}
});
};
/*
* Updates host in memory.
* TODO: Update host before anything else happens.
*/
exports.postRedfishHost = function(req, res) {
console.log(`Host POST: ${JSON.stringify(req.body, undefined, 3)}`);
let body = req.body;
if (body) {
let host = body.host;
updateHost(host);
res.json(body);
} else {
console.log("Host not updated...");
res.json({
error: "POST body should only contain attribute 'host'"
});
}
};
// TODO - this isn't actually doing anything at the moment.
// We need it to update some data so the app knows what to
// get and send to Influx.
exports.postSelectedMetrics = function(req, res) {
console.log(`Metrics POST: ${JSON.stringify(req.body, undefined, 3)}`);
let selectedMetrics = req.body;
if (!_.isEmpty(selectedMetrics.payload)) {
_.forOwn(selectedMetrics.payload, (val, key) => {
// patchMetricToEnabled(key);
});
// updateConfig(selectedMetrics.payload);
res.json(selectedMetrics);
} else {
res.json({
error: "Bad Format"
});
}
};
const updateConfig = newSelection => {
fs.readFile("metrics_config.json", "utf8", (err, data) => {
if (err) {
throw err;
} else {
configData = JSON.parse(data);
_.forOwn(newSelection, (val, key) => {
!configData.enabledReports.includes(key) &&
configData.enabledReports.push(key) &&
configData.selections.push({
from: key,
metrics: val
});
});
fs.writeFile(
"metrics_config.json",
JSON.stringify(configData, undefined, 3),
"utf8",
(err, data) => {
if (err) {
console.log(err);
}
}
);
}
});
};
/*
* Handler for front-end POST subscription type. If 'sub', it is set up
* to run the subscription workflow, which is currently working.
*/
exports.postSubType = function(req, res) {
console.log(`Sub type POST: ${JSON.stringify(req.body, undefined, 3)}`);
let selectedSubType = req.body;
if (
selectedSubType.type &&
["sse", "sub", "poll"].includes(selectedSubType.type)
) {
if (selectedSubType.type === "sub") {
subscribeToEvents();
}
res.json(selectedSubType);
} else {
res.json({
error:
"POST body must include property 'type'. Acceptable values: poll, sse, or sub"
});
}
};
/*
* This is the handler for events coming from Redfish service.
* So far, no simulator has been able to send events to rfInsight.
*
* TODO test on Linux?
*/
exports.handleEventIn = function(req, res) {
console.log("Received a metric report from Redfish service.");
res.json(req.body);
};
const subscribeToEvents = () => {
/*
* The post body for the subscription is based on the EventDestination
* schema. A POST to redfish/v1/EventService/Subscriptions with this body
* should result in a 201 response code. Verify POST success by doing a GET
* on redfish/v1/EventService/Subscriptions.
*/
request.post(
{
url: `${options.host}/redfish/v1/EventService/Subscriptions`,
json: true,
rejectUnauthorized: false,
/*
* See DMTF RMS github issue #53 for conversation regarding this format.
*/
body: {
EventFormatType: "MetricReport",
EventTypes: ["MetricReport"],
Destination: "http://localhost:8080/api/event_in"
}
},
(error, response, body) => {
if (error) {
console.log(error);
}
// console.log(response);
}
);
};
exports.getCurrentConfig = function(req, res) {
let configData;
fs.readFile("metrics_config.json", "utf8", (err, data) => {
if (err) {
throw err;
} else {
configData = JSON.parse(data);
res.json(configData);
}
});
};
exports.getRfModeller = function(req, res) {
res.render("rfModeller.hbs", {
pageTitle: "Redfish Modeller",
currentYear: new Date().getFullYear()
});
};
exports.getModellerConfig = function(req, res) {
let modellerConfig;
// let modellerConfig = require("resources/js/dataGenerator/config.json");
fs.readFile(
"./resources/js/dataGenerator/config.json",
"utf8",
(err, data) => {
if (err) {
throw err;
} else {
modellerConfig = JSON.parse(data);
res.json(modellerConfig);
}
}
);
};
exports.postModellerConfig = function(req, res) {
let modellerConfig = JSON.parse(data);
fs.writeFile(
"./resources/js/dataGenerator/config.json",
modellerConfig,
"utf8",
(err, data) => {
if (err) {
console.log(err);
}
}
);
};
const patchMetricToEnabled = report => {
request.patch(
{
url: `${def_path}/${report}`,
json: true,
body: {
// This is temporary and will need to be changed upon schema update.
// Status.State is read-only.
MetricReportDefinitionEnabled: true
}
},
(error, response, body) => {
// console.log(response);
}
);
};
const saveSelectionToDisk = selection => {
let currentConfiguration = JSON.stringify(selection, undefined, 3);
fs.writeFile(
"metrics_config.json",
currentConfiguration,
"utf8",
(err, data) => {
if (err) {
console.log(err);
}
}
);
};
const updateSubType = newSubType => {
fs.readFile("metrics_config.json", "utf8", (err, data) => {
if (err) {
throw err;
} else {
configData = JSON.parse(data);
configData.sub_method = newSubType;
fs.writeFile(
"metrics_config.json",
JSON.stringify(configData, undefined, 3),
"utf8",
err => {
if (err) {
console.log(err);
}
}
);
}
});
};
const updateHost = host => {
options.host = host;
def_path = `${options.host}${options.redfish_defs}`;
console.log(options.host);
};