-
Notifications
You must be signed in to change notification settings - Fork 2
/
delay.js
72 lines (65 loc) · 2.1 KB
/
delay.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
const {InfluxDB} = require('@influxdata/influxdb-client')
const {url, token, org, bucket} = require('./env')
const human = (delta) => {
if (delta == 0) {
return "0";
}
const plural = (numf, str, prefix, suffix) => {
const num = Math.round(numf);
return prefix +
num.toString() + " " +
(num == 1 ? str : str + "s") +
suffix;
}
const agoF = (num, str) => plural(num, str, "", " ago");
const inF = (num, str) => plural(num, str, "in ", "");
const scale = [60, 60, 24, 30, 365, 1e15];
const scaleStr = ["second", "minute", "hour", "day", "month", "year"];
let offset = (delta > 0 ? delta : -delta);
for (let i = 0; i != scale.length; ++i) {
if (offset < scale[i])
return delta < 0 ? agoF(offset, scaleStr[i]) : inF(offset, scaleStr[i]);
offset /= scale[i];
}
}
if (process.argv.length < 5) {
console.log("Usage: nodejs delay.js mirrorz.site.abbr from to")
}
const mirror = process.argv[2]
const from = process.argv[3]
const to = process.argv[4]
const queryApi = new InfluxDB({url, token}).getQueryApi(org)
const fluxQuery =`
from(bucket:"${bucket}")
|> range(start: -30m)
|> filter(fn: (r) => r._measurement == "repo" and r.mirror == "${mirror}")
|> map(fn: (r) => ({_value:r._value,name:r.name,_time:r._time,path:r.url}))
|> tail(n:1)
`
console.log(`From: ${from}`)
console.log(`To: ${to}`)
console.log(`Subject: MirrorZ report for ${mirror}`)
const delays = []
queryApi.queryRows(fluxQuery, {
next(row, tableMeta) {
const o = tableMeta.toObject(row)
log = `${o.name}:\t\t${human(o._value)}\t\tcollected ${human(Math.round((new Date(o._time) - new Date())/1000))}`
if (o._value == 0) {
delays.push([o._value , "--- " + log])
}
if (o._value < -24*60*60 && o._value > -24*60*60*3)
delays.push([o._value , "*** " + log])
if (o._value < -24*60*60*3)
delays.push([o._value , "!!! " + log])
},
error(error) {
console.error(error)
console.log('\nFinished ERROR')
},
complete() {
delays.sort((a, b) => a[0] - b[0])
for (d of delays)
console.log(d[1])
//console.log('\nFinished SUCCESS')
},
})