@perses-dev/components 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +18 -0
  2. package/dist/EChart.d.ts +4 -1
  3. package/dist/EChart.d.ts.map +1 -1
  4. package/dist/EChart.js +1 -1
  5. package/dist/{GaugeChart.d.ts → GaugeChart/GaugeChart.d.ts} +2 -1
  6. package/dist/GaugeChart/GaugeChart.d.ts.map +1 -0
  7. package/dist/GaugeChart/GaugeChart.js +1 -0
  8. package/dist/GaugeChart/index.d.ts +2 -0
  9. package/dist/GaugeChart/index.d.ts.map +1 -0
  10. package/dist/GaugeChart/index.js +1 -0
  11. package/dist/InfoTooltip/InfoTooltip.d.ts +17 -0
  12. package/dist/InfoTooltip/InfoTooltip.d.ts.map +1 -0
  13. package/dist/InfoTooltip/InfoTooltip.js +1 -0
  14. package/dist/InfoTooltip/InfoTooltip.test.d.ts +2 -0
  15. package/dist/InfoTooltip/InfoTooltip.test.d.ts.map +1 -0
  16. package/dist/InfoTooltip/InfoTooltip.test.js +1 -0
  17. package/dist/InfoTooltip/index.d.ts +2 -0
  18. package/dist/InfoTooltip/index.d.ts.map +1 -0
  19. package/dist/InfoTooltip/index.js +1 -0
  20. package/dist/LineChart/LineChart.d.ts +17 -0
  21. package/dist/LineChart/LineChart.d.ts.map +1 -0
  22. package/dist/LineChart/LineChart.js +1 -0
  23. package/dist/LineChart/index.d.ts +3 -0
  24. package/dist/LineChart/index.d.ts.map +1 -0
  25. package/dist/LineChart/index.js +1 -0
  26. package/dist/LineChart/utils.d.ts +12 -0
  27. package/dist/LineChart/utils.d.ts.map +1 -0
  28. package/dist/LineChart/utils.js +1 -0
  29. package/dist/{StatChart.d.ts → StatChart/StatChart.d.ts} +4 -9
  30. package/dist/StatChart/StatChart.d.ts.map +1 -0
  31. package/dist/StatChart/StatChart.js +1 -0
  32. package/dist/StatChart/StatChart.test.d.ts +2 -0
  33. package/dist/StatChart/StatChart.test.d.ts.map +1 -0
  34. package/dist/StatChart/StatChart.test.js +1 -0
  35. package/dist/StatChart/index.d.ts +2 -0
  36. package/dist/StatChart/index.d.ts.map +1 -0
  37. package/dist/StatChart/index.js +1 -0
  38. package/dist/{tooltip → Tooltip}/SeriesInfo.d.ts +0 -0
  39. package/dist/{tooltip → Tooltip}/SeriesInfo.d.ts.map +1 -1
  40. package/dist/{tooltip → Tooltip}/SeriesInfo.js +1 -1
  41. package/dist/{tooltip → Tooltip}/SeriesMarker.d.ts +0 -0
  42. package/dist/{tooltip → Tooltip}/SeriesMarker.d.ts.map +1 -1
  43. package/dist/{tooltip → Tooltip}/SeriesMarker.js +0 -0
  44. package/dist/{tooltip → Tooltip}/Tooltip.d.ts +1 -1
  45. package/dist/{tooltip → Tooltip}/Tooltip.d.ts.map +1 -1
  46. package/dist/{tooltip → Tooltip}/Tooltip.js +0 -0
  47. package/dist/{tooltip → Tooltip}/TooltipContent.d.ts +0 -0
  48. package/dist/{tooltip → Tooltip}/TooltipContent.d.ts.map +1 -1
  49. package/dist/{tooltip → Tooltip}/TooltipContent.js +0 -0
  50. package/dist/{tooltip → Tooltip}/focused-series.d.ts +1 -1
  51. package/dist/{tooltip → Tooltip}/focused-series.d.ts.map +1 -1
  52. package/dist/{tooltip → Tooltip}/focused-series.js +0 -0
  53. package/dist/Tooltip/focused-series.test.d.ts +2 -0
  54. package/dist/Tooltip/focused-series.test.d.ts.map +1 -0
  55. package/dist/Tooltip/focused-series.test.js +1 -0
  56. package/dist/Tooltip/index.d.ts +3 -0
  57. package/dist/Tooltip/index.d.ts.map +1 -0
  58. package/dist/Tooltip/index.js +1 -0
  59. package/dist/{tooltip → Tooltip}/tooltip-model.d.ts +0 -0
  60. package/dist/{tooltip → Tooltip}/tooltip-model.d.ts.map +1 -1
  61. package/dist/{tooltip → Tooltip}/tooltip-model.js +0 -0
  62. package/dist/cjs/EChart.js +3 -3
  63. package/dist/cjs/{GaugeChart.js → GaugeChart/GaugeChart.js} +12 -45
  64. package/dist/cjs/GaugeChart/index.js +29 -0
  65. package/dist/cjs/InfoTooltip/InfoTooltip.js +39 -0
  66. package/dist/cjs/InfoTooltip/InfoTooltip.test.js +33 -0
  67. package/dist/cjs/InfoTooltip/index.js +29 -0
  68. package/dist/cjs/{LineChart.js → LineChart/LineChart.js} +36 -109
  69. package/dist/cjs/LineChart/index.js +29 -0
  70. package/dist/cjs/LineChart/utils.js +72 -0
  71. package/dist/cjs/StatChart/StatChart.js +104 -0
  72. package/dist/cjs/StatChart/StatChart.test.js +45 -0
  73. package/dist/cjs/StatChart/index.js +29 -0
  74. package/dist/cjs/{tooltip → Tooltip}/SeriesInfo.js +2 -1
  75. package/dist/cjs/{tooltip → Tooltip}/SeriesMarker.js +0 -0
  76. package/dist/cjs/{tooltip → Tooltip}/Tooltip.js +0 -0
  77. package/dist/cjs/{tooltip → Tooltip}/TooltipContent.js +0 -0
  78. package/dist/cjs/{tooltip → Tooltip}/focused-series.js +0 -0
  79. package/dist/cjs/Tooltip/focused-series.test.js +57 -0
  80. package/dist/cjs/Tooltip/index.js +30 -0
  81. package/dist/cjs/{tooltip → Tooltip}/tooltip-model.js +0 -0
  82. package/dist/cjs/context/ChartsThemeProvider.js +36 -0
  83. package/dist/cjs/index.js +5 -4
  84. package/dist/cjs/model/{graph-model.js → graph.js} +0 -0
  85. package/dist/cjs/model/index.js +31 -0
  86. package/dist/cjs/model/theme.js +14 -0
  87. package/dist/cjs/model/units.js +58 -85
  88. package/dist/cjs/test/index.js +29 -0
  89. package/dist/cjs/test/render.js +26 -0
  90. package/dist/cjs/test/setup-tests.js +18 -0
  91. package/dist/cjs/utils/index.js +30 -0
  92. package/dist/cjs/utils/theme-gen.js +180 -0
  93. package/dist/cjs/utils/theme-gen.test.js +196 -0
  94. package/dist/context/ChartsThemeProvider.d.ts +11 -0
  95. package/dist/context/ChartsThemeProvider.d.ts.map +1 -0
  96. package/dist/context/ChartsThemeProvider.js +1 -0
  97. package/dist/index.d.ts +5 -4
  98. package/dist/index.d.ts.map +1 -1
  99. package/dist/index.js +1 -1
  100. package/dist/model/{graph-model.d.ts → graph.d.ts} +7 -1
  101. package/dist/model/graph.d.ts.map +1 -0
  102. package/dist/model/{graph-model.js → graph.js} +0 -0
  103. package/dist/model/index.d.ts +4 -0
  104. package/dist/model/index.d.ts.map +1 -0
  105. package/dist/model/index.js +1 -0
  106. package/dist/model/theme.d.ts +16 -0
  107. package/dist/model/theme.d.ts.map +1 -0
  108. package/dist/model/theme.js +1 -0
  109. package/dist/model/units.d.ts +22 -11
  110. package/dist/model/units.d.ts.map +1 -1
  111. package/dist/model/units.js +1 -1
  112. package/dist/test/index.d.ts +2 -0
  113. package/dist/test/index.d.ts.map +1 -0
  114. package/dist/test/index.js +1 -0
  115. package/dist/test/render.d.ts +7 -0
  116. package/dist/test/render.d.ts.map +1 -0
  117. package/dist/test/render.js +1 -0
  118. package/dist/test/setup-tests.d.ts +2 -0
  119. package/dist/test/setup-tests.d.ts.map +1 -0
  120. package/dist/test/setup-tests.js +1 -0
  121. package/dist/utils/index.d.ts +3 -0
  122. package/dist/utils/index.d.ts.map +1 -0
  123. package/dist/utils/index.js +1 -0
  124. package/dist/utils/theme-gen.d.ts +6 -0
  125. package/dist/utils/theme-gen.d.ts.map +1 -0
  126. package/dist/utils/theme-gen.js +1 -0
  127. package/dist/utils/theme-gen.test.d.ts +2 -0
  128. package/dist/utils/theme-gen.test.d.ts.map +1 -0
  129. package/dist/utils/theme-gen.test.js +1 -0
  130. package/package.json +5 -3
  131. package/dist/GaugeChart.d.ts.map +0 -1
  132. package/dist/GaugeChart.js +0 -1
  133. package/dist/LineChart.d.ts +0 -22
  134. package/dist/LineChart.d.ts.map +0 -1
  135. package/dist/LineChart.js +0 -1
  136. package/dist/StatChart.d.ts.map +0 -1
  137. package/dist/StatChart.js +0 -1
  138. package/dist/cjs/StatChart.js +0 -207
  139. package/dist/model/graph-model.d.ts.map +0 -1
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const focused_series_1 = require("./focused-series");
16
+ describe('getNearbySeries', () => {
17
+ const chartData = {
18
+ timeSeries: [
19
+ {
20
+ type: 'line',
21
+ name: 'env="demo", instance="demo.do.prometheus", job="node", mode="test"',
22
+ color: 'hsla(-1365438424,50%,50%,0.8)',
23
+ data: [
24
+ 0.0002315202231525094, 0.00022873082287300112, 0.00023152022315149463, 0.00023152022315149463,
25
+ 0.00022873082287300112,
26
+ ],
27
+ symbol: 'circle',
28
+ },
29
+ {
30
+ type: 'line',
31
+ name: 'env="demo", instance="demo.do.prometheus", job="node", mode="test alt"',
32
+ color: 'hsla(286664040,50%,50%,0.8)',
33
+ data: [0.05245188284519867, 0.0524463040446356, 0.0524463040446356, 0.05247140864723438, 0.052482566248230646],
34
+ symbol: 'circle',
35
+ },
36
+ ],
37
+ xAxis: [1654007865000, 1654007880000, 1654007895000, 1654007910000, 1654007925000],
38
+ rangeMs: 60000,
39
+ };
40
+ // https://echarts.apache.org/en/api.html#echartsInstance.convertFromPixel
41
+ const pointInGrid = [2, 0.0560655737704918]; // converted from chart.getZr() mousemove coordinates
42
+ const yBuffer = 0.02; // calculated from y axis interval
43
+ const focusedSeriesOutput = [
44
+ {
45
+ date: 'May 31, 2022, 2:38:15 PM',
46
+ datumIdx: 2,
47
+ markerColor: 'hsla(286664040,50%,50%,0.8)',
48
+ seriesName: 'env="demo", instance="demo.do.prometheus", job="node", mode="test alt"',
49
+ seriesIdx: 1,
50
+ x: 1654007895000,
51
+ y: 0.0524463040446356,
52
+ },
53
+ ];
54
+ it('should return focused series data for points nearby the cursor', () => {
55
+ expect((0, focused_series_1.getNearbySeries)(chartData, pointInGrid, yBuffer)).toEqual(focusedSeriesOutput);
56
+ });
57
+ });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./Tooltip"), exports);
30
+ __exportStar(require("./SeriesMarker"), exports);
File without changes
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useChartsTheme = exports.ChartsThemeContext = exports.ChartsThemeProvider = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2022 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // 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, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("react");
18
+ const echarts_1 = require("echarts");
19
+ function ChartsThemeProvider(props) {
20
+ const { children, themeName, chartsTheme } = props;
21
+ if (chartsTheme !== undefined && chartsTheme.echartsTheme !== undefined) {
22
+ // register ECharts theme to be used in individual charts, see: https://apache.github.io/echarts-handbook/en/concepts/style/#theme
23
+ (0, echarts_1.registerTheme)(themeName, chartsTheme.echartsTheme);
24
+ }
25
+ return (0, jsx_runtime_1.jsx)(exports.ChartsThemeContext.Provider, { value: chartsTheme, children: children });
26
+ }
27
+ exports.ChartsThemeProvider = ChartsThemeProvider;
28
+ exports.ChartsThemeContext = (0, react_1.createContext)(undefined);
29
+ function useChartsTheme() {
30
+ const ctx = (0, react_1.useContext)(exports.ChartsThemeContext);
31
+ if (ctx === undefined) {
32
+ throw new Error('No ChartsThemeContext found. Did you forget a Provider?');
33
+ }
34
+ return ctx;
35
+ }
36
+ exports.useChartsTheme = useChartsTheme;
package/dist/cjs/index.js CHANGED
@@ -29,9 +29,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  __exportStar(require("./EChart"), exports);
30
30
  __exportStar(require("./ErrorAlert"), exports);
