-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-summary-job.js
87 lines (75 loc) · 2.47 KB
/
day-summary-job.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
85
86
87
// Copyright (c) 2017 Sergii Popov
//
// GNU GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
'use strict';
let logger = require('./app/logger');
let moment = require('moment');
let measurementManager = require('./app/manager/measurement-manager');
let daySummaryModel = require('./app/model/day-summary-model');
let daySummaryProcessor = require('./app/day-summary-processor');
module.exports = {};
// routine to (re)calculate stats for the whole month
module.exports.month = function(date) {
let startDate = moment(date).date(1);
let endDate = moment(date).endOf('month');
let currentDate = moment(startDate);
while (currentDate.isBefore(endDate)) {
logger.log('Stat for', currentDate.format('YYYY-MM-DD'));
module.exports.run(currentDate.format('YYYY-MM-DD'), true);
currentDate.add(1, 'days');
}
};
// targetDate must be in format YYYY-MM-DD;
module.exports.run = function(targetDate, forceUpdate) {
let date;
if (!targetDate) {
date = moment().format('YYYY-MM-DD');
} else {
date = targetDate;
}
let promise = daySummaryModel.findAll({where: {
date: date
}});
if (forceUpdate) {
promise = promise.then((res) => {
let promises = res.map((model) => {return model.destroy()});
return Promise.all(promises);
});
}
promise.then(function() {
return measurementManager.full(date)
.then((data) => {
var models = daySummaryProcessor(date, data);
models.forEach((model) => {
daySummaryModel.create(model).then(() => {
logger.log('day summary logged', model);
}).catch((err) => {
logger.warn('day summary failed to log', err);
});
});
});
})
.catch((res) => {
logger.warn('Failed to get records from db', res);
});
// check if data exists
return promise;
};
if (require.main === module) {
// called from console - run with default params
module.exports.run('2017-03-31', true);
}