Skip to content

Commit dbf0f61

Browse files
jescaladavinayan3
authored andcommitted
Add "Load More" button for Metrics table in Runs Overview tab (#110)
* Add index/count based fetching for useRunMetricsBatch * Add quick fix for alignment config error * Add preliminary infinite scrolling properties * Add Load More button to Card * Fix and clean up useRunMetricsBatch hook * Type annotations and cleanup * Clean up old changes * Fix styling and add dynamic metric count to button * Fix broken test date check
1 parent 1cce6fa commit dbf0f61

File tree

7 files changed

+79
-16
lines changed

7 files changed

+79
-16
lines changed

src/e2e/Dashboard/DashboardContent.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,14 @@ test.describe('Dashboard', () => {
120120
const month = date.getUTCMonth() + 1; // getUTCMonth() is zero-based
121121
const day = date.getUTCDate();
122122

123-
const expectedText = `datetime(${year},${month},${day})<=run.created_at<datetime(${year},${month},${
124-
day + 1
125-
})`;
123+
const nextDayDate = new Date(date);
124+
nextDayDate.setUTCDate(day + 1);
125+
126+
const nextYear = nextDayDate.getUTCFullYear();
127+
const nextMonth = nextDayDate.getUTCMonth() + 1; // getUTCMonth() is zero-based
128+
const nextDay = nextDayDate.getUTCDate();
129+
130+
const expectedText = `datetime(${year},${month},${day})<=run.created_at<datetime(${nextYear},${nextMonth},${nextDay})`;
126131

127132
// Locate and click the current day cell in the heatmap
128133
const heatmap = await page.$('.CalendarHeatmap');

src/src/components/kit/Card/Card.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ export interface ICardProps {
1616
withSearchBar?: boolean;
1717
calcTableHeight?: boolean;
1818
};
19+
loadMore?: boolean;
20+
loadMoreHandler?: () => void;
21+
loadMoreText?: string;
1922
}

src/src/components/kit/Card/Card.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
margin-top: $space-xxxs;
1717
margin-bottom: $space-xxxs;
1818
}
19+
&__loadMoreButton {
20+
margin-top: $space-md;
21+
margin-bottom: $space-sm * -1;
22+
margin-left: 0;
23+
}
1924
}
2025
&__tableWrapper {
2126
height: toRem(356px);

src/src/components/kit/Card/Card.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import classNames from 'classnames';
33

4-
import { Text } from 'components/kit';
4+
import { Button, Text } from 'components/kit';
55
import DataList from 'components/kit/DataList';
66

77
import { ICardProps } from './Card.d';
@@ -29,6 +29,9 @@ function Card({
2929
className,
3030
dataListProps,
3131
children,
32+
loadMore,
33+
loadMoreHandler,
34+
loadMoreText,
3235
}: ICardProps): React.FunctionComponentElement<React.ReactNode> {
3336
const tableRef = React.useRef<any>(null);
3437

@@ -48,6 +51,18 @@ function Card({
4851
{subtitle}
4952
</Text>
5053
)}
54+
{loadMore && loadMoreHandler && (
55+
<span>
56+
<Button
57+
onClick={loadMoreHandler}
58+
className='Card__header__loadMoreButton'
59+
color='primary'
60+
variant='contained'
61+
>
62+
{loadMoreText || 'Load More'}
63+
</Button>
64+
</span>
65+
)}
5166
</div>
5267
{children || (
5368
<div className='Card__tableWrapper'>

src/src/pages/RunDetail/RunOverviewTab/RunOverviewTab.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ function RunOverviewTab({ runData, runHash }: IRunOverviewTabProps) {
2727
const overviewSectionRef = React.useRef<HTMLElement | any>(null);
2828
const overviewSectionContentRef = React.useRef<HTMLElement | any>(null);
2929
const [containerHeight, setContainerHeight] = React.useState<number>(0);
30+
const METRICS_FETCH_COUNT = 500;
31+
32+
const [startIndex, setStartIndex] = React.useState(0);
3033

3134
useRunMetricsBatch({
3235
runTraces: runData.runTraces,
3336
runHash,
37+
startIndex,
38+
count: METRICS_FETCH_COUNT,
3439
});
3540

3641
React.useEffect(() => {
@@ -75,6 +80,12 @@ function RunOverviewTab({ runData, runHash }: IRunOverviewTabProps) {
7580
sidebarRef?.current?.scrollTo(0, e.target.scrollTop);
7681
}
7782

83+
function fetchMoreMetrics() {
84+
if (runData.runMetricsBatch.length > startIndex + 1) {
85+
setStartIndex((prevIndex) => prevIndex + METRICS_FETCH_COUNT);
86+
}
87+
}
88+
7889
return (
7990
<ErrorBoundary>
8091
<section
@@ -105,6 +116,8 @@ function RunOverviewTab({ runData, runHash }: IRunOverviewTabProps) {
105116
isLoading={runData?.isRunBatchLoading}
106117
type='metric'
107118
runBatch={cardsData?.runMetricsBatch}
119+
totalMetrics={runData?.runTraces?.metric?.length}
120+
loadMoreHandler={fetchMoreMetrics}
108121
/>
109122
</ErrorBoundary>
110123
)}

src/src/pages/RunDetail/RunOverviewTab/components/MetricsCard/RunOverviewTabMetricsCard.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ function RunOverviewTabMetricsCard({
1616
isLoading,
1717
runBatch,
1818
type,
19+
totalMetrics = 0,
20+
loadMoreHandler,
1921
}: {
2022
isLoading: boolean;
2123
runBatch: any;
2224
type: 'metric' | 'systemMetric';
25+
totalMetrics?: number;
26+
loadMoreHandler?: () => void;
2327
}) {
2428
const tableData = React.useMemo(() => {
2529
if (runBatch) {
@@ -49,7 +53,7 @@ function RunOverviewTabMetricsCard({
4953
tint={50}
5054
className='RunOverviewTab__cardBox__tableTitleCount'
5155
>
52-
({runBatch?.length})
56+
({totalMetrics})
5357
</Text>
5458
</Text>
5559
),
@@ -92,7 +96,7 @@ function RunOverviewTabMetricsCard({
9296
),
9397
},
9498
],
95-
[runBatch],
99+
[totalMetrics],
96100
);
97101

98102
return (
@@ -116,6 +120,9 @@ function RunOverviewTabMetricsCard({
116120
title: 'No Results',
117121
},
118122
}}
123+
loadMore={loadMoreHandler && totalMetrics > runBatch?.length}
124+
loadMoreHandler={loadMoreHandler}
125+
loadMoreText={`Load More (${runBatch?.length}/${totalMetrics})`}
119126
/>
120127
</ErrorBoundary>
121128
);

src/src/pages/RunDetail/hooks/useRunMetricsBatch.tsx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,35 @@ import React from 'react';
22

33
import runDetailAppModel from 'services/models/runs/runDetailAppModel';
44

5-
function useRunMetricsBatch({ runTraces, runHash }: any) {
5+
function useRunMetricsBatch({
6+
runTraces,
7+
runHash,
8+
startIndex = 0,
9+
count = 500, // Error occurs if count is greater than 1000
10+
}: {
11+
runTraces: any;
12+
runHash: string;
13+
startIndex?: number;
14+
count?: number;
15+
}) {
616
React.useEffect(() => {
7-
const runsBatchRequestRef = runDetailAppModel.getRunMetricsBatch(
8-
runTraces.metric,
9-
runHash,
10-
);
17+
const fetchMetricsBatch = () => {
18+
const runsBatchRequestRef = runDetailAppModel.getRunMetricsBatch(
19+
startIndex !== undefined && count
20+
? runTraces.metric.slice(startIndex, startIndex + count)
21+
: runTraces.metric,
22+
runHash,
23+
);
1124

12-
runsBatchRequestRef.call();
25+
runsBatchRequestRef.call();
1326

14-
return () => {
15-
runsBatchRequestRef.abort();
27+
return () => {
28+
runsBatchRequestRef.abort();
29+
};
1630
};
17-
// eslint-disable-next-line react-hooks/exhaustive-deps
18-
}, [runTraces, runHash]);
31+
32+
fetchMetricsBatch();
33+
}, [runTraces, runHash, startIndex, count]);
1934
}
2035

2136
export default useRunMetricsBatch;

0 commit comments

Comments
 (0)