utils.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { ArrayVector, createTheme, DataFrame, FieldType, MutableDataFrame } from '@grafana/data';
  2. import {
  3. getEdgeFields,
  4. getNodeFields,
  5. getNodeGraphDataFrames,
  6. makeEdgesDataFrame,
  7. makeNodesDataFrame,
  8. processNodes,
  9. } from './utils';
  10. describe('processNodes', () => {
  11. const theme = createTheme();
  12. it('handles empty args', async () => {
  13. expect(processNodes(undefined, undefined, theme)).toEqual({ nodes: [], edges: [] });
  14. });
  15. it('returns proper nodes and edges', async () => {
  16. const { nodes, edges, legend } = processNodes(
  17. makeNodesDataFrame(3),
  18. makeEdgesDataFrame([
  19. [0, 1],
  20. [0, 2],
  21. [1, 2],
  22. ]),
  23. theme
  24. );
  25. const colorField = {
  26. config: {
  27. color: {
  28. mode: 'continuous-GrYlRd',
  29. },
  30. },
  31. index: 7,
  32. name: 'color',
  33. type: 'number',
  34. values: new ArrayVector([0.5, 0.5, 0.5]),
  35. };
  36. expect(nodes).toEqual([
  37. {
  38. arcSections: [
  39. {
  40. config: {
  41. color: {
  42. fixedColor: 'green',
  43. },
  44. },
  45. name: 'arc__success',
  46. type: 'number',
  47. values: new ArrayVector([0.5, 0.5, 0.5]),
  48. },
  49. {
  50. config: {
  51. color: {
  52. fixedColor: 'red',
  53. },
  54. },
  55. name: 'arc__errors',
  56. type: 'number',
  57. values: new ArrayVector([0.5, 0.5, 0.5]),
  58. },
  59. ],
  60. color: colorField,
  61. dataFrameRowIndex: 0,
  62. id: '0',
  63. incoming: 0,
  64. mainStat: {
  65. config: {},
  66. index: 3,
  67. name: 'mainstat',
  68. type: 'number',
  69. values: new ArrayVector([0.1, 0.1, 0.1]),
  70. },
  71. secondaryStat: {
  72. config: {},
  73. index: 4,
  74. name: 'secondarystat',
  75. type: 'number',
  76. values: new ArrayVector([2, 2, 2]),
  77. },
  78. subTitle: 'service',
  79. title: 'service:0',
  80. },
  81. {
  82. arcSections: [
  83. {
  84. config: {
  85. color: {
  86. fixedColor: 'green',
  87. },
  88. },
  89. name: 'arc__success',
  90. type: 'number',
  91. values: new ArrayVector([0.5, 0.5, 0.5]),
  92. },
  93. {
  94. config: {
  95. color: {
  96. fixedColor: 'red',
  97. },
  98. },
  99. name: 'arc__errors',
  100. type: 'number',
  101. values: new ArrayVector([0.5, 0.5, 0.5]),
  102. },
  103. ],
  104. color: colorField,
  105. dataFrameRowIndex: 1,
  106. id: '1',
  107. incoming: 1,
  108. mainStat: {
  109. config: {},
  110. index: 3,
  111. name: 'mainstat',
  112. type: 'number',
  113. values: new ArrayVector([0.1, 0.1, 0.1]),
  114. },
  115. secondaryStat: {
  116. config: {},
  117. index: 4,
  118. name: 'secondarystat',
  119. type: 'number',
  120. values: new ArrayVector([2, 2, 2]),
  121. },
  122. subTitle: 'service',
  123. title: 'service:1',
  124. },
  125. {
  126. arcSections: [
  127. {
  128. config: {
  129. color: {
  130. fixedColor: 'green',
  131. },
  132. },
  133. name: 'arc__success',
  134. type: 'number',
  135. values: new ArrayVector([0.5, 0.5, 0.5]),
  136. },
  137. {
  138. config: {
  139. color: {
  140. fixedColor: 'red',
  141. },
  142. },
  143. name: 'arc__errors',
  144. type: 'number',
  145. values: new ArrayVector([0.5, 0.5, 0.5]),
  146. },
  147. ],
  148. color: colorField,
  149. dataFrameRowIndex: 2,
  150. id: '2',
  151. incoming: 2,
  152. mainStat: {
  153. config: {},
  154. index: 3,
  155. name: 'mainstat',
  156. type: 'number',
  157. values: new ArrayVector([0.1, 0.1, 0.1]),
  158. },
  159. secondaryStat: {
  160. config: {},
  161. index: 4,
  162. name: 'secondarystat',
  163. type: 'number',
  164. values: new ArrayVector([2, 2, 2]),
  165. },
  166. subTitle: 'service',
  167. title: 'service:2',
  168. },
  169. ]);
  170. expect(edges).toEqual([
  171. {
  172. dataFrameRowIndex: 0,
  173. id: '0--1',
  174. mainStat: '',
  175. secondaryStat: '',
  176. source: '0',
  177. target: '1',
  178. },
  179. {
  180. dataFrameRowIndex: 1,
  181. id: '0--2',
  182. mainStat: '',
  183. secondaryStat: '',
  184. source: '0',
  185. target: '2',
  186. },
  187. {
  188. dataFrameRowIndex: 2,
  189. id: '1--2',
  190. mainStat: '',
  191. secondaryStat: '',
  192. source: '1',
  193. target: '2',
  194. },
  195. ]);
  196. expect(legend).toEqual([
  197. {
  198. color: 'green',
  199. name: 'arc__success',
  200. },
  201. {
  202. color: 'red',
  203. name: 'arc__errors',
  204. },
  205. ]);
  206. });
  207. it('detects dataframes correctly', () => {
  208. const validFrames = [
  209. new MutableDataFrame({
  210. refId: 'hasPreferredVisualisationType',
  211. fields: [],
  212. meta: {
  213. preferredVisualisationType: 'nodeGraph',
  214. },
  215. }),
  216. new MutableDataFrame({
  217. refId: 'hasName',
  218. fields: [],
  219. name: 'nodes',
  220. }),
  221. new MutableDataFrame({
  222. refId: 'nodes', // hasRefId
  223. fields: [],
  224. }),
  225. new MutableDataFrame({
  226. refId: 'hasValidNodesShape',
  227. fields: [{ name: 'id', type: FieldType.string }],
  228. }),
  229. new MutableDataFrame({
  230. refId: 'hasValidEdgesShape',
  231. fields: [
  232. { name: 'id', type: FieldType.string },
  233. { name: 'source', type: FieldType.string },
  234. { name: 'target', type: FieldType.string },
  235. ],
  236. }),
  237. ];
  238. const invalidFrames = [
  239. new MutableDataFrame({
  240. refId: 'invalidData',
  241. fields: [],
  242. }),
  243. ];
  244. const frames = [...validFrames, ...invalidFrames];
  245. const nodeGraphFrames = getNodeGraphDataFrames(frames as DataFrame[]);
  246. expect(nodeGraphFrames.length).toBe(5);
  247. expect(nodeGraphFrames).toEqual(validFrames);
  248. });
  249. it('getting fields is case insensitive', () => {
  250. const nodeFrame = new MutableDataFrame({
  251. refId: 'nodes',
  252. fields: [
  253. { name: 'id', type: FieldType.string, values: ['id'] },
  254. { name: 'title', type: FieldType.string, values: ['title'] },
  255. { name: 'SUBTITLE', type: FieldType.string, values: ['subTitle'] },
  256. { name: 'mainstat', type: FieldType.string, values: ['mainStat'] },
  257. { name: 'seconDarysTat', type: FieldType.string, values: ['secondaryStat'] },
  258. ],
  259. });
  260. const nodeFields = getNodeFields(nodeFrame);
  261. expect(nodeFields.id).toBeDefined();
  262. expect(nodeFields.title).toBeDefined();
  263. expect(nodeFields.subTitle).toBeDefined();
  264. expect(nodeFields.mainStat).toBeDefined();
  265. expect(nodeFields.secondaryStat).toBeDefined();
  266. const edgeFrame = new MutableDataFrame({
  267. refId: 'nodes',
  268. fields: [
  269. { name: 'id', type: FieldType.string, values: ['id'] },
  270. { name: 'source', type: FieldType.string, values: ['title'] },
  271. { name: 'TARGET', type: FieldType.string, values: ['subTitle'] },
  272. { name: 'mainstat', type: FieldType.string, values: ['mainStat'] },
  273. { name: 'secondarystat', type: FieldType.string, values: ['secondaryStat'] },
  274. ],
  275. });
  276. const edgeFields = getEdgeFields(edgeFrame);
  277. expect(edgeFields.id).toBeDefined();
  278. expect(edgeFields.source).toBeDefined();
  279. expect(edgeFields.target).toBeDefined();
  280. expect(edgeFields.mainStat).toBeDefined();
  281. expect(edgeFields.secondaryStat).toBeDefined();
  282. });
  283. });