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

better data sources; fixed measure values; misc #8

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 17 additions & 3 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ function metric_string_parse(item): number | null {
if (!item) return null;
return +item.metrics[0].value;
}
ipcMain.on("metrics", async (event) => {

// better dev quality, temp solution
async function getMetrics(): Promise<Metrics> {
console.log("DEBUG: getMetrics start");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should consider using logger library (like winston or pino) in order to include debug logs but turn them down in releases

const res = await fetch("http://testnet-3.arweave.net:1984/metrics");
const data = await res.text();
Comment on lines 53 to 54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to the PR, but we are perhaps missing try/catch here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context of this PR no. Because I want that promise should be error, so FE will also receive error, so I can see it in browser console

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may end up crashing nodejs if it's not caught anywhere, in a single threaded system it would be fatal error. It's not a big deal for now, I mainly thinking it makes sense to give better error messages. Uncaught errors in promises are quite hard to read and understand.


const parsed: MinorParser[] = parsePrometheusTextFormat(data);
let data_unpackaged = 0;
let data_packaged = 0;
let storage_available = 0;
const packing_item = parsed.find(
(item: MinorParser) => item.name === "v2_index_data_size_by_packing",
);
Expand All @@ -64,6 +68,7 @@ ipcMain.on("metrics", async (event) => {
} else {
data_packaged += +item.value;
}
storage_available += +item.labels.partition_size;
});
}
const hash_rate = metric_string_parse(
Expand All @@ -87,15 +92,24 @@ ipcMain.on("metrics", async (event) => {
}
}
}
const metrics: Metrics = {
const weave_size = metric_string_parse(
parsed.find((item: MinorParser) => item.name === "weave_size"),
);
console.log("DEBUG: getMetrics complete");
return {
data_unpackaged,
data_packaged,
storage_available,
weave_size,
hash_rate,
earnings,
vdf_time_lower_bound,
};
}

event.reply("metrics", metrics);
const cached_metrics: Promise<Metrics> = getMetrics();
hlolli marked this conversation as resolved.
Show resolved Hide resolved
ipcMain.on("metrics", async (event) => {
event.reply("metrics", await cached_metrics);
});

ipcMain.on("open-url", async (event, arg) => {
Expand Down
10 changes: 7 additions & 3 deletions renderer/components/Charts/DataRelated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function DataRelated({
width: "2%",
}}
>
<TopArrow value={data_package.size} unit={data_package.unit} color="#7BF05E" />
<TopArrow value={data_package.display_value} unit={data_package.unit} color="#7BF05E" />
</div>

<div
Expand All @@ -22,11 +22,15 @@ export default function DataRelated({
width: "4%",
}}
>
<BottomArrow value={storage_available.size} unit={storage_available.unit} color="#1D2988" />
<BottomArrow
value={storage_available.display_value}
unit={storage_available.unit}
color="#1D2988"
/>
</div>

<div className="w-full bg-[#A7A7A7] hover:bg-[#989797] h-full cursor-pointer relative group">
<TopArrow value={total_size.size} unit={total_size.unit} color="#A7A7A7" />
<TopArrow value={total_size.display_value} unit={total_size.unit} color="#A7A7A7" />
</div>
</div>
);
Expand Down
33 changes: 14 additions & 19 deletions renderer/pages/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { setMinorState, selectMinorState } from "../store/minorSlice";
import { useDispatch, useSelector } from "react-redux";
import ScrollSpy from "react-ui-scrollspy";
import DataRelated from "../components/Charts/DataRelated";
import { Metrics } from "../types/Minor";
import { Metrics } from "../../types/metrics";
import { DataRelatedChart } from "../types/Charts";
import { convertToGbFromPetabytes } from "../util/minor";
import { fmt_size } from "../util/minor";

interface MenuItems {
label: string;
Expand All @@ -22,16 +22,20 @@ export default function DashboardPage() {
const minorState = useSelector(selectMinorState);
const dispatch = useDispatch();
const [dataRelated, setDataRelated] = React.useState<DataRelatedChart>({
// TODO loading state
data_package: {
size: 0,
unit: "GB",
value: 0,
display_value: "0",
unit: "TB",
},
storage_available: {
size: 0,
unit: "GB",
value: 0,
display_value: "0",
unit: "TB",
},
total_size: {
size: 0,
value: 0,
display_value: "0",
unit: "TB",
},
});
Expand Down Expand Up @@ -69,18 +73,9 @@ export default function DashboardPage() {
setDataRelated((prev) => {
return {
...prev,
data_package: {
size: convertToGbFromPetabytes(data.data_packaged) || 0,
unit: "GB",
},
storage_available: {
size: convertToGbFromPetabytes(data.data_unpackaged) || 0,
unit: "GB",
},
total_size: {
size: data.data_packaged + data.data_unpackaged,
unit: "TB",
},
data_package: fmt_size(data.data_packaged),
storage_available: fmt_size(data.storage_available),
total_size: fmt_size(data.weave_size),
};
});
}
Expand Down
25 changes: 11 additions & 14 deletions renderer/types/Charts.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
export type ValueWithMeasureValue = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love that you made this type. This is a bit controversial in typescript, but you could consider replacing type with interface if it's a simple record type (they are generally faster and more composeable), not a blocker and this is just a comment :)

value: number;
display_value: string;
unit: string;
};
export type DataRelatedChart = {
data_package: {
size: number;
unit: string;
};
storage_available: {
size: number;
unit: string;
};
total_size: {
size: number;
unit: string;
};
data_package: ValueWithMeasureValue;
storage_available: ValueWithMeasureValue;
total_size: ValueWithMeasureValue;
};

// TODO merge into 1 type
export type TopArrow = {
value: number;
value: string;
unit: string;
color: string;
};

export type BottomArrow = {
value: number;
value: string;
unit: string;
color: string;
};
37 changes: 34 additions & 3 deletions renderer/util/minor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
export function convertToGbFromPetabytes(value: number): number {
const gigabytes = value * 1e9; // 1 petabyte = 1e9 gigabytes
return parseFloat(gigabytes.toFixed(2));
import { ValueWithMeasureValue } from "../types/Charts";
const size_mv_list = ["B", "KB", "MB", "GB", "TB", "PB"];

// rough port from
// https://github.com/virdpool/ar_miner_ui_playground/blob/e28a8034bd367a35fe40878c90c606e456b2346f/electron_app/htdocs/util/fmt.coffee#L6
// TODO refactor
export function fmt_size(orig_value: number): ValueWithMeasureValue {
hlolli marked this conversation as resolved.
Show resolved Hide resolved
let idx = 0;
let value = orig_value;
while (idx < size_mv_list.length) {
const unit = size_mv_list[idx];
if (value < 900 || idx + 1 >= size_mv_list.length) {
if (idx === 0) {
return {
value: orig_value,
display_value: value.toString(),
unit,
};
} else {
return {
value: orig_value,
display_value: value.toFixed(2),
unit,
};
}
}
value /= 1024;
idx++;
}
return {
value: orig_value,
display_value: "0",
unit: "B",
};
}
2 changes: 2 additions & 0 deletions types/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export type Metrics = {
data_unpackaged: number;
data_packaged: number;
storage_available: number;
weave_size: number;
hash_rate: number;
earnings: number;
vdf_time_lower_bound: number;
Expand Down