-
Notifications
You must be signed in to change notification settings - Fork 433
perf(desktop): paginate usage activity #4539
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| export { | ||
| shortUsageActivitySessionId, | ||
| UsageActivityPagination, | ||
| type UsageActivityPage, | ||
| } from './ui/usage-activity-pagination.js'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { Pagination, paginateData } from '@astryxdesign/core'; | ||
| import { HStack } from '@maka/ui'; | ||
| import { useState, type ReactNode } from 'react'; | ||
| import { parseDesktopSessionKey } from '../../../../shared/runtime-host-identity.js'; | ||
|
|
||
| export interface UsageActivityPage<Item> { | ||
| items: Item[]; | ||
| rowIndexStart: number; | ||
| rowCount: number; | ||
| reset(): void; | ||
| } | ||
|
|
||
| export function UsageActivityPagination<Item>(props: { | ||
| items: Item[]; | ||
| pageSize: number; | ||
| children(page: UsageActivityPage<Item>): ReactNode; | ||
| }) { | ||
| const [page, setPage] = useState(1); | ||
| const pageCount = Math.max(1, Math.ceil(props.items.length / props.pageSize)); | ||
| const currentPage = Math.min(page, pageCount); | ||
|
|
||
| return ( | ||
| <> | ||
| {props.children({ | ||
| items: paginateData(props.items, currentPage, props.pageSize), | ||
| rowIndexStart: (currentPage - 1) * props.pageSize + 1, | ||
| rowCount: props.items.length, | ||
| reset: () => setPage(1), | ||
| })} | ||
| {pageCount > 1 ? ( | ||
| <HStack hAlign="center"> | ||
| <Pagination | ||
| page={currentPage} | ||
| onChange={setPage} | ||
| totalItems={props.items.length} | ||
| pageSize={props.pageSize} | ||
| size="sm" | ||
| /> | ||
| </HStack> | ||
| ) : null} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export function shortUsageActivitySessionId(sessionKey: string) { | ||
|
Contributor
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. P3: this helper is about session ids, not pagination, and its only caller is still |
||
| try { | ||
| return shortenSessionId(parseDesktopSessionKey(sessionKey).sessionId); | ||
| } catch { | ||
| return shortenSessionId(sessionKey); | ||
| } | ||
| } | ||
|
|
||
| function shortenSessionId(sessionId: string) { | ||
| return sessionId.length > 8 ? sessionId.slice(0, 8) : sessionId; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,6 @@ import { | |
| proportional, | ||
| } from '@astryxdesign/core'; | ||
| import { uiLocaleToIntlLocale } from '@maka/core/ui-locale'; | ||
| import { parseDesktopSessionKey } from '../../shared/runtime-host-identity.js'; | ||
| import { | ||
| type AppSettings, | ||
| type UpdateAppSettingsResult, | ||
|
|
@@ -51,6 +50,11 @@ import { | |
| Banner, | ||
| } from '@maka/ui'; | ||
| import { ICON_SIZE, Activity, BarChart3, Cpu, Database, RefreshCcw, Search } from '@maka/ui/icons'; | ||
| import { | ||
| shortUsageActivitySessionId, | ||
| UsageActivityPagination, | ||
| type UsageActivityPage, | ||
| } from '../features/usage-activity'; | ||
| import { | ||
| getUsageSettingsCopy, | ||
| type UsageSettingsCopy, | ||
|
|
@@ -63,6 +67,8 @@ import { useOptimisticSettingsDraft } from './use-optimistic-settings-draft'; | |
|
|
||
| type UsageActiveTab = AppSettings['usage']['activeTab']; | ||
|
|
||
| const USAGE_REQUESTS_PAGE_SIZE = 50; | ||
|
|
||
| export function UsageSettingsPage(props: { | ||
| settings: AppSettings; | ||
| stats: UsageStats | null; | ||
|
|
@@ -303,13 +309,25 @@ function UsageRequestsPanel(props: { | |
| endContent={<Button variant="secondary" size="sm" onClick={props.onEnableDetails} label={props.copy.showDetails} />} /> | ||
| ); | ||
| } | ||
| return ( | ||
| <> | ||
| function renderPage({ | ||
| items, | ||
| rowIndexStart, | ||
| rowCount, | ||
| reset, | ||
| }: UsageActivityPage<UsageStats['logs'][number]>) { | ||
| const clearFilters = () => { | ||
| reset(); | ||
| props.onClearFilters(); | ||
| }; | ||
| return <> | ||
| <div className="settingsUsageFilters" role="group" aria-label={props.copy.filtersAria}> | ||
| <div className="settingsUsageModelFilter"> | ||
| <TextInput | ||
| value={props.modelFilter} | ||
| onChange={(value) => props.onModelFilterChange(value)} | ||
| onChange={(value) => { | ||
| reset(); | ||
| props.onModelFilterChange(value); | ||
| }} | ||
| placeholder={props.copy.filterPlaceholder} | ||
| label={props.copy.filterAria} | ||
| isLabelHidden | ||
|
|
@@ -327,7 +345,10 @@ function UsageRequestsPanel(props: { | |
| { value: 'aborted', label: props.copy.statuses[3] }, | ||
| ]} | ||
| width={320} | ||
| onChange={(value) => props.onStatusChange(value as AppSettings['usage']['status'])} | ||
| onChange={(value) => { | ||
| reset(); | ||
| props.onStatusChange(value as AppSettings['usage']['status']); | ||
| }} | ||
| /> | ||
| <div className="settingsUsageDetailToggle"> | ||
| <span>{props.copy.details}</span> | ||
|
|
@@ -346,12 +367,14 @@ function UsageRequestsPanel(props: { | |
| isDisabled={!props.hasRequestFilters} | ||
| aria-hidden={!props.hasRequestFilters ? 'true' : undefined} | ||
| tabIndex={!props.hasRequestFilters ? -1 : undefined} | ||
| onClick={props.hasRequestFilters ? props.onClearFilters : undefined} | ||
| onClick={props.hasRequestFilters ? clearFilters : undefined} | ||
| label={props.copy.clearFilters} | ||
| /> | ||
| </div> | ||
| <UsageStatsTable | ||
| ariaLabel={props.copy.tables.requestsAria} | ||
| rowIndexStart={rowIndexStart} | ||
| rowCount={rowCount} | ||
| columns={[ | ||
| { header: props.copy.tables.requestHeaders[0], width: 168 }, | ||
| { header: props.copy.tables.requestHeaders[1], width: 72 }, | ||
|
|
@@ -362,7 +385,7 @@ function UsageRequestsPanel(props: { | |
| { header: props.copy.tables.requestHeaders[6], numeric: true }, | ||
| { header: props.copy.tables.requestHeaders[7], width: 72 }, | ||
| ]} | ||
| rows={props.logs.map((row) => [ | ||
| rows={items.map((row) => [ | ||
| new Date(row.ts).toLocaleString(uiLocaleToIntlLocale(props.locale)), | ||
| usageRequestKindLabel(row.kind, props.copy), | ||
| usageRequestTarget(row), | ||
|
|
@@ -383,12 +406,17 @@ function UsageRequestsPanel(props: { | |
| variant="ghost" | ||
| size="sm" | ||
| label={props.copy.clearFilters} | ||
| onClick={props.onClearFilters} | ||
| onClick={clearFilters} | ||
| /> | ||
| ) : undefined, | ||
| }} | ||
| /> | ||
| </> | ||
| </>; | ||
| } | ||
| return ( | ||
| <UsageActivityPagination items={props.logs} pageSize={USAGE_REQUESTS_PAGE_SIZE}> | ||
|
Contributor
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. P2: changing the time range does not return the table to page 1. Smallest fix: |
||
| {renderPage} | ||
| </UsageActivityPagination> | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -501,19 +529,7 @@ function usageRequestSessionCell(row: UsageStats['logs'][number], copy: UsageSet | |
| function usageSessionDisplayLabel(row: UsageStats['logs'][number], copy: UsageSettingsCopy) { | ||
| const name = row.sessionName?.trim(); | ||
| if (name) return name; | ||
| return `${copy.tables.untitledSession} · ${shortRealSessionId(row.sessionId ?? '')}`; | ||
| } | ||
|
|
||
| function shortRealSessionId(sessionKey: string) { | ||
| try { | ||
| return shortUsageSessionId(parseDesktopSessionKey(sessionKey).sessionId); | ||
| } catch { | ||
| return shortUsageSessionId(sessionKey); | ||
| } | ||
| } | ||
|
|
||
| function shortUsageSessionId(sessionId: string) { | ||
| return sessionId.length > 8 ? sessionId.slice(0, 8) : sessionId; | ||
| return `${copy.tables.untitledSession} · ${shortUsageActivitySessionId(row.sessionId ?? '')}`; | ||
| } | ||
|
|
||
| function usageRequestStatusLabel(status: UsageStats['logs'][number]['status'], copy: UsageSettingsCopy) { | ||
|
|
@@ -574,6 +590,8 @@ interface UsageEmpty { | |
|
|
||
| function UsageStatsTable(props: { | ||
| ariaLabel: string; | ||
| rowIndexStart?: number; | ||
| rowCount?: number; | ||
| columns: UsageColumn[]; | ||
| rows: Array<Array<ReactNode>>; | ||
| empty: UsageEmpty; | ||
|
|
@@ -613,6 +631,8 @@ function UsageStatsTable(props: { | |
| <Card className="settingsUsageTable" padding={3}> | ||
| <Table | ||
| aria-label={props.ariaLabel} | ||
| rowIndexStart={props.rowIndexStart} | ||
| rowCount={props.rowCount} | ||
| data={data} | ||
| columns={columns} | ||
| idKey="id" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3:
@astryxdesign/core@0.5.2already ships the seam this reimplements.useTablePaginationis the Table plugin for exactly this shape (consumer owns the page state, the plugin rendersPaginationaround the table, withposition,align,sizeandlabel). The PR usespaginateDatafrom the same module but routes around the plugin with a render-prop component, a new feature directory and a barrel.The smaller equivalent: hold
pageinUsageRequestsPanel(above theshowDetailsearly return), callpaginateDatadirectly, and pass the plugin throughUsageStatsTable. That deletesfeatures/usage-activity/entirely, theUsageActivityPagetype, and therenderer-architecture.jsonplusastryx-surface-file-inventorychurn, and turns the reset into a plainsetPage(1). I have not rendered the plugin's chrome to confirm it matches the currentHStack+Paginationlayout; if it does not, the fallback is still to move the state intoUsageRequestsPaneland drop the wrapper while keeping the hand-placedPagination.