@perses-dev/components 0.14.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 (178) hide show
  1. package/dist/{TimeRangeSelector → DateTimeRangePicker}/AbsoluteTimePicker.d.ts +0 -0
  2. package/dist/DateTimeRangePicker/AbsoluteTimePicker.d.ts.map +1 -0
  3. package/dist/{TimeRangeSelector → DateTimeRangePicker}/AbsoluteTimePicker.js +38 -33
  4. package/dist/DateTimeRangePicker/AbsoluteTimePicker.js.map +1 -0
  5. package/dist/DateTimeRangePicker/DateTimeRangePicker.d.ts +11 -0
  6. package/dist/DateTimeRangePicker/DateTimeRangePicker.d.ts.map +1 -0
  7. package/dist/DateTimeRangePicker/DateTimeRangePicker.js +77 -0
  8. package/dist/DateTimeRangePicker/DateTimeRangePicker.js.map +1 -0
  9. package/dist/{TimeRangeSelector → DateTimeRangePicker}/TimeRangeSelector.d.ts +0 -0
  10. package/dist/DateTimeRangePicker/TimeRangeSelector.d.ts.map +1 -0
  11. package/dist/{TimeRangeSelector → DateTimeRangePicker}/TimeRangeSelector.js +0 -0
  12. package/dist/DateTimeRangePicker/TimeRangeSelector.js.map +1 -0
  13. package/dist/{TimeRangeSelector → DateTimeRangePicker}/index.d.ts +1 -0
  14. package/dist/DateTimeRangePicker/index.d.ts.map +1 -0
  15. package/dist/{TimeRangeSelector → DateTimeRangePicker}/index.js +1 -0
  16. package/dist/DateTimeRangePicker/index.js.map +1 -0
  17. package/dist/{TimeRangeSelector → DateTimeRangePicker}/utils.d.ts +0 -0
  18. package/dist/DateTimeRangePicker/utils.d.ts.map +1 -0
  19. package/dist/{TimeRangeSelector → DateTimeRangePicker}/utils.js +0 -0
  20. package/dist/DateTimeRangePicker/utils.js.map +1 -0
  21. package/dist/Legend/CompactLegend.d.ts +6 -1
  22. package/dist/Legend/CompactLegend.d.ts.map +1 -1
  23. package/dist/Legend/CompactLegend.js +16 -7
  24. package/dist/Legend/CompactLegend.js.map +1 -1
  25. package/dist/Legend/Legend.d.ts.map +1 -1
  26. package/dist/Legend/Legend.js +4 -9
  27. package/dist/Legend/Legend.js.map +1 -1
  28. package/dist/Legend/ListLegend.d.ts +3 -0
  29. package/dist/Legend/ListLegend.d.ts.map +1 -1
  30. package/dist/Legend/ListLegend.js +18 -2
  31. package/dist/Legend/ListLegend.js.map +1 -1
  32. package/dist/Legend/ListLegendItem.d.ts +2 -1
  33. package/dist/Legend/ListLegendItem.d.ts.map +1 -1
  34. package/dist/Legend/ListLegendItem.js +11 -9
  35. package/dist/Legend/ListLegendItem.js.map +1 -1
  36. package/dist/LineChart/LineChart.js +2 -1
  37. package/dist/LineChart/LineChart.js.map +1 -1
  38. package/dist/OptionsEditorLayout/OptionsEditorColumn.d.ts +13 -0
  39. package/dist/OptionsEditorLayout/OptionsEditorColumn.d.ts.map +1 -0
  40. package/dist/OptionsEditorLayout/OptionsEditorColumn.js +28 -0
  41. package/dist/OptionsEditorLayout/OptionsEditorColumn.js.map +1 -0
  42. package/dist/OptionsEditorLayout/OptionsEditorControl.d.ts +5 -0
  43. package/dist/OptionsEditorLayout/OptionsEditorControl.d.ts.map +1 -0
  44. package/dist/OptionsEditorLayout/OptionsEditorControl.js +50 -0
  45. package/dist/OptionsEditorLayout/OptionsEditorControl.js.map +1 -0
  46. package/dist/OptionsEditorLayout/OptionsEditorGrid.d.ts +13 -0
  47. package/dist/OptionsEditorLayout/OptionsEditorGrid.d.ts.map +1 -0
  48. package/dist/OptionsEditorLayout/OptionsEditorGrid.js +26 -0
  49. package/dist/OptionsEditorLayout/OptionsEditorGrid.js.map +1 -0
  50. package/dist/OptionsEditorLayout/OptionsEditorGroup.d.ts +13 -0
  51. package/dist/OptionsEditorLayout/OptionsEditorGroup.d.ts.map +1 -0
  52. package/dist/OptionsEditorLayout/OptionsEditorGroup.js +36 -0
  53. package/dist/OptionsEditorLayout/OptionsEditorGroup.js.map +1 -0
  54. package/dist/OptionsEditorLayout/index.d.ts +5 -0
  55. package/dist/OptionsEditorLayout/index.d.ts.map +1 -0
  56. package/dist/OptionsEditorLayout/index.js +18 -0
  57. package/dist/OptionsEditorLayout/index.js.map +1 -0
  58. package/dist/Tooltip/SeriesInfo.d.ts +1 -0
  59. package/dist/Tooltip/SeriesInfo.d.ts.map +1 -1
  60. package/dist/Tooltip/SeriesInfo.js +3 -3
  61. package/dist/Tooltip/SeriesInfo.js.map +1 -1
  62. package/dist/Tooltip/Tooltip.d.ts +2 -1
  63. package/dist/Tooltip/Tooltip.d.ts.map +1 -1
  64. package/dist/Tooltip/Tooltip.js +2 -2
  65. package/dist/Tooltip/Tooltip.js.map +1 -1
  66. package/dist/Tooltip/TooltipContent.d.ts.map +1 -1
  67. package/dist/Tooltip/TooltipContent.js +2 -1
  68. package/dist/Tooltip/TooltipContent.js.map +1 -1
  69. package/dist/Tooltip/focused-series.d.ts +4 -3
  70. package/dist/Tooltip/focused-series.d.ts.map +1 -1
  71. package/dist/Tooltip/focused-series.js +6 -3
  72. package/dist/Tooltip/focused-series.js.map +1 -1
  73. package/dist/Tooltip/focused-series.test.js +20 -2
  74. package/dist/Tooltip/focused-series.test.js.map +1 -1
  75. package/dist/UnitSelector/UnitSelector.d.ts +8 -0
  76. package/dist/UnitSelector/UnitSelector.d.ts.map +1 -0
  77. package/dist/UnitSelector/UnitSelector.js +112 -0
  78. package/dist/UnitSelector/UnitSelector.js.map +1 -0
  79. package/dist/UnitSelector/UnitSelector.test.d.ts +2 -0
  80. package/dist/UnitSelector/UnitSelector.test.d.ts.map +1 -0
  81. package/dist/UnitSelector/UnitSelector.test.js +211 -0
  82. package/dist/UnitSelector/UnitSelector.test.js.map +1 -0
  83. package/dist/UnitSelector/index.d.ts +2 -0
  84. package/dist/UnitSelector/index.d.ts.map +1 -0
  85. package/dist/UnitSelector/index.js +15 -0
  86. package/dist/UnitSelector/index.js.map +1 -0
  87. package/dist/cjs/{TimeRangeSelector → DateTimeRangePicker}/AbsoluteTimePicker.js +37 -32
  88. package/dist/cjs/DateTimeRangePicker/DateTimeRangePicker.js +83 -0
  89. package/dist/cjs/{TimeRangeSelector → DateTimeRangePicker}/TimeRangeSelector.js +0 -0
  90. package/dist/cjs/{TimeRangeSelector → DateTimeRangePicker}/index.js +1 -0
  91. package/dist/cjs/{TimeRangeSelector → DateTimeRangePicker}/utils.js +0 -0
  92. package/dist/cjs/Legend/CompactLegend.js +12 -6
  93. package/dist/cjs/Legend/Legend.js +4 -9
  94. package/dist/cjs/Legend/ListLegend.js +15 -1
  95. package/dist/cjs/Legend/ListLegendItem.js +48 -13
  96. package/dist/cjs/LineChart/LineChart.js +2 -1
  97. package/dist/cjs/OptionsEditorLayout/OptionsEditorColumn.js +32 -0
  98. package/dist/cjs/OptionsEditorLayout/OptionsEditorControl.js +61 -0
  99. package/dist/cjs/OptionsEditorLayout/OptionsEditorGrid.js +29 -0
  100. package/dist/cjs/OptionsEditorLayout/OptionsEditorGroup.js +40 -0
  101. package/dist/cjs/OptionsEditorLayout/index.js +31 -0
  102. package/dist/cjs/Tooltip/SeriesInfo.js +3 -3
  103. package/dist/cjs/Tooltip/Tooltip.js +2 -2
  104. package/dist/cjs/Tooltip/TooltipContent.js +2 -1
  105. package/dist/cjs/Tooltip/focused-series.js +6 -3
  106. package/dist/cjs/Tooltip/focused-series.test.js +20 -2
  107. package/dist/cjs/UnitSelector/UnitSelector.js +118 -0
  108. package/dist/cjs/UnitSelector/UnitSelector.test.js +218 -0
  109. package/dist/cjs/UnitSelector/index.js +28 -0
  110. package/dist/cjs/index.js +4 -2
  111. package/dist/cjs/model/units/bytes.js +60 -0
  112. package/dist/cjs/model/units/constants.js +23 -0
  113. package/dist/cjs/model/units/decimal.js +67 -0
  114. package/dist/cjs/model/units/index.js +30 -0
  115. package/dist/cjs/model/units/percent.js +64 -0
  116. package/dist/cjs/model/units/time.js +138 -0
  117. package/dist/cjs/model/units/types.js +28 -0
  118. package/dist/cjs/model/units/units.js +104 -0
  119. package/dist/cjs/utils/component-ids.js +31 -0
  120. package/dist/cjs/utils/index.js +1 -0
  121. package/dist/index.d.ts +4 -2
  122. package/dist/index.d.ts.map +1 -1
  123. package/dist/index.js +4 -2
  124. package/dist/index.js.map +1 -1
  125. package/dist/model/units/bytes.d.ts +12 -0
  126. package/dist/model/units/bytes.d.ts.map +1 -0
  127. package/dist/model/units/bytes.js +48 -0
  128. package/dist/model/units/bytes.js.map +1 -0
  129. package/dist/model/units/constants.d.ts +2 -0
  130. package/dist/model/units/constants.d.ts.map +1 -0
  131. package/dist/model/units/constants.js +17 -0
  132. package/dist/model/units/constants.js.map +1 -0
  133. package/dist/model/units/decimal.d.ts +24 -0
  134. package/dist/model/units/decimal.d.ts.map +1 -0
  135. package/dist/model/units/decimal.js +59 -0
  136. package/dist/model/units/decimal.js.map +1 -0
  137. package/dist/model/units/index.d.ts +4 -0
  138. package/dist/model/units/index.d.ts.map +1 -0
  139. package/dist/model/units/index.js +17 -0
  140. package/dist/model/units/index.js.map +1 -0
  141. package/dist/model/units/percent.d.ts +12 -0
  142. package/dist/model/units/percent.d.ts.map +1 -0
  143. package/dist/model/units/percent.js +51 -0
  144. package/dist/model/units/percent.js.map +1 -0
  145. package/dist/model/units/time.d.ts +11 -0
  146. package/dist/model/units/time.d.ts.map +1 -0
  147. package/dist/model/units/time.js +125 -0
  148. package/dist/model/units/time.js.map +1 -0
  149. package/dist/model/units/types.d.ts +38 -0
  150. package/dist/model/units/types.d.ts.map +1 -0
  151. package/dist/model/units/types.js +22 -0
  152. package/dist/model/units/types.js.map +1 -0
  153. package/dist/model/units/units.d.ts +40 -0
  154. package/dist/model/units/units.d.ts.map +1 -0
  155. package/dist/model/units/units.js +83 -0
  156. package/dist/model/units/units.js.map +1 -0
  157. package/dist/utils/component-ids.d.ts +8 -0
  158. package/dist/utils/component-ids.d.ts.map +1 -0
  159. package/dist/utils/component-ids.js +27 -0
  160. package/dist/utils/component-ids.js.map +1 -0
  161. package/dist/utils/index.d.ts +1 -0
  162. package/dist/utils/index.d.ts.map +1 -1
  163. package/dist/utils/index.js +1 -0
  164. package/dist/utils/index.js.map +1 -1
  165. package/package.json +2 -2
  166. package/dist/TimeRangeSelector/AbsoluteTimePicker.d.ts.map +0 -1
  167. package/dist/TimeRangeSelector/AbsoluteTimePicker.js.map +0 -1
  168. package/dist/TimeRangeSelector/TimeRangeSelector.d.ts.map +0 -1
  169. package/dist/TimeRangeSelector/TimeRangeSelector.js.map +0 -1
  170. package/dist/TimeRangeSelector/index.d.ts.map +0 -1
  171. package/dist/TimeRangeSelector/index.js.map +0 -1
  172. package/dist/TimeRangeSelector/utils.d.ts.map +0 -1
  173. package/dist/TimeRangeSelector/utils.js.map +0 -1
  174. package/dist/cjs/model/units.js +0 -203
  175. package/dist/model/units.d.ts +0 -36
  176. package/dist/model/units.d.ts.map +0 -1
  177. package/dist/model/units.js +0 -196
  178. package/dist/model/units.js.map +0 -1
