2019-01-23 03:28:45 +00:00
|
|
|
var request = require('request-promise');
|
2020-02-12 22:59:51 +00:00
|
|
|
var request = require('request-promise');
|
2019-09-01 17:01:38 +00:00
|
|
|
var common = require('../../common');
|
2021-02-21 19:02:31 +00:00
|
|
|
var logger = require('../shared/logger');
|
2019-03-02 17:55:19 +00:00
|
|
|
var options = {};
|
2020-02-12 22:59:51 +00:00
|
|
|
var num_max_events = 100;
|
|
|
|
var responseData = { forwarding_events: [], last_offset_index: 0 };
|
2019-01-23 03:28:45 +00:00
|
|
|
|
2020-02-12 22:59:51 +00:00
|
|
|
exports.forwardingHistory = (req, res, next) => {
|
|
|
|
this.getAllForwardingEvents(req.body.start_time, req.body.end_time, 0, (eventsResponse) => {
|
|
|
|
if (eventsResponse.error) {
|
|
|
|
res.status(500).json(eventsResponse);
|
|
|
|
} else {
|
|
|
|
res.status(201).json(eventsResponse);
|
|
|
|
}
|
2020-01-06 15:42:42 +00:00
|
|
|
});
|
2020-02-12 22:59:51 +00:00
|
|
|
};
|
2020-01-06 15:42:42 +00:00
|
|
|
|
2020-02-12 22:59:51 +00:00
|
|
|
exports.getAllForwardingEvents = (start, end, offset, callback) => {
|
2020-02-14 00:53:43 +00:00
|
|
|
if (offset === 0) { responseData = { forwarding_events: [], last_offset_index: 0 }; }
|
2020-02-12 22:59:51 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/switch';
|
2020-02-12 22:59:51 +00:00
|
|
|
options.form = {};
|
|
|
|
if (start) { options.form.start_time = start; }
|
|
|
|
if (end) { options.form.end_time = end; }
|
|
|
|
options.form.num_max_events = num_max_events;
|
|
|
|
options.form.index_offset = offset;
|
|
|
|
options.form = JSON.stringify(options.form);
|
|
|
|
logger.info({fileName: 'Switch', msg: 'Forwarding History Params: ' + options.form});
|
|
|
|
request.post(options).then((body) => {
|
|
|
|
logger.info({fileName: 'Switch', msg: 'Forwarding History: ' + JSON.stringify(body)});
|
2020-02-13 02:27:46 +00:00
|
|
|
if (body.forwarding_events) {
|
|
|
|
responseData.forwarding_events.push(...body.forwarding_events);
|
2020-02-12 22:59:51 +00:00
|
|
|
}
|
2020-02-13 02:27:46 +00:00
|
|
|
if (!body.last_offset_index || body.last_offset_index < offset + num_max_events) {
|
|
|
|
responseData.last_offset_index = body.last_offset_index ? body.last_offset_index : 0
|
|
|
|
if (responseData.forwarding_events) {
|
|
|
|
responseData.forwarding_events.forEach(event => {
|
|
|
|
event.timestamp_str = !event.timestamp ? '' : common.convertTimestampToDate(event.timestamp);
|
|
|
|
});
|
|
|
|
responseData.forwarding_events = common.sortDescByKey(responseData.forwarding_events, 'timestamp');
|
|
|
|
}
|
|
|
|
callback(responseData);
|
|
|
|
} else {
|
|
|
|
this.getAllForwardingEvents(start, end, offset + num_max_events, callback);
|
|
|
|
}
|
2020-05-03 19:52:38 +00:00
|
|
|
})
|
|
|
|
.catch(errRes => {
|
|
|
|
let err = JSON.parse(JSON.stringify(errRes));
|
|
|
|
if (err.options && err.options.headers && err.options.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
logger.error({fileName: 'Switch', lineNum: 54, msg: 'Get All Forwarding Events Error: ' + JSON.stringify(err)});
|
2020-02-12 22:59:51 +00:00
|
|
|
callback({
|
|
|
|
message: "Forwarding Events Failed!",
|
2019-01-23 03:28:45 +00:00
|
|
|
error: err.error
|
|
|
|
});
|
2020-01-06 15:42:42 +00:00
|
|
|
});
|
2020-02-12 22:59:51 +00:00
|
|
|
}
|