explore.ts 10 KB

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