explore.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. import {
  2. DEFAULT_RANGE,
  3. serializeStateToUrlParam,
  4. parseUrlState,
  5. updateHistory,
  6. clearHistory,
  7. hasNonEmptyQuery,
  8. instanceOfDataQueryError,
  9. getValueWithRefId,
  10. getFirstQueryErrorWithoutRefId,
  11. getRefIds,
  12. refreshIntervalToSortOrder,
  13. SortOrder,
  14. sortLogsResult,
  15. } from './explore';
  16. import { ExploreUrlState, ExploreMode } from 'app/types/explore';
  17. import store from 'app/core/store';
  18. import { LogsDedupStrategy, LogsModel, LogLevel } from '@grafana/data';
  19. import { DataQueryError } from '@grafana/ui';
  20. import { liveOption, offOption } from '@grafana/ui/src/components/RefreshPicker/RefreshPicker';
  21. const DEFAULT_EXPLORE_STATE: ExploreUrlState = {
  22. datasource: null,
  23. queries: [],
  24. range: DEFAULT_RANGE,
  25. mode: ExploreMode.Metrics,
  26. ui: {
  27. showingGraph: true,
  28. showingTable: true,
  29. showingLogs: true,
  30. dedupStrategy: LogsDedupStrategy.none,
  31. },
  32. };
  33. describe('state functions', () => {
  34. describe('parseUrlState', () => {
  35. it('returns default state on empty string', () => {
  36. expect(parseUrlState('')).toMatchObject({
  37. datasource: null,
  38. queries: [],
  39. range: DEFAULT_RANGE,
  40. });
  41. });
  42. it('returns a valid Explore state from URL parameter', () => {
  43. const paramValue =
  44. '%7B"datasource":"Local","queries":%5B%7B"expr":"metric"%7D%5D,"range":%7B"from":"now-1h","to":"now"%7D%7D';
  45. expect(parseUrlState(paramValue)).toMatchObject({
  46. datasource: 'Local',
  47. queries: [{ expr: 'metric' }],
  48. range: {
  49. from: 'now-1h',
  50. to: 'now',
  51. },
  52. });
  53. });
  54. it('returns a valid Explore state from a compact URL parameter', () => {
  55. const paramValue = '%5B"now-1h","now","Local","5m",%7B"expr":"metric"%7D,"ui"%5D';
  56. expect(parseUrlState(paramValue)).toMatchObject({
  57. datasource: 'Local',
  58. queries: [{ expr: 'metric' }],
  59. range: {
  60. from: 'now-1h',
  61. to: 'now',
  62. },
  63. });
  64. });
  65. });
  66. describe('serializeStateToUrlParam', () => {
  67. it('returns url parameter value for a state object', () => {
  68. const state = {
  69. ...DEFAULT_EXPLORE_STATE,
  70. datasource: 'foo',
  71. queries: [
  72. {
  73. expr: 'metric{test="a/b"}',
  74. },
  75. {
  76. expr: 'super{foo="x/z"}',
  77. },
  78. ],
  79. range: {
  80. from: 'now-5h',
  81. to: 'now',
  82. },
  83. };
  84. expect(serializeStateToUrlParam(state)).toBe(
  85. '{"datasource":"foo","queries":[{"expr":"metric{test=\\"a/b\\"}"},' +
  86. '{"expr":"super{foo=\\"x/z\\"}"}],"range":{"from":"now-5h","to":"now"},' +
  87. '"mode":"Metrics",' +
  88. '"ui":{"showingGraph":true,"showingTable":true,"showingLogs":true,"dedupStrategy":"none"}}'
  89. );
  90. });
  91. it('returns url parameter value for a state object', () => {
  92. const state = {
  93. ...DEFAULT_EXPLORE_STATE,
  94. datasource: 'foo',
  95. queries: [
  96. {
  97. expr: 'metric{test="a/b"}',
  98. },
  99. {
  100. expr: 'super{foo="x/z"}',
  101. },
  102. ],
  103. range: {
  104. from: 'now-5h',
  105. to: 'now',
  106. },
  107. };
  108. expect(serializeStateToUrlParam(state, true)).toBe(
  109. '["now-5h","now","foo",{"expr":"metric{test=\\"a/b\\"}"},{"expr":"super{foo=\\"x/z\\"}"},{"mode":"Metrics"},{"ui":[true,true,true,"none"]}]'
  110. );
  111. });
  112. });
  113. describe('interplay', () => {
  114. it('can parse the serialized state into the original state', () => {
  115. const state = {
  116. ...DEFAULT_EXPLORE_STATE,
  117. datasource: 'foo',
  118. queries: [
  119. {
  120. expr: 'metric{test="a/b"}',
  121. },
  122. {
  123. expr: 'super{foo="x/z"}',
  124. },
  125. ],
  126. range: {
  127. from: 'now - 5h',
  128. to: 'now',
  129. },
  130. };
  131. const serialized = serializeStateToUrlParam(state);
  132. const parsed = parseUrlState(serialized);
  133. expect(state).toMatchObject(parsed);
  134. });
  135. it('can parse the compact serialized state into the original state', () => {
  136. const state = {
  137. ...DEFAULT_EXPLORE_STATE,
  138. datasource: 'foo',
  139. queries: [
  140. {
  141. expr: 'metric{test="a/b"}',
  142. },
  143. {
  144. expr: 'super{foo="x/z"}',
  145. },
  146. ],
  147. range: {
  148. from: 'now - 5h',
  149. to: 'now',
  150. },
  151. };
  152. const serialized = serializeStateToUrlParam(state, true);
  153. const parsed = parseUrlState(serialized);
  154. expect(state).toMatchObject(parsed);
  155. });
  156. });
  157. });
  158. describe('updateHistory()', () => {
  159. const datasourceId = 'myDatasource';
  160. const key = `grafana.explore.history.${datasourceId}`;
  161. beforeEach(() => {
  162. clearHistory(datasourceId);
  163. expect(store.exists(key)).toBeFalsy();
  164. });
  165. test('should save history item to localStorage', () => {
  166. const expected = [
  167. {
  168. query: { refId: '1', expr: 'metric' },
  169. },
  170. ];
  171. expect(updateHistory([], datasourceId, [{ refId: '1', expr: 'metric' }])).toMatchObject(expected);
  172. expect(store.exists(key)).toBeTruthy();
  173. expect(store.getObject(key)).toMatchObject(expected);
  174. });
  175. });
  176. describe('hasNonEmptyQuery', () => {
  177. test('should return true if one query is non-empty', () => {
  178. expect(hasNonEmptyQuery([{ refId: '1', key: '2', context: 'explore', expr: 'foo' }])).toBeTruthy();
  179. });
  180. test('should return false if query is empty', () => {
  181. expect(hasNonEmptyQuery([{ refId: '1', key: '2', context: 'panel' }])).toBeFalsy();
  182. });
  183. test('should return false if no queries exist', () => {
  184. expect(hasNonEmptyQuery([])).toBeFalsy();
  185. });
  186. });
  187. describe('instanceOfDataQueryError', () => {
  188. describe('when called with a DataQueryError', () => {
  189. it('then it should return true', () => {
  190. const error: DataQueryError = {
  191. message: 'A message',
  192. status: '200',
  193. statusText: 'Ok',
  194. };
  195. const result = instanceOfDataQueryError(error);
  196. expect(result).toBe(true);
  197. });
  198. });
  199. describe('when called with a non DataQueryError', () => {
  200. it('then it should return false', () => {
  201. const error = {};
  202. const result = instanceOfDataQueryError(error);
  203. expect(result).toBe(false);
  204. });
  205. });
  206. });
  207. describe('hasRefId', () => {
  208. describe('when called with a null value', () => {
  209. it('then it should return null', () => {
  210. const input: any = null;
  211. const result = getValueWithRefId(input);
  212. expect(result).toBeNull();
  213. });
  214. });
  215. describe('when called with a non object value', () => {
  216. it('then it should return null', () => {
  217. const input = 123;
  218. const result = getValueWithRefId(input);
  219. expect(result).toBeNull();
  220. });
  221. });
  222. describe('when called with an object that has refId', () => {
  223. it('then it should return the object', () => {
  224. const input = { refId: 'A' };
  225. const result = getValueWithRefId(input);
  226. expect(result).toBe(input);
  227. });
  228. });
  229. describe('when called with an array that has refId', () => {
  230. it('then it should return the object', () => {
  231. const input = [123, null, {}, { refId: 'A' }];
  232. const result = getValueWithRefId(input);
  233. expect(result).toBe(input[3]);
  234. });
  235. });
  236. describe('when called with an object that has refId somewhere in the object tree', () => {
  237. it('then it should return the object', () => {
  238. const input: any = { data: [123, null, {}, { series: [123, null, {}, { refId: 'A' }] }] };
  239. const result = getValueWithRefId(input);
  240. expect(result).toBe(input.data[3].series[3]);
  241. });
  242. });
  243. });
  244. describe('getFirstQueryErrorWithoutRefId', () => {
  245. describe('when called with a null value', () => {
  246. it('then it should return null', () => {
  247. const errors: DataQueryError[] = null;
  248. const result = getFirstQueryErrorWithoutRefId(errors);
  249. expect(result).toBeNull();
  250. });
  251. });
  252. describe('when called with an array with only refIds', () => {
  253. it('then it should return undefined', () => {
  254. const errors: DataQueryError[] = [{ refId: 'A' }, { refId: 'B' }];
  255. const result = getFirstQueryErrorWithoutRefId(errors);
  256. expect(result).toBeUndefined();
  257. });
  258. });
  259. describe('when called with an array with and without refIds', () => {
  260. it('then it should return undefined', () => {
  261. const errors: DataQueryError[] = [
  262. { refId: 'A' },
  263. { message: 'A message' },
  264. { refId: 'B' },
  265. { message: 'B message' },
  266. ];
  267. const result = getFirstQueryErrorWithoutRefId(errors);
  268. expect(result).toBe(errors[1]);
  269. });
  270. });
  271. });
  272. describe('getRefIds', () => {
  273. describe('when called with a null value', () => {
  274. it('then it should return empty array', () => {
  275. const input: any = null;
  276. const result = getRefIds(input);
  277. expect(result).toEqual([]);
  278. });
  279. });
  280. describe('when called with a non object value', () => {
  281. it('then it should return empty array', () => {
  282. const input = 123;
  283. const result = getRefIds(input);
  284. expect(result).toEqual([]);
  285. });
  286. });
  287. describe('when called with an object that has refId', () => {
  288. it('then it should return an array with that refId', () => {
  289. const input = { refId: 'A' };
  290. const result = getRefIds(input);
  291. expect(result).toEqual(['A']);
  292. });
  293. });
  294. describe('when called with an array that has refIds', () => {
  295. it('then it should return an array with unique refIds', () => {
  296. const input = [123, null, {}, { refId: 'A' }, { refId: 'A' }, { refId: 'B' }];
  297. const result = getRefIds(input);
  298. expect(result).toEqual(['A', 'B']);
  299. });
  300. });
  301. describe('when called with an object that has refIds somewhere in the object tree', () => {
  302. it('then it should return return an array with unique refIds', () => {
  303. const input: any = {
  304. data: [
  305. 123,
  306. null,
  307. { refId: 'B', series: [{ refId: 'X' }] },
  308. { refId: 'B' },
  309. {},
  310. { series: [123, null, {}, { refId: 'A' }] },
  311. ],
  312. };
  313. const result = getRefIds(input);
  314. expect(result).toEqual(['B', 'X', 'A']);
  315. });
  316. });
  317. });
  318. describe('refreshIntervalToSortOrder', () => {
  319. describe('when called with live option', () => {
  320. it('then it should return ascending', () => {
  321. const result = refreshIntervalToSortOrder(liveOption.value);
  322. expect(result).toBe(SortOrder.Ascending);
  323. });
  324. });
  325. describe('when called with off option', () => {
  326. it('then it should return descending', () => {
  327. const result = refreshIntervalToSortOrder(offOption.value);
  328. expect(result).toBe(SortOrder.Descending);
  329. });
  330. });
  331. describe('when called with 5s option', () => {
  332. it('then it should return descending', () => {
  333. const result = refreshIntervalToSortOrder('5s');
  334. expect(result).toBe(SortOrder.Descending);
  335. });
  336. });
  337. describe('when called with undefined', () => {
  338. it('then it should return descending', () => {
  339. const result = refreshIntervalToSortOrder(undefined);
  340. expect(result).toBe(SortOrder.Descending);
  341. });
  342. });
  343. });
  344. describe('sortLogsResult', () => {
  345. const firstRow = {
  346. timestamp: '2019-01-01T21:00:0.0000000Z',
  347. entry: '',
  348. hasAnsi: false,
  349. labels: {},
  350. logLevel: LogLevel.info,
  351. raw: '',
  352. timeEpochMs: 0,
  353. timeFromNow: '',
  354. timeLocal: '',
  355. timeUtc: '',
  356. };
  357. const sameAsFirstRow = firstRow;
  358. const secondRow = {
  359. timestamp: '2019-01-01T22:00:0.0000000Z',
  360. entry: '',
  361. hasAnsi: false,
  362. labels: {},
  363. logLevel: LogLevel.info,
  364. raw: '',
  365. timeEpochMs: 0,
  366. timeFromNow: '',
  367. timeLocal: '',
  368. timeUtc: '',
  369. };
  370. describe('when called with SortOrder.Descending', () => {
  371. it('then it should sort descending', () => {
  372. const logsResult: LogsModel = {
  373. rows: [firstRow, sameAsFirstRow, secondRow],
  374. hasUniqueLabels: false,
  375. };
  376. const result = sortLogsResult(logsResult, SortOrder.Descending);
  377. expect(result).toEqual({
  378. rows: [secondRow, firstRow, sameAsFirstRow],
  379. hasUniqueLabels: false,
  380. });
  381. });
  382. });
  383. describe('when called with SortOrder.Ascending', () => {
  384. it('then it should sort ascending', () => {
  385. const logsResult: LogsModel = {
  386. rows: [secondRow, firstRow, sameAsFirstRow],
  387. hasUniqueLabels: false,
  388. };
  389. const result = sortLogsResult(logsResult, SortOrder.Ascending);
  390. expect(result).toEqual({
  391. rows: [firstRow, sameAsFirstRow, secondRow],
  392. hasUniqueLabels: false,
  393. });
  394. });
  395. });
  396. });