Skip to content

Commit

Permalink
Merge pull request #16 from ArweaveTeam/feat/config_aware_poll
Browse files Browse the repository at this point in the history
Feat/config aware poll
  • Loading branch information
vird authored Nov 24, 2023
2 parents 1a91ad9 + c6a1cda commit 3d64b4b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
50 changes: 37 additions & 13 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import parsePrometheusTextFormat from "parse-prometheus-text-format";
import { createWindow } from "./helpers";
import { PrometheusMetricParser } from "./types/prometheus";
import { SetMetricsStateActionPayload } from "../types/metrics";
import * as config from "./config";

const isProd = process.env.NODE_ENV === "production";

Expand Down Expand Up @@ -54,14 +55,36 @@ ipcMain.on("message", async (event, arg) => {
event.reply("message", `${arg} World!`);
});

// TODO make generic function for creating pub+sub endpoints
// TODO make class for subscription management
let cachedMetrics: SetMetricsStateActionPayload | null = null;
let cachedMetricsStr = "";
// TODO list of webContents
// let cachedMetricsSubList = [];
let cachedMetricsIsSubActive = false;
let cachedMetricsTimeout: NodeJS.Timeout | undefined;
let cachedMetricsUpdateInProgress = false;
// TODO move to config
const currentNodeId = 0;

function metricStringParse(item: PrometheusMetricParser | undefined): number | null {
if (!item) return null;
return +item.metrics[0].value;
}

async function getMetrics(): Promise<SetMetricsStateActionPayload> {
console.log("DEBUG: getMetrics start");
const res = await fetch("http://testnet-3.arweave.net:1984/metrics");
const nodeList = config.configHandler.configGetNodes();
// temp for development
const node = nodeList[currentNodeId] || {
id: "",
name: "",
host: "testnet-3.arweave.net",
port: "1984",
protocol: "http",
};
const url = `${node.protocol}://${node.host}:${node.port}/metrics`;
const res = await fetch(url);
const data = await res.text();

const parsed: PrometheusMetricParser[] = parsePrometheusTextFormat(data) || [];
Expand Down Expand Up @@ -118,19 +141,10 @@ async function getMetrics(): Promise<SetMetricsStateActionPayload> {
hashRate,
earnings,
vdfTimeLowerBound,
node,
};
}

// TODO make generic function for creating pub+sub endpoints
// TODO make class for subscription management
let cachedMetrics: SetMetricsStateActionPayload | null = null;
let cachedMetricsStr = "";
// TODO list of webContents
// let cachedMetricsSubList = [];
let cachedMetricsIsSubActive = false;
let cachedMetricsTimeout: NodeJS.Timeout | null = null;
let cachedMetricsUpdateInProgress = false;

async function cachedMetricsUpdate() {
try {
cachedMetricsUpdateInProgress = true;
Expand All @@ -153,7 +167,7 @@ async function cachedMetricsUpdatePing() {
if (cachedMetricsUpdateInProgress) return;
if (cachedMetricsTimeout) {
clearTimeout(cachedMetricsTimeout);
cachedMetricsTimeout = null;
cachedMetricsTimeout = undefined;
// extra push fast. Needed on initial subscription
cachedMetricsPush();
await cachedMetricsUpdate();
Expand All @@ -167,15 +181,25 @@ async function cachedMetricsUpdatePing() {
cachedMetricsTimeout = setTimeout(async () => {
// extra check needed
if (!isAlive) return;
cachedMetricsTimeout = null;
cachedMetricsTimeout = undefined;
await cachedMetricsUpdate();
cachedMetricsPush();
cachedMetricsUpdatePing();
}, delay);
}

config.eventHub.on("nodes_update", () => {
const node = config.configHandler.configGetNodes()[currentNodeId];
if (cachedMetrics && JSON.stringify(cachedMetrics.node) !== JSON.stringify(node)) {
cachedMetrics = null;
cachedMetricsPush();
}
cachedMetricsUpdatePing();
});

(async function () {
await cachedMetricsUpdate();
cachedMetricsPush();
cachedMetricsUpdatePing();
})();

Expand Down
4 changes: 4 additions & 0 deletions main/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { EventEmitter } from "events";
import Store from "electron-store";
import { v4 as uuidv4 } from "uuid";
import { ArweaveMinerUiConfig, ArweaveNodeConfig, NewArweaveNodeConfig } from "../types/config";

export const eventHub = new EventEmitter();
eventHub.setMaxListeners(Infinity);
export let selectedNode: ArweaveNodeConfig | undefined;

const schema = {
Expand Down Expand Up @@ -43,6 +46,7 @@ export const configHandler = {
const currentNodes = store.get("nodes", []);
const newNodes = [...currentNodes, newNode];
store.set("nodes", newNodes);
eventHub.emit("nodes_update", {});
return newNode;
},
setSelectedNodeById: (id: string) => {
Expand Down
2 changes: 2 additions & 0 deletions types/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ArweaveNodeConfig } from "./config"
export interface SetMetricsStateActionPayload {
dataUnpacked: number | null;
dataPacked: number | null;
Expand All @@ -6,4 +7,5 @@ export interface SetMetricsStateActionPayload {
hashRate: number | null;
earnings: number | null;
vdfTimeLowerBound: number | null;
node : ArweaveNodeConfig;
}

0 comments on commit 3d64b4b

Please sign in to comment.