-
Notifications
You must be signed in to change notification settings - Fork 619
[NEB-92] Dashboard: Add Nebula Analytics dashboard #6198
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 26 additions & 6 deletions
32
apps/dashboard/src/app/team/[team_slug]/(team)/~/nebula/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...pp/team/[team_slug]/[project_slug]/nebula/components/analytics/fetch-nebula-analytics.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import "server-only"; | ||
| import { unstable_cache } from "next/cache"; | ||
|
|
||
| export type NebulaAnalyticsDataItem = { | ||
| date: string; | ||
| totalPromptTokens: number; | ||
| totalCompletionTokens: number; | ||
| totalSessions: number; | ||
| totalRequests: number; | ||
| }; | ||
|
|
||
| export const fetchNebulaAnalytics = unstable_cache( | ||
| async (params: { | ||
| accountId: string; | ||
| authToken: string; | ||
| from: string; | ||
| to: string; | ||
| interval: "day" | "week"; | ||
| }) => { | ||
| const analyticsEndpoint = process.env.ANALYTICS_SERVICE_URL as string; | ||
| const url = new URL(`${analyticsEndpoint}/v1/nebula/usage`); | ||
| url.searchParams.set("accountId", params.accountId); | ||
| url.searchParams.set("from", params.from); | ||
| url.searchParams.set("to", params.to); | ||
| url.searchParams.set("interval", params.interval); | ||
|
|
||
| const res = await fetch(url, { | ||
| headers: { | ||
| Authorization: `Bearer ${params.authToken}`, | ||
| }, | ||
| }); | ||
|
|
||
| if (!res.ok) { | ||
| const error = await res.text(); | ||
| return { | ||
| ok: false as const, | ||
| error: error, | ||
| }; | ||
| } | ||
|
|
||
| const resData = await res.json(); | ||
|
|
||
| return { | ||
| ok: true as const, | ||
| data: resData.data as NebulaAnalyticsDataItem[], | ||
| }; | ||
| }, | ||
| ["nebula-analytics"], | ||
| { | ||
| revalidate: 60 * 60, // 1 hour | ||
| }, | ||
| ); |
53 changes: 53 additions & 0 deletions
53
...p/team/[team_slug]/[project_slug]/nebula/components/analytics/nebula-analytics-filter.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| "use client"; | ||
|
|
||
| import { | ||
| useResponsiveSearchParams, | ||
| useSetResponsiveSearchParams, | ||
| } from "responsive-rsc"; | ||
| import { DateRangeSelector } from "../../../../../../../components/analytics/date-range-selector"; | ||
| import { IntervalSelector } from "../../../../../../../components/analytics/interval-selector"; | ||
| import { | ||
| getNebulaFiltersFromSearchParams, | ||
| normalizeTimeISOString, | ||
| } from "../../../../../../../lib/time"; | ||
|
|
||
| export function NebulaAnalyticsFilter() { | ||
| const responsiveSearchParams = useResponsiveSearchParams(); | ||
| const setResponsiveSearchParams = useSetResponsiveSearchParams(); | ||
|
|
||
| const { range, interval } = getNebulaFiltersFromSearchParams({ | ||
| from: responsiveSearchParams.from, | ||
| to: responsiveSearchParams.to, | ||
| interval: responsiveSearchParams.interval, | ||
| }); | ||
|
|
||
| return ( | ||
| <div className="flex items-center gap-3"> | ||
| <DateRangeSelector | ||
| range={range} | ||
| popoverAlign="end" | ||
| setRange={(newRange) => { | ||
| setResponsiveSearchParams((v) => { | ||
| return { | ||
| ...v, | ||
| from: normalizeTimeISOString(newRange.from), | ||
| to: normalizeTimeISOString(newRange.to), | ||
| }; | ||
| }); | ||
| }} | ||
| /> | ||
|
|
||
| <IntervalSelector | ||
| intervalType={interval} | ||
| setIntervalType={(newInterval) => { | ||
| setResponsiveSearchParams((v) => { | ||
| return { | ||
| ...v, | ||
| interval: newInterval, | ||
| }; | ||
| }); | ||
| }} | ||
| /> | ||
| </div> | ||
| ); | ||
| } |
115 changes: 115 additions & 0 deletions
115
...am/[team_slug]/[project_slug]/nebula/components/analytics/nebula-analytics-ui.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { TabButtons } from "@/components/ui/tabs"; | ||
| import type { Meta, StoryObj } from "@storybook/react"; | ||
| import { subDays } from "date-fns"; | ||
| import { useState } from "react"; | ||
| import { mobileViewport } from "../../../../../../../stories/utils"; | ||
| import type { NebulaAnalyticsDataItem } from "./fetch-nebula-analytics"; | ||
| import { NebulaAnalyticsDashboardUI } from "./nebula-analytics-ui"; | ||
|
|
||
| const meta = { | ||
| title: "Nebula/Analytics", | ||
| component: Story, | ||
| parameters: { | ||
| nextjs: { | ||
| appDirectory: true, | ||
| }, | ||
| }, | ||
| } satisfies Meta<typeof Story>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const Desktop: Story = { | ||
| args: {}, | ||
| }; | ||
|
|
||
| export const Mobile: Story = { | ||
| args: {}, | ||
| parameters: { | ||
| viewport: mobileViewport("iphone14"), | ||
| }, | ||
| }; | ||
|
|
||
| type VariantTab = "30-day" | "7-day" | "pending" | "60-day"; | ||
|
|
||
| function Story() { | ||
| const [tab, setTab] = useState<VariantTab>("60-day"); | ||
| return ( | ||
| <div className="container flex flex-col gap-8 py-10"> | ||
| <div> | ||
| <h2 className="mb-1 font-semibold text-xl tracking-tight"> | ||
| Story Variants | ||
| </h2> | ||
| <TabButtons | ||
| tabs={[ | ||
| { | ||
| name: "60 Days", | ||
| onClick: () => setTab("60-day"), | ||
| isActive: tab === "60-day", | ||
| isEnabled: true, | ||
| }, | ||
| { | ||
| name: "30 Days", | ||
| onClick: () => setTab("30-day"), | ||
| isActive: tab === "30-day", | ||
| isEnabled: true, | ||
| }, | ||
| { | ||
| name: "7 Days", | ||
| onClick: () => setTab("7-day"), | ||
| isActive: tab === "7-day", | ||
| isEnabled: true, | ||
| }, | ||
| { | ||
| name: "Pending", | ||
| onClick: () => setTab("pending"), | ||
| isActive: tab === "pending", | ||
| isEnabled: true, | ||
| }, | ||
| ]} | ||
| /> | ||
| </div> | ||
|
|
||
| {tab === "60-day" && ( | ||
| <NebulaAnalyticsDashboardUI | ||
| data={generateRandomNebulaAnalyticsData(60)} | ||
| isPending={false} | ||
| /> | ||
| )} | ||
|
|
||
| {tab === "30-day" && ( | ||
| <NebulaAnalyticsDashboardUI | ||
| data={generateRandomNebulaAnalyticsData(30)} | ||
| isPending={false} | ||
| /> | ||
| )} | ||
|
|
||
| {tab === "7-day" && ( | ||
| <NebulaAnalyticsDashboardUI | ||
| data={generateRandomNebulaAnalyticsData(7)} | ||
| isPending={false} | ||
| /> | ||
| )} | ||
|
|
||
| {tab === "pending" && ( | ||
| <NebulaAnalyticsDashboardUI data={[]} isPending={true} /> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function generateRandomNebulaAnalyticsData( | ||
| days: number, | ||
| ): NebulaAnalyticsDataItem[] { | ||
| return Array.from({ length: days }, (_, i) => ({ | ||
| date: subDays(new Date(), i).toISOString(), | ||
| totalPromptTokens: randomInt(1000), | ||
| totalCompletionTokens: randomInt(1000), | ||
| totalSessions: randomInt(100), | ||
| totalRequests: randomInt(4000), | ||
| })); | ||
| } | ||
|
|
||
| function randomInt(max: number) { | ||
| return Math.floor(Math.random() * max); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.