Skip to content

Commit

Permalink
feat: switch to gauge for topology metrics
Browse files Browse the repository at this point in the history
Closes #1477
  • Loading branch information
mainawycliffe committed Nov 9, 2023
1 parent 6addf52 commit a160298
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/components/TopologyCard/Property.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Age } from "../../ui/Age";
import { isEmpty } from "../Canary/utils";
import { Icon } from "../Icon";
import {
FormatPropertyCPUMemory,
FormatPropertyCurrency,
FormatPropertyDefault,
FormatPropertyURL
Expand Down Expand Up @@ -36,6 +37,10 @@ export function FormatProperty({
return <FormatPropertyCurrency property={property} />;
}

if (property.name === "cpu" || property.name === "memory") {
return <FormatPropertyCPUMemory property={property} />;
}

return <FormatPropertyDefault property={property} short={short} />;
}

Expand Down
27 changes: 27 additions & 0 deletions src/components/TopologyCard/Utils/FormatProperties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useMemo, useState } from "react";
import { formatBytes } from "../../../utils/common";
import { isEmpty } from "lodash";
import { TopologyProperty } from "../../../api/types/topology";
import ProgressBar from "../../../ui/stats/ProgressBar";

type FormatPropertyProps = {
property?: TopologyProperty;
Expand Down Expand Up @@ -74,6 +75,32 @@ function convertUnitsToDisplayValue(value: number, unit?: string) {
return value;
}

export function FormatPropertyCPUMemory({ property }: FormatPropertyProps) {
if (
!property ||
!property.value ||
(property?.name !== "cpu" && property?.name !== "memory")
) {
return null;
}

console.log("property", property);

const value = property.value;
const max = property.max;

if (max) {
const percent = (Number(value) / Number(max)) * 100;
return (
<div data-tip={`${percent.toFixed(2)}% of ${max}`} className="block w-14">
<ProgressBar value={percent} />
</div>
);
}

return <span data-tip={value.toString()}>{value.toString()}</span>;
}

export function FormatPropertyDefault({
property,
short
Expand Down
29 changes: 29 additions & 0 deletions src/ui/stats/ProgressBar.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { ComponentProps } from "react";
import { Story, Meta } from "@storybook/react";
import ProgressBar from "./ProgressBar";

export default {
title: "ProgressBar",
component: ProgressBar
} as Meta;

const Template: Story<ComponentProps<typeof ProgressBar>> = (args) => (
<div className="flex flex-col space-y-4">
<ProgressBar {...args} />
</div>
);

export const FivePercent = Template.bind({});
FivePercent.args = {
value: 5
};

export const FiftyPercent = Template.bind({});
FiftyPercent.args = {
value: 50
};

export const EightyPercent = Template.bind({});
EightyPercent.args = {
value: 80
};
29 changes: 29 additions & 0 deletions src/ui/stats/ProgressBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { CSSProperties } from "react";

interface Props {
value: number;
className?: string;
}

export default function ProgressBar({ value, className = "h-3" }: Props) {
const barStyle: CSSProperties = {
width: `${value}%`,
backgroundColor: value > 70 ? "red" : "green"
};

const roundingClassName =
value < 25
? "rounded-l-2xl"
: value > 50
? "rounded-full"
: "rounded-l-xl rounded-r-xl";

return (
<div className={`relative rounded-full bg-gray-400 ${className}`}>
<div
className={`absolute top-0 left-0 h-full ${roundingClassName}`}
style={barStyle}
></div>
</div>
);
}

0 comments on commit a160298

Please sign in to comment.