Query History: Remove migration (#67470)

This commit is contained in:
Giordano Ricci
2023-04-28 16:03:51 +01:00
committed by GitHub
parent 91471ac7ae
commit b5a2c3c7f5
19 changed files with 3 additions and 677 deletions
@@ -61,7 +61,6 @@ function setup(queries: DataQuery[]) {
right: undefined,
richHistoryStorageFull: false,
richHistoryLimitExceededWarningShown: false,
richHistoryMigrationFailed: false,
};
const store = configureStore({ explore: initialState, user: { orgId: 1 } as UserState });
@@ -22,8 +22,6 @@ import { lastUsedDatasourceKeyForOrgId } from 'app/core/utils/explore';
import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource';
import { configureStore } from 'app/store/configureStore';
import { RICH_HISTORY_KEY, RichHistoryLocalStorageDTO } from '../../../../core/history/RichHistoryLocalStorage';
import { RICH_HISTORY_SETTING_KEYS } from '../../../../core/history/richHistoryLocalStorageUtils';
import { LokiDatasource } from '../../../../plugins/datasource/loki/datasource';
import { LokiQuery } from '../../../../plugins/datasource/loki/types';
import { ExploreId } from '../../../../types';
@@ -196,18 +194,3 @@ export const withinExplore = (exploreId: ExploreId) => {
const container = screen.getAllByTestId('data-testid Explore');
return within(container[exploreId === ExploreId.left ? 0 : 1]);
};
export const localStorageHasAlreadyBeenMigrated = () => {
window.localStorage.setItem(RICH_HISTORY_SETTING_KEYS.migrated, 'true');
};
export const setupLocalStorageRichHistory = (dsName: string) => {
const richHistoryDTO: RichHistoryLocalStorageDTO = {
ts: Date.now(),
datasourceName: dsName,
starred: true,
comment: '',
queries: [{ refId: 'A' }],
};
window.localStorage.setItem(RICH_HISTORY_KEY, JSON.stringify([richHistoryDTO]));
};
@@ -31,13 +31,7 @@ import {
switchToQueryHistoryTab,
} from './helper/interactions';
import { makeLogsQueryResponse } from './helper/query';
import {
localStorageHasAlreadyBeenMigrated,
setupExplore,
setupLocalStorageRichHistory,
tearDown,
waitForExplore,
} from './helper/setup';
import { setupExplore, tearDown, waitForExplore } from './helper/setup';
const fetchMock = jest.fn();
const postMock = jest.fn();
@@ -225,56 +219,8 @@ describe('Explore: Query History', () => {
assertDataSourceFilterVisibility(false);
});
describe('local storage migration', () => {
it('does not migrate if query history is not enabled', async () => {
config.queryHistoryEnabled = false;
const { datasources } = setupExplore();
setupLocalStorageRichHistory('loki');
(datasources.loki.query as jest.Mock).mockReturnValueOnce(makeLogsQueryResponse());
getMock.mockReturnValue({ result: { queryHistory: [] } });
await waitForExplore();
await openQueryHistory();
expect(postMock).not.toBeCalledWith('/api/query-history/migrate', { queries: [] });
expect(reportInteractionMock).toBeCalledWith('grafana_explore_query_history_opened', {
queryHistoryEnabled: false,
});
});
it('migrates query history from local storage', async () => {
config.queryHistoryEnabled = true;
const { datasources } = setupExplore();
setupLocalStorageRichHistory('loki');
(datasources.loki.query as jest.Mock).mockReturnValueOnce(makeLogsQueryResponse());
fetchMock.mockReturnValue(of({ data: { result: { queryHistory: [] } } }));
await waitForExplore();
await openQueryHistory();
expect(fetchMock).toBeCalledWith(
expect.objectContaining({
url: expect.stringMatching('/api/query-history/migrate'),
data: { queries: [expect.objectContaining({ datasourceUid: 'loki-uid' })] },
})
);
fetchMock.mockReset();
fetchMock.mockReturnValue(of({ data: { result: { queryHistory: [] } } }));
await closeQueryHistory();
await openQueryHistory();
expect(fetchMock).not.toBeCalledWith(
expect.objectContaining({
url: expect.stringMatching('/api/query-history/migrate'),
})
);
expect(reportInteractionMock).toBeCalledWith('grafana_explore_query_history_opened', {
queryHistoryEnabled: true,
});
});
});
it('pagination', async () => {
config.queryHistoryEnabled = true;
localStorageHasAlreadyBeenMigrated();
const { datasources } = setupExplore();
(datasources.loki.query as jest.Mock).mockReturnValueOnce(makeLogsQueryResponse());
fetchMock.mockReturnValue(
@@ -1,18 +1,13 @@
import { AnyAction, createAction } from '@reduxjs/toolkit';
import { HistoryItem } from '@grafana/data';
import { config, logError } from '@grafana/runtime';
import { DataQuery } from '@grafana/schema';
import { RICH_HISTORY_SETTING_KEYS } from 'app/core/history/richHistoryLocalStorageUtils';
import store from 'app/core/store';
import {
addToRichHistory,
deleteAllFromRichHistory,
deleteQueryInRichHistory,
getRichHistory,
getRichHistorySettings,
LocalStorageMigrationStatus,
migrateQueryHistoryFromLocalStorage,
updateCommentInRichHistory,
updateRichHistorySettings,
updateStarredInRichHistory,
@@ -24,7 +19,6 @@ import { RichHistorySearchFilters, RichHistorySettings } from '../../../core/uti
import {
richHistoryLimitExceededAction,
richHistoryMigrationFailedAction,
richHistorySearchFiltersUpdatedAction,
richHistorySettingsUpdatedAction,
richHistoryStorageFullAction,
@@ -173,21 +167,6 @@ export const clearRichHistoryResults = (exploreId: ExploreId): ThunkResult<void>
*/
export const initRichHistory = (): ThunkResult<void> => {
return async (dispatch, getState) => {
const queriesMigrated = store.getBool(RICH_HISTORY_SETTING_KEYS.migrated, false);
const migrationFailedDuringThisSession = getState().explore.richHistoryMigrationFailed;
// Query history migration should always be successful, but in case of unexpected errors we ensure
// the migration attempt happens only once per session, and the user is informed about the failure
// in a way that can help with potential investigation.
if (config.queryHistoryEnabled && !queriesMigrated && !migrationFailedDuringThisSession) {
const migrationResult = await migrateQueryHistoryFromLocalStorage();
if (migrationResult.status === LocalStorageMigrationStatus.Failed) {
dispatch(richHistoryMigrationFailedAction());
logError(migrationResult.error!, { explore: { event: 'QueryHistoryMigrationFailed' } });
} else {
store.set(RICH_HISTORY_SETTING_KEYS.migrated, true);
}
}
let settings = getState().explore.richHistorySettings;
if (!settings) {
settings = await getRichHistorySettings();
@@ -31,7 +31,6 @@ export const richHistoryUpdatedAction = createAction<{ richHistoryResults: RichH
);
export const richHistoryStorageFullAction = createAction('explore/richHistoryStorageFullAction');
export const richHistoryLimitExceededAction = createAction('explore/richHistoryLimitExceededAction');
export const richHistoryMigrationFailedAction = createAction('explore/richHistoryMigrationFailedAction');
export const richHistorySettingsUpdatedAction = createAction<RichHistorySettings>('explore/richHistorySettingsUpdated');
export const richHistorySearchFiltersUpdatedAction = createAction<{
@@ -175,7 +174,6 @@ export const initialExploreState: ExploreState = {
correlations: undefined,
richHistoryStorageFull: false,
richHistoryLimitExceededWarningShown: false,
richHistoryMigrationFailed: false,
largerExploreId: undefined,
maxedExploreId: undefined,
evenSplitPanes: true,
@@ -256,13 +254,6 @@ export const exploreReducer = (state = initialExploreState, action: AnyAction):
};
}
if (richHistoryMigrationFailedAction.match(action)) {
return {
...state,
richHistoryMigrationFailed: true,
};
}
if (resetExploreAction.match(action)) {
const leftState = state[ExploreId.left];
const rightState = state[ExploreId.right];