2024-01-12 08:05:46 -06:00
|
|
|
import { customAlphabet } from 'nanoid';
|
2019-09-12 10:28:46 -05:00
|
|
|
import { Unsubscribable } from 'rxjs';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
2019-08-28 11:24:52 -05:00
|
|
|
import {
|
2023-11-20 13:22:01 -06:00
|
|
|
AdHocVariableFilter,
|
2019-12-27 02:11:16 -06:00
|
|
|
CoreApp,
|
2020-06-30 07:51:04 -05:00
|
|
|
DataQuery,
|
2019-10-31 04:48:05 -05:00
|
|
|
DataQueryRequest,
|
2019-11-07 06:49:45 -06:00
|
|
|
DataSourceApi,
|
2022-08-31 09:24:20 -05:00
|
|
|
DataSourceRef,
|
2020-06-30 07:51:04 -05:00
|
|
|
DefaultTimeZone,
|
2019-10-31 04:48:05 -05:00
|
|
|
HistoryItem,
|
2019-11-07 06:49:45 -06:00
|
|
|
IntervalValues,
|
|
|
|
LogsDedupStrategy,
|
2020-08-06 11:35:49 -05:00
|
|
|
LogsSortOrder,
|
2022-01-17 05:48:26 -06:00
|
|
|
rangeUtil,
|
2019-11-07 06:49:45 -06:00
|
|
|
RawTimeRange,
|
2023-09-14 09:12:20 -05:00
|
|
|
ScopedVars,
|
2019-11-07 06:49:45 -06:00
|
|
|
TimeRange,
|
|
|
|
TimeZone,
|
2023-08-17 05:06:52 -05:00
|
|
|
toURLRange,
|
2020-04-20 00:37:38 -05:00
|
|
|
urlUtil,
|
2019-08-28 11:24:52 -05:00
|
|
|
} from '@grafana/data';
|
2023-09-14 09:12:20 -05:00
|
|
|
import { getDataSourceSrv } from '@grafana/runtime';
|
2019-10-31 04:48:05 -05:00
|
|
|
import { RefreshPicker } from '@grafana/ui';
|
2022-04-22 08:33:13 -05:00
|
|
|
import store from 'app/core/store';
|
2023-03-13 07:53:19 -05:00
|
|
|
import { ExpressionDatasourceUID } from 'app/features/expressions/types';
|
2023-06-21 04:06:28 -05:00
|
|
|
import { QueryOptions, QueryTransaction } from 'app/types/explore';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { getNextRefIdChar } from './query';
|
2018-10-01 05:56:26 -05:00
|
|
|
|
2019-01-31 12:38:49 -06:00
|
|
|
export const DEFAULT_UI_STATE = {
|
2019-02-07 10:46:33 -06:00
|
|
|
dedupStrategy: LogsDedupStrategy.none,
|
2019-01-31 12:38:49 -06:00
|
|
|
};
|
|
|
|
|
2024-01-12 08:05:46 -06:00
|
|
|
export const ID_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|
|
|
const nanoid = customAlphabet(ID_ALPHABET, 3);
|
|
|
|
|
2018-11-21 07:45:57 -06:00
|
|
|
const MAX_HISTORY_ITEMS = 100;
|
|
|
|
|
2023-06-06 09:31:39 -05:00
|
|
|
const LAST_USED_DATASOURCE_KEY = 'grafana.explore.datasource';
|
|
|
|
const lastUsedDatasourceKeyForOrgId = (orgId: number) => `${LAST_USED_DATASOURCE_KEY}.${orgId}`;
|
|
|
|
export const getLastUsedDatasourceUID = (orgId: number) =>
|
|
|
|
store.getObject<string>(lastUsedDatasourceKeyForOrgId(orgId));
|
|
|
|
export const setLastUsedDatasourceUID = (orgId: number, datasourceUID: string) =>
|
|
|
|
store.setObject(lastUsedDatasourceKeyForOrgId(orgId), datasourceUID);
|
2019-01-10 07:24:31 -06:00
|
|
|
|
2019-11-07 06:49:45 -06:00
|
|
|
export interface GetExploreUrlArguments {
|
2023-09-14 09:12:20 -05:00
|
|
|
queries: DataQuery[];
|
|
|
|
dsRef: DataSourceRef | null | undefined;
|
2023-09-04 08:08:52 -05:00
|
|
|
timeRange: TimeRange;
|
2023-09-14 09:12:20 -05:00
|
|
|
scopedVars: ScopedVars | undefined;
|
2023-11-20 13:22:01 -06:00
|
|
|
adhocFilters?: AdHocVariableFilter[];
|
2019-11-07 06:49:45 -06:00
|
|
|
}
|
2020-07-09 08:16:35 -05:00
|
|
|
|
2023-06-21 04:06:28 -05:00
|
|
|
export function generateExploreId() {
|
2024-01-12 08:05:46 -06:00
|
|
|
while (true) {
|
|
|
|
const id = nanoid(3);
|
|
|
|
|
|
|
|
if (!/^\d+$/.test(id)) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 04:06:28 -05:00
|
|
|
}
|
|
|
|
|
2021-10-21 03:46:40 -05:00
|
|
|
/**
|
|
|
|
* Returns an Explore-URL that contains a panel's queries and the dashboard time range.
|
|
|
|
*/
|
2020-03-25 06:25:39 -05:00
|
|
|
export async function getExploreUrl(args: GetExploreUrlArguments): Promise<string | undefined> {
|
2023-11-20 13:22:01 -06:00
|
|
|
const { queries, dsRef, timeRange, scopedVars, adhocFilters } = args;
|
2023-10-26 08:16:22 -05:00
|
|
|
const interpolatedQueries = (
|
|
|
|
await Promise.allSettled(
|
|
|
|
queries
|
|
|
|
// Explore does not support expressions so filter those out
|
|
|
|
.filter((q) => q.datasource?.uid !== ExpressionDatasourceUID)
|
|
|
|
.map(async (q) => {
|
|
|
|
// if the query defines a datasource, use that one, otherwise use the one from the panel, which should always be defined.
|
|
|
|
// this will rejects if the datasource is not found, or return the default one if dsRef is not provided.
|
|
|
|
const queryDs = await getDataSourceSrv().get(q.datasource || dsRef);
|
|
|
|
|
|
|
|
return {
|
|
|
|
// interpolate the query using its datasource `interpolateVariablesInQueries` method if defined, othewise return the query as-is.
|
2023-11-20 13:22:01 -06:00
|
|
|
...(queryDs.interpolateVariablesInQueries?.([q], scopedVars ?? {}, adhocFilters)[0] || q),
|
2023-10-26 08:16:22 -05:00
|
|
|
// But always set the datasource as it's required in Explore.
|
|
|
|
// NOTE: if for some reason the query has the "mixed" datasource, we omit the property;
|
|
|
|
// Upon initialization, Explore use its own logic to determine the datasource.
|
|
|
|
...(!queryDs.meta.mixed && { datasource: queryDs.getRef() }),
|
|
|
|
};
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
<T>(promise: PromiseSettledResult<T>): promise is PromiseFulfilledResult<T> => promise.status === 'fulfilled'
|
|
|
|
)
|
|
|
|
.map((q) => q.value);
|
|
|
|
|
|
|
|
const exploreState = JSON.stringify({
|
|
|
|
[generateExploreId()]: { range: toURLRange(timeRange.raw), queries: interpolatedQueries, datasource: dsRef?.uid },
|
|
|
|
});
|
|
|
|
return urlUtil.renderUrl('/explore', { panes: exploreState, schemaVersion: 1 });
|
2018-09-24 10:47:43 -05:00
|
|
|
}
|
2018-10-01 05:56:26 -05:00
|
|
|
|
2024-02-16 08:10:22 -06:00
|
|
|
export function requestIdGenerator(exploreId: string) {
|
|
|
|
return `explore_${exploreId}`;
|
|
|
|
}
|
|
|
|
|
2019-01-10 07:24:31 -06:00
|
|
|
export function buildQueryTransaction(
|
2023-06-21 04:06:28 -05:00
|
|
|
exploreId: string,
|
2019-05-10 07:00:39 -05:00
|
|
|
queries: DataQuery[],
|
2019-01-10 07:24:31 -06:00
|
|
|
queryOptions: QueryOptions,
|
2019-04-29 11:28:41 -05:00
|
|
|
range: TimeRange,
|
2020-04-27 08:28:06 -05:00
|
|
|
scanning: boolean,
|
Correlations: Add an editor in Explore (#73315)
* Start adding correlations editor mode
* Add selector for merge conflict resolution
* Enable saving
* Build out new corelation helper component
* flesh out save with label/description, change color
* Have breadcrumb exit correlation editor mode
* Add extension property to show/hide, use it for correlations
* Bring in feature toggle
* Remove unnecessary param
* Cleanup
* Parse logs json
* Work on correlation edit mode bar
* Tinker with a top element for the editor mode
* Handle various explore state changes with correlations editor mode
* WIP - add unsaved changes modal
* Have correlation bar always rendered, sometimes hidden
* Add various prompt modals
* Clear correlations data on mode bar unmount, only use not left pane changes to count as dirty
* Move special logic to explore
* Remove all shouldShow logic from plugin extensions
* remove grafana data changes
* WIP - clean up correlations state
* Interpolate data before sending to onclick
* Override outline button coloring to account for dark background
* More cleanup, more color tweaking
* Prettier formatting, change state to refer to editor
* Fix tests
* More state change tweaks
* ensure correlation save ability, change correlation editor state vars
* fix import
* Remove independent selector for editorMode, work close pane into editor exit flow
* Add change datasource post action
* Clean up based on PR feedback, handle closing left panel with helper better
* Remove breadcrumb additions, add section and better ID to cmd palette action
* Interpolate query results if it is ran with a helper with vars
* Pass the datasource query along with the correlate link to ensure the datasource unique ID requirement passes
* Use different onmount function to capture state of panes at time of close instead of time of mount
* Fix node graph’s datalink not working
* Actually commit the fix to saving
* Fix saving correlations with mixed datasource to use the first query correlation UID
* Add tracking
* Use query datasources in mixed scenario, move exit tracking to click handler
* Add correlations to a place where both can be used in the correlations editor
* Be more selective on when we await the datasource get
* Fix CSS to use objects
* Update betterer
* Add test around new decorator functionality
* Add tests for decorate with correlations
* Some reorganization and a few tweaks based on feedback
* Move dirty state change to state function and out of component
* Change the verbiage around a little
* Various suggestions from Gio
Co-authored-by: Giordano Ricci <me@giordanoricci.com>
* More small Gio-related tweaks
* Tie helper data to datasource - clear it out when the datasource changes
* Missed another Gio tweak
* Fix linter error
* Only clear helper data on left pane changes
* Add height offset for correlation editor bar so it doesn’t scroll off page
---------
Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>
Co-authored-by: Giordano Ricci <me@giordanoricci.com>
2023-10-03 09:28:29 -05:00
|
|
|
timeZone?: TimeZone,
|
|
|
|
scopedVars?: ScopedVars
|
2019-01-10 07:24:31 -06:00
|
|
|
): QueryTransaction {
|
2024-01-12 08:05:46 -06:00
|
|
|
const panelId = Number.parseInt(exploreId, 36);
|
2019-09-16 05:35:39 -05:00
|
|
|
const { interval, intervalMs } = getIntervals(range, queryOptions.minInterval, queryOptions.maxDataPoints);
|
|
|
|
|
2019-09-03 15:04:33 -05:00
|
|
|
const request: DataQueryRequest = {
|
2019-12-27 02:11:16 -06:00
|
|
|
app: CoreApp.Explore,
|
2019-09-03 15:04:33 -05:00
|
|
|
// TODO probably should be taken from preferences but does not seem to be used anyway.
|
2020-04-27 08:28:06 -05:00
|
|
|
timezone: timeZone || DefaultTimeZone,
|
2019-09-12 10:28:46 -05:00
|
|
|
startTime: Date.now(),
|
2019-01-10 07:24:31 -06:00
|
|
|
interval,
|
|
|
|
intervalMs,
|
2024-01-12 08:05:46 -06:00
|
|
|
panelId,
|
2020-07-16 08:00:28 -05:00
|
|
|
targets: queries, // Datasources rely on DataQueries being passed under the targets key.
|
2019-04-29 11:28:41 -05:00
|
|
|
range,
|
2024-02-16 08:10:22 -06:00
|
|
|
requestId: requestIdGenerator(exploreId),
|
2019-04-29 11:28:41 -05:00
|
|
|
rangeRaw: range.raw,
|
2019-01-10 07:24:31 -06:00
|
|
|
scopedVars: {
|
|
|
|
__interval: { text: interval, value: interval },
|
|
|
|
__interval_ms: { text: intervalMs, value: intervalMs },
|
Correlations: Add an editor in Explore (#73315)
* Start adding correlations editor mode
* Add selector for merge conflict resolution
* Enable saving
* Build out new corelation helper component
* flesh out save with label/description, change color
* Have breadcrumb exit correlation editor mode
* Add extension property to show/hide, use it for correlations
* Bring in feature toggle
* Remove unnecessary param
* Cleanup
* Parse logs json
* Work on correlation edit mode bar
* Tinker with a top element for the editor mode
* Handle various explore state changes with correlations editor mode
* WIP - add unsaved changes modal
* Have correlation bar always rendered, sometimes hidden
* Add various prompt modals
* Clear correlations data on mode bar unmount, only use not left pane changes to count as dirty
* Move special logic to explore
* Remove all shouldShow logic from plugin extensions
* remove grafana data changes
* WIP - clean up correlations state
* Interpolate data before sending to onclick
* Override outline button coloring to account for dark background
* More cleanup, more color tweaking
* Prettier formatting, change state to refer to editor
* Fix tests
* More state change tweaks
* ensure correlation save ability, change correlation editor state vars
* fix import
* Remove independent selector for editorMode, work close pane into editor exit flow
* Add change datasource post action
* Clean up based on PR feedback, handle closing left panel with helper better
* Remove breadcrumb additions, add section and better ID to cmd palette action
* Interpolate query results if it is ran with a helper with vars
* Pass the datasource query along with the correlate link to ensure the datasource unique ID requirement passes
* Use different onmount function to capture state of panes at time of close instead of time of mount
* Fix node graph’s datalink not working
* Actually commit the fix to saving
* Fix saving correlations with mixed datasource to use the first query correlation UID
* Add tracking
* Use query datasources in mixed scenario, move exit tracking to click handler
* Add correlations to a place where both can be used in the correlations editor
* Be more selective on when we await the datasource get
* Fix CSS to use objects
* Update betterer
* Add test around new decorator functionality
* Add tests for decorate with correlations
* Some reorganization and a few tweaks based on feedback
* Move dirty state change to state function and out of component
* Change the verbiage around a little
* Various suggestions from Gio
Co-authored-by: Giordano Ricci <me@giordanoricci.com>
* More small Gio-related tweaks
* Tie helper data to datasource - clear it out when the datasource changes
* Missed another Gio tweak
* Fix linter error
* Only clear helper data on left pane changes
* Add height offset for correlation editor bar so it doesn’t scroll off page
---------
Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>
Co-authored-by: Giordano Ricci <me@giordanoricci.com>
2023-10-03 09:28:29 -05:00
|
|
|
...scopedVars,
|
2019-01-10 07:24:31 -06:00
|
|
|
},
|
2019-04-11 06:57:04 -05:00
|
|
|
maxDataPoints: queryOptions.maxDataPoints,
|
2020-07-16 08:00:28 -05:00
|
|
|
liveStreaming: queryOptions.liveStreaming,
|
2019-01-10 07:24:31 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2019-05-10 07:00:39 -05:00
|
|
|
queries,
|
2019-09-03 15:04:33 -05:00
|
|
|
request,
|
2019-01-10 07:24:31 -06:00
|
|
|
scanning,
|
|
|
|
id: generateKey(), // reusing for unique ID
|
|
|
|
done: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-15 10:26:23 -05:00
|
|
|
export const clearQueryKeys: (query: DataQuery) => DataQuery = ({ key, ...rest }) => rest;
|
2018-11-22 04:02:53 -06:00
|
|
|
|
2023-05-22 05:53:58 -05:00
|
|
|
export const safeStringifyValue = (value: unknown, space?: number) => {
|
Explore: Add transformations to correlation data links (#61799)
* bring in source from database
* bring in transformations from database
* add regex transformations to scopevar
* Consolidate types, add better example, cleanup
* Add var only if match
* Change ScopedVar to not require text, do not leak transformation-made variables between links
* Add mappings and start implementing logfmt
* Add mappings and start implementing logfmt
* Remove mappings, turn off global regex
* Add example yaml and omit transformations if empty
* Fix the yaml
* Add logfmt transformation
* Cleanup transformations and yaml
* add transformation field to FE types and use it, safeStringify logfmt values
* Add tests, only safe stringify if non-string, fix bug with safe stringify where it would return empty string with false value
* Add test for transformation field
* Do not add null transformations object
* Break out transformation logic, add tests to backend code
* Fix lint errors I understand 😅
* Fix the backend lint error
* Remove unnecessary code and mark new Transformations object as internal
* Add support for named capture groups
* Remove type assertion
* Remove variable name from transformation
* Add test for overriding regexes
* Add back variable name field, but change to mapValue
* fix go api test
* Change transformation types to enum, add better provisioning checks for bad type name and format
* Check for expression with regex transformations
2023-02-22 06:53:03 -06:00
|
|
|
if (value === undefined || value === null) {
|
2019-05-10 07:00:39 -05:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.stringify(value, null, space);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2018-11-21 07:45:57 -06:00
|
|
|
export function generateKey(index = 0): string {
|
2020-08-26 04:38:39 -05:00
|
|
|
return `Q-${uuidv4()}-${index}`;
|
2018-11-21 07:45:57 -06:00
|
|
|
}
|
|
|
|
|
2022-08-31 09:24:20 -05:00
|
|
|
export async function generateEmptyQuery(
|
|
|
|
queries: DataQuery[],
|
|
|
|
index = 0,
|
|
|
|
dataSourceOverride?: DataSourceRef
|
|
|
|
): Promise<DataQuery> {
|
|
|
|
let datasourceInstance: DataSourceApi | undefined;
|
|
|
|
let datasourceRef: DataSourceRef | null | undefined;
|
|
|
|
let defaultQuery: Partial<DataQuery> | undefined;
|
|
|
|
|
|
|
|
// datasource override is if we have switched datasources with no carry-over - we want to create a new query with a datasource we define
|
2022-10-14 08:12:04 -05:00
|
|
|
// it's also used if there's a root datasource and there were no previous queries
|
2022-08-31 09:24:20 -05:00
|
|
|
if (dataSourceOverride) {
|
|
|
|
datasourceRef = dataSourceOverride;
|
|
|
|
} else if (queries.length > 0 && queries[queries.length - 1].datasource) {
|
|
|
|
// otherwise use last queries' datasource
|
|
|
|
datasourceRef = queries[queries.length - 1].datasource;
|
|
|
|
} else {
|
|
|
|
datasourceInstance = await getDataSourceSrv().get();
|
|
|
|
defaultQuery = datasourceInstance.getDefaultQuery?.(CoreApp.Explore);
|
|
|
|
datasourceRef = datasourceInstance.getRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!datasourceInstance) {
|
|
|
|
datasourceInstance = await getDataSourceSrv().get(datasourceRef);
|
|
|
|
defaultQuery = datasourceInstance.getDefaultQuery?.(CoreApp.Explore);
|
|
|
|
}
|
|
|
|
|
2023-04-28 09:01:50 -05:00
|
|
|
return { ...defaultQuery, refId: getNextRefIdChar(queries), key: generateKey(index), datasource: datasourceRef };
|
2018-11-21 07:45:57 -06:00
|
|
|
}
|
|
|
|
|
2019-05-10 07:00:39 -05:00
|
|
|
export const generateNewKeyAndAddRefIdIfMissing = (target: DataQuery, queries: DataQuery[], index = 0): DataQuery => {
|
|
|
|
const key = generateKey(index);
|
|
|
|
const refId = target.refId || getNextRefIdChar(queries);
|
|
|
|
|
|
|
|
return { ...target, refId, key };
|
|
|
|
};
|
|
|
|
|
2018-11-21 07:45:57 -06:00
|
|
|
/**
|
|
|
|
* Ensure at least one target exists and that targets have the necessary keys
|
2022-08-31 09:24:20 -05:00
|
|
|
*
|
|
|
|
* This will return an empty array if there are no datasources, as Explore is not usable in that state
|
2018-11-21 07:45:57 -06:00
|
|
|
*/
|
2022-08-31 09:24:20 -05:00
|
|
|
export async function ensureQueries(
|
|
|
|
queries?: DataQuery[],
|
|
|
|
newQueryDataSourceOverride?: DataSourceRef
|
|
|
|
): Promise<DataQuery[]> {
|
2018-11-21 09:28:30 -06:00
|
|
|
if (queries && typeof queries === 'object' && queries.length > 0) {
|
2019-05-10 07:00:39 -05:00
|
|
|
const allQueries = [];
|
|
|
|
for (let index = 0; index < queries.length; index++) {
|
|
|
|
const query = queries[index];
|
|
|
|
const key = generateKey(index);
|
|
|
|
let refId = query.refId;
|
|
|
|
if (!refId) {
|
|
|
|
refId = getNextRefIdChar(allQueries);
|
|
|
|
}
|
|
|
|
|
2022-09-19 09:43:16 -05:00
|
|
|
// if a query has a datasource, validate it and only add it if valid
|
|
|
|
// if a query doesn't have a datasource, do not worry about it at this step
|
|
|
|
let validDS = true;
|
|
|
|
if (query.datasource) {
|
|
|
|
try {
|
|
|
|
await getDataSourceSrv().get(query.datasource.uid);
|
|
|
|
} catch {
|
|
|
|
console.error(`One of the queries has a datasource that is no longer available and was removed.`);
|
|
|
|
validDS = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validDS) {
|
|
|
|
allQueries.push({
|
|
|
|
...query,
|
|
|
|
refId,
|
|
|
|
key,
|
|
|
|
});
|
|
|
|
}
|
2019-05-10 07:00:39 -05:00
|
|
|
}
|
|
|
|
return allQueries;
|
2018-11-21 07:45:57 -06:00
|
|
|
}
|
2022-08-31 09:24:20 -05:00
|
|
|
try {
|
|
|
|
// if a datasource override get its ref, otherwise get the default datasource
|
|
|
|
const emptyQueryRef = newQueryDataSourceOverride ?? (await getDataSourceSrv().get()).getRef();
|
|
|
|
const emptyQuery = await generateEmptyQuery(queries ?? [], undefined, emptyQueryRef);
|
|
|
|
return [emptyQuery];
|
|
|
|
} catch {
|
|
|
|
// if there are no datasources, return an empty array because we will not allow use of explore
|
|
|
|
// this will occur on init of explore with no datasources defined
|
|
|
|
return [];
|
|
|
|
}
|
2018-11-21 07:45:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-25 08:25:17 -05:00
|
|
|
* A target is non-empty when it has keys (with non-empty values) other than refId, key, context and datasource.
|
|
|
|
* FIXME: While this is reasonable for practical use cases, a query without any propery might still be "non-empty"
|
|
|
|
* in its own scope, for instance when there's no user input needed. This might be the case for an hypothetic datasource in
|
|
|
|
* which query options are only set in its config and the query object itself, as generated from its query editor it's always "empty"
|
2018-11-21 07:45:57 -06:00
|
|
|
*/
|
2021-10-25 08:25:17 -05:00
|
|
|
const validKeys = ['refId', 'key', 'context', 'datasource'];
|
2021-09-15 10:26:23 -05:00
|
|
|
export function hasNonEmptyQuery<TQuery extends DataQuery>(queries: TQuery[]): boolean {
|
2019-01-11 11:26:56 -06:00
|
|
|
return (
|
|
|
|
queries &&
|
2024-01-15 08:29:39 -06:00
|
|
|
queries.some((query) => {
|
|
|
|
const entries = Object.entries(query)
|
|
|
|
.filter(([key, _]) => validKeys.indexOf(key) === -1)
|
|
|
|
.filter(([_, value]) => value);
|
|
|
|
return entries.length > 0;
|
2019-06-04 07:07:25 -05:00
|
|
|
})
|
2018-12-04 04:00:21 -06:00
|
|
|
);
|
2018-11-21 07:45:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the query history. Side-effect: store history in local storage
|
|
|
|
*/
|
2021-09-15 10:26:23 -05:00
|
|
|
export function updateHistory<T extends DataQuery>(
|
2019-01-18 11:59:32 -06:00
|
|
|
history: Array<HistoryItem<T>>,
|
|
|
|
datasourceId: string,
|
|
|
|
queries: T[]
|
|
|
|
): Array<HistoryItem<T>> {
|
2018-11-21 07:45:57 -06:00
|
|
|
const ts = Date.now();
|
2020-04-20 08:13:02 -05:00
|
|
|
let updatedHistory = history;
|
2021-01-20 00:59:48 -06:00
|
|
|
queries.forEach((query) => {
|
2020-04-20 08:13:02 -05:00
|
|
|
updatedHistory = [{ query, ts }, ...updatedHistory];
|
2018-11-21 07:45:57 -06:00
|
|
|
});
|
|
|
|
|
2020-04-20 08:13:02 -05:00
|
|
|
if (updatedHistory.length > MAX_HISTORY_ITEMS) {
|
|
|
|
updatedHistory = updatedHistory.slice(0, MAX_HISTORY_ITEMS);
|
2018-11-21 07:45:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Combine all queries of a datasource type into one history
|
|
|
|
const historyKey = `grafana.explore.history.${datasourceId}`;
|
2020-04-20 08:13:02 -05:00
|
|
|
try {
|
|
|
|
store.setObject(historyKey, updatedHistory);
|
|
|
|
return updatedHistory;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return history;
|
|
|
|
}
|
2018-11-21 07:45:57 -06:00
|
|
|
}
|
2018-11-22 04:02:53 -06:00
|
|
|
|
2022-08-31 09:24:20 -05:00
|
|
|
export const getQueryKeys = (queries: DataQuery[]): string[] => {
|
2020-03-25 06:25:39 -05:00
|
|
|
const queryKeys = queries.reduce<string[]>((newQueryKeys, query, index) => {
|
2022-08-31 09:24:20 -05:00
|
|
|
const primaryKey = query.datasource?.uid || query.key;
|
2019-02-04 06:41:29 -06:00
|
|
|
return newQueryKeys.concat(`${primaryKey}-${index}`);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return queryKeys;
|
|
|
|
};
|
2019-04-29 11:28:41 -05:00
|
|
|
|
2021-09-30 02:40:02 -05:00
|
|
|
export const getTimeRange = (timeZone: TimeZone, rawRange: RawTimeRange, fiscalYearStartMonth: number): TimeRange => {
|
2022-02-03 07:45:29 -06:00
|
|
|
let range = rangeUtil.convertRawToRange(rawRange, timeZone, fiscalYearStartMonth);
|
|
|
|
|
|
|
|
return range;
|
2019-04-29 11:28:41 -05:00
|
|
|
};
|
|
|
|
|
2019-11-26 03:01:32 -06:00
|
|
|
export const refreshIntervalToSortOrder = (refreshInterval?: string) =>
|
2020-08-06 11:35:49 -05:00
|
|
|
RefreshPicker.isLive(refreshInterval) ? LogsSortOrder.Ascending : LogsSortOrder.Descending;
|
2019-06-03 07:54:32 -05:00
|
|
|
|
2020-07-09 08:16:35 -05:00
|
|
|
export const stopQueryState = (querySubscription: Unsubscribable | undefined) => {
|
2019-09-12 10:28:46 -05:00
|
|
|
if (querySubscription) {
|
|
|
|
querySubscription.unsubscribe();
|
2019-08-28 11:24:52 -05:00
|
|
|
}
|
|
|
|
};
|
2019-09-16 05:35:39 -05:00
|
|
|
|
2020-07-09 08:16:35 -05:00
|
|
|
export function getIntervals(range: TimeRange, lowLimit?: string, resolution?: number): IntervalValues {
|
2019-09-16 05:35:39 -05:00
|
|
|
if (!resolution) {
|
|
|
|
return { interval: '1s', intervalMs: 1000 };
|
|
|
|
}
|
|
|
|
|
2020-09-03 01:54:06 -05:00
|
|
|
return rangeUtil.calculateInterval(range, resolution, lowLimit);
|
2019-09-16 05:35:39 -05:00
|
|
|
}
|
2020-01-17 07:28:00 -06:00
|
|
|
|
2020-10-14 10:08:27 -05:00
|
|
|
export const copyStringToClipboard = (string: string) => {
|
2023-12-12 09:55:03 -06:00
|
|
|
if (navigator.clipboard && window.isSecureContext) {
|
|
|
|
navigator.clipboard.writeText(string);
|
|
|
|
} else {
|
|
|
|
const el = document.createElement('textarea');
|
|
|
|
el.value = string;
|
|
|
|
document.body.appendChild(el);
|
|
|
|
el.select();
|
|
|
|
document.execCommand('copy');
|
|
|
|
document.body.removeChild(el);
|
|
|
|
}
|
2020-10-14 10:08:27 -05:00
|
|
|
};
|