forked from MathMan05/JankClient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.js
132 lines (112 loc) · 3.22 KB
/
stats.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
const { getAPIURLs, handleEndpoint } = require("./util.js")
const fs = require("node:fs")
let uptimeObject = {}
if (fs.existsSync("./instanceUptime.json")) uptimeObject = JSON.parse(fs.readFileSync("./instanceUptime.json", "utf8"))
const calcStats = instance => {
const obj = uptimeObject[instance.name]
if (!obj) return
const day = Date.now() - 1000 * 60 * 60 * 24
const week = Date.now() - 1000 * 60 * 60 * 24 * 7
let alltime = -1
let totalTimePassed = 0
let daytime = -1
let weektime = -1
let online = false
let i = 0
for (const thing of obj) {
online = thing.online
let stamp = thing.time
if (alltime == -1) alltime = 0
let timepassed
if (obj[i + 1]) timepassed = obj[i + 1].time - stamp
else timepassed = Date.now() - stamp
totalTimePassed += timepassed
alltime += online * timepassed
if (stamp + timepassed > week) {
if (stamp < week) {
timepassed -= week - stamp
stamp = week
}
weektime += online * timepassed
if (stamp + timepassed > day) {
if (stamp < day) {
timepassed -= day - stamp
stamp = day
}
daytime += online * timepassed
}
}
i++
}
instance.online = online
alltime /= totalTimePassed
if (totalTimePassed > 1000 * 60 * 60 * 24) {
if (daytime == -1) daytime = online * 1000 * 60 * 60 * 24
daytime /= 1000 * 60 * 60 * 24
if (totalTimePassed > 1000 * 60 * 60 * 24 * 7) {
if (weektime == -1) weektime = online * 1000 * 60 * 60 * 24 * 7
weektime /= 1000 * 60 * 60 * 24 * 7
} else weektime = alltime
} else {
weektime = alltime
daytime = alltime
}
instance.uptime = {daytime, weektime, alltime}
}
const setStatus = (instance, status) => {
const name = instance.name
let obj = uptimeObject[name]
let needSetting = false
if (obj) {
if (obj.at(-1).online != status) needSetting = true
} else {
obj = []
uptimeObject[name] = obj
needSetting = true
}
if (needSetting) {
obj.push({time: Date.now(), online: status})
fs.writeFile("./instanceUptime.json", JSON.stringify(uptimeObject, null, "\t"), () => {})
}
calcStats(instance)
}
const observe = async instances => {
const resolveInstance = async instance => {
calcStats(instance)
let api
const urls = await getAPIURLs(instance.url)
if (urls) api = urls.api
if (!api) {
setStatus(instance, false)
console.warn("[" + new Date().toISOString() + "] Cannot resolve API URL for " + instance.name)
setTimeout(() => {
resolveInstance(instance)
}, 1000 * 60 * 20 + Math.round(Math.random() * 1000 * 60 * 20))
return
}
api = handleEndpoint(api)
const check = async () => {
const res = await fetch(api + "/ping", {
method: "HEAD"
}).catch(e => {
console.warn("[" + new Date().toISOString() + "] Unable to reach " + instance.name + " (" + e.message + ")")
setStatus(instance, false)
})
if (res) {
console.warn("[" + new Date().toISOString() + "] Got " + res.status + " from " + instance.name)
setStatus(instance, res.ok)
}
}
setTimeout(() => {
check()
setInterval(() => {
check()
}, 1000 * 60 * 20 + Math.round(Math.random() * 1000 * 60 * 10))
}, Math.random() * 1000 * 60 * 10)
}
for (const instance of instances) {
resolveInstance(instance)
}
}
module.exports.observe = observe
module.exports.uptime = uptimeObject