Display of online state for nodes and hostname for monitoring info in admin panel.
This commit is contained in:
parent
c0ae97e298
commit
cd746a41ea
9 changed files with 168 additions and 56 deletions
server
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE node_state ADD COLUMN hostname VARCHAR(32);
|
||||
ALTER TABLE node_state ADD COLUMN monitoring_state VARCHAR(10);
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
angular.module('ffffng').factory('NodeInformationRetrievalJob', function (MonitoringService, Logger) {
|
||||
return {
|
||||
description: 'Fetches the nodes.json and calculates and stores the monitoring status for nodes with active monitoring.',
|
||||
description: 'Fetches the nodes.json and calculates and stores the monitoring / online status for registered nodes.',
|
||||
|
||||
run: function (callback) {
|
||||
MonitoringService.retrieveNodeInformation(function (err) {
|
||||
|
|
|
@ -22,6 +22,7 @@ require('./app');
|
|||
require('./router');
|
||||
require('./libs');
|
||||
|
||||
require('./utils/databaseUtil');
|
||||
require('./utils/errorTypes');
|
||||
require('./utils/resources');
|
||||
require('./utils/strings');
|
||||
|
|
|
@ -3,8 +3,11 @@
|
|||
angular.module('ffffng').factory('NodeResource', function (
|
||||
Constraints,
|
||||
Validator,
|
||||
Logger,
|
||||
MonitoringService,
|
||||
NodeService,
|
||||
_,
|
||||
deepExtend,
|
||||
Strings,
|
||||
Resources,
|
||||
ErrorTypes
|
||||
|
@ -110,22 +113,62 @@ angular.module('ffffng').factory('NodeResource', function (
|
|||
return node.token;
|
||||
});
|
||||
|
||||
var filteredNodes = Resources.filter(
|
||||
realNodes,
|
||||
['hostname', 'nickname', 'email', 'token', 'mac', 'key'],
|
||||
restParams
|
||||
);
|
||||
var total = filteredNodes.length;
|
||||
var macs = _.map(realNodes, function (node) {
|
||||
return node.mac;
|
||||
});
|
||||
|
||||
var sortedNodes = Resources.sort(
|
||||
filteredNodes,
|
||||
['hostname', 'nickname', 'email', 'token', 'mac', 'key', 'coords', 'monitoringState'],
|
||||
restParams
|
||||
);
|
||||
var pageNodes = Resources.getPageEntities(sortedNodes, restParams);
|
||||
MonitoringService.getByMacs(macs, function (err, nodeStateByMac) {
|
||||
if (err) {
|
||||
Logger.tag('nodes', 'admin').error('Error getting nodes by MACs:', err);
|
||||
return Resources.error(res, {data: 'Internal error.', type: ErrorTypes.internalError});
|
||||
}
|
||||
|
||||
res.set('X-Total-Count', total);
|
||||
return Resources.success(res, pageNodes);
|
||||
var enhancedNodes = _.map(realNodes, function (node) {
|
||||
var nodeState = nodeStateByMac[node.mac];
|
||||
if (nodeState) {
|
||||
return deepExtend({}, node, {
|
||||
onlineState: nodeState.state
|
||||
});
|
||||
}
|
||||
|
||||
return node;
|
||||
});
|
||||
|
||||
var filteredNodes = Resources.filter(
|
||||
enhancedNodes,
|
||||
[
|
||||
'hostname',
|
||||
'nickname',
|
||||
'email',
|
||||
'token',
|
||||
'mac',
|
||||
'key',
|
||||
'onlineState'
|
||||
],
|
||||
restParams
|
||||
);
|
||||
var total = filteredNodes.length;
|
||||
|
||||
var sortedNodes = Resources.sort(
|
||||
filteredNodes,
|
||||
[
|
||||
'hostname',
|
||||
'nickname',
|
||||
'email',
|
||||
'token',
|
||||
'mac',
|
||||
'key',
|
||||
'coords',
|
||||
'onlineState',
|
||||
'monitoringState'
|
||||
],
|
||||
restParams
|
||||
);
|
||||
var pageNodes = Resources.getPageEntities(sortedNodes, restParams);
|
||||
|
||||
res.set('X-Total-Count', total);
|
||||
return Resources.success(res, pageNodes);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -5,7 +5,9 @@ angular.module('ffffng')
|
|||
_,
|
||||
async,
|
||||
config,
|
||||
deepExtend,
|
||||
Database,
|
||||
DatabaseUtil,
|
||||
ErrorTypes,
|
||||
Logger,
|
||||
moment,
|
||||
|
@ -34,10 +36,12 @@ angular.module('ffffng')
|
|||
|
||||
return Database.run(
|
||||
'INSERT INTO node_state ' +
|
||||
'(mac, state, last_seen, import_timestamp, (last_status_mail_sent, last_status_mail_type) OR last_status_mail_type IS NULL) ' +
|
||||
'(hostname, mac, monitoring_state, state, last_seen, import_timestamp, last_status_mail_sent, last_status_mail_type) ' +
|
||||
'VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[
|
||||
node.hostname,
|
||||
node.mac,
|
||||
node.monitoringState,
|
||||
nodeData.state,
|
||||
nodeData.lastSeen.unix(),
|
||||
nodeData.importTimestamp.unix(),
|
||||
|
@ -66,45 +70,37 @@ angular.module('ffffng')
|
|||
|
||||
return Database.run(
|
||||
'UPDATE node_state ' +
|
||||
'SET state = ?, last_seen = ?, import_timestamp = ?, modified_at = ?' +
|
||||
'SET hostname = ?, monitoring_state = ?, state = ?, last_seen = ?, import_timestamp = ?, modified_at = ?' +
|
||||
'WHERE id = ? AND mac = ?',
|
||||
[
|
||||
nodeData.state, nodeData.lastSeen.unix(), nodeData.importTimestamp.unix(), moment().unix(),
|
||||
row.id, node.mac
|
||||
node.hostname,
|
||||
node.monitoringState,
|
||||
nodeData.state,
|
||||
nodeData.lastSeen.unix(),
|
||||
nodeData.importTimestamp.unix(),
|
||||
moment().unix(),
|
||||
|
||||
row.id,
|
||||
node.mac
|
||||
],
|
||||
callback
|
||||
);
|
||||
}
|
||||
|
||||
function deleteNodeInformation(nodeData, node, callback) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('Node is not being monitored, deleting monitoring data: %s', nodeData.mac);
|
||||
return Database.run(
|
||||
'DELETE FROM node_state WHERE mac = ? AND import_timestamp < ?',
|
||||
[node.mac, nodeData.importTimestamp.unix()],
|
||||
callback
|
||||
);
|
||||
}
|
||||
|
||||
function storeNodeInformation(nodeData, node, callback) {
|
||||
if (node.monitoring && node.monitoringConfirmed) {
|
||||
Logger.tag('monitoring', 'information-retrieval').debug('Node is being monitored: %s', nodeData.mac);
|
||||
Logger.tag('monitoring', 'information-retrieval').debug('Storing status for node: %s', nodeData.mac);
|
||||
|
||||
return Database.get('SELECT * FROM node_state WHERE mac = ?', [node.mac], function (err, row) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
return Database.get('SELECT * FROM node_state WHERE mac = ?', [node.mac], function (err, row) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (_.isUndefined(row)) {
|
||||
return insertNodeInformation(nodeData, node, callback);
|
||||
} else {
|
||||
return updateNodeInformation(nodeData, node, row, callback);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return deleteNodeInformation(nodeData, node, callback);
|
||||
}
|
||||
if (_.isUndefined(row)) {
|
||||
return insertNodeInformation(nodeData, node, callback);
|
||||
} else {
|
||||
return updateNodeInformation(nodeData, node, row, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var isValidMac = Validator.forConstraint(Constraints.node.mac);
|
||||
|
@ -221,7 +217,7 @@ angular.module('ffffng')
|
|||
function (nodeState, mailCallback) {
|
||||
var mac = nodeState.mac;
|
||||
Logger.tag('monitoring', 'mail-sending').debug('Loading node data for: %s', mac);
|
||||
NodeService.findNodeDataByMac(mac, function (err, node, nodeSecrets) {
|
||||
NodeService.getNodeDataByMac(mac, function (err, node, nodeSecrets) {
|
||||
if (err) {
|
||||
Logger
|
||||
.tag('monitoring', 'mail-sending')
|
||||
|
@ -267,10 +263,10 @@ angular.module('ffffng')
|
|||
var now = moment().unix();
|
||||
Database.run(
|
||||
'UPDATE node_state ' +
|
||||
'SET modified_at = ?, last_status_mail_sent = ?, last_status_mail_type = ?' +
|
||||
'SET hostname = ?, monitoring_state = ?, modified_at = ?, last_status_mail_sent = ?, last_status_mail_type = ?' +
|
||||
'WHERE id = ?',
|
||||
[
|
||||
now, now, mailType,
|
||||
node.hostname, node.monitoringState, now, now, mailType,
|
||||
nodeState.id
|
||||
],
|
||||
mailCallback
|
||||
|
@ -357,7 +353,9 @@ angular.module('ffffng')
|
|||
getAll: function (restParams, callback) {
|
||||
var sortFields = [
|
||||
'id',
|
||||
'hostname',
|
||||
'mac',
|
||||
'monitoring_state',
|
||||
'state',
|
||||
'last_seen',
|
||||
'import_timestamp',
|
||||
|
@ -367,7 +365,9 @@ angular.module('ffffng')
|
|||
'modified_at'
|
||||
];
|
||||
var filterFields = [
|
||||
'hostname',
|
||||
'mac',
|
||||
'monitoring_state',
|
||||
'state',
|
||||
'last_status_mail_type'
|
||||
];
|
||||
|
@ -406,6 +406,31 @@ angular.module('ffffng')
|
|||
);
|
||||
},
|
||||
|
||||
getByMacs: function (macs, callback) {
|
||||
if (_.isEmpty(macs)) {
|
||||
return callback(null, {});
|
||||
}
|
||||
|
||||
var inCondition = DatabaseUtil.inCondition('mac', macs);
|
||||
|
||||
Database.all(
|
||||
'SELECT * FROM node_state WHERE ' + inCondition.query,
|
||||
_.concat([], inCondition.params),
|
||||
function (err, rows) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var nodeStateByMac = {};
|
||||
_.each(rows, function (row) {
|
||||
nodeStateByMac[row.mac] = row;
|
||||
});
|
||||
|
||||
callback(null, nodeStateByMac);
|
||||
}
|
||||
);
|
||||
},
|
||||
|
||||
confirm: function (token, callback) {
|
||||
NodeService.getNodeDataByMonitoringToken(token, function (err, node, nodeSecrets) {
|
||||
if (err) {
|
||||
|
@ -490,7 +515,7 @@ angular.module('ffffng')
|
|||
function (nodeData, nodeCallback) {
|
||||
Logger.tag('monitoring', 'information-retrieval').debug('Importing: %s', nodeData.mac);
|
||||
|
||||
NodeService.findNodeDataByMac(nodeData.mac, function (err, node) {
|
||||
NodeService.getNodeDataByMac(nodeData.mac, function (err, node) {
|
||||
if (err) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
|
|
|
@ -424,7 +424,7 @@ angular.module('ffffng')
|
|||
getAllNodes: function (callback) {
|
||||
findNodeFiles({}, function (err, files) {
|
||||
if (err) {
|
||||
Logger.tag('nodes').error('Error getting all nodes:', error);
|
||||
Logger.tag('nodes').error('Error getting all nodes:', err);
|
||||
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
||||
}
|
||||
|
||||
|
@ -434,7 +434,7 @@ angular.module('ffffng')
|
|||
parseNodeFile,
|
||||
function (err, nodes) {
|
||||
if (err) {
|
||||
Logger.tag('nodes').error('Error getting all nodes:', error);
|
||||
Logger.tag('nodes').error('Error getting all nodes:', err);
|
||||
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
||||
}
|
||||
|
||||
|
@ -444,7 +444,7 @@ angular.module('ffffng')
|
|||
});
|
||||
},
|
||||
|
||||
findNodeDataByMac: function (mac, callback) {
|
||||
getNodeDataByMac: function (mac, callback) {
|
||||
return findNodeDataByFilePattern({ mac: mac }, callback);
|
||||
},
|
||||
|
||||
|
|
12
server/utils/databaseUtil.js
Normal file
12
server/utils/databaseUtil.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('ffffng').factory('DatabaseUtil', function (_) {
|
||||
return {
|
||||
inCondition: function (field, list) {
|
||||
return {
|
||||
query: '(' + field + ' IN (' + _.join(_.times(list.length, _.constant('?')), ', ') + '))',
|
||||
params: list
|
||||
};
|
||||
}
|
||||
};
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue