Skip to content

Commit

Permalink
Update app.js
Browse files Browse the repository at this point in the history
  • Loading branch information
manustars authored Oct 7, 2024
1 parent c0729ef commit 7193dd5
Showing 1 changed file with 108 additions and 84 deletions.
192 changes: 108 additions & 84 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,113 +116,137 @@ const deviceStatusInfo = new Gauge({
});

async function refreshMetrics() {
minerStatuses.reset()
devicesStatuses.reset()
rigStatusTime.reset()
rigJoinTime.reset()
deviceTemp.reset()
deviceLoad.reset()
devicePower.reset()
deviceStatusInfo.reset()
deviceSpeed.reset()
// Reset delle metriche all'inizio
minerStatuses.reset();
devicesStatuses.reset();
rigStatusTime.reset();
rigJoinTime.reset();
deviceTemp.reset();
deviceLoad.reset();
devicePower.reset();
deviceStatusInfo.reset();
deviceSpeed.reset();

let totalRigsCount = 0;
let totalDevicesCount = 0;
let totalProfitabilityCount = 0;

let currentPage = 0;
let hasNextPage = true;

try {
const rawResponse = await nhClient.getMiningRigs()
const data = rawResponse.data
//console.log(data)
totalRigs.set(data.totalRigs)
totalDevices.set(data.totalDevices)
totalProfitability.set(data.totalProfitability)
unpaidAmount.set(+data.unpaidAmount)
Object.keys(data.minerStatuses).forEach(k => minerStatuses.labels(k).set(data.minerStatuses[k]))
Object.keys(data.devicesStatuses).forEach(k => devicesStatuses.labels(k).set(data.devicesStatuses[k]))
data.miningRigs.forEach(rig => {
if (rig.v4 && rig.v4.mmv) {
rigStatusTime.labels(rig.v4.mmv.workerName, rig.rigId, rig.minerStatus).set(rig.statusTime);

(rig.v4.devices || []).forEach((device, index) => {
console.log("Device", index + 1, ":", device);
const dsv = device.dsv; // Dettagli dsv
const osv = device.osv; // Dettagli osv
try {
const temperatureEntry = device.odv.find(entry => entry.key === "Temperature");
if (temperatureEntry) {
deviceTemp.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass).set(parseFloat(temperatureEntry.value));
} else {
deviceTemp.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass).set(parseFloat(0));
// Loop su tutte le pagine
while (hasNextPage) {
const rawResponse = await nhClient.getMiningRigs({ page: currentPage });
const data = rawResponse.data;

// Accumulo dei valori globali
totalRigsCount += data.totalRigs;
totalDevicesCount += data.totalDevices;
totalProfitabilityCount += parseFloat(data.totalProfitability || 0);

// Aggiornamento degli stati dei miner e dei dispositivi per la pagina corrente
Object.keys(data.minerStatuses).forEach(k => minerStatuses.labels(k).set(data.minerStatuses[k]));
Object.keys(data.devicesStatuses).forEach(k => devicesStatuses.labels(k).set(data.devicesStatuses[k]));

// Elaborazione delle rig
data.miningRigs.forEach(rig => {
if (rig.v4 && rig.v4.mmv) {
rigStatusTime.labels(rig.v4.mmv.workerName, rig.rigId, rig.minerStatus).set(rig.statusTime);

(rig.v4.devices || []).forEach((device, index) => {
try {
const temperatureEntry = device.odv.find(entry => entry.key === "Temperature");
deviceTemp.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass)
.set(temperatureEntry ? parseFloat(temperatureEntry.value) : 0);

const loadEntry = device.odv.find(entry => entry.key === "Load");
deviceLoad.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass)
.set(loadEntry ? parseFloat(loadEntry.value) : 0);

const powerEntry = device.odv.find(entry => entry.key === "Power usage");
devicePower.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass)
.set(powerEntry ? parseFloat(powerEntry.value) : -1);

deviceStatusInfo.labels(rig.v4.mmv.workerName, rig.v4.versions[0], device.dsv.name, device.dsv.id, device.dsv.deviceClass, device.mdv.state)
.set(1);

device.speeds.forEach(speed => {
deviceSpeed.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass, speed.algorithm, speed.displaySuffix)
.set(+speed.speed);
});
} catch (e) {
console.error("Errore durante il parsing del dispositivo: ", e);
}
const loadEntry = device.odv.find(entry => entry.key === "Load");
const loadValue = loadEntry ? parseFloat(loadEntry.value) : 0;
deviceLoad.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass).set(loadValue);

const powerEntry = device.odv.find(entry => entry.key === "Power usage");
if (powerEntry) {
devicePower.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass).set(parseFloat(powerEntry.value));
} else {
devicePower.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass).set(parseFloat(-1));
}
deviceStatusInfo.labels(rig.v4.mmv.workerName, rig.v4.versions[0], device.dsv.name, device.dsv.id, device.dsv.deviceClass, device.mdv.state).set(parseFloat(1));
device.speeds.forEach(speed => {
deviceSpeed.labels(rig.v4.mmv.workerName, device.dsv.name, device.dsv.id, device.dsv.deviceClass, speed.algorithm, speed.displaySuffix).set(+speed.speed);
});
});
} else {
rigStatusTime.labels(rig.name, rig.rigId, rig.status).set(rig.statusTime);
try {
rigJoinTime.labels(rig.name, rig.rigId).set(rig.joinTime);
} catch (e) {
console.error("Errore durante il parsing del dispositivo: ", e);
console.error("Errore durante il settaggio di rigJoinTime: ", e);
}

});
} else {
rigStatusTime.labels(rig.name, rig.rigId, rig.status).set(rig.statusTime);
try {
rigJoinTime.labels(rig.name, rig.rigId).set(rig.joinTime);
} catch (e) {
console.error("Errore durante il settaggio di rigJoinTime: ", e);
(rig.devices || []).forEach(device => {
try {
deviceTemp.labels(rig.name, device.name, device.id, device.deviceType.enumName).set(device.temperature);
deviceLoad.labels(rig.name, device.name, device.id, device.deviceType.enumName).set(device.load);
devicePower.labels(rig.name, device.name, device.id, device.deviceType.enumName).set(device.powerUsage);
deviceStatusInfo.labels(rig.name, rig.softwareVersions, device.name, device.id, device.deviceType.enumName, device.status.enumName)
.set(1);

device.speeds.forEach(speed => {
deviceSpeed.labels(rig.name, device.name, device.id, device.deviceType.enumName, speed.algorithm, speed.displaySuffix)
.set(+speed.speed);
});
} catch (e) {
console.error("Errore durante il parsing del dispositivo: ", e);
}
});
}
});

