datalinks.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { DataQueryResponse, dateMath } from '@grafana/data';
  2. import { setDataSourceSrv } from '@grafana/runtime';
  3. import { addDataLinksToLogsResponse } from './datalinks';
  4. describe('addDataLinksToLogsResponse', () => {
  5. it('should add data links to response', async () => {
  6. const mockResponse: DataQueryResponse = {
  7. data: [
  8. {
  9. fields: [
  10. {
  11. name: '@message',
  12. config: {},
  13. },
  14. {
  15. name: '@xrayTraceId',
  16. config: {},
  17. },
  18. ],
  19. refId: 'A',
  20. },
  21. ],
  22. };
  23. const mockOptions: any = {
  24. targets: [
  25. {
  26. refId: 'A',
  27. expression: 'stats count(@message) by bin(1h)',
  28. logGroupNames: ['fake-log-group-one', 'fake-log-group-two'],
  29. region: 'us-east-1',
  30. },
  31. ],
  32. };
  33. const time = {
  34. from: dateMath.parse('2016-12-31 15:00:00Z', false)!,
  35. to: dateMath.parse('2016-12-31 16:00:00Z', false)!,
  36. };
  37. setDataSourceSrv({
  38. async get() {
  39. return {
  40. name: 'Xray',
  41. };
  42. },
  43. } as any);
  44. await addDataLinksToLogsResponse(
  45. mockResponse,
  46. mockOptions,
  47. { ...time, raw: time },
  48. (s) => s ?? '',
  49. (v) => [v] ?? [],
  50. (r) => r,
  51. 'xrayUid'
  52. );
  53. expect(mockResponse).toMatchObject({
  54. data: [
  55. {
  56. fields: [
  57. {
  58. name: '@message',
  59. config: {
  60. links: [
  61. {
  62. url: "https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#logs-insights:queryDetail=~(end~'2016-12-31T16*3a00*3a00.000Z~start~'2016-12-31T15*3a00*3a00.000Z~timeType~'ABSOLUTE~tz~'UTC~editorString~'stats*20count*28*40message*29*20by*20bin*281h*29~isLiveTail~false~source~(~'fake-log-group-one~'fake-log-group-two))",
  63. title: 'View in CloudWatch console',
  64. },
  65. ],
  66. },
  67. },
  68. {
  69. name: '@xrayTraceId',
  70. config: {
  71. links: [
  72. {
  73. url: '',
  74. title: 'Xray',
  75. internal: {
  76. query: { query: '${__value.raw}', region: 'us-east-1', queryType: 'getTrace' },
  77. datasourceUid: 'xrayUid',
  78. datasourceName: 'Xray',
  79. },
  80. },
  81. ],
  82. },
  83. },
  84. ],
  85. refId: 'A',
  86. },
  87. ],
  88. });
  89. });
  90. });