This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
149 lines (138 loc) · 5.2 KB
/
app.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
global.__basedir = __dirname;
const settings = require('./settings.js')
const fs = require('fs')
const path = require('path')
const { spawn } = require('child_process');
const async = require('async')
const { DateTime } = require("luxon")
const Duration = require("luxon").Duration
const generate = require('csv-generate')
const needle = require('needle')
const Papa = require('papaparse')
const WUFOO = require('./routes/WUFOO.js')
const wufoo = new WUFOO()
const express = require('express')
const app = express()
// app.use(express.static(path.join(__dirname, "public")));
//
// app.listen(settings.app.port, 'localhost', () => {
// console.log('Listening to requests on localhost:${port}');
// });
function dataFetch(){
async.waterfall([
function(cb) {
console.log("getting data from wufoo ...")
var results = []
async.each(settings.wufoo.hashes, function(surveyHash, callback) {
wufoo.getAllEntries(surveyHash, function(err, entries){
results = results.concat(entries)
callback();
});
}, function(err){
cb(err, results)
});
},
function(result, cb) {
function compare( a, b ) {
if ( a._dateTime < b._dateTime ){
return -1;
}
if ( a._dateTime > b._dateTime ){
return 1;
}
return 0;
}
result.sort( compare );
console.log("writing all data to csv ...")
// var fileName = DateTime.local().toFormat("yyyy-MM-dd") + " all.csv";
var fileName = "screening-data.csv"
const outputFile = path.join(settings.app.folderPath,fileName);
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: outputFile,
header: settings.wufoo.header
});
csvWriter.writeRecords(result) // returns a promise
.then(() => {
// console.log("running Rscript ...")
// const rscript = spawn('Rscript', [path.join(settings.app.folderPath,"volunteer-monitoring-daily-report.R")])
// rscript.stdout.on('data', (data) => {
// console.log(`Rscript stdout: ${data}`);
// });
// rscript.stderr.on('data', (data) => {
// console.error(`Rscript stderr: ${data}`);
// });
// rscript.on('close', (code) => {
// console.log(`child process for Rscript exited with code ${code}`);
// cb(null, outputFile);
// });
cb(null);
});
}
// ,function(outputFile, cb){
// console.log("creating filtered csv with most recent ...")
// Papa.parse(fs.readFileSync(outputFile, 'utf8'), {
// header: true,
// skipEmptyLines: true,
// error: function(error) {
// if(error) console.log(error)
// },
// complete: function(results) {
// var filteredResults = []
// var hours = 0;
// // var endTime = DateTime.fromObject({ hour: 17, zone: 'America/New_York' });
// // if(endTime.weekday === 2) { hours = 120; } // Tues
// // if(endTime.weekday === 4) { hours = 48; } // Thurs
// // var dur = Duration.fromObject({hours: hours});
// // var startTime = endTime.minus(dur);
// var endTime = DateTime.fromObject({ hour: 15, zone: 'America/New_York' });
// var dur = Duration.fromObject({days: 7});
// var startTime = endTime.minus(dur);
// async.each(results.data, function(row, eachCallback) {
// var rowDt = DateTime.fromISO(row._dateTime);
// if(rowDt < endTime && rowDt > startTime) {
// filteredResults = filteredResults.concat(row);
// }
// eachCallback();
// }, function(err){
// if(err) cb(err)
// var filteredCsv = Papa.unparse(filteredResults);
// var filteredFileName = endTime.toFormat("yyyy-MM-dd'T'HH'-'mmZZZZ") + " from after " + startTime.toFormat("yyyy-MM-dd'T'HH'-'mm") + ".csv";
// var outputfilteredCsv = path.join(settings.app.folderPath,filteredFileName)
// if (fs.existsSync(outputfilteredCsv)) {
// fs.unlinkSync(outputfilteredCsv);
// }
// fs.writeFileSync(outputfilteredCsv, filteredCsv);
// cb()
// });
// }
// });
// }
],
function(err, result){
if(err) {
// ... TODO
console.log(err)
} else {
console.log("finished, starting sync ...")
const syncScript = spawn('onedrive', ["--synchronize"])
syncScript.stdout.on('data', (data) => {
console.log(`onedrive stdout: ${data}`);
});
syncScript.stderr.on('data', (data) => {
console.error(`onedrive stderr: ${data}`);
});
syncScript.on('close', (code) => {
console.log(`child process for onedrive exited with code ${code}`);
});
}
}
)
}
dataFetch();
// run something every Wednesday at 3pm .... '00 00 15 * * 3'
var CronJob = require('cron').CronJob;
// run every hour
new CronJob('00 00 * * * *', function() {
dataFetch();
}, null, true, 'America/New_York').start();