extractFields.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { toDataFrame } from '@grafana/data/src/dataframe/processDataFrame';
  2. import { ExtractFieldsOptions, extractFieldsTransformer } from './extractFields';
  3. describe('Fields from JSON', () => {
  4. it('adds fields from JSON in string', async () => {
  5. const cfg: ExtractFieldsOptions = {
  6. source: 'line',
  7. replace: true,
  8. };
  9. const data = toDataFrame({
  10. columns: ['ts', 'line'],
  11. rows: appl,
  12. });
  13. const frames = extractFieldsTransformer.transformer(cfg)([data]);
  14. expect(frames.length).toEqual(1);
  15. expect(
  16. frames[0].fields.reduce((acc, v) => {
  17. acc[v.name] = v.type;
  18. return acc;
  19. }, {} as any)
  20. ).toMatchInlineSnapshot(`
  21. Object {
  22. "a": "string",
  23. "av": "number",
  24. "c": "string",
  25. "e": "number",
  26. "ev": "string",
  27. "h": "string",
  28. "l": "string",
  29. "o": "string",
  30. "op": "string",
  31. "s": "number",
  32. "sym": "string",
  33. "v": "number",
  34. "vw": "string",
  35. "z": "number",
  36. }
  37. `);
  38. });
  39. });
  40. const appl = [
  41. [
  42. '1636678740000000000',
  43. '{"a":"148.1673","av":41941752,"c":"148.25","e":1636678800000,"ev":"AM","h":"148.28","l":"148.22","o":"148.25","op":"148.96","s":1636678740000,"sym":"AAPL","v":2903,"vw":"148.2545","z":152}',
  44. ],
  45. [
  46. '1636678680000000000',
  47. '{"a":"148.1673","av":41938849,"c":"148.25","e":1636678740000,"ev":"AM","h":"148.27","l":"148.25","o":"148.26","op":"148.96","s":1636678680000,"sym":"AAPL","v":7589,"vw":"148.2515","z":329}',
  48. ],
  49. [
  50. '1636678620000000000',
  51. '{"a":"148.1672","av":41931260,"c":"148.27","e":1636678680000,"ev":"AM","h":"148.27","l":"148.25","o":"148.27","op":"148.96","s":1636678620000,"sym":"AAPL","v":6138,"vw":"148.2541","z":245}',
  52. ],
  53. [
  54. '1636678560000000000',
  55. '{"a":"148.1672","av":41925122,"c":"148.28","e":1636678620000,"ev":"AM","h":"148.29","l":"148.27","o":"148.27","op":"148.96","s":1636678560000,"sym":"AAPL","v":1367,"vw":"148.2816","z":56}',
  56. ],
  57. [
  58. '1636678500000000000',
  59. '{"a":"148.1672","av":41923755,"c":"148.25","e":1636678560000,"ev":"AM","h":"148.27","l":"148.25","o":"148.25","op":"148.96","s":1636678500000,"sym":"AAPL","v":556,"vw":"148.2539","z":55}',
  60. ],
  61. [
  62. '1636678440000000000',
  63. '{"a":"148.1672","av":41923199,"c":"148.28","e":1636678500000,"ev":"AM","h":"148.28","l":"148.25","o":"148.25","op":"148.96","s":1636678440000,"sym":"AAPL","v":451,"vw":"148.2614","z":56}',
  64. ],
  65. [
  66. '1636678380000000000',
  67. '{"a":"148.1672","av":41922748,"c":"148.24","e":1636678440000,"ev":"AM","h":"148.24","l":"148.24","o":"148.24","op":"148.96","s":1636678380000,"sym":"AAPL","v":344,"vw":"148.2521","z":24}',
  68. ],
  69. [
  70. '1636678320000000000',
  71. '{"a":"148.1672","av":41922404,"c":"148.28","e":1636678380000,"ev":"AM","h":"148.28","l":"148.24","o":"148.24","op":"148.96","s":1636678320000,"sym":"AAPL","v":705,"vw":"148.2543","z":64}',
  72. ],
  73. [
  74. '1636678260000000000',
  75. '{"a":"148.1672","av":41921699,"c":"148.25","e":1636678320000,"ev":"AM","h":"148.25","l":"148.25","o":"148.25","op":"148.96","s":1636678260000,"sym":"AAPL","v":1054,"vw":"148.2513","z":131}',
  76. ],
  77. ];