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

Feat: node health indicator #11

Merged
merged 4 commits into from
Jul 5, 2024
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
23 changes: 21 additions & 2 deletions frontend/src/components/ui/progress.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import * as ProgressPrimitive from "@radix-ui/react-progress";
import * as React from "react";

import { cn } from "src/lib/utils";

Expand All @@ -23,4 +23,23 @@ const Progress = React.forwardRef<
));
Progress.displayName = ProgressPrimitive.Root.displayName;

export { Progress };
const CircleProgress = React.forwardRef<
React.ElementRef<typeof ProgressPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ProgressPrimitive.Root>
>(({ className, value, ...props }, ref) => (
<ProgressPrimitive.Root
ref={ref}
className={cn(
`relative h-20 w-20 overflow-hidden rounded-full flex justify-center items-center`,
className
)}
{...props}
style={{
background: `radial-gradient(closest-side, hsl(var(--background)) 79%, transparent 80% 100%), conic-gradient(hsl(var(--primary)) ${value || 0}%, hsl(var(--secondary)) 0)`,
}}
>
{props.children || <div className="">{`${value || 0}%`}</div>}
</ProgressPrimitive.Root>
));

export { CircleProgress, Progress };
55 changes: 52 additions & 3 deletions frontend/src/screens/channels/Channels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CopyIcon,
ExternalLinkIcon,
HandCoins,
Heart,
Hotel,
InfoIcon,
MoreHorizontal,
Expand Down Expand Up @@ -43,7 +44,7 @@ import {
DropdownMenuTrigger,
} from "src/components/ui/dropdown-menu.tsx";
import { LoadingButton } from "src/components/ui/loading-button.tsx";
import { Progress } from "src/components/ui/progress.tsx";
import { CircleProgress, Progress } from "src/components/ui/progress.tsx";
import {
Table,
TableBody,
Expand Down Expand Up @@ -95,6 +96,8 @@ export default function Channels() {
const [drainingAlbySharedFunds, setDrainingAlbySharedFunds] =
React.useState(false);

const nodeHealth = channels ? getNodeHealth(channels) : 0;

// TODO: move to NWC backend
const loadNodeStats = React.useCallback(async () => {
if (!channels) {
Expand Down Expand Up @@ -287,7 +290,7 @@ export default function Channels() {
title="Liquidity"
description="Manage your lightning node liquidity"
contentRight={
<>
<div className="flex gap-3 items-center justify-center">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="default">
Expand Down Expand Up @@ -363,7 +366,19 @@ export default function Channels() {
{/* <Link to="/channels/new">
<Button>Open Channel</Button>
</Link> */}
</>
<ExternalLink to="https://guides.getalby.com/user-guide/v/alby-account-and-browser-extension/alby-hub/liquidity/node-health">
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<CircleProgress value={nodeHealth} className="w-9 h-9">
<Heart className="w-4 h-4" />
</CircleProgress>
</TooltipTrigger>
<TooltipContent>Node health: {nodeHealth}%</TooltipContent>
</Tooltip>
</TooltipProvider>
</ExternalLink>
</div>
}
></AppHeader>

Expand Down Expand Up @@ -797,3 +812,37 @@ export default function Channels() {
</>
);
}

function getNodeHealth(channels: Channel[]) {
const totalChannelCapacitySats = channels
.map((channel) => (channel.localBalance + channel.remoteBalance) / 1000)
.reduce((a, b) => a + b, 0);

const averageChannelBalance =
channels
.map((channel) => {
const totalBalance = channel.localBalance + channel.remoteBalance;
const expectedBalance = totalBalance / 2;
const actualBalance =
Math.min(channel.localBalance, channel.remoteBalance) /
expectedBalance;
return actualBalance;
})
.reduce((a, b) => a + b, 0) / (channels.length || 1);

const numUniqueChannelPartners = new Set(
channels.map((channel) => channel.remotePubkey)
).size;

let nodeHealth = Math.ceil(
Math.min(3, numUniqueChannelPartners) *
(100 / 3) * // 3 channels is great
(Math.min(totalChannelCapacitySats, 1_000_000) / 1_000_000) * // 1 million sats or more is great
averageChannelBalance // perfectly balanced is great!
);

// above calculation is a bit harsh
nodeHealth = Math.min(nodeHealth * 2, 100);

return nodeHealth;
}
Loading