in source/services/anomaly/lib/anomaly.js [74:170]
anomaly.prototype.createAnomaly = function(record, cb) {
if (record.low_limit < record.value) {
var params = {
TableName: ddbTable,
IndexName: 'vin-trip_id-index',
KeyConditionExpression: 'vin = :vin and trip_id = :trip_id',
ExpressionAttributeValues: {
':vin': record.vin,
':trip_id': record.trip_id
}
};
let docClient = new AWS.DynamoDB.DocumentClient(dynamoConfig);
docClient.query(params, function(err, adata) {
if (err) {
console.log(err);
return cb(err, null);
}
if (adata) {
let _exist = _.find(adata.Items, function(item) {
return item.telemetric === record.telemetric;
});
if (!_exist) {
let _anomaly = {
anomaly_id: shortid.generate(),
value: record.value,
trip_id: record.trip_id,
vin: record.vin,
anomaly_score: record.ANOMALY_SCORE,
telemetric: record.telemetric,
identified_at: moment(record.ts).utc().format(),
created_at: moment().utc().format(),
updated_at: moment().utc().format()
};
let params = {
TableName: ddbTable,
Item: _anomaly
};
let docClient = new AWS.DynamoDB.DocumentClient(dynamoConfig);
docClient.put(params, function(err, data) {
if (err) {
console.log(err);
return cb(err, null);
}
let _anomalyInfo = humanizeAnomaly(record.telemetric);
let _mobile = [
'Connected Car Notification. Your vehicle issued a anomaly alert for',
_anomalyInfo
].join(' ');
let _hud = [
'An anomaly was detected for',
_anomalyInfo
].join(' ');
let _message = {
type: 'anomaly',
mobile: _mobile,
mqtt: _hud
}
sendNotification(record.vin, _message, function(err, msg_data) {
if (err) {
console.log(err);
return cb(err, null);
}
console.log(msg_data);
return cb(null, _anomaly);
});
});
} else {
return cb(null, {});
}
} else {
return cb({
error: {
message: 'Error occured querying anomaly table.'
}
}, null);
}
});
} else {
return cb(null, {});
}
};