explore.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Libraries
  2. import _ from 'lodash';
  3. // Services & Utils
  4. import * as dateMath from 'app/core/utils/datemath';
  5. import { renderUrl } from 'app/core/utils/url';
  6. import kbn from 'app/core/utils/kbn';
  7. import store from 'app/core/store';
  8. import { parse as parseDate } from 'app/core/utils/datemath';
  9. import { colors } from '@grafana/ui';
  10. import TableModel, { mergeTablesIntoModel } from 'app/core/table_model';
  11. // Types
  12. import { RawTimeRange, IntervalValues, DataQuery, DataSourceApi } from '@grafana/ui/src/types';
  13. import TimeSeries from 'app/core/time_series2';
  14. import {
  15. ExploreUrlState,
  16. HistoryItem,
  17. QueryTransaction,
  18. ResultType,
  19. QueryIntervals,
  20. QueryOptions,
  21. } from 'app/types/explore';
  22. import { LogsDedupStrategy } from 'app/core/logs_model';
  23. export const DEFAULT_RANGE = {
  24. from: 'now-6h',
  25. to: 'now',
  26. };
  27. export const DEFAULT_UI_STATE = {
  28. showingTable: true,
  29. showingGraph: true,
  30. showingLogs: true,
  31. dedupStrategy: LogsDedupStrategy.none,
  32. };
  33. const MAX_HISTORY_ITEMS = 100;
  34. export const LAST_USED_DATASOURCE_KEY = 'grafana.explore.datasource';
  35. /**
  36. * Returns an Explore-URL that contains a panel's queries and the dashboard time range.
  37. *
  38. * @param panel Origin panel of the jump to Explore
  39. * @param panelTargets The origin panel's query targets
  40. * @param panelDatasource The origin panel's datasource
  41. * @param datasourceSrv Datasource service to query other datasources in case the panel datasource is mixed
  42. * @param timeSrv Time service to get the current dashboard range from
  43. */
  44. export async function getExploreUrl(
  45. panel: any,
  46. panelTargets: any[],
  47. panelDatasource: any,
  48. datasourceSrv: any,
  49. timeSrv: any
  50. ) {
  51. let exploreDatasource = panelDatasource;
  52. let exploreTargets: DataQuery[] = panelTargets;
  53. let url;
  54. // Mixed datasources need to choose only one datasource
  55. if (panelDatasource.meta.id === 'mixed' && panelTargets) {
  56. // Find first explore datasource among targets
  57. let mixedExploreDatasource;
  58. for (const t of panel.targets) {
  59. const datasource = await datasourceSrv.get(t.datasource);
  60. if (datasource && datasource.meta.explore) {
  61. mixedExploreDatasource = datasource;
  62. break;
  63. }
  64. }
  65. // Add all its targets
  66. if (mixedExploreDatasource) {
  67. exploreDatasource = mixedExploreDatasource;
  68. exploreTargets = panelTargets.filter(t => t.datasource === mixedExploreDatasource.name);
  69. }
  70. }
  71. if (panelDatasource) {
  72. const range = timeSrv.timeRangeForUrl();
  73. let state: Partial<ExploreUrlState> = { range };
  74. if (exploreDatasource.getExploreState) {
  75. state = { ...state, ...exploreDatasource.getExploreState(exploreTargets) };
  76. } else {
  77. state = {
  78. ...state,
  79. datasource: panelDatasource.name,
  80. queries: exploreTargets.map(t => ({ ...t, datasource: panelDatasource.name })),
  81. };
  82. }
  83. const exploreState = JSON.stringify(state);
  84. url = renderUrl('/explore', { left: exploreState });
  85. }
  86. return url;
  87. }
  88. export function buildQueryTransaction(
  89. query: DataQuery,
  90. rowIndex: number,
  91. resultType: ResultType,
  92. queryOptions: QueryOptions,
  93. range: RawTimeRange,
  94. queryIntervals: QueryIntervals,
  95. scanning: boolean
  96. ): QueryTransaction {
  97. const { interval, intervalMs } = queryIntervals;
  98. const configuredQueries = [
  99. {
  100. ...query,
  101. ...queryOptions,
  102. },
  103. ];
  104. // Clone range for query request
  105. // const queryRange: RawTimeRange = { ...range };
  106. // const { from, to, raw } = this.timeSrv.timeRange();
  107. // Most datasource is using `panelId + query.refId` for cancellation logic.
  108. // Using `format` here because it relates to the view panel that the request is for.
  109. // However, some datasources don't use `panelId + query.refId`, but only `panelId`.
  110. // Therefore panel id has to be unique.
  111. const panelId = `${queryOptions.format}-${query.key}`;
  112. const options = {
  113. interval,
  114. intervalMs,
  115. panelId,
  116. targets: configuredQueries, // Datasources rely on DataQueries being passed under the targets key.
  117. range: {
  118. from: dateMath.parse(range.from, false),
  119. to: dateMath.parse(range.to, true),
  120. raw: range,
  121. },
  122. rangeRaw: range,
  123. scopedVars: {
  124. __interval: { text: interval, value: interval },
  125. __interval_ms: { text: intervalMs, value: intervalMs },
  126. },
  127. };
  128. return {
  129. options,
  130. query,
  131. resultType,
  132. rowIndex,
  133. scanning,
  134. id: generateKey(), // reusing for unique ID
  135. done: false,
  136. latency: 0,
  137. };
  138. }
  139. export const clearQueryKeys: ((query: DataQuery) => object) = ({ key, refId, ...rest }) => rest;
  140. const isMetricSegment = (segment: { [key: string]: string }) => segment.hasOwnProperty('expr');
  141. const isUISegment = (segment: { [key: string]: string }) => segment.hasOwnProperty('ui');
  142. export function parseUrlState(initial: string | undefined): ExploreUrlState {
  143. let uiState = DEFAULT_UI_STATE;
  144. if (initial) {
  145. try {
  146. const parsed = JSON.parse(decodeURI(initial));
  147. if (Array.isArray(parsed)) {
  148. if (parsed.length <= 3) {
  149. throw new Error('Error parsing compact URL state for Explore.');
  150. }
  151. const range = {
  152. from: parsed[0],
  153. to: parsed[1],
  154. };
  155. const datasource = parsed[2];
  156. let queries = [];
  157. parsed.slice(3).forEach(segment => {
  158. if (isMetricSegment(segment)) {
  159. queries = [...queries, segment];
  160. }
  161. if (isUISegment(segment)) {
  162. uiState = {
  163. showingGraph: segment.ui[0],
  164. showingLogs: segment.ui[1],
  165. showingTable: segment.ui[2],
  166. dedupStrategy: segment.ui[3],
  167. };
  168. }
  169. });
  170. return { datasource, queries, range, ui: uiState };
  171. }
  172. return parsed;
  173. } catch (e) {
  174. console.error(e);
  175. }
  176. }
  177. return { datasource: null, queries: [], range: DEFAULT_RANGE, ui: uiState };
  178. }
  179. export function serializeStateToUrlParam(urlState: ExploreUrlState, compact?: boolean): string {
  180. if (compact) {
  181. return JSON.stringify([
  182. urlState.range.from,
  183. urlState.range.to,
  184. urlState.datasource,
  185. ...urlState.queries,
  186. { ui: [!!urlState.ui.showingGraph, !!urlState.ui.showingLogs, !!urlState.ui.showingTable, urlState.ui.dedupStrategy] },
  187. ]);
  188. }
  189. return JSON.stringify(urlState);
  190. }
  191. export function generateKey(index = 0): string {
  192. return `Q-${Date.now()}-${Math.random()}-${index}`;
  193. }
  194. export function generateRefId(index = 0): string {
  195. return `${index + 1}`;
  196. }
  197. export function generateEmptyQuery(index = 0): { refId: string; key: string } {
  198. return { refId: generateRefId(index), key: generateKey(index) };
  199. }
  200. /**
  201. * Ensure at least one target exists and that targets have the necessary keys
  202. */
  203. export function ensureQueries(queries?: DataQuery[]): DataQuery[] {
  204. if (queries && typeof queries === 'object' && queries.length > 0) {
  205. return queries.map((query, i) => ({ ...query, ...generateEmptyQuery(i) }));
  206. }
  207. return [{ ...generateEmptyQuery() }];
  208. }
  209. /**
  210. * A target is non-empty when it has keys (with non-empty values) other than refId and key.
  211. */
  212. export function hasNonEmptyQuery<TQuery extends DataQuery = any>(queries: TQuery[]): boolean {
  213. return (
  214. queries &&
  215. queries.some(
  216. query =>
  217. Object.keys(query)
  218. .map(k => query[k])
  219. .filter(v => v).length > 2
  220. )
  221. );
  222. }
  223. export function calculateResultsFromQueryTransactions(
  224. queryTransactions: QueryTransaction[],
  225. datasource: any,
  226. graphInterval: number
  227. ) {
  228. const graphResult = _.flatten(
  229. queryTransactions.filter(qt => qt.resultType === 'Graph' && qt.done && qt.result).map(qt => qt.result)
  230. );
  231. const tableResult = mergeTablesIntoModel(
  232. new TableModel(),
  233. ...queryTransactions
  234. .filter(qt => qt.resultType === 'Table' && qt.done && qt.result && qt.result.columns && qt.result.rows)
  235. .map(qt => qt.result)
  236. );
  237. const logsResult =
  238. datasource && datasource.mergeStreams
  239. ? datasource.mergeStreams(
  240. _.flatten(
  241. queryTransactions.filter(qt => qt.resultType === 'Logs' && qt.done && qt.result).map(qt => qt.result)
  242. ),
  243. graphInterval
  244. )
  245. : undefined;
  246. return {
  247. graphResult,
  248. tableResult,
  249. logsResult,
  250. };
  251. }
  252. export function getIntervals(range: RawTimeRange, lowLimit: string, resolution: number): IntervalValues {
  253. if (!resolution) {
  254. return { interval: '1s', intervalMs: 1000 };
  255. }
  256. const absoluteRange: RawTimeRange = {
  257. from: parseDate(range.from, false),
  258. to: parseDate(range.to, true),
  259. };
  260. return kbn.calculateInterval(absoluteRange, resolution, lowLimit);
  261. }
  262. export function makeTimeSeriesList(dataList) {
  263. return dataList.map((seriesData, index) => {
  264. const datapoints = seriesData.datapoints || [];
  265. const alias = seriesData.target;
  266. const colorIndex = index % colors.length;
  267. const color = colors[colorIndex];
  268. const series = new TimeSeries({
  269. datapoints,
  270. alias,
  271. color,
  272. unit: seriesData.unit,
  273. });
  274. return series;
  275. });
  276. }
  277. /**
  278. * Update the query history. Side-effect: store history in local storage
  279. */
  280. export function updateHistory<T extends DataQuery = any>(
  281. history: Array<HistoryItem<T>>,
  282. datasourceId: string,
  283. queries: T[]
  284. ): Array<HistoryItem<T>> {
  285. const ts = Date.now();
  286. queries.forEach(query => {
  287. history = [{ query, ts }, ...history];
  288. });
  289. if (history.length > MAX_HISTORY_ITEMS) {
  290. history = history.slice(0, MAX_HISTORY_ITEMS);
  291. }
  292. // Combine all queries of a datasource type into one history
  293. const historyKey = `grafana.explore.history.${datasourceId}`;
  294. store.setObject(historyKey, history);
  295. return history;
  296. }
  297. export function clearHistory(datasourceId: string) {
  298. const historyKey = `grafana.explore.history.${datasourceId}`;
  299. store.delete(historyKey);
  300. }
  301. export const getQueryKeys = (queries: DataQuery[], datasourceInstance: DataSourceApi): string[] => {
  302. const queryKeys = queries.reduce((newQueryKeys, query, index) => {
  303. const primaryKey = datasourceInstance && datasourceInstance.name ? datasourceInstance.name : query.key;
  304. return newQueryKeys.concat(`${primaryKey}-${index}`);
  305. }, []);
  306. return queryKeys;
  307. };