Skip to content

Commit 844abae

Browse files
Change: [DI-26395] - Update filters to use the resources from useQuery cache in CloudPulse metrics (linode#12678)
* DI-26395 : Initial changes for reduction in instances call * DI-26395 : Cypress changes * DI-26395 : Cypress changes * DI-26395 : Code refactoring changes * DI-26395 : Add changetset * DI-26395 : No xFilter needed for firewall service * DI-26395 : Code refactoring and updates
1 parent 3ec53b7 commit 844abae

22 files changed

+491
-233
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@linode/manager": Changed
3+
---
4+
5+
Update logic in metrics filters to use the resources from `useResources` useQuery cache in CloudPulse metrics ([#12678](https://github.com/linode/manager/pull/12678))

packages/manager/cypress/e2e/core/cloudpulse/cloudpulse-dashboard-errors.spec.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ describe('Tests for API error handling', () => {
389389
// Wait for the API calls .
390390
cy.wait(['@fetchServices', '@fetchDashboard']);
391391

392+
// simulate an error on instances call before changing the region again
393+
mockGetDatabasesError('Internal Server Error').as(
394+
'getDatabaseInstancesError'
395+
);
396+
392397
// Select a dashboard from the autocomplete input
393398
ui.autocomplete
394399
.findByLabel('Dashboard')
@@ -400,33 +405,6 @@ describe('Tests for API error handling', () => {
400405
.should('be.visible')
401406
.click();
402407

403-
// Select a Database Engine from the autocomplete input.
404-
ui.autocomplete
405-
.findByLabel('Database Engine')
406-
.should('be.visible')
407-
.type(engine);
408-
409-
ui.autocompletePopper.findByTitle(engine).should('be.visible').click();
410-
411-
// Select a region from the dropdown.
412-
ui.regionSelect.find().click();
413-
ui.regionSelect
414-
.findItemByRegionId(mockRegions[0].id, mockRegions)
415-
.should('be.visible')
416-
.click();
417-
418-
// simulate an error on instances call before changing the region again
419-
mockGetDatabasesError('Internal Server Error').as(
420-
'getDatabaseInstancesError'
421-
);
422-
423-
// Select a region from the dropdown.
424-
ui.regionSelect.find().click();
425-
ui.regionSelect
426-
.findItemByRegionId(mockRegions[1].id, mockRegions)
427-
.should('be.visible')
428-
.click();
429-
430408
// Wait for the intercepted request to complete
431409
cy.wait('@getDatabaseInstancesError');
432410

packages/manager/src/components/GenerateFirewallDialog/useCreateFirewallFromTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const createFirewallFromTemplate = async (options: {
6565
// Get firewalls and firewall template in parallel
6666
const [{ rules, slug }, firewalls] = await Promise.all([
6767
queryClient.ensureQueryData(firewallQueries.template(templateSlug)),
68-
queryClient.fetchQuery(firewallQueries.firewalls._ctx.all), // must fetch fresh data if generating more than one firewall
68+
queryClient.fetchQuery(firewallQueries.firewalls._ctx.all()), // must fetch fresh data if generating more than one firewall
6969
]);
7070

7171
if (updateProgress) {

packages/manager/src/features/CloudPulse/Alerts/AlertRegions/AlertRegions.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React from 'react';
66
import { DebouncedSearchTextField } from 'src/components/DebouncedSearchTextField';
77
import { useResourcesQuery } from 'src/queries/cloudpulse/resources';
88

9+
import { RESOURCE_FILTER_MAP } from '../../Utils/constants';
910
import {
1011
type AlertFormMode,
1112
REGION_GROUP_INFO_MESSAGE,
@@ -17,7 +18,7 @@ import { getFilteredRegions } from '../Utils/utils';
1718
import { DisplayAlertRegions } from './DisplayAlertRegions';
1819

1920
import type { AlertRegion } from './DisplayAlertRegions';
20-
import type { CloudPulseServiceType, Filter } from '@linode/api-v4';
21+
import type { CloudPulseServiceType } from '@linode/api-v4';
2122

2223
interface AlertRegionsProps {
2324
/**
@@ -48,19 +49,11 @@ export const AlertRegions = React.memo((props: AlertRegionsProps) => {
4849
const { data: regions, isLoading: isRegionsLoading } = useRegionsQuery();
4950
const [selectedRegions, setSelectedRegions] = React.useState<string[]>(value);
5051
const [showSelected, setShowSelected] = React.useState<boolean>(false);
51-
52-
const resourceFilterMap: Record<string, Filter> = {
53-
dbaas: {
54-
platform: 'rdbms-default',
55-
},
56-
};
5752
const { data: resources, isLoading: isResourcesLoading } = useResourcesQuery(
5853
Boolean(serviceType && regions?.length),
5954
serviceType === null ? undefined : serviceType,
6055
{},
61-
{
62-
...(resourceFilterMap[serviceType ?? ''] ?? {}),
63-
}
56+
{ ...(RESOURCE_FILTER_MAP[serviceType ?? ''] ?? {}) }
6457
);
6558

6659
const handleSelectionChange = React.useCallback(

packages/manager/src/features/CloudPulse/Alerts/AlertsResources/AlertsResources.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export const AlertResources = React.memo((props: AlertResourcesProp) => {
132132

133133
const supportedRegionIds = getSupportedRegionIds(regions, serviceType);
134134
const xFilterToBeApplied: Filter | undefined = React.useMemo(() => {
135+
if (serviceType === 'firewall') {
136+
return undefined;
137+
}
138+
135139
const regionFilter: Filter = supportedRegionIds
136140
? {
137141
'+or': supportedRegionIds.map((regionId) => ({

packages/manager/src/features/CloudPulse/Dashboard/CloudPulseDashboard.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
useGetCloudPulseMetricDefinitionsByServiceType,
1010
} from 'src/queries/cloudpulse/services';
1111

12+
import { RESOURCE_FILTER_MAP } from '../Utils/constants';
1213
import { useAclpPreference } from '../Utils/UserPreference';
1314
import {
1415
renderPlaceHolder,
@@ -92,7 +93,7 @@ export const CloudPulseDashboard = (props: DashboardProperties) => {
9293
Boolean(dashboard?.service_type),
9394
dashboard?.service_type,
9495
{},
95-
dashboard?.service_type === 'dbaas' ? { platform: 'rdbms-default' } : {}
96+
RESOURCE_FILTER_MAP[dashboard?.service_type ?? ''] ?? {}
9697
);
9798

9899
const {
@@ -131,7 +132,13 @@ export const CloudPulseDashboard = (props: DashboardProperties) => {
131132
}
132133

133134
if (isMetricDefinitionLoading || isDashboardLoading || isResourcesLoading) {
134-
return <CircleProgress />;
135+
return (
136+
<CircleProgress
137+
sx={(theme) => ({
138+
padding: theme.spacingFunction(16),
139+
})}
140+
/>
141+
);
135142
}
136143

137144
if (!dashboard) {

packages/manager/src/features/CloudPulse/Dashboard/CloudPulseDashboardLanding.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ export interface FilterData {
1818
id: { [filterKey: string]: FilterValueType };
1919
label: { [filterKey: string]: string[] };
2020
}
21+
export interface CloudPulseMetricsFilter {
22+
[key: string]: FilterValueType;
23+
}
2124
export interface DashboardProp {
2225
dashboard?: Dashboard;
23-
filterValue: {
24-
[key: string]: FilterValueType;
25-
};
26+
filterValue: CloudPulseMetricsFilter;
2627
timeDuration?: DateTimeWithPreset;
2728
}
2829

packages/manager/src/features/CloudPulse/Overview/GlobalFilters.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { screen } from '@testing-library/react';
22
import React from 'react';
33

4+
import { databaseInstanceFactory } from 'src/factories';
45
import { renderWithTheme } from 'src/utilities/testHelpers';
56

67
import { GlobalFilters } from './GlobalFilters';
@@ -19,6 +20,19 @@ const setup = () => {
1920
/>
2021
);
2122
};
23+
24+
const queryMocks = vi.hoisted(() => ({
25+
useResourcesQuery: vi.fn().mockReturnValue({}),
26+
}));
27+
28+
vi.mock('src/queries/cloudpulse/resources', async () => {
29+
const actual = await vi.importActual('src/queries/cloudpulse/resources');
30+
return {
31+
...actual,
32+
useResourcesQuery: queryMocks.useResourcesQuery,
33+
};
34+
});
35+
2236
describe('Global filters component test', () => {
2337
it('Should render refresh button', () => {
2438
setup();
@@ -40,4 +54,16 @@ describe('Global filters component test', () => {
4054

4155
expect(timeRangeSelect).toBeInTheDocument();
4256
});
57+
58+
it('Should show circle progress if resources call is loading', async () => {
59+
queryMocks.useResourcesQuery.mockReturnValue({
60+
data: [{ ...databaseInstanceFactory.build(), clusterSize: 1 }],
61+
isLoading: true,
62+
});
63+
64+
setup();
65+
66+
const progress = await screen.findByTestId('circle-progress');
67+
expect(progress).toBeInTheDocument();
68+
});
4369
});

packages/manager/src/features/CloudPulse/Overview/GlobalFilters.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import { GridLegacy } from '@mui/material';
44
import * as React from 'react';
55

66
import Reload from 'src/assets/icons/refresh.svg';
7+
import { useResourcesQuery } from 'src/queries/cloudpulse/resources';
78

89
import { CloudPulseDashboardFilterBuilder } from '../shared/CloudPulseDashboardFilterBuilder';
910
import { CloudPulseDashboardSelect } from '../shared/CloudPulseDashboardSelect';
1011
import { CloudPulseDateTimeRangePicker } from '../shared/CloudPulseDateTimeRangePicker';
1112
import { CloudPulseTooltip } from '../shared/CloudPulseTooltip';
1213
import { convertToGmt } from '../Utils/CloudPulseDateTimePickerUtils';
13-
import { DASHBOARD_ID, REFRESH, TIME_DURATION } from '../Utils/constants';
14+
import {
15+
DASHBOARD_ID,
16+
REFRESH,
17+
RESOURCE_FILTER_MAP,
18+
TIME_DURATION,
19+
} from '../Utils/constants';
1420
import { useAclpPreference } from '../Utils/UserPreference';
1521

1622
import type { FilterValueType } from '../Dashboard/CloudPulseDashboardLanding';
@@ -88,6 +94,14 @@ export const GlobalFilters = React.memo((props: GlobalFilterProperties) => {
8894
handleAnyFilterChange(REFRESH, Date.now(), []);
8995
}, []);
9096

97+
const { isLoading, isError } = useResourcesQuery(
98+
selectedDashboard !== undefined,
99+
selectedDashboard?.service_type ?? '',
100+
{},
101+
102+
RESOURCE_FILTER_MAP[selectedDashboard?.service_type ?? ''] ?? {}
103+
);
104+
91105
return (
92106
<GridLegacy container>
93107
<GridLegacy item xs={12}>
@@ -150,6 +164,8 @@ export const GlobalFilters = React.memo((props: GlobalFilterProperties) => {
150164
dashboard={selectedDashboard}
151165
emitFilterChange={emitFilterChange}
152166
handleToggleAppliedFilter={handleToggleAppliedFilter}
167+
isError={isError}
168+
isLoading={isLoading}
153169
isServiceAnalyticsIntegration={false}
154170
preferences={preferences}
155171
/>

0 commit comments

Comments
 (0)