mirror of
https://github.com/grafana/grafana.git
synced 2025-02-14 09:33:34 -06:00
* Load Rich History when the container is opened * Store rich history for each pane separately * Do not update currently opened query history when an item is added It's impossible to figure out if the item should be added or not, because filters are applied in the backend. We don't want to replicate that filtering logic in frontend. One way to make it work could be by refreshing both panes. * Test starring and deleting query history items when both panes are open * Remove e2e dependency on ExploreId * Fix unit test * Assert exact queries * Simplify test * Fix e2e tests * Fix toolbar a11y * Reload the history after an item is added * Fix unit test * Remove references to Explore from generic PageToolbar component * Update test name * Fix test assertion * Add issue item to TODO * Improve test assertion * Simplify test setup * Move query history settings to persistence layer * Fix test import * Fix unit test * Fix unit test * Test local storage settings API * Code formatting * Fix linting errors * Add an integration test * Add missing aria role * Fix a11y issues * Fix a11y issues * Use divs instead of ul/li Otherwis,e pa11y-ci reports the error below claiming there are no children with role=tab: Certain ARIA roles must contain particular children (https://dequeuniversity.com/rules/axe/4.3/aria-required-children?application=axeAPI) (#reactRoot > div > main > div:nth-child(3) > div > div:nth-child(1) > div > div:nth-child(1) > div > div > nav > div:nth-child(2) > ul) <ul class="css-af3vye" role="tablist"><li class="css-1ciwanz"><a href...</ul> * Clean up settings tab * Remove redundant aria label * Remove redundant container * Clean up test assertions * Move filtering to persistence layer * Move filtering to persistence layer * Simplify applying filters * Split applying filters and reloading the history * Debounce updating filters * Update tests * Fix waiting for debounced results * Clear results when switching tabs * Improve test coverage * Update docs * Revert extra handling for uid (will be added when we introduce remote storage) * Create basic plan * Rename query history toggle * Add list of supported features and add ds name to RichHistoryQuery object * Clean up Removed planned items will be addressed in upcoming prs (filtering and pagination) * Handle data source filters * Simplify DTO conversion * Clean up * Fix betterer conflicts * Fix imports * Fix imports * Post-merge fixes * Use config instead of a feature flag * Use config instead of a feature flag * Update converter tests * Add tests for RichHistoryRemoteStorage * Simplify test setup * Simplify assertion * Add e2e test for query history * Remove duplicated entry * Fix unit tests * Improve readability * Remove unnecessary casting * Mock backend in integration tests * Remove unnecessary casting * Fix integration test * Update betterer results * Fix unit tests * Simplify testing with DataSourceSrv * Fix sorting and add to/from filtering * Basic pagination * Show load more only if there are items to load * Post-merge fixes * Change initial page limit to 100 * Fix unit tests * Fix linting errors * Test pagination * Fix query migration * Fix unit tests * Fix prettier * Remove full stop * Do not show number of queries with partial results to avoid confusion * Show number of displayed results when partial results are shown
233 lines
8.0 KiB
TypeScript
233 lines
8.0 KiB
TypeScript
import { AnyAction, createAction } from '@reduxjs/toolkit';
|
|
|
|
import { DataQuery, HistoryItem } from '@grafana/data';
|
|
import { config } from '@grafana/runtime';
|
|
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,
|
|
} from 'app/core/utils/richHistory';
|
|
import { ExploreId, ExploreItemState, ExploreState, RichHistoryQuery, ThunkResult } from 'app/types';
|
|
|
|
import { supportedFeatures } from '../../../core/history/richHistoryStorageProvider';
|
|
import { RichHistorySearchFilters, RichHistorySettings } from '../../../core/utils/richHistoryTypes';
|
|
|
|
import {
|
|
richHistoryLimitExceededAction,
|
|
richHistoryMigrationFailedAction,
|
|
richHistorySearchFiltersUpdatedAction,
|
|
richHistorySettingsUpdatedAction,
|
|
richHistoryStorageFullAction,
|
|
richHistoryUpdatedAction,
|
|
} from './main';
|
|
|
|
//
|
|
// Actions and Payloads
|
|
//
|
|
|
|
export interface HistoryUpdatedPayload {
|
|
exploreId: ExploreId;
|
|
history: HistoryItem[];
|
|
}
|
|
export const historyUpdatedAction = createAction<HistoryUpdatedPayload>('explore/historyUpdated');
|
|
|
|
//
|
|
// Action creators
|
|
//
|
|
|
|
type SyncHistoryUpdatesOptions = {
|
|
updatedQuery?: RichHistoryQuery;
|
|
deletedId?: string;
|
|
};
|
|
|
|
/**
|
|
* Updates current state in both Explore panes after changing or deleting a query history item
|
|
*/
|
|
const updateRichHistoryState = ({ updatedQuery, deletedId }: SyncHistoryUpdatesOptions): ThunkResult<void> => {
|
|
return async (dispatch, getState) => {
|
|
forEachExplorePane(getState().explore, (item, exploreId) => {
|
|
const newRichHistory = item.richHistory
|
|
// update
|
|
.map((query) => (query.id === updatedQuery?.id ? updatedQuery : query))
|
|
// or remove
|
|
.filter((query) => query.id !== deletedId);
|
|
dispatch(
|
|
richHistoryUpdatedAction({
|
|
richHistoryResults: { richHistory: newRichHistory, total: item.richHistoryTotal },
|
|
exploreId,
|
|
})
|
|
);
|
|
});
|
|
};
|
|
};
|
|
|
|
const forEachExplorePane = (state: ExploreState, callback: (item: ExploreItemState, exploreId: ExploreId) => void) => {
|
|
callback(state.left, ExploreId.left);
|
|
state.right && callback(state.right, ExploreId.right);
|
|
};
|
|
|
|
export const addHistoryItem = (
|
|
datasourceUid: string,
|
|
datasourceName: string,
|
|
queries: DataQuery[]
|
|
): ThunkResult<void> => {
|
|
return async (dispatch, getState) => {
|
|
const { richHistoryStorageFull, limitExceeded } = await addToRichHistory(
|
|
datasourceUid,
|
|
datasourceName,
|
|
queries,
|
|
false,
|
|
'',
|
|
!getState().explore.richHistoryStorageFull,
|
|
!getState().explore.richHistoryLimitExceededWarningShown
|
|
);
|
|
if (richHistoryStorageFull) {
|
|
dispatch(richHistoryStorageFullAction());
|
|
}
|
|
if (limitExceeded) {
|
|
dispatch(richHistoryLimitExceededAction());
|
|
}
|
|
};
|
|
};
|
|
|
|
export const starHistoryItem = (id: string, starred: boolean): ThunkResult<void> => {
|
|
return async (dispatch, getState) => {
|
|
const updatedQuery = await updateStarredInRichHistory(id, starred);
|
|
dispatch(updateRichHistoryState({ updatedQuery }));
|
|
};
|
|
};
|
|
|
|
export const commentHistoryItem = (id: string, comment?: string): ThunkResult<void> => {
|
|
return async (dispatch) => {
|
|
const updatedQuery = await updateCommentInRichHistory(id, comment);
|
|
dispatch(updateRichHistoryState({ updatedQuery }));
|
|
};
|
|
};
|
|
|
|
export const deleteHistoryItem = (id: string): ThunkResult<void> => {
|
|
return async (dispatch) => {
|
|
const deletedId = await deleteQueryInRichHistory(id);
|
|
dispatch(updateRichHistoryState({ deletedId }));
|
|
};
|
|
};
|
|
|
|
export const deleteRichHistory = (): ThunkResult<void> => {
|
|
return async (dispatch) => {
|
|
await deleteAllFromRichHistory();
|
|
dispatch(
|
|
richHistoryUpdatedAction({ richHistoryResults: { richHistory: [], total: 0 }, exploreId: ExploreId.left })
|
|
);
|
|
dispatch(
|
|
richHistoryUpdatedAction({ richHistoryResults: { richHistory: [], total: 0 }, exploreId: ExploreId.right })
|
|
);
|
|
};
|
|
};
|
|
|
|
export const loadRichHistory = (exploreId: ExploreId): ThunkResult<void> => {
|
|
return async (dispatch, getState) => {
|
|
const filters = getState().explore![exploreId]?.richHistorySearchFilters;
|
|
if (filters) {
|
|
const richHistoryResults = await getRichHistory(filters);
|
|
dispatch(richHistoryUpdatedAction({ richHistoryResults, exploreId }));
|
|
}
|
|
};
|
|
};
|
|
|
|
export const loadMoreRichHistory = (exploreId: ExploreId): ThunkResult<void> => {
|
|
return async (dispatch, getState) => {
|
|
const currentFilters = getState().explore![exploreId]?.richHistorySearchFilters;
|
|
const currentRichHistory = getState().explore![exploreId]?.richHistory;
|
|
if (currentFilters && currentRichHistory) {
|
|
const nextFilters = { ...currentFilters, page: (currentFilters?.page || 1) + 1 };
|
|
const moreRichHistory = await getRichHistory(nextFilters);
|
|
const richHistory = [...currentRichHistory, ...moreRichHistory.richHistory];
|
|
dispatch(richHistorySearchFiltersUpdatedAction({ filters: nextFilters, exploreId }));
|
|
dispatch(
|
|
richHistoryUpdatedAction({ richHistoryResults: { richHistory, total: moreRichHistory.total }, exploreId })
|
|
);
|
|
}
|
|
};
|
|
};
|
|
|
|
export const clearRichHistoryResults = (exploreId: ExploreId): ThunkResult<void> => {
|
|
return async (dispatch) => {
|
|
dispatch(richHistorySearchFiltersUpdatedAction({ filters: undefined, exploreId }));
|
|
dispatch(richHistoryUpdatedAction({ richHistoryResults: { richHistory: [], total: 0 }, exploreId }));
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Initialize query history pane. To load history it requires settings to be loaded first
|
|
* (but only once per session). Filters are initialised by the tab (starred or home).
|
|
*/
|
|
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 migrationStatus = await migrateQueryHistoryFromLocalStorage();
|
|
if (migrationStatus === LocalStorageMigrationStatus.Failed) {
|
|
dispatch(richHistoryMigrationFailedAction());
|
|
} else {
|
|
store.set(RICH_HISTORY_SETTING_KEYS.migrated, true);
|
|
}
|
|
}
|
|
let settings = getState().explore.richHistorySettings;
|
|
if (!settings) {
|
|
settings = await getRichHistorySettings();
|
|
dispatch(richHistorySettingsUpdatedAction(settings));
|
|
}
|
|
};
|
|
};
|
|
|
|
export const updateHistorySettings = (settings: RichHistorySettings): ThunkResult<void> => {
|
|
return async (dispatch) => {
|
|
dispatch(richHistorySettingsUpdatedAction(settings));
|
|
await updateRichHistorySettings(settings);
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Assumed this can be called only when settings and filters are initialised
|
|
*/
|
|
export const updateHistorySearchFilters = (
|
|
exploreId: ExploreId,
|
|
filters: RichHistorySearchFilters
|
|
): ThunkResult<void> => {
|
|
return async (dispatch, getState) => {
|
|
await dispatch(richHistorySearchFiltersUpdatedAction({ exploreId, filters: { ...filters } }));
|
|
const currentSettings = getState().explore.richHistorySettings!;
|
|
if (supportedFeatures().lastUsedDataSourcesAvailable) {
|
|
await dispatch(
|
|
updateHistorySettings({
|
|
...currentSettings,
|
|
lastUsedDatasourceFilters: filters.datasourceFilters,
|
|
})
|
|
);
|
|
}
|
|
};
|
|
};
|
|
|
|
export const historyReducer = (state: ExploreItemState, action: AnyAction): ExploreItemState => {
|
|
if (historyUpdatedAction.match(action)) {
|
|
return {
|
|
...state,
|
|
history: action.payload.history,
|
|
};
|
|
}
|
|
return state;
|
|
};
|