PanelQueryState.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Libraries
  2. import { isArray, isEqual, isString } from 'lodash';
  3. // Utils & Services
  4. import { getBackendSrv } from 'app/core/services/backend_srv';
  5. import { dateMath } from '@grafana/data';
  6. import {
  7. guessFieldTypes,
  8. LoadingState,
  9. toLegacyResponseData,
  10. DataFrame,
  11. toDataFrame,
  12. isDataFrame,
  13. } from '@grafana/data';
  14. // Types
  15. import {
  16. DataSourceApi,
  17. DataQueryRequest,
  18. PanelData,
  19. DataQueryError,
  20. DataStreamObserver,
  21. DataStreamState,
  22. DataQueryResponseData,
  23. } from '@grafana/ui';
  24. export class PanelQueryState {
  25. // The current/last running request
  26. request = {
  27. startTime: 0,
  28. endTime: 1000, // Somethign not zero
  29. } as DataQueryRequest;
  30. // The result back from the datasource query
  31. response = {
  32. state: LoadingState.NotStarted,
  33. series: [],
  34. } as PanelData;
  35. // Active stream results
  36. streams: DataStreamState[] = [];
  37. sendFrames = false;
  38. sendLegacy = false;
  39. // A promise for the running query
  40. private executor?: Promise<PanelData> = null;
  41. private rejector = (reason?: any) => {};
  42. private datasource: DataSourceApi = {} as any;
  43. isFinished(state: LoadingState) {
  44. return state === LoadingState.Done || state === LoadingState.Error;
  45. }
  46. isStarted() {
  47. return this.response.state !== LoadingState.NotStarted;
  48. }
  49. isSameQuery(ds: DataSourceApi, req: DataQueryRequest) {
  50. if (ds !== this.datasource) {
  51. return false;
  52. }
  53. // For now just check that the targets look the same
  54. return isEqual(this.request.targets, req.targets);
  55. }
  56. /**
  57. * Return the currently running query
  58. */
  59. getActiveRunner(): Promise<PanelData> | undefined {
  60. return this.executor;
  61. }
  62. cancel(reason: string) {
  63. const { request } = this;
  64. this.executor = null;
  65. try {
  66. // If no endTime the call to datasource.query did not complete
  67. // call rejector to reject the executor promise
  68. if (!request.endTime) {
  69. request.endTime = Date.now();
  70. this.rejector('Canceled:' + reason);
  71. }
  72. // Cancel any open HTTP request with the same ID
  73. if (request.requestId) {
  74. getBackendSrv().resolveCancelerIfExists(request.requestId);
  75. }
  76. } catch (err) {
  77. console.log('Error canceling request', err);
  78. }
  79. // Close any open streams
  80. this.closeStreams(true);
  81. }
  82. execute(ds: DataSourceApi, req: DataQueryRequest): Promise<PanelData> {
  83. this.request = req;
  84. this.datasource = ds;
  85. // Return early if there are no queries to run
  86. if (!req.targets.length) {
  87. console.log('No queries, so return early');
  88. this.request.endTime = Date.now();
  89. this.closeStreams();
  90. return Promise.resolve(
  91. (this.response = {
  92. state: LoadingState.Done,
  93. series: [], // Clear the data
  94. legacy: [],
  95. })
  96. );
  97. }
  98. // Set the loading state immediatly
  99. this.response.state = LoadingState.Loading;
  100. this.executor = new Promise<PanelData>((resolve, reject) => {
  101. this.rejector = reject;
  102. return ds
  103. .query(this.request, this.dataStreamObserver)
  104. .then(resp => {
  105. if (!isArray(resp.data)) {
  106. throw new Error(`Expected response data to be array, got ${typeof resp.data}.`);
  107. }
  108. this.request.endTime = Date.now();
  109. this.executor = null;
  110. // Make sure we send something back -- called run() w/o subscribe!
  111. if (!(this.sendFrames || this.sendLegacy)) {
  112. this.sendFrames = true;
  113. }
  114. // Save the result state
  115. this.response = {
  116. state: LoadingState.Done,
  117. request: this.request,
  118. series: this.sendFrames ? getProcessedDataFrames(resp.data) : [],
  119. legacy: this.sendLegacy ? translateToLegacyData(resp.data) : undefined,
  120. };
  121. resolve(this.validateStreamsAndGetPanelData());
  122. })
  123. .catch(err => {
  124. this.executor = null;
  125. resolve(this.setError(err));
  126. });
  127. });
  128. return this.executor;
  129. }
  130. // Send a notice when the stream has updated the current model
  131. onStreamingDataUpdated: () => void;
  132. // This gets all stream events and keeps track of them
  133. // it will then delegate real changes to the PanelQueryRunner
  134. dataStreamObserver: DataStreamObserver = (stream: DataStreamState) => {
  135. // Streams only work with the 'series' format
  136. this.sendFrames = true;
  137. // Add the stream to our list
  138. let found = false;
  139. const active = this.streams.map(s => {
  140. if (s.key === stream.key) {
  141. found = true;
  142. return stream;
  143. }
  144. return s;
  145. });
  146. if (!found) {
  147. if (shouldDisconnect(this.request, stream)) {
  148. console.log('Got stream update from old stream, unsubscribing');
  149. stream.unsubscribe();
  150. return;
  151. }
  152. active.push(stream);
  153. }
  154. this.streams = active;
  155. this.onStreamingDataUpdated();
  156. };
  157. closeStreams(keepSeries = false) {
  158. if (!this.streams.length) {
  159. return;
  160. }
  161. const series: DataFrame[] = [];
  162. for (const stream of this.streams) {
  163. if (stream.data) {
  164. series.push.apply(series, stream.data);
  165. }
  166. try {
  167. stream.unsubscribe();
  168. } catch {
  169. console.log('Failed to unsubscribe to stream');
  170. }
  171. }
  172. this.streams = [];
  173. // Move the series from streams to the resposne
  174. if (keepSeries) {
  175. const { response } = this;
  176. this.response = {
  177. ...response,
  178. series: [
  179. ...response.series,
  180. ...series, // Append the streamed series
  181. ],
  182. };
  183. }
  184. }
  185. /**
  186. * This is called before broadcasting data to listeners. Given that
  187. * stream events can happen at any point, we need to make sure to
  188. * only return data from active streams.
  189. */
  190. validateStreamsAndGetPanelData(): PanelData {
  191. const { response, streams, request } = this;
  192. // When not streaming, return the response + request
  193. if (!streams.length) {
  194. return {
  195. ...response,
  196. request: request,
  197. };
  198. }
  199. let done = this.isFinished(response.state);
  200. const series = [...response.series];
  201. const active: DataStreamState[] = [];
  202. for (const stream of this.streams) {
  203. if (shouldDisconnect(request, stream)) {
  204. console.log('getPanelData() - shouldDisconnect true, unsubscribing to steam');
  205. stream.unsubscribe();
  206. continue;
  207. }
  208. active.push(stream);
  209. series.push.apply(series, stream.data);
  210. if (!this.isFinished(stream.state)) {
  211. done = false;
  212. }
  213. }
  214. this.streams = active;
  215. // Update the time range
  216. let timeRange = this.request.range;
  217. if (isString(timeRange.raw.from)) {
  218. timeRange = {
  219. from: dateMath.parse(timeRange.raw.from, false),
  220. to: dateMath.parse(timeRange.raw.to, true),
  221. raw: timeRange.raw,
  222. };
  223. }
  224. return {
  225. state: done ? LoadingState.Done : LoadingState.Streaming,
  226. series, // Union of series from response and all streams
  227. legacy: this.sendLegacy ? translateToLegacyData(series) : undefined,
  228. request: {
  229. ...this.request,
  230. range: timeRange, // update the time range
  231. },
  232. };
  233. }
  234. /**
  235. * Make sure all requested formats exist on the data
  236. */
  237. getDataAfterCheckingFormats(): PanelData {
  238. const { response, sendLegacy, sendFrames } = this;
  239. if (sendLegacy && (!response.legacy || !response.legacy.length)) {
  240. response.legacy = response.series.map(v => toLegacyResponseData(v));
  241. }
  242. if (sendFrames && !response.series.length && response.legacy) {
  243. response.series = response.legacy.map(v => toDataFrame(v));
  244. }
  245. return this.validateStreamsAndGetPanelData();
  246. }
  247. setError(err: any): PanelData {
  248. if (!this.request.endTime) {
  249. this.request.endTime = Date.now();
  250. }
  251. this.closeStreams(true);
  252. this.response = {
  253. ...this.response, // Keep any existing data
  254. state: LoadingState.Error,
  255. error: toDataQueryError(err),
  256. };
  257. return this.validateStreamsAndGetPanelData();
  258. }
  259. }
  260. export function shouldDisconnect(source: DataQueryRequest, state: DataStreamState) {
  261. // It came from the same the same request, so keep it
  262. if (source === state.request || state.request.requestId.startsWith(source.requestId)) {
  263. return false;
  264. }
  265. // We should be able to check that it is the same query regardless of
  266. // if it came from the same request. This will be important for #16676
  267. return true;
  268. }
  269. export function toDataQueryError(err: any): DataQueryError {
  270. const error = (err || {}) as DataQueryError;
  271. if (!error.message) {
  272. if (typeof err === 'string' || err instanceof String) {
  273. return { message: err } as DataQueryError;
  274. }
  275. let message = 'Query error';
  276. if (error.message) {
  277. message = error.message;
  278. } else if (error.data && error.data.message) {
  279. message = error.data.message;
  280. } else if (error.data && error.data.error) {
  281. message = error.data.error;
  282. } else if (error.status) {
  283. message = `Query error: ${error.status} ${error.statusText}`;
  284. }
  285. error.message = message;
  286. }
  287. return error;
  288. }
  289. function translateToLegacyData(data: DataQueryResponseData) {
  290. return data.map((v: any) => {
  291. if (isDataFrame(v)) {
  292. return toLegacyResponseData(v);
  293. }
  294. return v;
  295. });
  296. }
  297. /**
  298. * All panels will be passed tables that have our best guess at colum type set
  299. *
  300. * This is also used by PanelChrome for snapshot support
  301. */
  302. export function getProcessedDataFrames(results?: DataQueryResponseData[]): DataFrame[] {
  303. if (!isArray(results)) {
  304. return [];
  305. }
  306. const series: DataFrame[] = [];
  307. for (const r of results) {
  308. if (r) {
  309. series.push(guessFieldTypes(toDataFrame(r)));
  310. }
  311. }
  312. return series;
  313. }