explore.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import {
  2. DEFAULT_RANGE,
  3. serializeStateToUrlParam,
  4. parseUrlState,
  5. updateHistory,
  6. clearHistory,
  7. hasNonEmptyQuery,
  8. getValueWithRefId,
  9. getFirstQueryErrorWithoutRefId,
  10. getRefIds,
  11. refreshIntervalToSortOrder,
  12. SortOrder,
  13. sortLogsResult,
  14. } from './explore';
  15. import { ExploreUrlState, ExploreMode } from 'app/types/explore';
  16. import store from 'app/core/store';
  17. import { LogsDedupStrategy, LogsModel, LogLevel } from '@grafana/data';
  18. import { DataQueryError } from '@grafana/ui';
  19. import { liveOption, offOption } from '@grafana/ui/src/components/RefreshPicker/RefreshPicker';
  20. const DEFAULT_EXPLORE_STATE: ExploreUrlState = {
  21. datasource: null,
  22. queries: [],
  23. range: DEFAULT_RANGE,
  24. mode: ExploreMode.Metrics,
  25. ui: {
  26. showingGraph: true,
  27. showingTable: true,
  28. showingLogs: true,
  29. dedupStrategy: LogsDedupStrategy.none,
  30. },
  31. originPanelId: undefined,
  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('hasRefId', () => {
  188. describe('when called with a null value', () => {
  189. it('then it should return null', () => {
  190. const input: any = null;
  191. const result = getValueWithRefId(input);
  192. expect(result).toBeNull();
  193. });
  194. });
  195. describe('when called with a non object value', () => {
  196. it('then it should return null', () => {
  197. const input = 123;
  198. const result = getValueWithRefId(input);
  199. expect(result).toBeNull();
  200. });
  201. });
  202. describe('when called with an object that has refId', () => {
  203. it('then it should return the object', () => {
  204. const input = { refId: 'A' };
  205. const result = getValueWithRefId(input);
  206. expect(result).toBe(input);
  207. });
  208. });
  209. describe('when called with an array that has refId', () => {
  210. it('then it should return the object', () => {
  211. const input = [123, null, {}, { refId: 'A' }];
  212. const result = getValueWithRefId(input);
  213. expect(result).toBe(input[3]);
  214. });
  215. });
  216. describe('when called with an object that has refId somewhere in the object tree', () => {
  217. it('then it should return the object', () => {
  218. const input: any = { data: [123, null, {}, { series: [123, null, {}, { refId: 'A' }] }] };
  219. const result = getValueWithRefId(input);
  220. expect(result).toBe(input.data[3].series[3]);
  221. });
  222. });
  223. });
  224. describe('getFirstQueryErrorWithoutRefId', () => {
  225. describe('when called with a null value', () => {
  226. it('then it should return null', () => {
  227. const errors: DataQueryError[] = null;
  228. const result = getFirstQueryErrorWithoutRefId(errors);
  229. expect(result).toBeNull();
  230. });
  231. });
  232. describe('when called with an array with only refIds', () => {
  233. it('then it should return undefined', () => {
  234. const errors: DataQueryError[] = [{ refId: 'A' }, { refId: 'B' }];
  235. const result = getFirstQueryErrorWithoutRefId(errors);
  236. expect(result).toBeUndefined();
  237. });
  238. });
  239. describe('when called with an array with and without refIds', () => {
  240. it('then it should return undefined', () => {
  241. const errors: DataQueryError[] = [
  242. { refId: 'A' },
  243. { message: 'A message' },
  244. { refId: 'B' },
  245. { message: 'B message' },
  246. ];
  247. const result = getFirstQueryErrorWithoutRefId(errors);
  248. expect(result).toBe(errors[1]);
  249. });
  250. });
  251. });
  252. describe('getRefIds', () => {
  253. describe('when called with a null value', () => {
  254. it('then it should return empty array', () => {
  255. const input: any = null;
  256. const result = getRefIds(input);
  257. expect(result).toEqual([]);
  258. });
  259. });
  260. describe('when called with a non object value', () => {
  261. it('then it should return empty array', () => {
  262. const input = 123;
  263. const result = getRefIds(input);
  264. expect(result).toEqual([]);
  265. });
  266. });
  267. describe('when called with an object that has refId', () => {
  268. it('then it should return an array with that refId', () => {
  269. const input = { refId: 'A' };
  270. const result = getRefIds(input);
  271. expect(result).toEqual(['A']);
  272. });
  273. });
  274. describe('when called with an array that has refIds', () => {
  275. it('then it should return an array with unique refIds', () => {
  276. const input = [123, null, {}, { refId: 'A' }, { refId: 'A' }, { refId: 'B' }];
  277. const result = getRefIds(input);
  278. expect(result).toEqual(['A', 'B']);
  279. });
  280. });
  281. describe('when called with an object that has refIds somewhere in the object tree', () => {
  282. it('then it should return return an array with unique refIds', () => {
  283. const input: any = {
  284. data: [
  285. 123,
  286. null,
  287. { refId: 'B', series: [{ refId: 'X' }] },
  288. { refId: 'B' },
  289. {},
  290. { series: [123, null, {}, { refId: 'A' }] },
  291. ],
  292. };
  293. const result = getRefIds(input);
  294. expect(result).toEqual(['B', 'X', 'A']);
  295. });
  296. });
  297. });
  298. describe('refreshIntervalToSortOrder', () => {
  299. describe('when called with live option', () => {
  300. it('then it should return ascending', () => {
  301. const result = refreshIntervalToSortOrder(liveOption.value);
  302. expect(result).toBe(SortOrder.Ascending);
  303. });
  304. });
  305. describe('when called with off option', () => {
  306. it('then it should return descending', () => {
  307. const result = refreshIntervalToSortOrder(offOption.value);
  308. expect(result).toBe(SortOrder.Descending);
  309. });
  310. });
  311. describe('when called with 5s option', () => {
  312. it('then it should return descending', () => {
  313. const result = refreshIntervalToSortOrder('5s');
  314. expect(result).toBe(SortOrder.Descending);
  315. });
  316. });
  317. describe('when called with undefined', () => {
  318. it('then it should return descending', () => {
  319. const result = refreshIntervalToSortOrder(undefined);
  320. expect(result).toBe(SortOrder.Descending);
  321. });
  322. });
  323. });
  324. describe('sortLogsResult', () => {
  325. const firstRow = {
  326. timestamp: '2019-01-01T21:00:0.0000000Z',
  327. entry: '',
  328. hasAnsi: false,
  329. labels: {},
  330. logLevel: LogLevel.info,
  331. raw: '',
  332. timeEpochMs: 0,
  333. timeFromNow: '',
  334. timeLocal: '',
  335. timeUtc: '',
  336. };
  337. const sameAsFirstRow = firstRow;
  338. const secondRow = {
  339. timestamp: '2019-01-01T22:00:0.0000000Z',
  340. entry: '',
  341. hasAnsi: false,
  342. labels: {},
  343. logLevel: LogLevel.info,
  344. raw: '',
  345. timeEpochMs: 0,
  346. timeFromNow: '',
  347. timeLocal: '',
  348. timeUtc: '',
  349. };
  350. describe('when called with SortOrder.Descending', () => {
  351. it('then it should sort descending', () => {
  352. const logsResult: LogsModel = {
  353. rows: [firstRow, sameAsFirstRow, secondRow],
  354. hasUniqueLabels: false,
  355. };
  356. const result = sortLogsResult(logsResult, SortOrder.Descending);
  357. expect(result).toEqual({
  358. rows: [secondRow, firstRow, sameAsFirstRow],
  359. hasUniqueLabels: false,
  360. });
  361. });
  362. });
  363. describe('when called with SortOrder.Ascending', () => {
  364. it('then it should sort ascending', () => {
  365. const logsResult: LogsModel = {
  366. rows: [secondRow, firstRow, sameAsFirstRow],
  367. hasUniqueLabels: false,
  368. };
  369. const result = sortLogsResult(logsResult, SortOrder.Ascending);
  370. expect(result).toEqual({
  371. rows: [firstRow, sameAsFirstRow, secondRow],
  372. hasUniqueLabels: false,
  373. });
  374. });
  375. });
  376. });