Skip to content
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

[Discover] Display Cache Time and Clear Cache Button #8214

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/8214.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Add last updated time and cache refresh button to Discover Advanced Dataset Selector ([#8214](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8214))
4 changes: 4 additions & 0 deletions src/plugins/data/common/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
if (ourKey != null) ours.push(ourKey);
});
}

clear(): void {
this.engine.clear();

Check warning on line 66 in src/plugins/data/common/storage/storage.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/storage/storage.ts#L66

Added line #L66 was not covered by tests
}
}

export function createStorage(deps: { engine: IStorageEngine; prefix: string }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ describe('DatasetService', () => {
service = new DatasetService(uiSettings, sessionStorage);
});

test('registerType and getType', () => {
const mockType = {
id: 'test-type',
title: 'Test Type',
meta: { icon: { type: 'test' } },
toDataset: jest.fn(),
fetch: jest.fn(),
fetchFields: jest.fn(),
supportedLanguages: jest.fn(),
};
const mockResult = {
id: 'test-structure',
title: 'Test Structure',
type: 'test-type',
children: [{ id: 'child1', title: 'Child 1', type: 'test-type' }],
};

const mockPath: DataStructure[] = [{ id: 'root', title: 'Root', type: 'root' }];

const mockType = {
id: 'test-type',
title: 'Test Type',
meta: { icon: { type: 'test' } },
toDataset: jest.fn(),
fetch: jest.fn().mockResolvedValue(mockResult),
fetchFields: jest.fn(),
supportedLanguages: jest.fn(),
};

test('registerType and getType', () => {
service.registerType(mockType);
expect(service.getType('test-type')).toBe(mockType);
});
Expand All @@ -52,25 +61,9 @@ describe('DatasetService', () => {
});

test('fetchOptions caches and returns data structures', async () => {
const mockType = {
id: 'test-type',
title: 'Test Type',
meta: { icon: { type: 'test' } },
toDataset: jest.fn(),
fetch: jest.fn().mockResolvedValue({
id: 'test-structure',
title: 'Test Structure',
type: 'test-type',
children: [{ id: 'child1', title: 'Child 1', type: 'test-type' }],
}),
fetchFields: jest.fn(),
supportedLanguages: jest.fn(),
};

service.registerType(mockType);

const path: DataStructure[] = [{ id: 'root', title: 'Root', type: 'root' }];
const result = await service.fetchOptions(mockDataPluginServices, path, 'test-type');
const result = await service.fetchOptions(mockDataPluginServices, mockPath, 'test-type');

expect(result).toEqual({
id: 'test-structure',
Expand All @@ -79,8 +72,30 @@ describe('DatasetService', () => {
children: [{ id: 'child1', title: 'Child 1', type: 'test-type' }],
});

const cachedResult = await service.fetchOptions(mockDataPluginServices, path, 'test-type');
const cachedResult = await service.fetchOptions(mockDataPluginServices, mockPath, 'test-type');
expect(cachedResult).toEqual(result);
expect(mockType.fetch).toHaveBeenCalledTimes(2);
});

test('clear cache', async () => {
service.registerType(mockType);

await service.fetchOptions(mockDataPluginServices, mockPath, 'test-type');
expect(sessionStorage.keys().length === 1);

service.clearCache();
expect(sessionStorage.keys().length === 0);
});

test('caching object correctly sets last cache time', async () => {
service.registerType(mockType);

const time = Date.now();

Date.now = jest.fn(() => time);

await service.fetchOptions(mockDataPluginServices, mockPath, 'test-type');

expect(service.getLastCacheTime()).toEqual(time);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
}

private cacheDataStructure(dataType: string, dataStructure: DataStructure) {
this.setLastCacheTime(Date.now());

Check warning on line 141 in src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts#L141

Added line #L141 was not covered by tests
const cachedDataStructure: CachedDataStructure = {
id: dataStructure.id,
title: dataStructure.title,
Expand All @@ -164,6 +165,18 @@
});
}

public clearCache(): void {
this.sessionStorage.clear();

Check warning on line 169 in src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts#L169

Added line #L169 was not covered by tests
}

public getLastCacheTime(): number | undefined {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit i think there is precedent with updatedAt

return Number(this.sessionStorage.get('lastCacheTime')) || undefined;
}

private setLastCacheTime(time: number): void {
this.sessionStorage.set('lastCacheTime', time);

Check warning on line 177 in src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts#L177

Added line #L177 was not covered by tests
}

private async fetchDefaultDataset(): Promise<Dataset | undefined> {
const defaultIndexPatternId = this.uiSettings.get('defaultIndex');
if (!defaultIndexPatternId) {
Expand Down
90 changes: 67 additions & 23 deletions src/plugins/data/public/ui/dataset_selector/dataset_explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import React, { useState } from 'react';
import {
EuiButton,
EuiButtonEmpty,
EuiFlexGroup,
EuiFlexItem,
EuiIcon,
EuiLink,
EuiModalBody,
Expand All @@ -19,6 +21,7 @@ import {
EuiToolTip,
} from '@elastic/eui';
import { FormattedMessage } from '@osd/i18n/react';
import moment from 'moment';
import { BaseDataset, DATA_STRUCTURE_META_TYPES, DataStructure } from '../../../common';
import { QueryStringContract } from '../../query';
import { IDataPluginServices } from '../../types';
Expand All @@ -38,6 +41,7 @@ export const DatasetExplorer = ({
onNext: (dataset: BaseDataset) => void;
onCancel: () => void;
}) => {
const uiSettings = services.uiSettings;
const [explorerDataset, setExplorerDataset] = useState<BaseDataset | undefined>(undefined);
const [loading, setLoading] = useState<boolean>(false);

Expand Down Expand Up @@ -68,31 +72,71 @@ export const DatasetExplorer = ({
return (
<>
<EuiModalHeader>
<EuiModalHeaderTitle>
<h1>
<FormattedMessage
id="data.explorer.datasetSelector.advancedSelector.title.step1"
defaultMessage="Step 1: Select data"
/>
</h1>
<EuiText>
<p>
<FormattedMessage
id="data.explorer.datasetSelector.advancedSelector.description"
defaultMessage="Select from those available to you. "
/>
<EuiLink
href={`${services.http.basePath.get()}/app/management/opensearch-dashboards/dataSources`}
target="_blank"
>
<EuiFlexGroup alignItems="center" justifyContent="flexEnd" gutterSize="s">
<EuiFlexItem grow={true}>
<EuiModalHeaderTitle>
<h1>
<FormattedMessage
id="data.explorer.datasetSelector.advancedSelector.dataSourceManagement.title"
defaultMessage="Manage data sources"
id="data.explorer.datasetSelector.advancedSelector.title.step1"
defaultMessage="Step 1: Select data"
/>
</EuiLink>
</p>
</EuiText>
</EuiModalHeaderTitle>
</h1>
<EuiText>
<p>
<FormattedMessage
id="data.explorer.datasetSelector.advancedSelector.description"
defaultMessage="Select from those available to you. "
/>
<EuiLink
href={`${services.http.basePath.get()}/app/management/opensearch-dashboards/dataSources`}
target="_blank"
>
<FormattedMessage
id="data.explorer.datasetSelector.advancedSelector.dataSourceManagement.title"
defaultMessage="Manage data sources"
/>
</EuiLink>
</p>
</EuiText>
</EuiModalHeaderTitle>
</EuiFlexItem>
{queryString.getDatasetService().getLastCacheTime() && (
<EuiFlexItem grow={false}>
<EuiFlexGroup gutterSize="xs">
<EuiFlexItem grow={false}>
<EuiText size="s">
<FormattedMessage
id="data.explorer.datasetSelector.advancedSelector.lastUpdatedTime"
defaultMessage={`Last updated at: ${moment(
queryString.getDatasetService().getLastCacheTime()
)
.format(uiSettings.get('dateFormat'))
.toString()}. `}
/>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButtonEmpty
onClick={() => {
queryString.getDatasetService().clearCache();
onCancel();
}}
size="xs"
iconSide="left"
iconType="refresh"
iconGap="s"
flush="both"
>
<FormattedMessage
id="data.explorer.datasetSelector.advancedSelector.refreshCacheButton"
defaultMessage="Refresh Cache"
/>
</EuiButtonEmpty>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
)}
</EuiFlexGroup>
</EuiModalHeader>
<EuiModalBody>
<div
Expand Down
Loading