@perses-dev/components 0.15.0 → 0.16.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 (126) hide show
  1. package/dist/LineChart/LineChart.js +2 -1
  2. package/dist/LineChart/LineChart.js.map +1 -1
  3. package/dist/OptionsEditorLayout/OptionsEditorColumn.d.ts +13 -0
  4. package/dist/OptionsEditorLayout/OptionsEditorColumn.d.ts.map +1 -0
  5. package/dist/OptionsEditorLayout/OptionsEditorColumn.js +28 -0
  6. package/dist/OptionsEditorLayout/OptionsEditorColumn.js.map +1 -0
  7. package/dist/OptionsEditorLayout/OptionsEditorControl.d.ts +5 -0
  8. package/dist/OptionsEditorLayout/OptionsEditorControl.d.ts.map +1 -0
  9. package/dist/OptionsEditorLayout/OptionsEditorControl.js +50 -0
  10. package/dist/OptionsEditorLayout/OptionsEditorControl.js.map +1 -0
  11. package/dist/OptionsEditorLayout/OptionsEditorGrid.d.ts +13 -0
  12. package/dist/OptionsEditorLayout/OptionsEditorGrid.d.ts.map +1 -0
  13. package/dist/OptionsEditorLayout/OptionsEditorGrid.js +26 -0
  14. package/dist/OptionsEditorLayout/OptionsEditorGrid.js.map +1 -0
  15. package/dist/OptionsEditorLayout/OptionsEditorGroup.d.ts +13 -0
  16. package/dist/OptionsEditorLayout/OptionsEditorGroup.d.ts.map +1 -0
  17. package/dist/OptionsEditorLayout/OptionsEditorGroup.js +36 -0
  18. package/dist/OptionsEditorLayout/OptionsEditorGroup.js.map +1 -0
  19. package/dist/OptionsEditorLayout/index.d.ts +5 -0
  20. package/dist/OptionsEditorLayout/index.d.ts.map +1 -0
  21. package/dist/OptionsEditorLayout/index.js +18 -0
  22. package/dist/OptionsEditorLayout/index.js.map +1 -0
  23. package/dist/Tooltip/SeriesInfo.d.ts +1 -0
  24. package/dist/Tooltip/SeriesInfo.d.ts.map +1 -1
  25. package/dist/Tooltip/SeriesInfo.js +3 -3
  26. package/dist/Tooltip/SeriesInfo.js.map +1 -1
  27. package/dist/Tooltip/Tooltip.d.ts +2 -1
  28. package/dist/Tooltip/Tooltip.d.ts.map +1 -1
  29. package/dist/Tooltip/Tooltip.js +2 -2
  30. package/dist/Tooltip/Tooltip.js.map +1 -1
  31. package/dist/Tooltip/TooltipContent.d.ts.map +1 -1
  32. package/dist/Tooltip/TooltipContent.js +2 -1
  33. package/dist/Tooltip/TooltipContent.js.map +1 -1
  34. package/dist/Tooltip/focused-series.d.ts +4 -3
  35. package/dist/Tooltip/focused-series.d.ts.map +1 -1
  36. package/dist/Tooltip/focused-series.js +6 -3
  37. package/dist/Tooltip/focused-series.js.map +1 -1
  38. package/dist/Tooltip/focused-series.test.js +20 -2
  39. package/dist/Tooltip/focused-series.test.js.map +1 -1
  40. package/dist/UnitSelector/UnitSelector.d.ts +8 -0
  41. package/dist/UnitSelector/UnitSelector.d.ts.map +1 -0
  42. package/dist/UnitSelector/UnitSelector.js +112 -0
  43. package/dist/UnitSelector/UnitSelector.js.map +1 -0
  44. package/dist/UnitSelector/UnitSelector.test.d.ts +2 -0
  45. package/dist/UnitSelector/UnitSelector.test.d.ts.map +1 -0
  46. package/dist/UnitSelector/UnitSelector.test.js +211 -0
  47. package/dist/UnitSelector/UnitSelector.test.js.map +1 -0
  48. package/dist/UnitSelector/index.d.ts +2 -0
  49. package/dist/UnitSelector/index.d.ts.map +1 -0
  50. package/dist/UnitSelector/index.js +15 -0
  51. package/dist/UnitSelector/index.js.map +1 -0
  52. package/dist/cjs/LineChart/LineChart.js +2 -1
  53. package/dist/cjs/OptionsEditorLayout/OptionsEditorColumn.js +32 -0
  54. package/dist/cjs/OptionsEditorLayout/OptionsEditorControl.js +61 -0
  55. package/dist/cjs/OptionsEditorLayout/OptionsEditorGrid.js +29 -0
  56. package/dist/cjs/OptionsEditorLayout/OptionsEditorGroup.js +40 -0
  57. package/dist/cjs/OptionsEditorLayout/index.js +31 -0
  58. package/dist/cjs/Tooltip/SeriesInfo.js +3 -3
  59. package/dist/cjs/Tooltip/Tooltip.js +2 -2
  60. package/dist/cjs/Tooltip/TooltipContent.js +2 -1
  61. package/dist/cjs/Tooltip/focused-series.js +6 -3
  62. package/dist/cjs/Tooltip/focused-series.test.js +20 -2
  63. package/dist/cjs/UnitSelector/UnitSelector.js +118 -0
  64. package/dist/cjs/UnitSelector/UnitSelector.test.js +218 -0
  65. package/dist/cjs/UnitSelector/index.js +28 -0
  66. package/dist/cjs/index.js +4 -2
  67. package/dist/cjs/model/units/bytes.js +60 -0
  68. package/dist/cjs/model/units/constants.js +23 -0
  69. package/dist/cjs/model/units/decimal.js +67 -0
  70. package/dist/cjs/model/units/index.js +30 -0
  71. package/dist/cjs/model/units/percent.js +64 -0
  72. package/dist/cjs/model/units/time.js +138 -0
  73. package/dist/cjs/model/units/types.js +28 -0
  74. package/dist/cjs/model/units/units.js +104 -0
  75. package/dist/cjs/utils/component-ids.js +31 -0
  76. package/dist/cjs/utils/index.js +1 -0
  77. package/dist/index.d.ts +4 -2
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js +4 -2
  80. package/dist/index.js.map +1 -1
  81. package/dist/model/units/bytes.d.ts +12 -0
  82. package/dist/model/units/bytes.d.ts.map +1 -0
  83. package/dist/model/units/bytes.js +48 -0
  84. package/dist/model/units/bytes.js.map +1 -0
  85. package/dist/model/units/constants.d.ts +2 -0
  86. package/dist/model/units/constants.d.ts.map +1 -0
  87. package/dist/model/units/constants.js +17 -0
  88. package/dist/model/units/constants.js.map +1 -0
  89. package/dist/model/units/decimal.d.ts +24 -0
  90. package/dist/model/units/decimal.d.ts.map +1 -0
  91. package/dist/model/units/decimal.js +59 -0
  92. package/dist/model/units/decimal.js.map +1 -0
  93. package/dist/model/units/index.d.ts +4 -0
  94. package/dist/model/units/index.d.ts.map +1 -0
  95. package/dist/model/units/index.js +17 -0
  96. package/dist/model/units/index.js.map +1 -0
  97. package/dist/model/units/percent.d.ts +12 -0
  98. package/dist/model/units/percent.d.ts.map +1 -0
  99. package/dist/model/units/percent.js +51 -0
  100. package/dist/model/units/percent.js.map +1 -0
  101. package/dist/model/units/time.d.ts +11 -0
  102. package/dist/model/units/time.d.ts.map +1 -0
  103. package/dist/model/units/time.js +125 -0
  104. package/dist/model/units/time.js.map +1 -0
  105. package/dist/model/units/types.d.ts +38 -0
  106. package/dist/model/units/types.d.ts.map +1 -0
  107. package/dist/model/units/types.js +22 -0
  108. package/dist/model/units/types.js.map +1 -0
  109. package/dist/model/units/units.d.ts +40 -0
  110. package/dist/model/units/units.d.ts.map +1 -0
  111. package/dist/model/units/units.js +83 -0
  112. package/dist/model/units/units.js.map +1 -0
  113. package/dist/utils/component-ids.d.ts +8 -0
  114. package/dist/utils/component-ids.d.ts.map +1 -0
  115. package/dist/utils/component-ids.js +27 -0
  116. package/dist/utils/component-ids.js.map +1 -0
  117. package/dist/utils/index.d.ts +1 -0
  118. package/dist/utils/index.d.ts.map +1 -1
  119. package/dist/utils/index.js +1 -0
  120. package/dist/utils/index.js.map +1 -1
  121. package/package.json +2 -2
  122. package/dist/cjs/model/units.js +0 -203
  123. package/dist/model/units.d.ts +0 -36
  124. package/dist/model/units.d.ts.map +0 -1
  125. package/dist/model/units.js +0 -196
  126. package/dist/model/units.js.map +0 -1
