Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get hashrate from new experimental metrics #22

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ ipcMain.on("message", async (event, arg) => {
// TODO make generic function for creating pub+sub endpoints
// TODO make class for subscription management
let cachedMetrics: SetMetricsStateActionPayload | null = null;
let longCachedMetrics: SetMetricsStateActionPayload | null = null;
let cachedMetricsStr = "";
// TODO list of webContents
// let cachedMetricsSubList = [];
Expand Down Expand Up @@ -108,10 +109,10 @@ async function getMetrics(): Promise<SetMetricsStateActionPayload> {
}
});
}
const hashRate = metricStringParse(
const networkHashRate = metricStringParse(
parsed.find((item: PrometheusMetricParser) => item.name === "average_network_hash_rate"),
);
const earnings = metricStringParse(
const avgBlockReward = metricStringParse(
parsed.find((item: PrometheusMetricParser) => item.name === "average_block_reward"),
);

Expand All @@ -132,23 +133,53 @@ async function getMetrics(): Promise<SetMetricsStateActionPayload> {
const weaveSize = metricStringParse(
parsed.find((item: PrometheusMetricParser) => item.name === "weave_size"),
);

const hashCountWatermark: number = parsed
.filter(
(item: PrometheusMetricParser) =>
item.name === "hash_1chunk_counter" || item.name === "hash_2chunk_counter",
)
.map(metricStringParse)
.filter((t) => t)
.reduce((a, b) => a! + b!, 0)!;

const ts = Date.now();
let hashRate = 0;
if (longCachedMetrics) {
const oldTs = longCachedMetrics.ts;
const oldHashCountWatermark = longCachedMetrics.hashCountWatermark;
const deltaHashCount = hashCountWatermark - oldHashCountWatermark;
const deltaTs = ts - oldTs;
hashRate = (deltaHashCount / deltaTs) * 1000;
}

console.log("DEBUG: getMetrics complete");
return {
dataUnpacked,
dataPacked,
storageAvailable,
weaveSize,
hashCountWatermark,
hashRate,
earnings,
weaveSize,
networkHashRate,
avgBlockReward,
earnings: 0, // TODO call /balance/<mining_addr>
vdfTimeLowerBound,
node,
ts,
};
}

async function cachedMetricsUpdate() {
try {
cachedMetricsUpdateInProgress = true;
cachedMetrics = await getMetrics();
if (
!longCachedMetrics ||
longCachedMetrics.hashCountWatermark > cachedMetrics.hashCountWatermark
) {
longCachedMetrics = cachedMetrics;
}
} catch (err) {
console.error(err);
}
Expand Down
20 changes: 17 additions & 3 deletions renderer/pages/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { useDispatch } from "react-redux";
import ScrollSpy from "react-ui-scrollspy";
import isElectron from "is-electron";
import { MainLayout } from "../layouts/MainLayout";
import { useEarnings, useHashRate } from "../store/metricsSlice/metricsSliceHooks";
import {
useEarnings,
useHashRate,
useAvgBlockReward,
} from "../store/metricsSlice/metricsSliceHooks";
import { setMetricsState } from "../store/metricsSlice/metricsSlice";
import { SetMetricsStateActionPayload } from "../../types/metrics";
import DataRelated from "../components/DataRelated";
Expand All @@ -26,6 +30,7 @@ const menuItems: MenuItems[] = [
{ label: "Data Related", target: "sub-section-1-1" },
{ label: "Hash Rate", target: "sub-section-1-2" },
{ label: "Earnings", target: "sub-section-1-3" },
{ label: "Average Block Reward", target: "sub-section-1-4" },
],
},
{
Expand All @@ -43,6 +48,7 @@ export default function DashboardPage() {
const dispatch = useDispatch();
const { hashRate } = useHashRate();
const { earnings } = useEarnings();
const { avgBlockReward } = useAvgBlockReward();

const [activeMenu, setActiveMenu] = useState<string>();

Expand Down Expand Up @@ -143,13 +149,21 @@ export default function DashboardPage() {
<div id="sub-section-1-2" className="bg-gray-100 p-4 rounded-lg h-64">
<h3 className="text-lg font-medium mb-2">Hash Rate</h3>
{typeof hashRate === "number" && (
<p className="text-gray-700">Hash rate: {hashRate}</p>
<p className="text-gray-700">Hash rate: {hashRate.toFixed(2)}</p>
)}
</div>
<div id="sub-section-1-3" className="bg-gray-100 p-4 rounded-lg h-64">
<h3 className="text-lg font-medium mb-2">Earnings</h3>
{typeof earnings === "number" && (
<p className="text-gray-700">Earnings: {earnings}</p>
<p className="text-gray-700">Earnings: {earnings} AR</p>
)}
</div>
<div id="sub-section-1-4" className="bg-gray-100 p-4 rounded-lg h-64">
<h3 className="text-lg font-medium mb-2">Average Block Reward</h3>
{typeof avgBlockReward === "number" && (
<p className="text-gray-700">
Average Block Reward: {(avgBlockReward / 1e12).toFixed(4)} AR
</p>
)}
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions renderer/store/metricsSlice/metricsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface MetricsSliceReducerState {
storageAvailable: number | undefined;
weaveSize: number | undefined;
hashRate: number | undefined;
avgBlockReward: number | undefined;
earnings: number | undefined;
vdfTimeLowerBound: number | undefined;
}
Expand All @@ -20,6 +21,7 @@ const initialMetricsState: MetricsSliceReducerState = {
storageAvailable: undefined,
weaveSize: undefined,
hashRate: undefined,
avgBlockReward: undefined,
earnings: undefined,
vdfTimeLowerBound: undefined,
};
Expand All @@ -38,6 +40,7 @@ export const metricsSlice = createSlice({
storageAvailable,
weaveSize,
hashRate,
avgBlockReward,
earnings,
vdfTimeLowerBound,
} = action.payload;
Expand All @@ -46,6 +49,7 @@ export const metricsSlice = createSlice({
state.storageAvailable = storageAvailable ?? undefined;
state.weaveSize = weaveSize ?? undefined;
state.hashRate = hashRate ?? undefined;
state.avgBlockReward = avgBlockReward ?? undefined;
state.earnings = earnings ?? undefined;
state.vdfTimeLowerBound = vdfTimeLowerBound ?? undefined;
},
Expand Down
7 changes: 7 additions & 0 deletions renderer/store/metricsSlice/metricsSliceHooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useSelector } from "react-redux";
import {
selectHashRate,
selectAvgBlockReward,
selectEarnings,
selectDataPacked,
selectDataUnpacked,
Expand All @@ -14,6 +15,12 @@ export const useHashRate = () => {
});
};

export const useAvgBlockReward = () => {
return useSelector(selectAvgBlockReward, (prev, current) => {
return prev.avgBlockReward === current.avgBlockReward;
});
};

export const useEarnings = () => {
return useSelector(selectEarnings, (prev, current) => {
return prev.earnings === current.earnings;
Expand Down
4 changes: 4 additions & 0 deletions renderer/store/metricsSlice/metricsSliceSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export const selectHashRate = (state: AppState) => ({
hashRate: state.metrics.hashRate,
});

export const selectAvgBlockReward = (state: AppState) => ({
avgBlockReward: state.metrics.avgBlockReward,
});

export const selectEarnings = (state: AppState) => ({
earnings: state.metrics.earnings,
});
Expand Down
6 changes: 5 additions & 1 deletion types/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ export interface SetMetricsStateActionPayload {
dataUnpacked: number | null;
dataPacked: number | null;
storageAvailable: number | null;
weaveSize: number | null;
hashCountWatermark: number;
hashRate: number | null;
weaveSize: number | null;
networkHashRate: number | null;
avgBlockReward: number | null;
earnings: number | null;
vdfTimeLowerBound: number | null;
node : ArweaveNodeConfig;
ts: number;
}