Skip to content

Commit

Permalink
feat: live speed
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Dec 3, 2024
1 parent b84be53 commit 420c1d9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
43 changes: 32 additions & 11 deletions src/components/ServerOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { Card, CardContent } from "@/components/ui/card";
import { cn } from "@/lib/utils";
import { useTranslation } from "react-i18next";
import { formatBytes } from "@/lib/format";
import { Separator } from "./ui/separator";

type ServerOverviewProps = {
online: number;
offline: number;
total: number;
up: number;
down: number;
upSpeed: number;
downSpeed: number;
};

export default function ServerOverview({
Expand All @@ -17,14 +20,16 @@ export default function ServerOverview({
total,
up,
down,
upSpeed,
downSpeed,
}: ServerOverviewProps) {
const { t } = useTranslation();

return (
<>
<section className="grid grid-cols-2 gap-4 lg:grid-cols-4">
<Card className={cn("hover:border-blue-500 transition-all")}>
<CardContent className="px-6 py-3">
<CardContent className="flex h-full items-center px-6 py-3">
<section className="flex flex-col gap-1">
<p className="text-sm font-medium md:text-base">
{t("serverOverview.totalServers")}
Expand All @@ -43,7 +48,7 @@ export default function ServerOverview({
" hover:ring-green-500 ring-1 ring-transparent transition-all",
)}
>
<CardContent className="px-6 py-3">
<CardContent className="flex h-full items-center px-6 py-3">
<section className="flex flex-col gap-1">
<p className="text-sm font-medium md:text-base">
{t("serverOverview.onlineServers")}
Expand All @@ -64,7 +69,7 @@ export default function ServerOverview({
" hover:ring-red-500 ring-1 ring-transparent transition-all",
)}
>
<CardContent className="px-6 py-3">
<CardContent className="flex h-full items-center px-6 py-3">
<section className="flex flex-col gap-1">
<p className="text-sm font-medium md:text-base">
{t("serverOverview.offlineServers")}
Expand All @@ -84,20 +89,36 @@ export default function ServerOverview({
" hover:ring-purple-500 ring-1 ring-transparent transition-all",
)}
>
<CardContent className="relative px-6 py-3">
<CardContent className="flex h-full items-center relative px-6 py-3">
<section className="flex flex-col gap-1">
<p className="text-sm font-medium md:text-base">
{t("serverOverview.totalBandwidth")}
</p>

<section className="flex flex-col sm:flex-row pt-[8px] sm:items-center items-start gap-1">
<p className="text-[12px] text-nowrap font-semibold">
<div className="flex items-center gap-1">
<p className="text-sm font-medium md:text-base">
{t("serverOverview.totalBandwidth")}
</p>
<Separator
orientation="vertical"
className="h-4 w-[1px]"
/>
<p className="text-sm font-medium md:text-base">
{t("serverOverview.speed")}
</p>
</div>
<section className="flex flex-row sm:items-center items-start gap-1">
<p className="sm:text-[12px] text-[10px] text-nowrap font-semibold">
{formatBytes(up)}
</p>
<p className="text-[12px] text-nowrap font-semibold">
<p className="sm:text-[12px] text-[10px] text-nowrap font-semibold">
{formatBytes(down)}
</p>
</section>
<section className="flex flex-row sm:items-center items-start gap-1">
<p className="sm:text-[12px] text-[10px] text-nowrap font-semibold">
{formatBytes(upSpeed)}/s
</p>
<p className="sm:text-[12px] text-[10px] text-nowrap font-semibold">
{formatBytes(downSpeed)}/s
</p>
</section>
</section>
</CardContent>
</Card>
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"totalServers": "Total Servers",
"onlineServers": "Online Servers",
"offlineServers": "Offline Servers",
"totalBandwidth": "Total Bandwidth"
"totalBandwidth": "Total Bandwidth",
"speed": "Speed"
},
"serverCard": {
"mem": "MEM",
Expand Down
3 changes: 2 additions & 1 deletion src/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"totalServers": "服务器总数",
"onlineServers": "在线服务器",
"offlineServers": "离线服务器",
"totalBandwidth": "总流量"
"totalBandwidth": "总流量",
"speed": "速率"
},
"serverCard": {
"mem": "内存",
Expand Down
3 changes: 2 additions & 1 deletion src/locales/zh-TW/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"totalServers": "總服務器",
"onlineServers": "線上服務器",
"offlineServers": "離線服務器",
"totalBandwidth": "總帶寬"
"totalBandwidth": "總帶寬",
"speed": "速率"
},
"serverCard": {
"mem": "內存",
Expand Down
19 changes: 19 additions & 0 deletions src/pages/Server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ export default function Servers() {
0,
) || 0;

const upSpeed =
nezhaWsData?.servers?.reduce(
(total, server) =>
formatNezhaInfo(nezhaWsData.now, server).online
? total + (server.state?.net_out_speed ?? 0)
: total,
0,
) || 0;
const downSpeed =
nezhaWsData?.servers?.reduce(
(total, server) =>
formatNezhaInfo(nezhaWsData.now, server).online
? total + (server.state?.net_in_speed ?? 0)
: total,
0,
) || 0;

const filteredServers =
nezhaWsData?.servers?.filter((server) => {
if (currentGroup === "All") return true;
Expand All @@ -123,6 +140,8 @@ export default function Servers() {
offline={offlineServers}
up={up}
down={down}
upSpeed={upSpeed}
downSpeed={downSpeed}
/>
<section className="flex mt-6 items-center gap-2 w-full overflow-hidden">
<button
Expand Down

0 comments on commit 420c1d9

Please sign in to comment.