@@ -0,0 +1,32 @@
1
+ // Copyright 2022 The Perses Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", {
15
+ value: true
16
+ });
17
+ Object.defineProperty(exports, "OptionsEditorColumn", {
18
+ enumerable: true,
19
+ get: ()=>OptionsEditorColumn
20
+ });
21
+ const _jsxRuntime = require("react/jsx-runtime");
22
+ const _material = require("@mui/material");
23
+ const OptionsEditorColumn = ({ children })=>{
24
+ return /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Grid, {
25
+ item: true,
26
+ xs: 4,
27
+ children: /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Stack, {
28
+ spacing: 3,
29
+ children: children
30
+ })
31
+ });
32
+ };
@@ -0,0 +1,61 @@
1
+ // Copyright 2022 The Perses Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", {
15
+ value: true
16
+ });
17
+ Object.defineProperty(exports, "OptionsEditorControl", {
18
+ enumerable: true,
19
+ get: ()=>OptionsEditorControl
20
+ });
21
+ const _jsxRuntime = require("react/jsx-runtime");
22
+ const _material = require("@mui/material");
23
+ const _react = /*#__PURE__*/ _interopRequireDefault(require("react"));
24
+ const _utils = require("../utils");
25
+ function _interopRequireDefault(obj) {
26
+ return obj && obj.__esModule ? obj : {
27
+ default: obj
28
+ };
29
+ }
30
+ const OptionsEditorControl = ({ label , control })=>{
31
+ // Make sure we have a unique ID we can use for associating labels and
32
+ // controls for a11y.
33
+ const generatedControlId = (0, _utils.useId)('EditorSectionControl');
34
+ const controlId = `${generatedControlId}-control`;
35
+ const controlProps = {
36
+ id: controlId
37
+ };
38
+ return /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.FormControl, {
39
+ children: /*#__PURE__*/ (0, _jsxRuntime.jsxs)(_material.Stack, {
40
+ direction: "row",
41
+ alignItems: "center",
42
+ justifyContent: "space-between",
43
+ children: [
44
+ /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.FormLabel, {
45
+ htmlFor: controlId,
46
+ children: label
47
+ }),
48
+ /*#__PURE__*/ (0, _jsxRuntime.jsxs)(_material.Box, {
49
+ sx: {
50
+ width: '160px',
51
+ textAlign: 'right'
52
+ },
53
+ children: [
54
+ " ",
55
+ /*#__PURE__*/ _react.default.cloneElement(control, controlProps)
56
+ ]
57
+ })
58
+ ]
59
+ })
60
+ });
61
+ };
@@ -0,0 +1,29 @@
1
+ // Copyright 2022 The Perses Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", {
15
+ value: true
16
+ });
17
+ Object.defineProperty(exports, "OptionsEditorGrid", {
18
+ enumerable: true,
19
+ get: ()=>OptionsEditorGrid
20
+ });
21
+ const _jsxRuntime = require("react/jsx-runtime");
22
+ const _material = require("@mui/material");
23
+ const OptionsEditorGrid = ({ children })=>{
24
+ return /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Grid, {
25
+ container: true,
26
+ spacing: 8,
27
+ children: children
28
+ });
29
+ };
@@ -0,0 +1,40 @@
1
+ // Copyright 2022 The Perses Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", {
15
+ value: true
16
+ });
17
+ Object.defineProperty(exports, "OptionsEditorGroup", {
18
+ enumerable: true,
19
+ get: ()=>OptionsEditorGroup
20
+ });
21
+ const _jsxRuntime = require("react/jsx-runtime");
22
+ const _material = require("@mui/material");
23
+ const OptionsEditorGroup = ({ title , children })=>{
24
+ return /*#__PURE__*/ (0, _jsxRuntime.jsxs)(_material.Box, {
25
+ children: [
26
+ /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Typography, {
27
+ variant: "overline",
28
+ component: "h4",
29
+ borderBottom: 1,
30
+ borderColor: "grey.300",
31
+ marginBottom: (theme)=>theme.spacing(1),
32
+ children: title
33
+ }),
34
+ /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Stack, {
35
+ spacing: 1,
36
+ children: children
37
+ })
38
+ ]
39
+ });
40
+ };
@@ -0,0 +1,31 @@
1
+ // Copyright 2022 The Perses Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", {
15
+ value: true
16
+ });
17
+ _exportStar(require("./OptionsEditorColumn"), exports);
18
+ _exportStar(require("./OptionsEditorControl"), exports);
19
+ _exportStar(require("./OptionsEditorGrid"), exports);
20
+ _exportStar(require("./OptionsEditorGroup"), exports);
21
+ function _exportStar(from, to) {
22
+ Object.keys(from).forEach(function(k) {
23
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
24
+ enumerable: true,
25
+ get: function() {
26
+ return from[k];
27
+ }
28
+ });
29
+ });
30
+ return from;
31
+ }
@@ -23,7 +23,7 @@ const _material = require("@mui/material");
23
23
  const _seriesMarker = require("./SeriesMarker");