// Controlla se c'è una pagina successiva
hasNextPage = data.pagination && data.pagination.nextPage !== null;
currentPage++;
}

// Dopo tutte le pagine, imposta i valori aggregati per le metriche globali
totalRigs.set(totalRigsCount);
totalDevices.set(totalDevicesCount);
totalProfitability.set(totalProfitabilityCount);

(rig.devices || []).forEach(device => {
try {
deviceTemp.labels(rig.name, device.name, device.id, device.deviceType.enumName).set(device.temperature);
deviceLoad.labels(rig.name, device.name, device.id, device.deviceType.enumName).set(device.load);
devicePower.labels(rig.name, device.name, device.id, device.deviceType.enumName).set(device.powerUsage);
deviceStatusInfo.labels(rig.name, rig.softwareVersions, device.name, device.id, device.deviceType.enumName, device.status.enumName).set(1);

device.speeds.forEach(speed => {
deviceSpeed.labels(rig.name, device.name, device.id, device.deviceType.enumName, speed.algorithm, speed.displaySuffix).set(+speed.speed);
})
} catch (e) {
console.log("there was an error parsing " + JSON.stringify(device) + " with ", e)
}
})
}
})
} catch (e) {
console.log("there was an error on request1 ", e)
console.error("Errore durante la richiesta dei mining rigs: ", e);
}

// Richiesta per i wallet (separata dalle rig)
try {
const rawResponse2 = await nhClient.getWallets()
const data2 = rawResponse2.data
//console.log(data2)
totalBtc.set(+data2.total.totalBalance)
//fiatRate.set(data2.totalBalance)
const rawResponse2 = await nhClient.getWallets();
const data2 = rawResponse2.data;
totalBtc.set(+data2.total.totalBalance);
unpaidAmount.set(+data2.total.unpaidAmount);
} catch (e) {
console.log("there was an error on request2 ", e)
console.error("Errore durante la richiesta dei wallet: ", e);
}

// Richiesta per i tassi di cambio (separata)
try {
const rawResponse3 = await nhClient.getExchangeRates()
const data3 = rawResponse3.data
//console.log(data3)
const rawResponse3 = await nhClient.getExchangeRates();
const data3 = rawResponse3.data;
rateGauges.forEach(r => {
try {
r.gauge.set(+data3[r.rate])
r.gauge.set(+data3[r.rate]);
} catch (e) {
console.log(`given rate ${r.rate} not found in ${data3}`)
console.error(`Il tasso ${r.rate} non è stato trovato nei dati: `, e);
}
})
});
} catch (e) {
console.log("there was an error on request3 ", e)
console.error("Errore durante la richiesta dei tassi di cambio: ", e);
}
}


// APIS

app.get('/', (req, res) => {
Expand Down

0 comments on commit 7193dd5

Please sign in to comment.