-
Notifications
You must be signed in to change notification settings - Fork 328
Add usage analytics filter panel component #30120
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
Closed
avervaet
wants to merge
11
commits into
avervaet/consumption-scope-query-auth-fix
from
avervaet/usage-analytics-filtering-react-component
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d42e6ba
Add usage analytics filter panel component with preview route
avervaet d6b7be5
Improving Usage filter panel component UI
avervaet 1e5f6cd
Move usage filter panel to the consumption analytics page
avervaet 2e8d58f
Use exhaustive switch with assertNeverAndIgnore in renderEntityIcon
avervaet 630687d
Split UsageFilterPanel into smaller sub-components
avervaet 5fc4e39
Move sub-component-local UI state out of UsageFilterPanel
avervaet 87c3a08
review changes
avervaet c42d4e1
clarity renaming
avervaet 75a2b5b
cleaning
avervaet 3a15cc5
Stop using arbitrary component size
avervaet 226a78b
Working on Aubin comments
avervaet 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| import { ConsumptionAttributionTable } from "@app/components/workspace/analytics/consumption/ConsumptionAttributionTable"; | ||
| import { ConsumptionOverview } from "@app/components/workspace/analytics/consumption/ConsumptionOverview"; | ||
| import { ConsumptionPeriodSelector } from "@app/components/workspace/analytics/consumption/ConsumptionPeriodSelector"; | ||
| import type { ConsumptionDimension } from "@app/components/workspace/analytics/consumption/consumptionDimensions"; | ||
| import { DEFAULT_CONSUMPTION_DIMENSION } from "@app/components/workspace/analytics/consumption/consumptionDimensions"; | ||
| import type { ConsumptionPeriodSelection } from "@app/lib/analytics/consumption_period"; | ||
| import { DEFAULT_CONSUMPTION_PERIOD } from "@app/lib/analytics/consumption_period"; | ||
| import { UsageFilterPanel } from "@app/components/workspace/analytics/UsageFilterPanel"; | ||
| import type { UsageFilter } from "@app/components/workspace/analytics/usageFilter"; | ||
| import { | ||
| USAGE_FILTER_MOCK_GROUPS, | ||
| USAGE_FILTER_MOCK_OPTIONS, | ||
| } from "@app/components/workspace/analytics/usageFilterMockData"; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed higher in the stack |
||
| import { useFeatureFlags, useWorkspace } from "@app/lib/auth/AuthContext"; | ||
| import { isNavigationLocked } from "@app/lib/navigation-lock"; | ||
| import { BarChart01, cn, Page, SafeSuspense, safeLazy } from "@dust-tt/sparkle"; | ||
|
|
@@ -37,6 +43,7 @@ | |
| const [dimension, setDimension] = useState<ConsumptionDimension>( | ||
| DEFAULT_CONSUMPTION_DIMENSION | ||
| ); | ||
| const [filter, setFilter] = useState<UsageFilter>({}); | ||
|
|
||
| if (!isEnabled) { | ||
| return ( | ||
|
|
@@ -75,13 +82,24 @@ | |
| /> | ||
| <div className="flex flex-col gap-8 pb-8"> | ||
| <ConsumptionOverview workspaceId={owner.sId} period={period} /> | ||
| <SafeSuspense fallback={<ChartFallback />}> | ||
| <ConsumptionChart | ||
| workspaceId={owner.sId} | ||
| period={period} | ||
| dimension={dimension} | ||
| /> | ||
| </SafeSuspense> | ||
| <div className="flex flex-col gap-2"> | ||
| <div className="flex justify-end"> | ||
| <UsageFilterPanel | ||
| owner={owner} | ||
| categoryOptions={USAGE_FILTER_MOCK_OPTIONS} | ||
| groups={USAGE_FILTER_MOCK_GROUPS} | ||
| filter={filter} | ||
| onFilterChange={setFilter} | ||
| /> | ||
| </div> | ||
| <SafeSuspense fallback={<ChartFallback />}> | ||
| <ConsumptionChart | ||
| workspaceId={owner.sId} | ||
| period={period} | ||
| dimension={dimension} | ||
| /> | ||
| </SafeSuspense> | ||
| </div> | ||
| <ConsumptionAttributionTable | ||
| workspaceId={owner.sId} | ||
| period={period} | ||
|
|
||
265 changes: 265 additions & 0 deletions
265
front/components/workspace/analytics/UsageFilterPanel.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,265 @@ | ||
| import type { | ||
| UsageFilter, | ||
| UsageFilterAgentOption, | ||
| UsageFilterCategory, | ||
| UsageFilterGroup, | ||
| UsageFilterMemberOption, | ||
| UsageFilterModelOption, | ||
| UsageFilterOptionForCategory, | ||
| UsageFilterScope, | ||
| UsageFilterSkillOption, | ||
| UsageFilterSourceOption, | ||
| UsageFilterToolOption, | ||
| UsageModelTier, | ||
| } from "@app/components/workspace/analytics/usageFilter"; | ||
| import { | ||
| USAGE_FILTER_CATEGORIES, | ||
| USAGE_FILTER_CATEGORY_LABEL, | ||
| USAGE_FILTER_SCOPES, | ||
| USAGE_MODEL_TIERS, | ||
| } from "@app/components/workspace/analytics/usageFilter"; | ||
| import { UsageFilterAgentScopeControls } from "@app/components/workspace/analytics/usageFilterPanel/UsageFilterAgentScopeControls"; | ||
| import { UsageFilterCategoryNav } from "@app/components/workspace/analytics/usageFilterPanel/UsageFilterCategoryNav"; | ||
| import { UsageFilterFooter } from "@app/components/workspace/analytics/usageFilterPanel/UsageFilterFooter"; | ||
| import { UsageFilterMemberGroupsControls } from "@app/components/workspace/analytics/usageFilterPanel/UsageFilterMemberGroupsControls"; | ||
| import { UsageFilterModelComplexityControls } from "@app/components/workspace/analytics/usageFilterPanel/UsageFilterModelComplexityControls"; | ||
| import { UsageFilterOptionCheckboxList } from "@app/components/workspace/analytics/usageFilterPanel/UsageFilterOptionCheckboxList"; | ||
| import { UsageFilterSelectionSummary } from "@app/components/workspace/analytics/usageFilterPanel/UsageFilterSelectionSummary"; | ||
| import { useUsageFilter } from "@app/components/workspace/analytics/useUsageFilter"; | ||
| import { useSearchMembers } from "@app/lib/swr/memberships"; | ||
| import type { LightWorkspaceType } from "@app/types/user"; | ||
| import { | ||
| BarChart05, | ||
| Button, | ||
| NavigationListLabel, | ||
| PopoverContent, | ||
| PopoverRoot, | ||
| PopoverTrigger, | ||
| SearchInput, | ||
| } from "@dust-tt/sparkle"; | ||
| import { useMemo, useState } from "react"; | ||
|
|
||
| interface UsageFilterPanelProps { | ||
| owner: LightWorkspaceType; | ||
| categoryOptions: { | ||
| agent: UsageFilterAgentOption[]; | ||
| model: UsageFilterModelOption[]; | ||
| tool: UsageFilterToolOption[]; | ||
| skill: UsageFilterSkillOption[]; | ||
| source: UsageFilterSourceOption[]; | ||
| }; | ||
| groups: UsageFilterGroup[]; | ||
| filter: UsageFilter; | ||
| onFilterChange: (next: UsageFilter) => void; | ||
| } | ||
|
|
||
| export function UsageFilterPanel({ | ||
| owner, | ||
| categoryOptions, | ||
| groups, | ||
| filter, | ||
| onFilterChange, | ||
| }: UsageFilterPanelProps) { | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| // Selections are staged while the panel is open and only propagated | ||
| // when the user clicks Apply. | ||
| const { | ||
| draftFilter, | ||
| setDraftFilter, | ||
| clearAllCategories, | ||
| clearCategory, | ||
| toggleOption, | ||
| removeOption, | ||
| selectAllFiltered, | ||
| } = useUsageFilter(filter); | ||
| const [activeCategory, setActiveCategory] = | ||
| useState<UsageFilterCategory>("agent"); | ||
| const [activeScope, setActiveScope] = useState<UsageFilterScope>( | ||
| USAGE_FILTER_SCOPES[0] | ||
| ); | ||
| const [activeTier, setActiveTier] = useState<UsageModelTier>( | ||
| USAGE_MODEL_TIERS[0] | ||
| ); | ||
| const [searchText, setSearchText] = useState(""); | ||
|
|
||
| const { members } = useSearchMembers({ | ||
| workspaceId: owner.sId, | ||
| searchTerm: activeCategory === "member" ? searchText : "", | ||
| pageIndex: 0, | ||
| pageSize: 100, | ||
| disabled: !isOpen || activeCategory !== "member", | ||
| }); | ||
|
|
||
| const memberOptions = useMemo<UsageFilterMemberOption[]>( | ||
| () => | ||
| members.map((m) => ({ | ||
| id: m.sId, | ||
| name: m.fullName, | ||
| kind: "member", | ||
| image: m.image, | ||
| })), | ||
| [members] | ||
| ); | ||
|
|
||
| const resolvedCategoryOptions = useMemo<{ | ||
| [C in UsageFilterCategory]: UsageFilterOptionForCategory<C>[]; | ||
| }>( | ||
| () => ({ | ||
| ...categoryOptions, | ||
| member: memberOptions, | ||
| }), | ||
| [categoryOptions, memberOptions] | ||
| ); | ||
|
|
||
| const activeOptions = resolvedCategoryOptions[activeCategory]; | ||
| const filteredOptions = useMemo(() => { | ||
| const search = searchText.trim().toLowerCase(); | ||
| return activeOptions.filter((option) => { | ||
| if (option.kind === "agent" && option.scope !== activeScope) { | ||
| return false; | ||
| } | ||
| if (option.kind === "model" && option.tier !== activeTier) { | ||
| return false; | ||
| } | ||
| if (search && !option.name.toLowerCase().includes(search)) { | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
| }, [activeOptions, searchText, activeScope, activeTier]); | ||
|
|
||
| const selectedIdsForActiveCategory = useMemo( | ||
| () => | ||
| new Set((draftFilter[activeCategory] ?? []).map((option) => option.id)), | ||
| [draftFilter, activeCategory] | ||
| ); | ||
|
|
||
| const appliedSelectionCount = useMemo( | ||
| () => | ||
| USAGE_FILTER_CATEGORIES.reduce( | ||
| (count, category) => count + (filter[category]?.length ?? 0), | ||
| 0 | ||
| ), | ||
| [filter] | ||
| ); | ||
|
|
||
| const categoriesWithSelection = useMemo( | ||
| () => | ||
| USAGE_FILTER_CATEGORIES.filter( | ||
| (category) => (draftFilter[category]?.length ?? 0) > 0 | ||
| ), | ||
| [draftFilter] | ||
| ); | ||
|
|
||
| const handleOpenChange = (open: boolean) => { | ||
| setIsOpen(open); | ||
| if (open) { | ||
| setDraftFilter(filter); | ||
| setSearchText(""); | ||
| } | ||
| }; | ||
|
|
||
| const handleCategoryChange = (category: UsageFilterCategory) => { | ||
| setActiveCategory(category); | ||
| setSearchText(""); | ||
| }; | ||
|
|
||
| const handleCancel = () => { | ||
| setIsOpen(false); | ||
| }; | ||
|
|
||
| const handleApply = () => { | ||
| onFilterChange(draftFilter); | ||
| setIsOpen(false); | ||
| }; | ||
|
|
||
| const activeCategorySelectionCount = draftFilter[activeCategory]?.length ?? 0; | ||
|
|
||
| return ( | ||
| <PopoverRoot open={isOpen} onOpenChange={handleOpenChange}> | ||
| <PopoverTrigger asChild> | ||
| <Button | ||
| icon={BarChart05} | ||
| label="Filters" | ||
| size="sm" | ||
| variant="outline" | ||
| isCounter={appliedSelectionCount > 0} | ||
| counterValue={String(appliedSelectionCount)} | ||
| /> | ||
| </PopoverTrigger> | ||
| <PopoverContent fullWidth align="end" className="w-auto rounded-2xl p-0"> | ||
| <div className="flex h-96 flex-row divide-x divide-border"> | ||
| <UsageFilterCategoryNav | ||
| categories={USAGE_FILTER_CATEGORIES} | ||
| draftFilter={draftFilter} | ||
| activeCategory={activeCategory} | ||
| onCategoryChange={handleCategoryChange} | ||
| /> | ||
| <div className="flex h-full w-72 flex-col gap-2 p-2"> | ||
| <NavigationListLabel | ||
| label={USAGE_FILTER_CATEGORY_LABEL[activeCategory]} | ||
| className="bg-transparent pt-1.5 pb-0 font-medium" | ||
| action={ | ||
| <Button | ||
| label="Clear" | ||
| size="xmini" | ||
| variant="ghost-secondary" | ||
| onClick={() => clearCategory(activeCategory)} | ||
| disabled={activeCategorySelectionCount === 0} | ||
| className={ | ||
| activeCategorySelectionCount === 0 ? "invisible" : undefined | ||
| } | ||
| /> | ||
| } | ||
| /> | ||
| <SearchInput | ||
| name="usage-filter-search" | ||
| value={searchText} | ||
| onChange={setSearchText} | ||
| placeholder={`Search ${USAGE_FILTER_CATEGORY_LABEL[activeCategory].toLowerCase()}`} | ||
| /> | ||
| {activeCategory === "member" && ( | ||
| <UsageFilterMemberGroupsControls groups={groups} /> | ||
| )} | ||
| {activeCategory === "model" && ( | ||
| <UsageFilterModelComplexityControls | ||
| models={categoryOptions.model} | ||
| selectedModelIds={selectedIdsForActiveCategory} | ||
| onToggleModel={(model) => toggleOption("model", model)} | ||
| activeTier={activeTier} | ||
| onTierChange={setActiveTier} | ||
| /> | ||
| )} | ||
| {activeCategory === "agent" && ( | ||
| <UsageFilterAgentScopeControls | ||
| activeScope={activeScope} | ||
| onScopeChange={setActiveScope} | ||
| /> | ||
| )} | ||
| <UsageFilterOptionCheckboxList | ||
| category={activeCategory} | ||
| categoryLabel={USAGE_FILTER_CATEGORY_LABEL[activeCategory]} | ||
| options={filteredOptions} | ||
| selectedIds={selectedIdsForActiveCategory} | ||
| onToggleOption={(option) => toggleOption(activeCategory, option)} | ||
| onSelectAll={() => | ||
| selectAllFiltered(activeCategory, filteredOptions) | ||
| } | ||
| /> | ||
| </div> | ||
| <UsageFilterSelectionSummary | ||
| categoriesWithSelection={categoriesWithSelection} | ||
| draftFilter={draftFilter} | ||
| onClearCategory={clearCategory} | ||
| onRemoveOption={removeOption} | ||
| /> | ||
| </div> | ||
| <UsageFilterFooter | ||
| onClearAll={clearAllCategories} | ||
| onCancel={handleCancel} | ||
| onApply={handleApply} | ||
| /> | ||
| </PopoverContent> | ||
| </PopoverRoot> | ||
| ); | ||
| } |
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.