Skip to content

Commit

Permalink
fix: analytics api return data
Browse files Browse the repository at this point in the history
  • Loading branch information
shadrach-tayo committed Nov 7, 2024
1 parent c6379f4 commit 3e0ab01
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/app/api/download/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function GET(_request: Request) {
headers: {
cookie: cookies().toString(),
"Content-Type": "application/json",
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InNoYWRyYWNoQGRlc2NpLmNvbSIsImlhdCI6MTcyODM5NjAyMCwiZXhwIjoxNzU5OTUzNjIwfQ.Qck4KQEmraFRaqjtb-fNCUR9aM_tVMWLeQbGwjgs-2k'
},
}
);
Expand Down
35 changes: 19 additions & 16 deletions src/components/molecules/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Overview } from "./Overview";
import { RecentSales } from "./RecentSales";
import { useSuspenseQuery } from "@tanstack/react-query";
import { useQuery, useSuspenseQuery } from "@tanstack/react-query";
import { getAnalytics } from "@/lib/api";
import { Activity, Box, HardDrive, LoaderIcon, UsersRound } from "lucide-react";
import { useRouter } from "next/navigation";
Expand Down Expand Up @@ -251,9 +251,11 @@ const MetricCard = ({
</Card>
);
};
const numberValue = (num: number) => num > 0 ? `+${num}` : '0';

function Analytics() {
const { data: analytics, isLoading } = useSuspenseQuery(getAnalytics);
const { data: analytics, isLoading, error, isError } = useSuspenseQuery(getAnalytics);
console.log({ analytics, isLoading, isError, error})
const byteValueNumberFormatter = Intl.NumberFormat("en", {
notation: "compact",
style: "unit",
Expand All @@ -270,74 +272,75 @@ function Analytics() {

if (!analytics) return <IncomingFeature />


return (
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
<MetricCard
header="New users"
value={`+${analytics.newUsersToday}`}
value={numberValue(analytics.newUsersToday)}
description="Today"
/>
<MetricCard
header="New users"
value={`+${analytics.newUsersInLast7Days}`}
value={numberValue(analytics.newUsersInLast7Days)}
description="Last 7 days"
/>
<MetricCard
header="New users"
value={`+${analytics.newUsersInLast30Days}`}
value={numberValue(analytics.newUsersInLast30Days)}
description="Last 30 days"
/>
<MetricCard
header="Active users"
value={`+${analytics.activeUsersToday}`}
value={numberValue(analytics.activeUsersToday)}
description="Today"
icon="activity"
/>
<MetricCard
header="Active users"
value={`+${analytics.activeUsersInLast7Days}`}
value={numberValue(analytics.activeUsersInLast7Days)}
description="Last 7 days"
icon="activity"
/>
<MetricCard
header="Active users"
value={`+${analytics.activeUsersInLast30Days}`}
value={numberValue(analytics.activeUsersInLast30Days)}
description="Last 30 days"
icon="activity"
/>
<MetricCard
header="New Nodes"
value={`+${analytics.newNodesToday}`}
value={numberValue(analytics.newNodesToday)}
description="Today"
icon="nodes"
/>
<MetricCard
header="New Nodes"
value={`+${analytics.newNodesInLast7Days}`}
value={numberValue(analytics.newNodesInLast7Days)}
description="Last 7 days"
icon="nodes"
/>
<MetricCard
header="New Nodes"
value={`+${analytics.newNodesInLast30Days}`}
value={numberValue(analytics.newNodesInLast30Days)}
description="Last 30 days"
icon="nodes"
/>
<MetricCard
header="Node views"
value={`+${analytics.nodeViewsToday}`}
value={numberValue(analytics.nodeViewsToday)}
description="Today"
icon="nodes"
/>
<MetricCard
header="Node views"
value={`+${analytics.nodeViewsInLast7Days}`}
value={numberValue(analytics.nodeViewsInLast7Days)}
description="Last 7 days"
icon="nodes"
/>
<MetricCard
header="Node views"
value={`+${analytics.nodeViewsInLast30Days}`}
value={numberValue(analytics.nodeViewsInLast30Days)}
description="Last 30 days"
icon="nodes"
/>
Expand All @@ -349,15 +352,15 @@ function Analytics() {
/>
<MetricCard
header="Uploaded Data"
value={`+${byteValueNumberFormatter.format(
value={`${byteValueNumberFormatter.format(
analytics.bytesInLast7Days
)}`}
description="Last 7 days"
icon="data"
/>
<MetricCard
header="Uploaded Data"
value={`+${byteValueNumberFormatter.format(
value={`${byteValueNumberFormatter.format(
analytics.bytesInLast30Days
)}`}
description="Last 30 days"
Expand Down
40 changes: 20 additions & 20 deletions src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export interface Analytics {
nodeViewsToday: number;
nodeViewsInLast7Days: number;
nodeViewsInLast30Days: number;
bytesToday: any;
bytesToday: number;
bytesInLast7Days: number;
bytesInLast30Days: number;
}
Expand All @@ -255,25 +255,25 @@ export const getAnalytics = queryOptions({
const response = await fetch(`${NODES_API_URL}/v1/admin/analytics`, {
credentials: "include",
});
const json = (await response.json()) as ApiResponse<Analytics>;
// return {
// "newUsersInLast30Days": 5,
// "newUsersInLast7Days": 2,
// "newUsersToday": 0,
// "newNodesInLast30Days": 2292,
// "newNodesInLast7Days": 454,
// "newNodesToday": 0,
// "activeUsersToday": 1,
// "activeUsersInLast7Days": 11,
// "activeUsersInLast30Days": 17,
// "nodeViewsToday": 2,
// "nodeViewsInLast7Days": 3485,
// "nodeViewsInLast30Days": 14245,
// "bytesToday": null,
// "bytesInLast7Days": 22890864,
// "bytesInLast30Days": 77358695
// }
return json.data ?? null;
const json = (await response.json()) as Analytics;
return json || null;
},
staleTime: 60 * 1000
});
// return {
// "newUsersInLast30Days": 5,
// "newUsersInLast7Days": 2,
// "newUsersToday": 0,
// "newNodesInLast30Days": 2292,
// "newNodesInLast7Days": 454,
// "newNodesToday": 0,
// "activeUsersToday": 1,
// "activeUsersInLast7Days": 11,
// "activeUsersInLast30Days": 17,
// "nodeViewsToday": 2,
// "nodeViewsInLast7Days": 3485,
// "nodeViewsInLast30Days": 14245,
// "bytesToday": null,
// "bytesInLast7Days": 22890864,
// "bytesInLast30Days": 77358695
// }

0 comments on commit 3e0ab01

Please sign in to comment.