Files
grafana/public/app/plugins/datasource/loki/querybuilder/components/LabelBrowserModal.tsx
Ivana Huckova 4fd1d92332 Loki: Remove relying on timeSrv.timeRange in LanguageProvider (#78450)
* Loki: Allow setting of timeRange when using languageProvider functions

* Loki: Use timerange where available for start

* Loki: Use timerange where available for fetchLabels

* Loki: Use timerange where available for fetchSeriesLabels

* Loki: Use timerange where available for fetchLabelValues

* Loki: Use timerange where available for getParserAndLabelKeys

* Loki: Update and add tests for fetchLabels

* Loki: Update and add tests for fetchSeriesLabels

* Loki: Update and add tests for fetchSeries

* Loki: Update and add tests for fetchLabelValues

* Loki: Update and add tests for fetchLabelValues

* Loki: Update and add tests for getParserAndLabelKeys

* Update public/app/plugins/datasource/loki/LanguageProvider.test.ts

Co-authored-by: Matias Chomicki <matyax@gmail.com>

* Update public/app/plugins/datasource/loki/LanguageProvider.test.ts

Co-authored-by: Matias Chomicki <matyax@gmail.com>

* Not needing to use languageProvider.getDefaultTime in Monaco

* Update comment

* Update getDefaultTimeRange to be ptivate

---------

Co-authored-by: Matias Chomicki <matyax@gmail.com>
2023-11-22 14:35:15 +01:00

98 lines
2.9 KiB
TypeScript

import { css } from '@emotion/css';
import React, { useState, useEffect } from 'react';
import { CoreApp, GrafanaTheme2, TimeRange } from '@grafana/data';
import { reportInteraction } from '@grafana/runtime';
import { LoadingPlaceholder, Modal, useStyles2 } from '@grafana/ui';
import { LocalStorageValueProvider } from 'app/core/components/LocalStorageValueProvider';
import { LokiLabelBrowser } from '../../components/LokiLabelBrowser';
import { LokiDatasource } from '../../datasource';
import { LokiQuery } from '../../types';
export interface Props {
isOpen: boolean;
datasource: LokiDatasource;
query: LokiQuery;
app?: CoreApp;
timeRange?: TimeRange;
onClose: () => void;
onChange: (query: LokiQuery) => void;
onRunQuery: () => void;
}
export const LabelBrowserModal = (props: Props) => {
const { isOpen, onClose, datasource, app, timeRange } = props;
const [labelsLoaded, setLabelsLoaded] = useState(false);
const [hasLogLabels, setHasLogLabels] = useState(false);
const LAST_USED_LABELS_KEY = 'grafana.datasources.loki.browser.labels';
const styles = useStyles2(getStyles);
useEffect(() => {
if (!isOpen) {
return;
}
datasource.languageProvider.fetchLabels({ timeRange }).then((labels) => {
setLabelsLoaded(true);
setHasLogLabels(labels.length > 0);
});
}, [datasource, isOpen, timeRange]);
const changeQuery = (value: string) => {
const { query, onChange, onRunQuery } = props;
const nextQuery = { ...query, expr: value };
onChange(nextQuery);
onRunQuery();
};
const onChange = (selector: string) => {
changeQuery(selector);
onClose();
};
const reportInteractionAndClose = () => {
reportInteraction('grafana_loki_label_browser_closed', {
app,
closeType: 'modalClose',
});
onClose();
};
return (
<Modal isOpen={isOpen} title="Label browser" onDismiss={reportInteractionAndClose} className={styles.modal}>
{!labelsLoaded && <LoadingPlaceholder text="Loading labels..." />}
{labelsLoaded && !hasLogLabels && <p>No labels found.</p>}
{labelsLoaded && hasLogLabels && (
<LocalStorageValueProvider<string[]> storageKey={LAST_USED_LABELS_KEY} defaultValue={[]}>
{(lastUsedLabels, onLastUsedLabelsSave, onLastUsedLabelsDelete) => {
return (
<LokiLabelBrowser
languageProvider={datasource.languageProvider}
onChange={onChange}
lastUsedLabels={lastUsedLabels}
storeLastUsedLabels={onLastUsedLabelsSave}
deleteLastUsedLabels={onLastUsedLabelsDelete}
app={app}
timeRange={timeRange}
/>
);
}}
</LocalStorageValueProvider>
)}
</Modal>
);
};
const getStyles = (theme: GrafanaTheme2) => {
return {
modal: css`
width: 85vw;
${theme.breakpoints.down('md')} {
width: 100%;
}
`,
};
};