Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The following changes was made to scripts in the jobs directory of this project: * Changed big if/else control structure for error management to if + early return This makes the code a bit easier to read as the indention level is kept as low as possible * removed commented code * removed unused imports/requires and unused declared variables * for a consistent use: changed all " to ' * merged multiple var definition blocks * moved variable declarations to the top of modules/functions * Changed usage of type-converting comparsion (equality, ==) to strict comparsion (strict equality, ===) where usage of type-converting comparsion doesn't seem to be explicitly needed. The same applies for non-equality comparsions (!= -> !==) In this commit, the every5minstat.js script is rewritten to fulfill the things above and make the code a lot smaller.
- Loading branch information
Showing
2 changed files
with
145 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,67 @@ | ||
var cronJob = require('cron').CronJob | ||
, logger = require('../logger') | ||
, mailer = require('../mailer') | ||
var cronJob = require('cron').CronJob, | ||
logger = require('../logger'), | ||
mailer = require('../mailer'), | ||
// Mongoose models | ||
, User = require('../models/user') | ||
, Openhab = require('../models/openhab') | ||
, OpenhabConfig = require('../models/openhabconfig') | ||
, Event = require('../models/event') | ||
, UserAccount = require('../models/useraccount'); | ||
User = require('../models/user'), | ||
Openhab = require('../models/openhab'), | ||
UserAccount = require('../models/useraccount'); | ||
|
||
// This job checks for openhabs which has been offline for more then 3 days and sends warning emails to their | ||
// owners, pointing out that we didn't see their openhabs for quite long time, not more then one email per 3 days | ||
|
||
module.exports = new cronJob('00 00 00 * * *', function() { | ||
logger.info("openHAB-cloud: checkopenhabsoffline job started"); | ||
// openhabs.find({status:'offline',last_online: {$lte:ISODate("2014-02-24T16:00:00.000X")}}).count() | ||
module.exports = new cronJob('00 00 00 * * *', function () { | ||
logger.info('openHAB-cloud: checkopenhabsoffline job started'); | ||
date3DaysAgo = new Date; | ||
date3DaysAgo.setDate(date3DaysAgo.getDate()-3); | ||
logger.info("openHAB-cloud: date3DaysAgo = " + date3DaysAgo); | ||
Openhab.find({status:'offline', last_online: {"$lt":date3DaysAgo}}, function(error, openhabs) { | ||
if (!error && openhabs) { | ||
logger.info("openHAB-cloud: Found " + openhabs.length + " openhabs"); | ||
// console.log(openhabs); | ||
for (var i in openhabs) { | ||
var openhab = openhabs[i]; | ||
if (!openhab.last_email_notification || openhab.last_email_notification < date3DaysAgo) { | ||
openhab.last_email_notification = new Date; | ||
openhab.save(); | ||
// console.log(openhab.uuid + ":"); | ||
UserAccount.findOne({_id:openhab.account}, function(error, userAccount) { | ||
if (!error && userAccount) { | ||
User.find({account: userAccount.id, role:"master"}, function(error, users) { | ||
if (!error && users) { | ||
for (var i in users) { | ||
var user = users[i]; | ||
var locals = { | ||
email: user.username | ||
}; | ||
mailer.sendEmail(user.username, "We are worried about your openHAB", | ||
"openhaboffline", locals, function(error) { | ||
if (!error) { | ||
// logger.info("mail sent"); | ||
} else | ||
logger.error("openHAB-cloud: Error sending email: " + error); | ||
}); | ||
} | ||
} | ||
}); | ||
} else if (error) { | ||
logger.error("openHAB-cloud: Error finding user account for openhab: " + error); | ||
} else { | ||
logger.error("openHAB-cloud: Unable to find user account for openhab which is nonsense"); | ||
} | ||
}); | ||
} | ||
logger.info('openHAB-cloud: date3DaysAgo = ' + date3DaysAgo); | ||
Openhab.find({status:'offline', last_online: {'$lt':date3DaysAgo}}, function (error, openhabs) { | ||
if (error) { | ||
logger.error('openHAB-cloud: Error finding offline openHABs: ' + error); | ||
} | ||
|
||
if (!openhabs) { | ||
logger.info('openHAB-cloud: No offline openHABs found'); | ||
} | ||
logger.info('openHAB-cloud: Found ' + openhabs.length + ' openhabs'); | ||
for (var i in openhabs) { | ||
var openhab = openhabs[i]; | ||
|
||
if (openhab.last_email_notification && openhab.last_email_notification > date3DaysAgo) { | ||
continue; | ||
} | ||
} else if (error) { | ||
logger.error("openHAB-cloud: Error finding offline openHABs: " + error); | ||
} else { | ||
logger.info("openHAB-cloud: No offline openHABs found"); | ||
|
||
openhab.last_email_notification = new Date; | ||
openhab.save(); | ||
|
||
UserAccount.findOne({_id:openhab.account}, function (error, userAccount) { | ||
if (error) { | ||
logger.error('openHAB-cloud: Error finding user account for openhab: ' + error); | ||
} | ||
|
||
if (!userAccount) { | ||
logger.error('openHAB-cloud: Unable to find user account for openhab which is nonsense'); | ||
} | ||
|
||
User.find({account: userAccount.id, role:'master'}, function (error, users) { | ||
if (error || !users) { | ||
return; | ||
} | ||
|
||
for (var i in users) { | ||
var user = users[i]; | ||
var locals = { | ||
email: user.username | ||
}; | ||
mailer.sendEmail(user.username, 'We are worried about your openHAB', | ||
'openhaboffline', locals, function (error) { | ||
if (!error) { | ||
// logger.info('mail sent'); | ||
} else | ||
logger.error('openHAB-cloud: Error sending email: ' + error); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
}); | ||
logger.info("openHAB-cloud: checkopenhabsoffline job finished"); | ||
logger.info('openHAB-cloud: checkopenhabsoffline job finished'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,90 @@ | ||
var cronJob = require('cron').CronJob | ||
, logger = require('../logger') | ||
, redis = require('../redis-helper'); | ||
// Mongoose models | ||
var User = require('../models/user'); | ||
var Openhab = require('../models/openhab'); | ||
var OpenhabConfig = require('../models/openhabconfig'); | ||
var Event = require('../models/event'); | ||
var Item = require('../models/item'); | ||
var UserDevice = require('../models/userdevice'); | ||
var UserAccount = require('../models/useraccount'); | ||
var Notification = require('../models/notification'); | ||
var AccessLog = require('../models/accesslog'); | ||
var OpenhabAccessLog = require('../models/openhabaccesslog'); | ||
var Invitation = require('../models/invitation'); | ||
var Myohstat = require('../models/myohstat'); | ||
|
||
module.exports = new cronJob('00 */5 * * * *', function() { | ||
logger.info("openHAB-cloud: every5min statistics collection job started"); | ||
Openhab.count({}, function(err, openhabCount) { | ||
if (!err) { | ||
Openhab.count({status: 'online'}, function(err, openhabOnlineCount) { | ||
if (!err) { | ||
User.count({}, function(err, userCount) { | ||
if (!err) { | ||
Invitation.count({used:true}, function(err, invitationUsedCount) { | ||
if (!err) { | ||
Invitation.count({used:false}, function(err, invitationUnusedCount) { | ||
if (!err) { | ||
UserDevice.count({}, function(err, userDeviceCount) { | ||
if (!err) { | ||
var newStat = new Myohstat({uC: userCount, oC: openhabCount, | ||
ooC: openhabOnlineCount, iuC: invitationUsedCount, | ||
iuuC: invitationUnusedCount, udC: userDeviceCount}); | ||
newStat.save(); | ||
// Set current statistics to redis | ||
redis.mset(["openhabCount", openhabCount, "openhabOnlineCount", openhabOnlineCount, | ||
"userCount", userCount, "invitationUsedCount", invitationUsedCount, | ||
"invitationUnusedCount", invitationUnusedCount, | ||
"userDeviceCount", userDeviceCount, "last5MinStatTimestamp", new Date()], function(err, result) { | ||
logger.info("openHAB-cloud: every5min statistics collection job finished"); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
var cronJob = require('cron').CronJob, | ||
logger = require('../logger'), | ||
redis = require('../redis-helper'), | ||
// Mongoose models | ||
User = require('../models/user'), | ||
Openhab = require('../models/openhab'), | ||
UserDevice = require('../models/userdevice'), | ||
Invitation = require('../models/invitation'), | ||
Myohstat = require('../models/myohstat'), | ||
stats = [], | ||
openhabCount, userCount, openhabOnlineCount, invitationUsedCount, | ||
invitationUnusedCount, userDeviceCount; | ||
|
||
/** | ||
* Callback function for a count operation on a Mongoose model. It will save the count in the | ||
* stats object, if there was no error, and calls the saveStats function to save the | ||
* statistics, if all counts are finished. | ||
* | ||
* Tis function expects, that the this argument is the name of the statistic value. | ||
* | ||
* @private | ||
*/ | ||
function countCallback (err, count) { | ||
if (!err) { | ||
stats[this] = count; | ||
} | ||
} | ||
|
||
/** | ||
* Checks, if all callbacks are executed and if so, validates the stats object. It will then, if the | ||
* validation succeeds, save the values to redis. | ||
*/ | ||
function saveStats() { | ||
var newStat; | ||
|
||
// validate the results | ||
if (Object.keys(stats).length !== 6) { | ||
// not all data could be retrieved | ||
logger.info('The length of the stats object does not match the expected one, can not save statistical data.'); | ||
return; | ||
} | ||
|
||
newStat = new Myohstat({ | ||
uC: stats['userCount'], | ||
oC: stats['openhabCount'], | ||
ooC: stats['openhabOnlineCount'], | ||
iuC: stats['invitationUsedCount'], | ||
iuuC: stats['invitationUnusedCount'], | ||
udC: stats['userDeviceCount'] | ||
}); | ||
newStat.save(); | ||
|
||
// Set current statistics to redis | ||
redis.mset( | ||
[ | ||
'openhabCount', | ||
stats['openhabCount'], | ||
'openhabOnlineCount', | ||
stats['openhabOnlineCount'], | ||
'userCount', | ||
stats['userCount'], | ||
'invitationUsedCount', | ||
stats['invitationUsedCount'], | ||
'invitationUnusedCount', | ||
stats['invitationUnusedCount'], | ||
'userDeviceCount', | ||
stats['userDeviceCount'], | ||
'last5MinStatTimestamp', | ||
new Date() | ||
], | ||
function (err, result) { | ||
logger.info('openHAB-cloud: every5min statistics collection job finished'); | ||
} | ||
); | ||
} | ||
|
||
module.exports = new cronJob('00 */5 * * * *', function () { | ||
var promises = []; | ||
|
||
logger.info('openHAB-cloud: every5min statistics collection job started'); | ||
|
||
promises.push(Openhab.count({}, countCallback.bind('openhabCount')).exec()); | ||
promises.push(Openhab.count({status: 'online'}, countCallback.bind('openhabOnlineCount')).exec()); | ||
promises.push(User.count({}, countCallback.bind('userCount')).exec()); | ||
promises.push(Invitation.count({used:true}, countCallback.bind('invitationUsedCount')).exec()); | ||
promises.push(Invitation.count({used:false}, countCallback.bind('invitationUnusedCount')).exec()); | ||
promises.push(UserDevice.count({}, countCallback.bind('userDeviceCount')).exec()); | ||
|
||
Promise.all(promises).then(saveStats); | ||
}); |