transformers.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import {
  20. AnnotationData,
  21. AnnotationSourceType,
  22. AnnotationStyle,
  23. AnnotationType,
  24. AxisType,
  25. CategoricalColorNamespace,
  26. EventAnnotationLayer,
  27. FormulaAnnotationLayer,
  28. IntervalAnnotationLayer,
  29. supersetTheme,
  30. TimeseriesAnnotationLayer,
  31. TimeseriesDataRecord,
  32. } from '@superset-ui/core';
  33. import { OrientationType } from '../../src';
  34. import {
  35. transformEventAnnotation,
  36. transformFormulaAnnotation,
  37. transformIntervalAnnotation,
  38. transformTimeseriesAnnotation,
  39. } from '../../src/Timeseries/transformers';
  40. const mockData: TimeseriesDataRecord[] = [
  41. {
  42. __timestamp: 10,
  43. },
  44. {
  45. __timestamp: 20,
  46. },
  47. ];
  48. const mockFormulaAnnotationLayer: FormulaAnnotationLayer = {
  49. annotationType: AnnotationType.Formula as const,
  50. name: 'My Formula',
  51. show: true,
  52. style: AnnotationStyle.Solid,
  53. value: '50',
  54. showLabel: true,
  55. };
  56. describe('transformFormulaAnnotation', () => {
  57. it('should transform data correctly', () => {
  58. expect(
  59. transformFormulaAnnotation(
  60. mockFormulaAnnotationLayer,
  61. mockData,
  62. '__timestamp',
  63. AxisType.Value,
  64. CategoricalColorNamespace.getScale(''),
  65. undefined,
  66. ).data,
  67. ).toEqual([
  68. [10, 50],
  69. [20, 50],
  70. ]);
  71. });
  72. it('should swap x and y for horizontal chart', () => {
  73. expect(
  74. transformFormulaAnnotation(
  75. mockFormulaAnnotationLayer,
  76. mockData,
  77. '__timestamp',
  78. AxisType.Value,
  79. CategoricalColorNamespace.getScale(''),
  80. undefined,
  81. OrientationType.Horizontal,
  82. ).data,
  83. ).toEqual([
  84. [50, 10],
  85. [50, 20],
  86. ]);
  87. });
  88. });
  89. const mockIntervalAnnotationLayer: IntervalAnnotationLayer = {
  90. name: 'Interval annotation layer',
  91. annotationType: AnnotationType.Interval as const,
  92. sourceType: AnnotationSourceType.Native as const,
  93. color: null,
  94. style: AnnotationStyle.Solid,
  95. width: 1,
  96. show: true,
  97. showLabel: false,
  98. value: 1,
  99. };
  100. const mockIntervalAnnotationData: AnnotationData = {
  101. 'Interval annotation layer': {
  102. records: [
  103. {
  104. start_dttm: 10,
  105. end_dttm: 12,
  106. short_descr: 'Timeseries 1',
  107. long_descr: '',
  108. json_metadata: '',
  109. },
  110. {
  111. start_dttm: 13,
  112. end_dttm: 15,
  113. short_descr: 'Timeseries 2',
  114. long_descr: '',
  115. json_metadata: '',
  116. },
  117. ],
  118. },
  119. };
  120. describe('transformIntervalAnnotation', () => {
  121. it('should transform data correctly', () => {
  122. expect(
  123. transformIntervalAnnotation(
  124. mockIntervalAnnotationLayer,
  125. mockData,
  126. mockIntervalAnnotationData,
  127. CategoricalColorNamespace.getScale(''),
  128. supersetTheme,
  129. )
  130. .map(annotation => annotation.markArea)
  131. .map(markArea => markArea.data),
  132. ).toEqual([
  133. [
  134. [
  135. { name: 'Interval annotation layer - Timeseries 1', xAxis: 10 },
  136. { xAxis: 12 },
  137. ],
  138. ],
  139. [
  140. [
  141. { name: 'Interval annotation layer - Timeseries 2', xAxis: 13 },
  142. { xAxis: 15 },
  143. ],
  144. ],
  145. ]);
  146. });
  147. it('should use yAxis for horizontal chart data', () => {
  148. expect(
  149. transformIntervalAnnotation(
  150. mockIntervalAnnotationLayer,
  151. mockData,
  152. mockIntervalAnnotationData,
  153. CategoricalColorNamespace.getScale(''),
  154. supersetTheme,
  155. undefined,
  156. OrientationType.Horizontal,
  157. )
  158. .map(annotation => annotation.markArea)
  159. .map(markArea => markArea.data),
  160. ).toEqual([
  161. [
  162. [
  163. { name: 'Interval annotation layer - Timeseries 1', yAxis: 10 },
  164. { yAxis: 12 },
  165. ],
  166. ],
  167. [
  168. [
  169. { name: 'Interval annotation layer - Timeseries 2', yAxis: 13 },
  170. { yAxis: 15 },
  171. ],
  172. ],
  173. ]);
  174. });
  175. });
  176. const mockEventAnnotationLayer: EventAnnotationLayer = {
  177. annotationType: AnnotationType.Event,
  178. color: null,
  179. name: 'Event annotation layer',
  180. show: true,
  181. showLabel: false,
  182. sourceType: AnnotationSourceType.Native,
  183. style: AnnotationStyle.Solid,
  184. value: 1,
  185. width: 1,
  186. };
  187. const mockEventAnnotationData: AnnotationData = {
  188. 'Event annotation layer': {
  189. records: [
  190. {
  191. start_dttm: 10,
  192. end_dttm: 12,
  193. short_descr: 'Test annotation',
  194. long_descr: '',
  195. json_metadata: '',
  196. },
  197. {
  198. start_dttm: 13,
  199. end_dttm: 15,
  200. short_descr: 'Test annotation 2',
  201. long_descr: '',
  202. json_metadata: '',
  203. },
  204. ],
  205. },
  206. };
  207. describe('transformEventAnnotation', () => {
  208. it('should transform data correctly', () => {
  209. expect(
  210. transformEventAnnotation(
  211. mockEventAnnotationLayer,
  212. mockData,
  213. mockEventAnnotationData,
  214. CategoricalColorNamespace.getScale(''),
  215. supersetTheme,
  216. )
  217. .map(annotation => annotation.markLine)
  218. .map(markLine => markLine.data),
  219. ).toEqual([
  220. [
  221. {
  222. name: 'Event annotation layer - Test annotation',
  223. xAxis: 10,
  224. },
  225. ],
  226. [{ name: 'Event annotation layer - Test annotation 2', xAxis: 13 }],
  227. ]);
  228. });
  229. it('should use yAxis for horizontal chart data', () => {
  230. expect(
  231. transformEventAnnotation(
  232. mockEventAnnotationLayer,
  233. mockData,
  234. mockEventAnnotationData,
  235. CategoricalColorNamespace.getScale(''),
  236. supersetTheme,
  237. undefined,
  238. OrientationType.Horizontal,
  239. )
  240. .map(annotation => annotation.markLine)
  241. .map(markLine => markLine.data),
  242. ).toEqual([
  243. [
  244. {
  245. name: 'Event annotation layer - Test annotation',
  246. yAxis: 10,
  247. },
  248. ],
  249. [{ name: 'Event annotation layer - Test annotation 2', yAxis: 13 }],
  250. ]);
  251. });
  252. });
  253. const mockTimeseriesAnnotationLayer: TimeseriesAnnotationLayer = {
  254. annotationType: AnnotationType.Timeseries,
  255. color: null,
  256. hideLine: false,
  257. name: 'Timeseries annotation layer',
  258. overrides: {
  259. time_range: null,
  260. },
  261. show: true,
  262. showLabel: false,
  263. showMarkers: false,
  264. sourceType: AnnotationSourceType.Line,
  265. style: AnnotationStyle.Solid,
  266. value: 1,
  267. width: 1,
  268. };
  269. const mockTimeseriesAnnotationData: AnnotationData = {
  270. 'Timeseries annotation layer': {
  271. records: [
  272. { x: 10, y: 12 },
  273. { x: 12, y: 15 },
  274. { x: 15, y: 20 },
  275. ],
  276. },
  277. };
  278. describe('transformTimeseriesAnnotation', () => {
  279. it('should transform data correctly', () => {
  280. expect(
  281. transformTimeseriesAnnotation(
  282. mockTimeseriesAnnotationLayer,
  283. 1,
  284. mockData,
  285. mockTimeseriesAnnotationData,
  286. CategoricalColorNamespace.getScale(''),
  287. ).map(annotation => annotation.data),
  288. ).toEqual([
  289. [
  290. [10, 12],
  291. [12, 15],
  292. [15, 20],
  293. ],
  294. ]);
  295. });
  296. it('should swap x and y for horizontal chart', () => {
  297. expect(
  298. transformTimeseriesAnnotation(
  299. mockTimeseriesAnnotationLayer,
  300. 1,
  301. mockData,
  302. mockTimeseriesAnnotationData,
  303. CategoricalColorNamespace.getScale(''),
  304. undefined,
  305. OrientationType.Horizontal,
  306. ).map(annotation => annotation.data),
  307. ).toEqual([
  308. [
  309. [12, 10],
  310. [15, 12],
  311. [20, 15],
  312. ],
  313. ]);
  314. });
  315. });