31
31
  __exportStar(require("./ErrorBoundary"), exports);
32
- __exportStar(require("./GaugeChart"), exports);
32
+ __exportStar(require("./InfoTooltip"), exports);
33
33
  __exportStar(require("./LineChart"), exports);
34
+ __exportStar(require("./GaugeChart"), exports);
34
35
  __exportStar(require("./StatChart"), exports);
35
- __exportStar(require("./utils/combine-sx"), exports);
36
- __exportStar(require("./model/graph-model"), exports);
37
- __exportStar(require("./model/units"), exports);
36
+ __exportStar(require("./context/ChartsThemeProvider"), exports);
37
+ __exportStar(require("./utils"), exports);
38
+ __exportStar(require("./model"), exports);
File without changes
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./graph"), exports);
30
+ __exportStar(require("./theme"), exports);
31
+ __exportStar(require("./units"), exports);
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -12,25 +12,33 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.SIMPLE_UNITS = exports.removeUnitNamespace = exports.SANCTIONED_UNITS = exports.isSanctionedSimpleUnitIdentifier = exports.abbreviateLargeNumber = exports.formatValue = void 0;
15
+ exports.formatNumber = exports.abbreviateLargeNumber = exports.formatValue = exports.DEFAULT_DECIMAL_PLACES = void 0;
16
16
  const date_fns_1 = require("date-fns");
