standardAnnotationSupport.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { FieldType, toDataFrame } from '@grafana/data';
  2. import { getAnnotationsFromData } from './standardAnnotationSupport';
  3. describe('DataFrame to annotations', () => {
  4. test('simple conversion', async () => {
  5. const frame = toDataFrame({
  6. fields: [
  7. { type: FieldType.time, values: [1, 2, 3, 4, 5] },
  8. { name: 'first string field', values: ['t1', 't2', 't3', null, undefined] },
  9. { name: 'tags', values: ['aaa,bbb', 'bbb,ccc', 'zyz', null, undefined] },
  10. ],
  11. });
  12. await expect(getAnnotationsFromData([frame])).toEmitValues([
  13. [
  14. {
  15. color: 'red',
  16. tags: ['aaa', 'bbb'],
  17. text: 't1',
  18. time: 1,
  19. type: 'default',
  20. },
  21. {
  22. color: 'red',
  23. tags: ['bbb', 'ccc'],
  24. text: 't2',
  25. time: 2,
  26. type: 'default',
  27. },
  28. {
  29. color: 'red',
  30. tags: ['zyz'],
  31. text: 't3',
  32. time: 3,
  33. type: 'default',
  34. },
  35. {
  36. color: 'red',
  37. time: 4,
  38. type: 'default',
  39. },
  40. {
  41. color: 'red',
  42. time: 5,
  43. type: 'default',
  44. },
  45. ],
  46. ]);
  47. });
  48. test('explicit mappings', async () => {
  49. const frame = toDataFrame({
  50. fields: [
  51. { name: 'time1', values: [111, 222, 333] },
  52. { name: 'time2', values: [100, 200, 300] },
  53. { name: 'aaaaa', values: ['a1', 'a2', 'a3'] },
  54. { name: 'bbbbb', values: ['b1', 'b2', 'b3'] },
  55. ],
  56. });
  57. const observable = getAnnotationsFromData([frame], {
  58. text: { value: 'bbbbb' },
  59. time: { value: 'time2' },
  60. timeEnd: { value: 'time1' },
  61. title: { value: 'aaaaa' },
  62. });
  63. await expect(observable).toEmitValues([
  64. [
  65. {
  66. color: 'red',
  67. text: 'b1',
  68. time: 100,
  69. timeEnd: 111,
  70. title: 'a1',
  71. type: 'default',
  72. },
  73. {
  74. color: 'red',
  75. text: 'b2',
  76. time: 200,
  77. timeEnd: 222,
  78. title: 'a2',
  79. type: 'default',
  80. },
  81. {
  82. color: 'red',
  83. text: 'b3',
  84. time: 300,
  85. timeEnd: 333,
  86. title: 'a3',
  87. type: 'default',
  88. },
  89. ],
  90. ]);
  91. });
  92. it('all valid key names should be included in the output result', async () => {
  93. const frame = toDataFrame({
  94. fields: [
  95. { name: 'time', values: [100] },
  96. { name: 'timeEnd', values: [200] },
  97. { name: 'title', values: ['title'] },
  98. { name: 'text', values: ['text'] },
  99. { name: 'tags', values: ['t1,t2,t3'] },
  100. { name: 'id', values: [1] },
  101. { name: 'userId', values: ['Admin'] },
  102. { name: 'login', values: ['admin'] },
  103. { name: 'email', values: ['admin@unknown.us'] },
  104. { name: 'prevState', values: ['normal'] },
  105. { name: 'newState', values: ['alerting'] },
  106. { name: 'data', values: [{ text: 'a', value: 'A' }] },
  107. { name: 'panelId', values: [4] },
  108. { name: 'alertId', values: [0] },
  109. ],
  110. });
  111. const observable = getAnnotationsFromData([frame]);
  112. await expect(observable).toEmitValues([
  113. [
  114. {
  115. color: 'red',
  116. data: { text: 'a', value: 'A' },
  117. email: 'admin@unknown.us',
  118. id: 1,
  119. login: 'admin',
  120. newState: 'alerting',
  121. panelId: 4,
  122. prevState: 'normal',
  123. tags: ['t1', 't2', 't3'],
  124. text: 'text',
  125. time: 100,
  126. timeEnd: 200,
  127. title: 'title',
  128. type: 'default',
  129. userId: 'Admin',
  130. alertId: 0,
  131. },
  132. ],
  133. ]);
  134. });
  135. it('key names that are not valid should be excluded in the output result', async () => {
  136. const frame = toDataFrame({
  137. fields: [
  138. { name: 'time', values: [100] },
  139. { name: 'text', values: ['text'] },
  140. { name: 'someData', values: [{ value: 'bar' }] },
  141. { name: 'panelSource', values: ['100'] },
  142. { name: 'timeStart', values: [100] },
  143. ],
  144. });
  145. const observable = getAnnotationsFromData([frame]);
  146. await expect(observable).toEmitValues([[{ color: 'red', text: 'text', time: 100, type: 'default' }]]);
  147. });
  148. });