explore.ts 11 KB

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