17
+ const mathjs_1 = require("mathjs");
18
+ exports.DEFAULT_DECIMAL_PLACES = 2;
17
19
  function formatValue(value, unitOptions) {
20
+ var _a;
18
21
  if (unitOptions === undefined) {
19
22
  return value.toString();
20
23
  }
24
+ if (isDecimalUnit(unitOptions)) {
25
+ return formatDecimal(value, unitOptions);
26
+ }
21
27
  if (isTimeUnit(unitOptions)) {
22
28
  return formatTime(value, unitOptions);
23
29
  }
24
30
  if (isPercentUnit(unitOptions)) {
25
31
  return formatPercent(value, unitOptions);
26
32
  }
27
- if (isDecimalUnit(unitOptions)) {
28
- return formatDecimal(value, unitOptions);
33
+ if (isBytesUnit(unitOptions)) {
34
+ const decimals = (_a = unitOptions.decimal_places) !== null && _a !== void 0 ? _a : exports.DEFAULT_DECIMAL_PLACES;
35
+ return formatBytes(value, decimals);
29
36
  }
30
37
  const exhaustive = unitOptions;
31
38
  throw new Error(`Unknown unit options ${exhaustive}`);
32
39
  }
33
40
  exports.formatValue = formatValue;
41
+ /* Time Unit Conversion */
34
42
  const timeUnitKinds = ['Milliseconds', 'Seconds', 'Minutes', 'Hours', 'Days', 'Weeks', 'Months', 'Years'];
35
43
  const timeUnitKindsSet = new Set(timeUnitKinds);
36
44
  function isTimeUnit(unitOptions) {
@@ -96,116 +104,81 @@ function formatTime(value, unitOptions) {
96
104
  }
97
105
  return `${years.toFixed()} years`;
98
106
  }
99
- const percentUnitKinds = ['Percent', 'PercentDecimal'];
107
+ /* Percent Unit Conversion */
108
+ const percentUnitKinds = ['Percent', 'PercentDecimal', '%'];
100
109
  const percentUnitKindsSet = new Set(percentUnitKinds);
101
110
  function isPercentUnit(unitOptions) {
102
111
  return percentUnitKindsSet.has(unitOptions.kind);
103
112
  }
104
113
  function formatPercent(value, unitOptions) {
114
+ var _a;
115
+ const decimals = (_a = unitOptions.decimal_places) !== null && _a !== void 0 ? _a : exports.DEFAULT_DECIMAL_PLACES;
105
116
  if (unitOptions.kind === 'PercentDecimal') {
106
117
  value = value * 100;
107
118
  }
108
- return value.toFixed(unitOptions.decimal_places) + '%';
119
+ return value.toFixed(decimals) + '%';
109
120
  }
121
+ /* Decimal Unit Conversion */
110
122
  const decimalUnitKinds = ['Decimal'];
111
123
  const decimalUnitKindsSet = new Set(decimalUnitKinds);
112
124
  function isDecimalUnit(unitOptions) {
113
125
  return decimalUnitKindsSet.has(unitOptions.kind);
114
126
  }
115
127
  function formatDecimal(value, unitOptions) {
116
- var _a, _b;
117
- const maximumFractionDigits = (_a = unitOptions.decimal_places) !== null && _a !== void 0 ? _a : 2;
118
- if (unitOptions.suffix !== undefined) {
119
- if (isSanctionedSimpleUnitIdentifier(unitOptions.suffix)) {
120
- const formatParams = {
121
- style: 'unit',
122
- minimumFractionDigits: 0,
123
- maximumFractionDigits: maximumFractionDigits,
124
- useGrouping: true,
125
- unit: unitOptions.suffix,
126
- unitDisplay: (_b = unitOptions.unitDisplay) !== null && _b !== void 0 ? _b : 'narrow',
127
- };
128
- const unitFormatter = new Intl.NumberFormat('en-US', formatParams);
129
- return unitFormatter.format(value);
130
- }
128
+ var _a;
129
+ const decimals = (_a = unitOptions.decimal_places) !== null && _a !== void 0 ? _a : exports.DEFAULT_DECIMAL_PLACES;
130
+ if (unitOptions.abbreviate === true) {
131
+ return abbreviateLargeNumber(value, decimals);
131
132
  }
132
133
  const formatParams = {
133
134
  style: 'decimal',
134
135
  minimumFractionDigits: 0,
135
- maximumFractionDigits: maximumFractionDigits,
136
+ maximumFractionDigits: decimals,
136
137
  useGrouping: true,
137
138
  };
138
139
  const decimalFormatter = new Intl.NumberFormat('en-US', formatParams);
139
140
  return decimalFormatter.format(value);
140
141
  }
141
- // Take a large number and abbreviate appropriate suffix
142
- // ex) 10000 -> 10k, 1000000 -> 1M
143
- function abbreviateLargeNumber(num) {
142
+ /**
143
+ * Takes large numbers and abbreviates them with the appropriate suffix
144
+ * 10123 -> 10.123k
145
+ * 1000000 -> 1M
146
+ */
147
+ function abbreviateLargeNumber(num, decimals = 2) {
148
+ const modifier = (n) => (0, mathjs_1.round)(n, decimals);
149
+ return formatNumber(num, modifier);
150
+ }
151
+ exports.abbreviateLargeNumber = abbreviateLargeNumber;
152
+ /**
153
+ * Takes large numbers, rounds and abbreviates them with the appropriate suffix
154
+ * Add modifier to run on output value prior to unit being added (defaults to rounding)
155
+ */
156
+ function formatNumber(num, modifier) {
157
+ const fn = modifier !== null && modifier !== void 0 ? modifier : Math.round;
144
158
  return num >= 1e12
145
- ? num / 1e12 + 'T'
159
+ ? fn(num / 1e12) + 'T'
146
160
  : num >= 1e9
147
- ? num / 1e9 + 'B'
161
+ ? fn(num / 1e9) + 'B'
148
162
  : num >= 1e6
149
- ? num / 1e6 + 'M'
163
+ ? fn(num / 1e6) + 'M'
150
164
  : num >= 1e3
151
- ? num / 1e3 + 'k'
152
- : num;
165
+ ? fn(num / 1e3) + 'K'
166
+ : num.toString();
153
167
  }
154
- exports.abbreviateLargeNumber = abbreviateLargeNumber;
155
- // Util to check unit name against ECMA standard: https://tc39.es/ecma402/#sec-issanctionedsimpleunitidentifier
156
- function isSanctionedSimpleUnitIdentifier(unitIdentifier) {
157
- return exports.SIMPLE_UNITS.indexOf(unitIdentifier) > -1;
168
+ exports.formatNumber = formatNumber;
169
+ /* Bytes Unit Conversion */
170
+ const bytesUnitKinds = ['Bytes'];
171
+ const bytesUnitKindsSet = new Set(bytesUnitKinds);
172
+ function isBytesUnit(unitOptions) {
173
+ return bytesUnitKindsSet.has(unitOptions.kind);
158
174
  }
159
- exports.isSanctionedSimpleUnitIdentifier = isSanctionedSimpleUnitIdentifier;
160
- // https://tc39.es/ecma402/#table-sanctioned-simple-unit-identifiers
161
- exports.SANCTIONED_UNITS = [
162
- 'angle-degree',
163
- 'area-acre',
164
- 'area-hectare',
165
- 'concentr-percent',
166
- 'digital-bit',
167
- 'digital-byte',
168
- 'digital-gigabit',
169
- 'digital-gigabyte',
170
- 'digital-kilobit',
171
- 'digital-kilobyte',
172
- 'digital-megabit',
173
- 'digital-megabyte',
174
- 'digital-petabyte',
175
- 'digital-terabit',
176
- 'digital-terabyte',
177
- 'duration-day',
178
- 'duration-hour',
179
- 'duration-millisecond',
180
- 'duration-minute',
181
- 'duration-month',
182
- 'duration-second',
183
- 'duration-week',
184
- 'duration-year',
185
- 'length-centimeter',
186
- 'length-foot',
187
- 'length-inch',
188
- 'length-kilometer',
189
- 'length-meter',
190
- 'length-mile-scandinavian',
191
- 'length-mile',
192
- 'length-millimeter',
193
- 'length-yard',
194
- 'mass-gram',
195
- 'mass-kilogram',
196
- 'mass-ounce',
197
- 'mass-pound',
198
- 'mass-stone',
199
- 'temperature-celsius',
200
- 'temperature-fahrenheit',
201
- 'volume-fluid-ounce',
202
- 'volume-gallon',
203
- 'volume-liter',
204
- 'volume-milliliter',
205
- ];
206
- // removes the namespace prefix, ex: duration-hour -> hour
207
- function removeUnitNamespace(unit) {
208
- return unit.slice(unit.indexOf('-') + 1);
175
+ // https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript/18650828#18650828
176
+ function formatBytes(bytes, decimals = 2) {
177
+ if (bytes === 0)
178
+ return '0 Bytes';
179
+ const k = 1024;
180
+ const dm = decimals < 0 ? 0 : decimals;
181
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
182
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
183
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
209
184
  }
210
- exports.removeUnitNamespace = removeUnitNamespace;
211
- exports.SIMPLE_UNITS = exports.SANCTIONED_UNITS.map(removeUnitNamespace);
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./render"), exports);
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.renderWithContext = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2022 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // 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, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("@testing-library/react");
18
+ const react_query_1 = require("react-query");
19
+ const queryClient = new react_query_1.QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false } } });
20
+ /**
21
+ * Test helper to render a React component with some common app-level providers wrapped around it.
22
+ */
23
+ function renderWithContext(ui, options) {
24
+ return (0, react_1.render)((0, jsx_runtime_1.jsx)(react_query_1.QueryClientProvider, { client: queryClient, children: ui }), options);
25
+ }
26
+ exports.renderWithContext = renderWithContext;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ // Add testing library assertions
16
+ require("@testing-library/jest-dom/extend-expect");
17
+ // Always mock e-charts during tests since we don't have a proper canvas in jsdom
18
+ jest.mock('echarts/core');
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./combine-sx"), exports);
30
+ __exportStar(require("./theme-gen"), exports);