24
24
  const _tooltipModel = require("./tooltip-model");
25
25
  function SeriesInfo(props) {
26
- const { seriesName , y , markerColor , totalSeries , wrapLabels } = props;
26
+ const { seriesName , formattedY , markerColor , totalSeries , wrapLabels } = props;
27
27
  // TODO (sjcobb): regex to remove __name__, improve series labels
28
28
  const formattedSeriesLabels = seriesName.replace(/[{}"]/g, '');
29
29
  if (totalSeries === 1) {
@@ -56,7 +56,7 @@ function SeriesInfo(props) {
56
56
  fontWeight: 700,
57
57
  paddingLeft: '2px'
58
58
  }),
59
- children: y
59
+ children: formattedY
60
60
  })
61
61
  ]
62
62
  })
@@ -142,7 +142,7 @@ function SeriesInfo(props) {
142
142
  textAlign: 'right',
143
143
  verticalAlign: 'top'
144
144
  },
145
- children: y
145
+ children: formattedY
146
146
  })
147
147
  ]
148
148
  });
@@ -64,14 +64,14 @@ function _interopRequireWildcard(obj, nodeInterop) {
64
64
  }
65
65
  return newObj;
66
66
  }
67
- const Tooltip = /*#__PURE__*/ _react.default.memo(function Tooltip({ chartRef , chartData , wrapLabels , pinTooltip }) {
67
+ const Tooltip = /*#__PURE__*/ _react.default.memo(function Tooltip({ chartRef , chartData , wrapLabels , pinTooltip , unit }) {
68
68
  const [pinnedPos, setPinnedPos] = (0, _react.useState)(null);
69
69
  const mousePos = (0, _tooltipModel.useMousePosition)();
70
70
  if (mousePos === null || mousePos.target === null) return null;
71
71
  // ensure user is hovering over a chart before checking for nearby series
72
72
  if (pinnedPos === null && mousePos.target.tagName !== 'CANVAS') return null;
73
73
  const chart = chartRef.current;
74
- const focusedSeries = (0, _focusedSeries.getFocusedSeriesData)(mousePos, chartData, pinnedPos, chart);
74
+ const focusedSeries = (0, _focusedSeries.getFocusedSeriesData)(mousePos, chartData, pinnedPos, chart, unit);
75
75
  var ref;
76
76
  const chartWidth = (ref = chart === null || chart === void 0 ? void 0 : chart.getWidth()) !== null && ref !== void 0 ? ref : 750;
77
77
  var ref1;
@@ -75,12 +75,13 @@ function TooltipContent(props) {
75
75
  sx: {
76
76
  display: 'table'
77
77
  },
78
- children: sortedFocusedSeries.map(({ datumIdx , seriesIdx , seriesName , y , markerColor })=>{
78
+ children: sortedFocusedSeries.map(({ datumIdx , seriesIdx , seriesName , y , formattedY , markerColor })=>{
79
79
  if (datumIdx === null || seriesIdx === null) return null;
80
80
  const key = seriesIdx.toString() + datumIdx.toString();
81
81
  return /*#__PURE__*/ (0, _jsxRuntime.jsx)(_seriesInfo.SeriesInfo, {
82
82
  seriesName: seriesName,
83
83
  y: y,
84
+ formattedY: formattedY,
84
85
  markerColor: markerColor,
85
86
  totalSeries: sortedFocusedSeries.length,
86
87
  wrapLabels: wrapLabels
@@ -24,8 +24,9 @@ _export(exports, {
24
24
  getNearbySeries: ()=>getNearbySeries,
25
25
  getFocusedSeriesData: ()=>getFocusedSeriesData
26
26
  });
27
+ const _model = require("../model");
27
28
  const _tooltipModel = require("./tooltip-model");
28
- function getNearbySeries(data, pointInGrid, yBuffer) {
29
+ function getNearbySeries(data, pointInGrid, yBuffer, unit) {
29
30
  const currentFocusedData = [];
30
31
  var ref;
31
32
  const focusedX = (ref = pointInGrid[0]) !== null && ref !== void 0 ? ref : null;
@@ -53,6 +54,7 @@ function getNearbySeries(data, pointInGrid, yBuffer) {
53
54
  // determine whether to convert timestamp to ms, see: https://stackoverflow.com/a/23982005/17575201
54
55
  const xValueMilliSeconds = xValue > 99999999999 ? xValue : xValue * 1000;
55
56
  const formattedDate = _tooltipModel.TOOLTIP_DATE_FORMAT.format(xValueMilliSeconds);
57
+ const formattedY = (0, _model.formatValue)(yValue, unit);
56
58
  currentFocusedData.push({
57
59
  seriesIdx: seriesIdx,
58
60
  datumIdx: datumIdx,
@@ -60,6 +62,7 @@ function getNearbySeries(data, pointInGrid, yBuffer) {
60
62
  date: formattedDate,
61
63
  x: xValue,
62
64
  y: yValue,
65
+ formattedY: formattedY,
63
66
  markerColor: markerColor.toString()
64
67
  });
65
68
  }
@@ -71,7 +74,7 @@ function getNearbySeries(data, pointInGrid, yBuffer) {
71
74
  }
72
75
  return currentFocusedData;
73
76
  }
74
- function getFocusedSeriesData(mousePos, chartData, pinnedPos, chart) {
77
+ function getFocusedSeriesData(mousePos, chartData, pinnedPos, chart, unit) {
75
78
  if (chart === undefined || mousePos === null) return [];
76
79
  // prevents multiple tooltips showing from adjacent charts
77
80
  let cursorTargetMatchesChart = false;
@@ -107,7 +110,7 @@ function getFocusedSeriesData(mousePos, chartData, pinnedPos, chart) {
107
110
  if (chart.containPixel('grid', pointInPixel)) {
108
111
  const pointInGrid = chart.convertFromPixel('grid', pointInPixel);
109
112
  if (pointInGrid[0] !== undefined && pointInGrid[1] !== undefined) {
110
- return getNearbySeries(chartData, pointInGrid, yBuffer);
113
+ return getNearbySeries(chartData, pointInGrid, yBuffer, unit);
111
114
  }
112
115
  }
113
116
  return [];
@@ -68,10 +68,28 @@ describe('getNearbySeries', ()=>{
68
68
  seriesName: 'env="demo", instance="demo.do.prometheus", job="node", mode="test alt"',
69
69
  seriesIdx: 1,
70
70
  x: 1654007895000,
71
- y: 0.0524463040446356
71
+ y: 0.0524463040446356,
72
+ formattedY: '0.05'
72
73
  }
73
74
  ];
74
75
  it('should return focused series data for points nearby the cursor', ()=>{
75
- expect((0, _focusedSeries.getNearbySeries)(chartData, pointInGrid, yBuffer)).toEqual(focusedSeriesOutput);
76
+ const decimalUnit = {
77
+ kind: 'Decimal',
78
+ decimal_places: 2
79
+ };
80
+ expect((0, _focusedSeries.getNearbySeries)(chartData, pointInGrid, yBuffer, decimalUnit)).toEqual(focusedSeriesOutput);
81
+ });
82
+ it('should return series values formatted as a percent', ()=>{
83
+ const percentFormattedOutput = [
84
+ ...focusedSeriesOutput
85
+ ];
86
+ if (percentFormattedOutput[0]) {
87
+ percentFormattedOutput[0].formattedY = '5%';
88
+ }
89
+ const percentFormattedUnit = {
90
+ kind: 'PercentDecimal',
91
+ decimal_places: 0
92
+ };
93
+ expect((0, _focusedSeries.getNearbySeries)(chartData, pointInGrid, yBuffer, percentFormattedUnit)).toEqual(percentFormattedOutput);
76
94
  });
77
95
  });
@@ -0,0 +1,118 @@
1
+ // Copyright 2022 The Perses Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", {
15
+ value: true
16
+ });
17
+ Object.defineProperty(exports, "UnitSelector", {
18
+ enumerable: true,
19
+ get: ()=>UnitSelector
20
+ });
21
+ const _jsxRuntime = require("react/jsx-runtime");
22
+ const _material = require("@mui/material");
23
+ const _model = require("../model");
24
+ const _optionsEditorLayout = require("../OptionsEditorLayout");
25
+ const KIND_OPTIONS = Object.entries(_model.UNIT_CONFIG).map(([id, config])=>{
26
+ return {
27
+ id: id,
28
+ ...config
29
+ };
30
+ }).filter((config)=>!config.disableSelectorOption);
31
+ const DECIMAL_OPTIONS = [
32
+ 0,
33
+ 1,
34
+ 2,
35
+ 3,
36
+ 4
37
+ ];
38
+ function UnitSelector({ value , onChange }) {
39
+ const hasDecimalPlaces = (0, _model.isUnitWithDecimalPlaces)(value);
40
+ const hasAbbreviate = (0, _model.isUnitWithAbbreviate)(value);
41
+ const handleKindChange = (_, newValue)=>{
42
+ onChange({
43
+ kind: newValue.id
44
+ });
45
+ };
46
+ const handleDecimalChange = (_, newValue)=>{
47
+ if (hasDecimalPlaces) {
48
+ onChange({
49
+ ...value,
50
+ decimal_places: newValue
51
+ });
52
+ }
53
+ };
54
+ const handleAbbreviateChange = (_, checked)=>{
55
+ if (hasAbbreviate) {
56
+ onChange({
57
+ ...value,
58
+ abbreviate: checked
59
+ });
60
+ }
61
+ };
62
+ const kindConfig = _model.UNIT_CONFIG[value.kind];
63
+ var _decimal_places;
64
+ return /*#__PURE__*/ (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
65
+ children: [
66
+ /*#__PURE__*/ (0, _jsxRuntime.jsx)(_optionsEditorLayout.OptionsEditorControl, {
67
+ label: "Abbreviate",
68
+ control: /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Switch, {
69
+ checked: hasAbbreviate ? !!value.abbreviate : false,
70
+ onChange: handleAbbreviateChange,
71
+ disabled: !hasAbbreviate
72
+ })
73
+ }),
74
+ /*#__PURE__*/ (0, _jsxRuntime.jsx)(_optionsEditorLayout.OptionsEditorControl, {
75
+ label: "Units",
76
+ control: /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Autocomplete, {
77
+ value: {
78
+ id: value.kind,
79
+ ...kindConfig
80
+ },
81
+ options: KIND_OPTIONS,
82
+ isOptionEqualToValue: (option, value)=>option.id === value.id,
83
+ groupBy: (option)=>option.group,
84
+ renderInput: (params)=>/*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.TextField, {
85
+ ...params
86
+ }),
87
+ renderOption: (renderOptsProps, option)=>{
88
+ // Custom option needed to get some increased left padding to make
89
+ // the items more distinct from the group label.
90
+ return /*#__PURE__*/ (0, _jsxRuntime.jsx)("li", {
91
+ ...renderOptsProps,
92
+ children: /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Box, {
93
+ paddingLeft: (theme)=>theme.spacing(1),
94
+ children: option.label
95
+ })
96
+ });
97
+ },
98
+ onChange: handleKindChange,
99
+ disableClearable: true
100
+ })
101
+ }),
102
+ /*#__PURE__*/ (0, _jsxRuntime.jsx)(_optionsEditorLayout.OptionsEditorControl, {
103
+ label: "Decimal",
104
+ control: /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Autocomplete, {
105
+ value: hasDecimalPlaces ? (_decimal_places = value.decimal_places) !== null && _decimal_places !== void 0 ? _decimal_places : _model.DEFAULT_DECIMAL_PLACES : 0,
106
+ options: DECIMAL_OPTIONS,
107
+ getOptionLabel: (option)=>`${option}`,
108
+ renderInput: (params)=>/*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.TextField, {
109
+ ...params
110
+ }),
111
+ onChange: handleDecimalChange,
112
+ disabled: !hasDecimalPlaces,
113
+ disableClearable: true
114
+ })
115
+ })
116
+ ]
117
+ });
118
+ }
@@ -0,0 +1,218 @@
1
+ // Copyright 2022 The Perses Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", {
15
+ value: true
16
+ });
17
+ const _jsxRuntime = require("react/jsx-runtime");
18
+ const _react = require("@testing-library/react");
19
+ const _userEvent = /*#__PURE__*/ _interopRequireDefault(require("@testing-library/user-event"));
20
+ const _unitSelector = require("./UnitSelector");
21
+ function _interopRequireDefault(obj) {
22
+ return obj && obj.__esModule ? obj : {
23
+ default: obj
24
+ };
25
+ }
26
+ describe('UnitSelector', ()=>{
27
+ const renderUnitSelector = (value, onChange = jest.fn())=>{
28
+ (0, _react.render)(/*#__PURE__*/ (0, _jsxRuntime.jsx)("div", {
29
+ children: /*#__PURE__*/ (0, _jsxRuntime.jsx)(_unitSelector.UnitSelector, {
30
+ value: value,
31
+ onChange: onChange
32
+ })
33
+ }));
34
+ };
35
+ const getUnitSelector = ()=>{
36
+ return _react.screen.getByRole('combobox', {
37
+ name: 'Units'
38
+ });
39
+ };
40
+ const getDecimalSelector = ()=>{
41
+ return _react.screen.getByRole('combobox', {
42
+ name: 'Decimal'
43
+ });
44
+ };
45
+ const getAbbreviateSwitch = ()=>{
46
+ return _react.screen.getByRole('checkbox', {
47
+ name: 'Abbreviate'
48
+ });
49
+ };
50
+ it('can change the unit kind by clicking', ()=>{
51
+ const onChange = jest.fn();
52
+ renderUnitSelector({
53
+ kind: 'Minutes'
54
+ }, onChange);
55
+ const unitSelector = getUnitSelector();
56
+ _userEvent.default.click(unitSelector);
57
+ const decimalOption = _react.screen.getByRole('option', {
58
+ name: 'Decimal'
59
+ });
60
+ _userEvent.default.click(decimalOption);
61
+ expect(onChange).toHaveBeenCalledWith({
62
+ kind: 'Decimal'
63
+ });
64
+ });
65
+ it('can change the unit kind using a keyboard', ()=>{
66
+ const onChange = jest.fn();
67
+ renderUnitSelector({
68
+ kind: 'Bytes'
69
+ }, onChange);
70
+ const unitSelector = getUnitSelector();
71
+ // Note that this tab order can change depending on the type because the
72
+ // abbreviate comes first and is disabled in some cases.
73
+ _userEvent.default.tab();
74
+ expect(unitSelector).toHaveFocus();
75
+ _userEvent.default.clear(unitSelector);
76
+ _userEvent.default.keyboard('years');
77
+ _react.screen.getByRole('option', {
78
+ name: 'Years'
79
+ });
80
+ _userEvent.default.keyboard('{arrowup}{enter}');
81
+ expect(onChange).toHaveBeenCalledWith({
82
+ kind: 'Years'
83
+ });
84
+ });
85
+ it('can change the decimal places by clicking', ()=>{
86
+ const onChange = jest.fn();
87
+ renderUnitSelector({
88
+ kind: 'Decimal',
89
+ decimal_places: 0,
90
+ abbreviate: true
91
+ }, onChange);
92
+ _userEvent.default.click(getDecimalSelector());
93
+ const decimalOption = _react.screen.getByRole('option', {
94
+ name: '1'
95
+ });
96
+ _userEvent.default.click(decimalOption);
97
+ expect(onChange).toHaveBeenCalledWith({
98
+ kind: 'Decimal',
99
+ decimal_places: 1,
100
+ abbreviate: true
101
+ });
102
+ });
103
+ it('can change the decimal places using a keyboard', ()=>{
104
+ const onChange = jest.fn();
105
+ renderUnitSelector({
106
+ kind: 'Percent'
107
+ }, onChange);
108
+ const decimalSelector = getDecimalSelector();
109
+ _userEvent.default.tab();
110
+ _userEvent.default.tab();
111
+ expect(decimalSelector).toHaveFocus();
112
+ _userEvent.default.clear(decimalSelector);
113
+ _userEvent.default.keyboard('3');
114
+ _react.screen.getByRole('option', {
115
+ name: '3'
116
+ });
117
+ _userEvent.default.keyboard('{arrowup}{enter}');
118
+ expect(onChange).toHaveBeenCalledWith({
119
+ kind: 'Percent',
120
+ decimal_places: 3
121
+ });
122
+ });
123
+ it('can change abbreviate by clicking', ()=>{
124
+ const onChange = jest.fn();
125
+ renderUnitSelector({
126
+ kind: 'Decimal',
127
+ decimal_places: 3,
128
+ abbreviate: true
129
+ }, onChange);
130
+ _userEvent.default.click(getAbbreviateSwitch());
131
+ expect(onChange).toHaveBeenCalledWith({
132
+ kind: 'Decimal',
133
+ decimal_places: 3,
134
+ abbreviate: false
135
+ });
136
+ });
137
+ it('can change abbreviate using a keyboard', ()=>{
138
+ const onChange = jest.fn();
139
+ renderUnitSelector({
140
+ kind: 'Decimal',
141
+ decimal_places: 0
142
+ }, onChange);
143
+ _userEvent.default.tab();
144
+ _userEvent.default.keyboard('{space}');
145
+ expect(onChange).toHaveBeenCalledWith({
146
+ kind: 'Decimal',
147
+ decimal_places: 0,
148
+ abbreviate: true
149
+ });
150
+ });
151
+ describe('with a time unit selected', ()=>{
152
+ it('does not allow the user to modify the decimal places', ()=>{
153
+ renderUnitSelector({
154
+ kind: 'Hours'
155
+ });
156
+ expect(getDecimalSelector()).toBeDisabled();
157
+ });
158
+ it('does not allow the user to set abbreviate', ()=>{
159
+ renderUnitSelector({
160
+ kind: 'Minutes'
161
+ });
162
+ expect(getAbbreviateSwitch()).toBeDisabled();
163
+ });
164
+ });
165
+ describe('with a percent unit selected', ()=>{
166
+ it('allows the user to modify the decimal places', ()=>{
167
+ renderUnitSelector({
168
+ kind: 'Percent'
169
+ });
170
+ expect(getDecimalSelector()).toBeEnabled();
171
+ });
172
+ it('does not allow the user to set abbreviate', ()=>{
173
+ renderUnitSelector({
174
+ kind: 'PercentDecimal'
175
+ });
176
+ expect(getAbbreviateSwitch()).toBeDisabled();
177
+ });
178
+ });
179
+ describe('with a decimal unit selected', ()=>{
180
+ it('allows the user to modify the decimal places', ()=>{
181
+ renderUnitSelector({
182
+ kind: 'Decimal'
183
+ });
184
+ expect(getDecimalSelector()).toBeEnabled();
185
+ });
186
+ it('allows the user to set abbreviate', ()=>{
187
+ renderUnitSelector({
188
+ kind: 'Decimal'
189
+ });
190
+ expect(getAbbreviateSwitch()).toBeEnabled();
191
+ });
192
+ });
193
+ describe('with a bytes unit selected', ()=>{
194
+ it('allows the user to modify the decimal places', ()=>{
195
+ renderUnitSelector({
196
+ kind: 'Bytes'
197
+ });
198
+ expect(getDecimalSelector()).toBeEnabled();
199
+ });
200
+ it('does not allow the user to set abbreviate', ()=>{
201
+ renderUnitSelector({
202
+ kind: 'Bytes'
203
+ });
204
+ expect(getAbbreviateSwitch()).toBeDisabled();
205
+ });
206
+ });
207
+ it('should not show an option for disabled units', ()=>{
208
+ const onChange = jest.fn();
209
+ renderUnitSelector({
210
+ kind: 'Decimal'
211
+ }, onChange);
212
+ _userEvent.default.click(getUnitSelector());
213
+ const percentShorthandOption = _react.screen.queryByRole('option', {
214
+ name: '%'
215
+ });
216
+ expect(percentShorthandOption).not.toBeInTheDocument();
217
+ });
218
+ });