explore.ts 9.8 KB

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