@@ -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
+ });
@@ -0,0 +1,28 @@
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("./UnitSelector"), exports);
18
+ function _exportStar(from, to) {
19
+ Object.keys(from).forEach(function(k) {
20
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
21
+ enumerable: true,
22
+ get: function() {
23
+ return from[k];
24
+ }
25
+ });
26
+ });
27
+ return from;
28
+ }
package/dist/cjs/index.js CHANGED
@@ -14,17 +14,19 @@
14
14
  Object.defineProperty(exports, "__esModule", {
15
15
  value: true
16
16
  });
17
+ _exportStar(require("./DateTimeRangePicker"), exports);
17
18
  _exportStar(require("./Drawer"), exports);
18
19
  _exportStar(require("./EChart"), exports);
19
20
  _exportStar(require("./ErrorAlert"), exports);
20
21
  _exportStar(require("./ErrorBoundary"), exports);
22
+ _exportStar(require("./GaugeChart"), exports);
21
23
  _exportStar(require("./InfoTooltip"), exports);
22
24
  _exportStar(require("./JSONEditor"), exports);
23
25
  _exportStar(require("./Legend"), exports);
24
26
  _exportStar(require("./LineChart"), exports);
25
- _exportStar(require("./GaugeChart"), exports);
27
+ _exportStar(require("./OptionsEditorLayout"), exports);
26
28
  _exportStar(require("./StatChart"), exports);
