PanelQueryState.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // Libraries
  2. import { isArray, isEqual, isString } from 'lodash';
  3. // Utils & Services
  4. import { getBackendSrv } from 'app/core/services/backend_srv';
  5. import {
  6. dateMath,
  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. if (stream.state === LoadingState.Error) {
  138. this.setError(stream.error);
  139. this.onStreamingDataUpdated();
  140. return;
  141. }
  142. // Add the stream to our list
  143. let found = false;
  144. const active = this.streams.map(s => {
  145. if (s.key === stream.key) {
  146. found = true;
  147. return stream;
  148. }
  149. return s;
  150. });
  151. if (!found) {
  152. if (shouldDisconnect(this.request, stream)) {
  153. console.log('Got stream update from old stream, unsubscribing');
  154. stream.unsubscribe();
  155. return;
  156. }
  157. active.push(stream);
  158. }
  159. this.streams = active;
  160. this.onStreamingDataUpdated();
  161. };
  162. closeStreams(keepSeries = false) {
  163. if (!this.streams.length) {
  164. return;
  165. }
  166. const series: DataFrame[] = [];
  167. for (const stream of this.streams) {
  168. if (stream.data) {
  169. series.push.apply(series, stream.data);
  170. }
  171. try {
  172. stream.unsubscribe();
  173. } catch {
  174. console.log('Failed to unsubscribe to stream');
  175. }
  176. }
  177. this.streams = [];
  178. // Move the series from streams to the resposne
  179. if (keepSeries) {
  180. const { response } = this;
  181. this.response = {
  182. ...response,
  183. series: [
  184. ...response.series,
  185. ...series, // Append the streamed series
  186. ],
  187. };
  188. }
  189. }
  190. /**
  191. * This is called before broadcasting data to listeners. Given that
  192. * stream events can happen at any point, we need to make sure to
  193. * only return data from active streams.
  194. */
  195. validateStreamsAndGetPanelData(): PanelData {
  196. const { response, streams, request } = this;
  197. // When not streaming, return the response + request
  198. if (!streams.length) {
  199. return {
  200. ...response,
  201. request: request,
  202. };
  203. }
  204. let done = this.isFinished(response.state);
  205. const series = [...response.series];
  206. const active: DataStreamState[] = [];
  207. for (const stream of this.streams) {
  208. if (shouldDisconnect(request, stream)) {
  209. console.log('getPanelData() - shouldDisconnect true, unsubscribing to steam');
  210. stream.unsubscribe();
  211. continue;
  212. }
  213. active.push(stream);
  214. series.push.apply(series, stream.data);
  215. if (!this.isFinished(stream.state)) {
  216. done = false;
  217. }
  218. }
  219. this.streams = active;
  220. // Update the time range
  221. let timeRange = this.request.range;
  222. if (isString(timeRange.raw.from)) {
  223. timeRange = {
  224. from: dateMath.parse(timeRange.raw.from, false),
  225. to: dateMath.parse(timeRange.raw.to, true),
  226. raw: timeRange.raw,
  227. };
  228. }
  229. return {
  230. state: done ? LoadingState.Done : LoadingState.Streaming,
  231. series, // Union of series from response and all streams
  232. legacy: this.sendLegacy ? translateToLegacyData(series) : undefined,
  233. request: {
  234. ...this.request,
  235. range: timeRange, // update the time range
  236. },
  237. };
  238. }
  239. /**
  240. * Make sure all requested formats exist on the data
  241. */
  242. getDataAfterCheckingFormats(): PanelData {
  243. const { response, sendLegacy, sendFrames } = this;
  244. if (sendLegacy && (!response.legacy || !response.legacy.length)) {
  245. response.legacy = response.series.map(v => toLegacyResponseData(v));
  246. }
  247. if (sendFrames && !response.series.length && response.legacy) {
  248. response.series = response.legacy.map(v => toDataFrame(v));
  249. }
  250. return this.validateStreamsAndGetPanelData();
  251. }
  252. setError(err: any): PanelData {
  253. if (!this.request.endTime) {
  254. this.request.endTime = Date.now();
  255. }
  256. this.closeStreams(true);
  257. this.response = {
  258. ...this.response, // Keep any existing data
  259. state: LoadingState.Error,
  260. error: toDataQueryError(err),
  261. };
  262. return this.validateStreamsAndGetPanelData();
  263. }
  264. }
  265. export function shouldDisconnect(source: DataQueryRequest, state: DataStreamState) {
  266. // It came from the same the same request, so keep it
  267. if (source === state.request || state.request.requestId.startsWith(source.requestId)) {
  268. return false;
  269. }
  270. // We should be able to check that it is the same query regardless of
  271. // if it came from the same request. This will be important for #16676
  272. return true;
  273. }
  274. export function toDataQueryError(err: any): DataQueryError {
  275. const error = (err || {}) as DataQueryError;
  276. if (!error.message) {
  277. if (typeof err === 'string' || err instanceof String) {
  278. return { message: err } as DataQueryError;
  279. }
  280. let message = 'Query error';
  281. if (error.message) {
  282. message = error.message;
  283. } else if (error.data && error.data.message) {
  284. message = error.data.message;
  285. } else if (error.data && error.data.error) {
  286. message = error.data.error;
  287. } else if (error.status) {
  288. message = `Query error: ${error.status} ${error.statusText}`;
  289. }
  290. error.message = message;
  291. }
  292. return error;
  293. }
  294. function translateToLegacyData(data: DataQueryResponseData) {
  295. return data.map((v: any) => {
  296. if (isDataFrame(v)) {
  297. return toLegacyResponseData(v);
  298. }
  299. return v;
  300. });
  301. }
  302. /**
  303. * All panels will be passed tables that have our best guess at colum type set
  304. *
  305. * This is also used by PanelChrome for snapshot support
  306. */
  307. export function getProcessedDataFrames(results?: DataQueryResponseData[]): DataFrame[] {
  308. if (!isArray(results)) {
  309. return [];
  310. }
  311. const series: DataFrame[] = [];
  312. for (const r of results) {
  313. if (r) {
  314. series.push(guessFieldTypes(toDataFrame(r)));
  315. }
  316. }
  317. return series;
  318. }