Fix: During import skip nodes which cannot be parsed.

This commit is contained in:
baldo 2018-01-05 20:31:29 +01:00
parent c6e94ecfc1
commit 8513ff0f3d

View file

@ -139,34 +139,14 @@ angular.module('ffffng')
var isValidMac = Validator.forConstraint(Constraints.node.mac); var isValidMac = Validator.forConstraint(Constraints.node.mac);
function parseNodesJson(body, callback) { function parseTimestamp (timestamp) {
Logger.tag('monitoring', 'information-retrieval').debug('Parsing nodes.json...');
function parseTimestamp(timestamp) {
if (!_.isString(timestamp)) { if (!_.isString(timestamp)) {
return moment.invalid(); return moment.invalid();
} }
return moment.utc(timestamp); return moment.utc(timestamp);
} }
var data = {}; function parseNode (importTimestamp, nodeData, nodeId) {
try {
var json = JSON.parse(body);
if (json.version !== 1) {
return callback(new Error('Unexpected nodes.json version: ' + json.version));
}
data.importTimestamp = parseTimestamp(json.timestamp);
if (!data.importTimestamp.isValid()) {
return callback(new Error('Invalid timestamp: ' + json.timestamp));
}
if (!_.isPlainObject(json.nodes)) {
return callback(new Error('Invalid nodes object type: ' + (typeof json.nodes)));
}
data.nodes = _.values(_.map(json.nodes, function (nodeData, nodeId) {
if (!_.isPlainObject(nodeData)) { if (!_.isPlainObject(nodeData)) {
throw new Error( throw new Error(
'Node ' + nodeId + ': Unexpected node type: ' + (typeof nodeData) 'Node ' + nodeId + ': Unexpected node type: ' + (typeof nodeData)
@ -180,8 +160,7 @@ angular.module('ffffng')
} }
if (!_.isPlainObject(nodeData.nodeinfo.network)) { if (!_.isPlainObject(nodeData.nodeinfo.network)) {
throw new Error( throw new Error(
'Node ' + nodeId + ': Unexpected nodeinfo.network type: ' + 'Node ' + nodeId + ': Unexpected nodeinfo.network type: ' + (typeof nodeData.nodeinfo.network)
(typeof nodeData.nodeinfo.network)
); );
} }
@ -212,18 +191,61 @@ angular.module('ffffng')
} }
var site = null; var site = null;
// jshint -W106
if (_.isPlainObject(nodeData.nodeinfo.system) && _.isString(nodeData.nodeinfo.system.site_code)) { if (_.isPlainObject(nodeData.nodeinfo.system) && _.isString(nodeData.nodeinfo.system.site_code)) {
site = nodeData.nodeinfo.system.site_code; site = nodeData.nodeinfo.system.site_code;
} }
// jshint +W106
return { return {
mac: mac, mac: mac,
importTimestamp: data.importTimestamp, importTimestamp: importTimestamp,
state: isOnline ? 'ONLINE' : 'OFFLINE', state: isOnline ? 'ONLINE' : 'OFFLINE',
lastSeen: lastSeen, lastSeen: lastSeen,
site: site site: site
}; };
})); }
function parseNodesJson (body, callback) {
Logger.tag('monitoring', 'information-retrieval').debug('Parsing nodes.json...');
var data = {};
try {
var json = JSON.parse(body);
if (json.version !== 1) {
return callback(new Error('Unexpected nodes.json version: ' + json.version));
}
data.importTimestamp = parseTimestamp(json.timestamp);
if (!data.importTimestamp.isValid()) {
return callback(new Error('Invalid timestamp: ' + json.timestamp));
}
if (!_.isPlainObject(json.nodes)) {
return callback(new Error('Invalid nodes object type: ' + (typeof json.nodes)));
}
data.nodes = _.filter(
_.values(
_.map(
json.nodes,
function (nodeData, nodeId) {
try {
return parseNode(data.importTimestamp, nodeData, nodeId);
}
catch (error) {
Logger.tag('monitoring', 'information-retrieval').error(error);
return null;
}
}
)
),
function (node) {
return node !== null;
}
);
} }
catch (error) { catch (error) {
return callback(error); return callback(error);