27
- _exportStar(require("./TimeRangeSelector"), exports);
29
+ _exportStar(require("./UnitSelector"), exports);
28
30
  _exportStar(require("./context/ChartsThemeProvider"), exports);
29
31
  _exportStar(require("./utils"), exports);
30
32
  _exportStar(require("./model"), exports);
@@ -0,0 +1,60 @@
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
+ function _export(target, all) {
18
+ for(var name in all)Object.defineProperty(target, name, {
19
+ enumerable: true,
20
+ get: all[name]
21
+ });
22
+ }
23
+ _export(exports, {
24
+ BYTES_GROUP_CONFIG: ()=>BYTES_GROUP_CONFIG,
25
+ BYTES_UNIT_CONFIG: ()=>BYTES_UNIT_CONFIG,
26
+ formatBytes: ()=>formatBytes
27
+ });
28
+ const bytesUnitKinds = [
29
+ 'Bytes'
30
+ ];
31
+ const BYTES_GROUP_CONFIG = {
32
+ label: 'Bytes',
33
+ decimal_places: true
34
+ };
35
+ const BYTES_UNIT_CONFIG = {
36
+ Bytes: {
37
+ group: 'Bytes',
38
+ label: 'Bytes'
39
+ }
40
+ };
41
+ function formatBytes(bytes, decimals = 2) {
42
+ if (bytes === 0) return '0 Bytes';
43
+ const k = 1024;
44
+ const dm = decimals < 0 ? 0 : decimals;
45
+ const sizes = [
46
+ 'Bytes',
47
+ 'KB',
48
+ 'MB',
49
+ 'GB',
50
+ 'TB',
51
+ 'PB',
52
+ 'EB',
53
+ 'ZB',
54
+ 'YB'
55
+ ];
56
+ // Math.max(0, ...) ensures that we don't return -1 as a value for the index.
57
+ // Why? When the number of bytes are between -1 and 1, Math.floor(Math.log(bytes)/Math.log(1024)) returns -1.
58
+ const i = Math.max(0, Math.floor(Math.log(bytes) / Math.log(k)));
59
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
60
+ }
@@ -0,0 +1,23 @@
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
+ // Common constants needed across individual unit groups and the overall
14
+ // combined units.
15
+ "use strict";
16
+ Object.defineProperty(exports, "__esModule", {
17
+ value: true
18
+ });
19
+ Object.defineProperty(exports, "DEFAULT_DECIMAL_PLACES", {
20
+ enumerable: true,
21
+ get: ()=>DEFAULT_DECIMAL_PLACES
22
+ });
23
+ const DEFAULT_DECIMAL_PLACES = 2;
@@ -0,0 +1,67 @@
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
+ function _export(target, all) {
18
+ for(var name in all)Object.defineProperty(target, name, {
19
+ enumerable: true,
20
+ get: all[name]
21
+ });
22
+ }
23
+ _export(exports, {
24
+ PERCENT_GROUP_CONFIG: ()=>PERCENT_GROUP_CONFIG,
25
+ DECIMAL_UNIT_CONFIG: ()=>DECIMAL_UNIT_CONFIG,
26
+ formatDecimal: ()=>formatDecimal,
27
+ abbreviateLargeNumber: ()=>abbreviateLargeNumber,
28
+ formatNumber: ()=>formatNumber
29
+ });
30
+ const _mathjs = require("../../utils/mathjs");
31
+ const _constants = require("./constants");
32
+ const decimalUnitKinds = [
33
+ 'Decimal'
34
+ ];
35
+ const PERCENT_GROUP_CONFIG = {
36
+ label: 'Percent',
37
+ decimal_places: true
38
+ };
39
+ const DECIMAL_UNIT_CONFIG = {
40
+ Decimal: {
41
+ group: 'Decimal',
42
+ label: 'Decimal'
43
+ }
44
+ };
45
+ function formatDecimal(value, unitOptions) {
46
+ var _decimal_places;
47
+ const decimals = (_decimal_places = unitOptions.decimal_places) !== null && _decimal_places !== void 0 ? _decimal_places : _constants.DEFAULT_DECIMAL_PLACES;
48
+ if (unitOptions.abbreviate === true) {
49
+ return abbreviateLargeNumber(value, decimals);
50
+ }
51
+ const formatParams = {
52
+ style: 'decimal',
53
+ minimumFractionDigits: decimals,
54
+ maximumFractionDigits: decimals,
55
+ useGrouping: true
56
+ };
57
+ const decimalFormatter = new Intl.NumberFormat('en-US', formatParams);
58
+ return decimalFormatter.format(value);
59
+ }
60
+ function abbreviateLargeNumber(num, decimals = 2) {
61
+ const modifier = (n)=>(0, _mathjs.round)(n, decimals);
62
+ return formatNumber(num, modifier);
63
+ }
64
+ function formatNumber(num, modifier) {
65
+ const fn = modifier !== null && modifier !== void 0 ? modifier : Math.round;
66
+ return num >= 1e12 ? fn(num / 1e12) + 'T' : num >= 1e9 ? fn(num / 1e9) + 'B' : num >= 1e6 ? fn(num / 1e6) + 'M' : num >= 1e3 ? fn(num / 1e3) + 'K' : num.toString();
67
+ }
@@ -0,0 +1,30 @@
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("./units"), exports);
18
+ _exportStar(require("./constants"), exports);
19
+ _exportStar(require("./types"), exports);
20
+ function _exportStar(from, to) {
21
+ Object.keys(from).forEach(function(k) {
22
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
23
+ enumerable: true,
24
+ get: function() {
25
+ return from[k];
26
+ }
27
+ });
28
+ });
29
+ return from;
30
+ }