-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (74 loc) · 2.5 KB
/
index.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
const Git = require("nodegit")
const publisher = require("./publish")
const loader = require("./loader")
const fs = require("fs");
async function publishLatest() {
const regionsObs = loader.loadFromRegions("./tmp/dati-json/dpc-covid19-ita-regioni-latest.json")
const provinceObs = loader.loadFromProvince("./tmp/dati-json/dpc-covid19-ita-province-latest.json")
console.log("Publishing latest data", provinceObs.length, regionsObs.length);
await publisher.clearLiveGraph()
await publishAll(regionsObs)
await publishAll(provinceObs)
await publisher.logLiveData()
console.log("Done");
}
async function publishAll(observations){
let count= 0
for (const obs of observations) {
try {
console.log(++count,observations.length);
await publisher.publishData(obs)
} catch (error) {
console.log(error)
console.log("err", obs)
}
}
}
function main() {
if (!fs.existsSync("./tmp")) {
console.log("Repository not found cloning into ./tmp")
Git.Clone("https://github.com/pcm-dpc/COVID-19.git", "./tmp").then(() => {
console.log("Cloned data repo");
return publishLatest()
}).catch(e => {
console.log(e)
});
} else {
console.log("Syncing repository");
Git.Repository.open("./tmp").then(function (repo) {
repository = repo;
return repository.fetchAll({
callbacks: {
credentials: function (url, userName) {
return Git.Cred.sshKeyFromAgent(userName);
},
certificateCheck: function () {
return 0;
}
}
});
})
// Now that we're finished fetching, go ahead and merge our local branch
// with the new one
.then(function () {
return repository.mergeBranches("master", "origin/master");
})
.then(function () {
console.log("Repository sync done");
return publishLatest()
}).catch(e =>{
console.log(e)
});
}
}
if (process.env.RUN_NOW) {
main()
} else {
var cron = require('node-cron');
console.log("Cron mode; scheduling task * * 19 * *")
cron.schedule('* * 19 * *', () => {
console.log('Uploading covid19 data from git');
console.log(new Date())
main()
});
}