persist some stuff

This commit is contained in:
Marco van Dijk 2023-11-03 04:11:37 +01:00
parent e51fb26db5
commit f7d1ee5cdd
3 changed files with 41 additions and 5 deletions

View File

@ -116,7 +116,7 @@ const batchPostStats = async function () {
"Content-Length": postData.length, "Content-Length": postData.length,
}, },
}; };
console.log("Uploading stats of " + postData.length + "B"); console.log("Uploading " + postData.length + " B of stats");
var req; var req;
if (CONF_MASTER_DOMAIN == "127.0.0.1" || CONF_MASTER_DOMAIN == "localhost") { if (CONF_MASTER_DOMAIN == "127.0.0.1" || CONF_MASTER_DOMAIN == "localhost") {
req = http.request(options, (res) => { req = http.request(options, (res) => {

View File

@ -17,6 +17,7 @@
"dependencies": { "dependencies": {
"ethers": "^6.8.1", "ethers": "^6.8.1",
"express": "^4.17.1", "express": "^4.17.1",
"node-persist": "^3.1.3",
"npm": "^8.5.2", "npm": "^8.5.2",
"prom-client": "^14.0.1" "prom-client": "^14.0.1"
}, },

View File

@ -1,6 +1,7 @@
const express = require("express"); const express = require("express");
const client = require("prom-client"); const client = require("prom-client");
const { ethers } = require("ethers"); const { ethers } = require("ethers");
const storage = require("node-persist");
const { const {
CONF_API_L1_HTTP, CONF_API_L1_HTTP,
CONF_API_L1_KEY, CONF_API_L1_KEY,
@ -17,7 +18,9 @@ const {
*/ */
const l1provider = new ethers.JsonRpcProvider(CONF_API_L1_HTTP + CONF_API_L1_KEY); const l1provider = new ethers.JsonRpcProvider(
CONF_API_L1_HTTP + CONF_API_L1_KEY
);
const masterRouter = express.Router(); const masterRouter = express.Router();
const register = new client.Registry(); const register = new client.Registry();
@ -85,6 +88,7 @@ function sleep(ms) {
let ensDomainCache = {}; let ensDomainCache = {};
let orchCache = {}; let orchCache = {};
let lastLeaderboardCheck = 0; let lastLeaderboardCheck = 0;
let isSynced = false;
/* /*
@ -129,6 +133,7 @@ const getEnsDomain = async function (addr) {
ensObj.timestamp ensObj.timestamp
); );
ensDomainCache[addr] = ensObj; ensDomainCache[addr] = ensObj;
await storage.setItem("ensDomainCache", ensDomainCache);
return ensObj.domain ? ensObj.domain : ensObj.address; return ensObj.domain ? ensObj.domain : ensObj.address;
} catch (err) { } catch (err) {
console.log(err); console.log(err);
@ -330,6 +335,7 @@ const onOrchUpdate = async function (id, obj, tag, region, livepeer_regions) {
newObj.instances[obj.resolv.resolvedTarget] = newInstance; newObj.instances[obj.resolv.resolvedTarget] = newInstance;
newObj.regionalStats[tag] = newRegion; newObj.regionalStats[tag] = newRegion;
orchCache[id.toLowerCase()] = newObj; orchCache[id.toLowerCase()] = newObj;
await storage.setItem("orchCache", orchCache);
// Update prometheus stats // Update prometheus stats
updatePrometheus(tag, id, obj.resolv.resolvedTarget, newObj); updatePrometheus(tag, id, obj.resolv.resolvedTarget, newObj);
@ -357,6 +363,10 @@ Public endpoints
// Mutate state with new stats // Mutate state with new stats
masterRouter.post("/collectStats", async (req, res) => { masterRouter.post("/collectStats", async (req, res) => {
if (!isSynced) {
res.send("busy");
return;
}
try { try {
const { batchResults, tag, key, region, livepeer_regions } = req.body; const { batchResults, tag, key, region, livepeer_regions } = req.body;
if (!batchResults || !tag || !key || !region || !livepeer_regions) { if (!batchResults || !tag || !key || !region || !livepeer_regions) {
@ -422,7 +432,10 @@ const updateScore = async function (address) {
const newSR = instance.round_trip_time / instance.seg_duration; const newSR = instance.round_trip_time / instance.seg_duration;
let latitude = null; let latitude = null;
let longitude = null; let longitude = null;
for (const [resolvedTarget, instance] of orchCache[address.toLowerCase()].instances) { console.log(address, orchCache[address.toLowerCase()]);
for (const [resolvedTarget, instance] of orchCache[
address.toLowerCase()
].instances) {
if (instance.livepeer_regions[region]) { if (instance.livepeer_regions[region]) {
latitude = instance.livepeer_regions[region].latitude; latitude = instance.livepeer_regions[region].latitude;
longitude = instance.livepeer_regions[region].longitude; longitude = instance.livepeer_regions[region].longitude;
@ -459,7 +472,8 @@ const updateScore = async function (address) {
} }
} }
if (hasEdited) { if (hasEdited) {
orchCache[address.toLowerCase()].leaderboardResults.lastTime = new Date().getTime(); orchCache[address.toLowerCase()].leaderboardResults.lastTime =
new Date().getTime();
} }
}; };
@ -469,6 +483,27 @@ const updateOrchScores = async function () {
} }
}; };
const recoverStorage = async function () {
await storage.init({
stringify: JSON.stringify,
parse: JSON.parse,
encoding: "utf8",
logging: false,
ttl: false,
forgiveParseErrors: false,
});
storedDomains = await storage.getItem("ensDomainCache");
if (storedDomains) {
ensDomainCache = storedDomains;
}
storedOrchs = await storage.getItem("orchCache");
if (storedOrchs) {
orchCache = storedOrchs;
}
isSynced = true;
};
recoverStorage();
const runTests = async function () { const runTests = async function () {
try { try {
const now = new Date().getTime(); const now = new Date().getTime();