Query History: Hide query history when anonymous user uses Explore (#49896)

* Hide query history when anonymous user uses Explore

* Show query history to anonymous users when local storage is used

* Fix integration test
This commit is contained in:
Piotr Jamróz 2022-06-02 15:51:11 +02:00 committed by GitHub
parent 927b6e33c0
commit 84860ffc96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 11 deletions

View File

@ -1,4 +1,5 @@
import { config } from '@grafana/runtime'; import { config } from '@grafana/runtime';
import { contextSrv } from 'app/core/core';
import { SortOrder } from '../utils/richHistoryTypes'; import { SortOrder } from '../utils/richHistoryTypes';
@ -19,6 +20,7 @@ interface RichHistorySupportedFeatures {
clearHistory: boolean; clearHistory: boolean;
onlyActiveDataSource: boolean; onlyActiveDataSource: boolean;
changeRetention: boolean; changeRetention: boolean;
queryHistoryAvailable: boolean;
} }
export const supportedFeatures = (): RichHistorySupportedFeatures => { export const supportedFeatures = (): RichHistorySupportedFeatures => {
@ -29,6 +31,7 @@ export const supportedFeatures = (): RichHistorySupportedFeatures => {
clearHistory: false, clearHistory: false,
onlyActiveDataSource: false, onlyActiveDataSource: false,
changeRetention: false, changeRetention: false,
queryHistoryAvailable: contextSrv.isSignedIn,
} }
: { : {
availableFilters: [SortOrder.Descending, SortOrder.Ascending, SortOrder.DatasourceAZ, SortOrder.DatasourceZA], availableFilters: [SortOrder.Descending, SortOrder.Ascending, SortOrder.DatasourceAZ, SortOrder.DatasourceZA],
@ -36,5 +39,6 @@ export const supportedFeatures = (): RichHistorySupportedFeatures => {
clearHistory: true, clearHistory: true,
onlyActiveDataSource: true, onlyActiveDataSource: true,
changeRetention: true, changeRetention: true,
queryHistoryAvailable: true,
}; };
}; };

View File

@ -11,6 +11,7 @@ import { selectors } from '@grafana/e2e-selectors';
import { Collapse, CustomScrollbar, ErrorBoundaryAlert, Themeable2, withTheme2, PanelContainer } from '@grafana/ui'; import { Collapse, CustomScrollbar, ErrorBoundaryAlert, Themeable2, withTheme2, PanelContainer } from '@grafana/ui';
import { FILTER_FOR_OPERATOR, FILTER_OUT_OPERATOR, FilterItem } from '@grafana/ui/src/components/Table/types'; import { FILTER_FOR_OPERATOR, FILTER_OUT_OPERATOR, FilterItem } from '@grafana/ui/src/components/Table/types';
import appEvents from 'app/core/app_events'; import appEvents from 'app/core/app_events';
import { supportedFeatures } from 'app/core/history/richHistoryStorageProvider';
import { getNodeGraphDataFrames } from 'app/plugins/panel/nodeGraph/utils'; import { getNodeGraphDataFrames } from 'app/plugins/panel/nodeGraph/utils';
import { StoreState } from 'app/types'; import { StoreState } from 'app/types';
import { AbsoluteTimeEvent } from 'app/types/events'; import { AbsoluteTimeEvent } from 'app/types/events';
@ -347,6 +348,7 @@ export class Explore extends React.PureComponent<Props, ExploreState> {
const styles = getStyles(theme); const styles = getStyles(theme);
const showPanels = queryResponse && queryResponse.state !== LoadingState.NotStarted; const showPanels = queryResponse && queryResponse.state !== LoadingState.NotStarted;
const showRichHistory = openDrawer === ExploreDrawer.RichHistory; const showRichHistory = openDrawer === ExploreDrawer.RichHistory;
const richHistoryRowButtonHidden = !supportedFeatures().queryHistoryAvailable;
const showQueryInspector = openDrawer === ExploreDrawer.QueryInspector; const showQueryInspector = openDrawer === ExploreDrawer.QueryInspector;
const showNoData = const showNoData =
queryResponse.state === LoadingState.Done && queryResponse.state === LoadingState.Done &&
@ -375,6 +377,7 @@ export class Explore extends React.PureComponent<Props, ExploreState> {
// We cannot show multiple traces at the same time right now so we do not show add query button. // We cannot show multiple traces at the same time right now so we do not show add query button.
//TODO:unification //TODO:unification
addQueryRowButtonHidden={false} addQueryRowButtonHidden={false}
richHistoryRowButtonHidden={richHistoryRowButtonHidden}
richHistoryButtonActive={showRichHistory} richHistoryButtonActive={showRichHistory}
queryInspectorButtonActive={showQueryInspector} queryInspectorButtonActive={showQueryInspector}
onClickAddQueryRowButton={this.onClickAddQueryRowButton} onClickAddQueryRowButton={this.onClickAddQueryRowButton}

View File

@ -20,10 +20,11 @@ describe('SecondaryActions', () => {
expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument(); expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument();
}); });
it('should not render add row button if addQueryRowButtonHidden=true', () => { it('should not render hidden elements', () => {
render( render(
<SecondaryActions <SecondaryActions
addQueryRowButtonHidden={true} addQueryRowButtonHidden={true}
richHistoryRowButtonHidden={true}
onClickAddQueryRowButton={noop} onClickAddQueryRowButton={noop}
onClickRichHistoryButton={noop} onClickRichHistoryButton={noop}
onClickQueryInspectorButton={noop} onClickQueryInspectorButton={noop}
@ -31,7 +32,7 @@ describe('SecondaryActions', () => {
); );
expect(screen.queryByRole('button', { name: /Add row button/i })).not.toBeInTheDocument(); expect(screen.queryByRole('button', { name: /Add row button/i })).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: /Rich history button/i })).toBeInTheDocument(); expect(screen.queryByRole('button', { name: /Rich history button/i })).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument(); expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument();
}); });

View File

@ -7,6 +7,7 @@ import { Button, HorizontalGroup, useTheme2 } from '@grafana/ui';
type Props = { type Props = {
addQueryRowButtonDisabled?: boolean; addQueryRowButtonDisabled?: boolean;
addQueryRowButtonHidden?: boolean; addQueryRowButtonHidden?: boolean;
richHistoryRowButtonHidden?: boolean;
richHistoryButtonActive?: boolean; richHistoryButtonActive?: boolean;
queryInspectorButtonActive?: boolean; queryInspectorButtonActive?: boolean;
@ -39,15 +40,17 @@ export function SecondaryActions(props: Props) {
Add query Add query
</Button> </Button>
)} )}
<Button {!props.richHistoryRowButtonHidden && (
variant="secondary" <Button
aria-label="Rich history button" variant="secondary"
className={cx({ ['explore-active-button']: props.richHistoryButtonActive })} aria-label="Rich history button"
onClick={props.onClickRichHistoryButton} className={cx({ ['explore-active-button']: props.richHistoryButtonActive })}
icon="history" onClick={props.onClickRichHistoryButton}
> icon="history"
Query history >
</Button> Query history
</Button>
)}
<Button <Button
variant="secondary" variant="secondary"
aria-label="Query inspector button" aria-label="Query inspector button"

View File

@ -54,6 +54,7 @@ jest.mock('@grafana/runtime', () => ({
jest.mock('app/core/core', () => ({ jest.mock('app/core/core', () => ({
contextSrv: { contextSrv: {
hasAccess: () => true, hasAccess: () => true,
isSignedIn: true,
}, },
})); }));