-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcronUpdate.js
84 lines (72 loc) · 2.71 KB
/
cronUpdate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const CronJob = require('cron').CronJob;
const db = require('./models');
const moment = require('moment');
const tz = 'Asia/Singapore';
console.log("Starting cronUpdate");
function replaceAt(string, index, replacement) {
return string.substring(0, index) + replacement + string.substring(index + replacement.length);
}
function incrementValueFromCSVLine(string, index, val) {
// return replaceAt(string, index, parseInt(string.charAt(index)) + val);
let nextCommaIndex;
for(let i = index; i < string.length; i++){
if(string[i] == ','){
nextCommaIndex = i;
break;
}
}
if(nextCommaIndex){
return string.substring(0, index) + (parseInt(string.substring(index, nextCommaIndex)) + val) + string.substring(nextCommaIndex);
}
return string.substring(0, index) + (parseInt(string.substring(index)) + val);
}
//Cronjob
//I did it this way instead of findall query because I thought we need to update the data per level
async function getNumberofActiveMachines(lvl) {
const laundrydata = db.Level.findByPk(lvl);
var numberofActiveMachines = 0;
await laundrydata.then(x => {
const wc = x.getDataValue("wc_status") > 0;
const we = x.getDataValue("we_status") > 0;
const dc = x.getDataValue("dc_status") > 0;
const de = x.getDataValue("de_status") > 0;
numberofActiveMachines = wc + we + dc + de;
}).catch(err => {
console.log(err);
});
return numberofActiveMachines;
}
function updateRecord(numOfActiveMachines, lvl) {
const laundrydata = db.Level.findByPk(lvl);
const day = moment().format('ddd').toLowerCase();
const hour = parseInt(moment().format('HH'));
let dailyData;
laundrydata.then(x => {
dailyData = x.getDataValue(day);
console.log(`level-${lvl}:numActive-${numOfActiveMachines}:before:day-${day}:hour-${hour}:${dailyData}`);
dailyData = incrementValueFromCSVLine(dailyData, hour * 2, numOfActiveMachines);
console.log(`level-${lvl}:numActive-${numOfActiveMachines}:afteri:day-${day}:hour-${hour}:${dailyData}`);
return dailyData;
})
.then(data =>{
let query = {};
query[day] = data;
db.Level.update(query, {
where: {
level: lvl
}
}).then(result => console.log(result));
}
).catch(err => console.log(err));
}
function doForAllLevel() {
const levelsWithLaundry = [5, 8, 11, 14, 17];
levelsWithLaundry.forEach(x => {
getNumberofActiveMachines(x).then(a => { updateRecord(a, x); });
});
}
function cronFunction () {
console.log(`${moment().format()}:Update for all level`);
doForAllLevel();
}
module.exports = cronFunction;