forked from akash-network/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviderStatusProvider.ts
173 lines (157 loc) · 5.46 KB
/
providerStatusProvider.ts
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
import https from "https";
import axios from "axios";
import { Provider } from "@shared/dbSchemas/akash";
import { asyncify, eachLimit } from "async";
import { ProviderSnapshot } from "@src/../../shared/dbSchemas/akash/providerSnapshot";
import { toUTC } from "@src/shared/utils/date";
const ConcurrentStatusCall = 10;
const StatusCallTimeout = 30_000; // 30 seconds
export async function syncProvidersInfo() {
let providers = await Provider.findAll({
where: {
deletedHeight: null
},
order: [["isOnline", "DESC"]]
});
const httpsAgent = new https.Agent({
rejectUnauthorized: false
});
let doneCount = 0;
await eachLimit(
providers,
ConcurrentStatusCall,
asyncify(async (provider: Provider) => {
try {
const response = await axios.get(provider.hostUri + "/status", {
httpsAgent: httpsAgent,
timeout: StatusCallTimeout
});
if (response.status !== 200) throw "Invalid response status: " + response.status;
const versionResponse = await axios.get(provider.hostUri + "/version", {
httpsAgent: httpsAgent,
timeout: StatusCallTimeout
});
const activeResources = sumResources(response.data.cluster.inventory.active);
const pendingResources = sumResources(response.data.cluster.inventory.pending);
const availableResources = sumResources(response.data.cluster.inventory.available);
const checkDate = toUTC(new Date());
await Provider.update(
{
isOnline: true,
error: null,
lastCheckDate: checkDate,
cosmosSdkVersion: versionResponse.data.akash.cosmosSdkVersion,
akashVersion: versionResponse.data.akash.version,
deploymentCount: response.data.manifest.deployments,
leaseCount: response.data.cluster.leases,
activeCPU: activeResources.cpu,
activeGPU: activeResources.gpu,
activeMemory: activeResources.memory,
activeStorage: activeResources.storage,
pendingCPU: pendingResources.cpu,
pendingGPU: pendingResources.gpu,
pendingMemory: pendingResources.memory,
pendingStorage: pendingResources.storage,
availableCPU: availableResources.cpu,
availableGPU: availableResources.gpu,
availableMemory: availableResources.memory,
availableStorage: availableResources.storage
},
{
where: { owner: provider.owner }
}
);
await ProviderSnapshot.create({
owner: provider.owner,
isOnline: true,
checkDate: checkDate,
deploymentCount: response.data.manifest.deployments,
leaseCount: response.data.cluster.leases,
activeCPU: activeResources.cpu,
activeGPU: activeResources.gpu,
activeMemory: activeResources.memory,
activeStorage: activeResources.storage,
pendingCPU: pendingResources.cpu,
pendingGPU: pendingResources.gpu,
pendingMemory: pendingResources.memory,
pendingStorage: pendingResources.storage,
availableCPU: availableResources.cpu,
availableGPU: availableResources.gpu,
availableMemory: availableResources.memory,
availableStorage: availableResources.storage
});
} catch (err) {
const checkDate = new Date();
await Provider.update(
{
isOnline: false,
lastCheckDate: checkDate,
error: err?.message || err,
akashVersion: null,
cosmosSdkVersion: null,
deploymentCount: null,
leaseCount: null,
activeCPU: null,
activeGPU: null,
activeMemory: null,
activeStorage: null,
pendingCPU: null,
pendingGPU: null,
pendingMemory: null,
pendingStorage: null,
availableCPU: null,
availableGPU: null,
availableMemory: null,
availableStorage: null
},
{
where: { owner: provider.owner }
}
);
await ProviderSnapshot.create({
owner: provider.owner,
isOnline: false,
error: err?.message || err,
checkDate: checkDate
});
} finally {
doneCount++;
console.log("Fetched provider info: " + doneCount + " / " + providers.length);
}
})
);
console.log("Finished refreshing provider infos");
}
function getStorageFromResource(resource) {
return Object.keys(resource).includes("storage_ephemeral") ? resource.storage_ephemeral : resource.storage;
}
function getUnitValue(resource) {
return typeof resource === "number" ? resource : parseInt(resource.units.val);
}
function getByteValue(val) {
return typeof val === "number" ? val : parseInt(val.size.val);
}
function sumResources(resources) {
const resourcesArr = resources?.nodes || resources || [];
return resourcesArr
.map((x) => ({
cpu: getUnitValue(x.cpu),
gpu: x.gpu ? getUnitValue(x.gpu) : 0,
memory: getByteValue(x.memory),
storage: getByteValue(getStorageFromResource(x))
}))
.reduce(
(prev, next) => ({
cpu: prev.cpu + next.cpu,
gpu: prev.gpu + next.gpu,
memory: prev.memory + next.memory,
storage: prev.storage + next.storage
}),
{
cpu: 0,
gpu: 0,
memory: 0,
storage: 0
}
);
}