2019-01-23 03:28:45 +00:00
|
|
|
var request = require('request-promise');
|
2019-09-01 17:01:38 +00:00
|
|
|
var common = require('../../common');
|
|
|
|
var logger = require('../logger');
|
2019-03-02 17:55:19 +00:00
|
|
|
var options = {};
|
2019-01-23 03:28:45 +00:00
|
|
|
|
|
|
|
exports.forwardingHistory = (req, res, next) => {
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/switch';
|
2019-01-23 03:28:45 +00:00
|
|
|
options.form = {};
|
|
|
|
if (undefined !== req.body.num_max_events) {
|
|
|
|
options.form.num_max_events = req.body.num_max_events;
|
|
|
|
}
|
|
|
|
if (undefined !== req.body.index_offset) {
|
|
|
|
options.form.index_offset = req.body.index_offset;
|
|
|
|
}
|
|
|
|
if (undefined !== req.body.end_time) {
|
|
|
|
options.form.end_time = req.body.end_time;
|
|
|
|
}
|
|
|
|
if (undefined !== req.body.start_time) {
|
|
|
|
options.form.start_time = req.body.start_time;
|
|
|
|
}
|
|
|
|
options.form = JSON.stringify(options.form);
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Switch', msg: 'Switch Post Options: ' + JSON.stringify(options)});
|
2019-01-23 03:28:45 +00:00
|
|
|
request.post(options).then((body) => {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Switch', msg: 'Switch Post Response: ' + JSON.stringify(body)});
|
2019-01-23 03:28:45 +00:00
|
|
|
if(undefined === body || body.error) {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Switch', lineNum: 27, msg: 'Switch Post Erroe: ' + JSON.stringify((undefined === body) ? 'Error From Server!' : body.error)});
|
2019-01-23 03:28:45 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: "Switch post failed!",
|
|
|
|
error: (undefined === body) ? 'Error From Server!' : body.error
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (undefined !== body.forwarding_events) {
|
|
|
|
body.forwarding_events.forEach(event => {
|
|
|
|
event.timestamp_str = (undefined === event.timestamp) ? '' : common.convertTimestampToDate(event.timestamp);
|
|
|
|
});
|
|
|
|
body.forwarding_events = common.sortDescByKey(body.forwarding_events, 'timestamp');
|
|
|
|
}
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Switch', msg: 'Forwarding History Received: ' + JSON.stringify(body)});
|
2019-01-23 03:28:45 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Switch', lineNum: 44, msg: 'Switch Post Error: ' + JSON.stringify(err)});
|
2019-01-23 03:28:45 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: "Switch post failed!",
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|