explore.ts 8.6 KB

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