-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
203 lines (163 loc) · 5.17 KB
/
update.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { Counter, Gauge } from "prom-client"
import { HiveOSAPI } from "./api.js"
if (typeof process.env.HIVEOS_API !== "string" || process.env.HIVEOS_API.length === 0) {
throw new Error(`process.env.HIVEOS_API is not set`)
}
export const hive = new HiveOSAPI(process.env.HIVEOS_API)
const hiveos_updates_total = new Counter({
name: "hiveos_updates_total",
help: "Total number of HiveOS updates",
})
const hiveos_updates_errors_total = new Counter({
name: "hiveos_updates_errors_total",
help: "Total number of HiveOS update errors",
labelNames: ["error"],
})
const hiveos_farms_total = new Gauge({
name: "hiveos_farms_total",
help: "Total number of farms",
})
const hiveos_workers_total = new Gauge({
name: "hiveos_workers_total",
help: "Total number of workers",
})
const hiveos_worker_problems_total = new Gauge({
name: "hiveos_worker_problems_total",
help: "Total number of workers with problems",
})
const hiveos_workers_online_total = new Gauge({
name: "hiveos_workers_online_total",
help: "Total number of online workers",
})
const hiveos_workers_offline_total = new Gauge({
name: "hiveos_workers_offline_total",
help: "Total number of offline workers",
})
const hiveos_boards_total = new Gauge({
name: "hiveos_boards_total",
help: "Total number of boards",
})
const hiveos_boards_online_total = new Gauge({
name: "hiveos_boards_online_total",
help: "Total number of online boards",
})
const hiveos_boards_offline_total = new Gauge({
name: "hiveos_boards_offline_total",
help: "Total number of offline boards",
})
const hiveos_farm_workers = new Gauge({
name: "hiveos_farm_workers",
help: "Farm number of workers",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_worker_problems = new Gauge({
name: "hiveos_farm_worker_problems",
help: "Farm number of workers with problems",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_workers_online = new Gauge({
name: "hiveos_farm_workers_online",
help: "Farm number of online workers",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_workers_offline = new Gauge({
name: "hiveos_farm_workers_offline",
help: "Farm number of offline workers",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_boards = new Gauge({
name: "hiveos_farm_boards",
help: "Farm number of boards",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_boards_online = new Gauge({
name: "hiveos_farm_boards_online",
help: "Farm number of online boards",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_boards_offline = new Gauge({
name: "hiveos_farm_boards_offline",
help: "Farm number of offline boards",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_balance = new Gauge({
name: "hiveos_farm_balance",
help: "Farm available HiveOS balance",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_paid = new Gauge({
name: "hiveos_farm_paid",
help: "Farm is HiveOS paid",
labelNames: ["farmId", "farmName"],
})
const hiveos_farm_money_days_left = new Gauge({
name: "hiveos_farm_money_days_left",
help: "HiveOS paid days left",
labelNames: ["farmId", "farmName"],
})
export async function update() {
hiveos_updates_total.inc()
let result
try {
result = await hive.request("/farms")
} catch (error) {
hiveos_updates_errors_total.inc({ error: error?.constructor?.name })
console.error(error)
}
hiveos_workers_total.set(0)
hiveos_worker_problems_total.set(0)
hiveos_workers_online_total.set(0)
hiveos_workers_offline_total.set(0)
hiveos_boards_total.set(0)
hiveos_boards_online_total.set(0)
hiveos_boards_offline_total.set(0)
const farms = result.data
hiveos_farms_total.set(farms.length)
for (const { id, name, stats, money } of farms) {
const {
asics_total,
asics_online,
asics_offline,
rigs_total,
rigs_online,
rigs_offline,
workers_with_problem,
boards_total,
boards_online,
boards_offline,
} = stats
const workers_total = asics_total + rigs_total
const workers_online = asics_online + rigs_online
const workers_offline = asics_offline + rigs_offline
const labels = { farmId: id, farmName: sanitizeFarmName(name) }
hiveos_farm_workers.set(labels, workers_total)
hiveos_farm_worker_problems.set(labels, workers_with_problem)
hiveos_farm_workers_online.set(labels, workers_online)
hiveos_farm_workers_offline.set(labels, workers_offline)
hiveos_farm_boards.set(labels, boards_total)
hiveos_farm_boards_online.set(labels, boards_online)
hiveos_farm_boards_offline.set(labels, boards_offline)
hiveos_workers_total.inc(workers_total)
hiveos_worker_problems_total.inc(workers_with_problem)
hiveos_workers_online_total.inc(workers_online)
hiveos_workers_offline_total.inc(workers_offline)
hiveos_boards_total.inc(boards_total)
hiveos_boards_online_total.inc(boards_online)
hiveos_boards_offline_total.inc(boards_offline)
const {
balance,
is_paid,
days_left,
} = money
hiveos_farm_balance.set(labels, balance)
hiveos_farm_paid.set(labels, is_paid === true ? 1 : 0)
hiveos_farm_money_days_left.set(labels, days_left || 0)
}
}
function sanitizeFarmName(name) {
const exclude = ["cryptotech", "farm", "hosting"]
return name
.split(" ")
.filter((part) => exclude.includes(part.toLowerCase()) !== true)
.join(" ")
}