@perses-dev/core 0.34.0 → 0.36.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 (72) hide show
  1. package/dist/cjs/model/calculations.js +112 -33
  2. package/dist/cjs/model/index.js +1 -0
  3. package/dist/cjs/model/legend.js +19 -2
  4. package/dist/cjs/model/units/bytes.js +84 -0
  5. package/dist/cjs/model/units/constants.js +23 -0
  6. package/dist/cjs/model/units/decimal.js +62 -0
  7. package/dist/cjs/model/units/index.js +31 -0
  8. package/dist/cjs/model/units/percent.js +73 -0
  9. package/dist/cjs/model/units/time.js +105 -0
  10. package/dist/cjs/model/units/types.js +28 -0
  11. package/dist/cjs/model/units/units.js +101 -0
  12. package/dist/cjs/model/units/utils.js +42 -0
  13. package/dist/cjs/model/variables.js +5 -0
  14. package/dist/model/calculations.d.ts +29 -5
  15. package/dist/model/calculations.d.ts.map +1 -1
  16. package/dist/model/calculations.js +121 -28
  17. package/dist/model/calculations.js.map +1 -1
  18. package/dist/model/dashboard.d.ts +1 -0
  19. package/dist/model/dashboard.d.ts.map +1 -1
  20. package/dist/model/dashboard.js.map +1 -1
  21. package/dist/model/index.d.ts +1 -0
  22. package/dist/model/index.d.ts.map +1 -1
  23. package/dist/model/index.js +1 -0
  24. package/dist/model/index.js.map +1 -1
  25. package/dist/model/legend.d.ts +5 -0
  26. package/dist/model/legend.d.ts.map +1 -1
  27. package/dist/model/legend.js +15 -1
  28. package/dist/model/legend.js.map +1 -1
  29. package/dist/model/thresholds.d.ts +4 -0
  30. package/dist/model/thresholds.d.ts.map +1 -1
  31. package/dist/model/thresholds.js.map +1 -1
  32. package/dist/model/units/bytes.d.ts +13 -0
  33. package/dist/model/units/bytes.d.ts.map +1 -0
  34. package/dist/model/units/bytes.js +66 -0
  35. package/dist/model/units/bytes.js.map +1 -0
  36. package/dist/model/units/constants.d.ts +2 -0
  37. package/dist/model/units/constants.d.ts.map +1 -0
  38. package/dist/model/units/constants.js +17 -0
  39. package/dist/model/units/constants.js.map +1 -0
  40. package/dist/model/units/decimal.d.ts +13 -0
  41. package/dist/model/units/decimal.d.ts.map +1 -0
  42. package/dist/model/units/decimal.js +49 -0
  43. package/dist/model/units/decimal.js.map +1 -0
  44. package/dist/model/units/index.d.ts +5 -0
  45. package/dist/model/units/index.d.ts.map +1 -0
  46. package/dist/model/units/index.js +18 -0
  47. package/dist/model/units/index.js.map +1 -0
  48. package/dist/model/units/percent.d.ts +12 -0
  49. package/dist/model/units/percent.d.ts.map +1 -0
  50. package/dist/model/units/percent.js +60 -0
  51. package/dist/model/units/percent.js.map +1 -0
  52. package/dist/model/units/time.d.ts +22 -0
  53. package/dist/model/units/time.d.ts.map +1 -0
  54. package/dist/model/units/time.js +91 -0
  55. package/dist/model/units/time.js.map +1 -0
  56. package/dist/model/units/types.d.ts +47 -0
  57. package/dist/model/units/types.d.ts.map +1 -0
  58. package/dist/model/units/types.js +22 -0
  59. package/dist/model/units/types.js.map +1 -0
  60. package/dist/model/units/units.d.ts +40 -0
  61. package/dist/model/units/units.d.ts.map +1 -0
  62. package/dist/model/units/units.js +80 -0
  63. package/dist/model/units/units.js.map +1 -0
  64. package/dist/model/units/utils.d.ts +4 -0
  65. package/dist/model/units/utils.d.ts.map +1 -0
  66. package/dist/model/units/utils.js +32 -0
  67. package/dist/model/units/utils.js.map +1 -0
  68. package/dist/model/variables.d.ts +1 -0
  69. package/dist/model/variables.d.ts.map +1 -1
  70. package/dist/model/variables.js +1 -1
  71. package/dist/model/variables.js.map +1 -1
  72. package/package.json +3 -2
@@ -23,67 +23,146 @@ function _export(target, all) {
23
23
  _export(exports, {
24
24
  CalculationsMap: ()=>CalculationsMap,
25
25
  CALCULATIONS_CONFIG: ()=>CALCULATIONS_CONFIG,
26
- DEFAULT_CALCULATION: ()=>DEFAULT_CALCULATION
26
+ DEFAULT_CALCULATION: ()=>DEFAULT_CALCULATION,
27
+ getCalculations: ()=>getCalculations,
28
+ getCalculation: ()=>getCalculation
27
29
  });
28
- const _findLast = /*#__PURE__*/ _interopRequireDefault(require("lodash/findLast"));
29
- const _meanBy = /*#__PURE__*/ _interopRequireDefault(require("lodash/meanBy"));
30
- const _sumBy = /*#__PURE__*/ _interopRequireDefault(require("lodash/sumBy"));
31
- function _interopRequireDefault(obj) {
32
- return obj && obj.__esModule ? obj : {
33
- default: obj
34
- };
35
- }
36
30
  const CalculationsMap = {
37
31
  First: first,
38
32
  Last: last,
33
+ FirstNumber: firstNumber,
39
34
  LastNumber: lastNumber,
40
35
  Mean: mean,
41
- Sum: sum
36
+ Sum: sum,
37
+ Min: min,
38
+ Max: max
42
39
  };
43
40
  const CALCULATIONS_CONFIG = {
44
41
  First: {
45
- label: 'First'
42
+ label: 'First',
43
+ description: 'First value'
46
44
  },
47
45
  Last: {
48
- label: 'Last'
46
+ label: 'Last',
47
+ description: 'Last value'
48
+ },
49
+ FirstNumber: {
50
+ label: 'First *',
51
+ description: 'First numeric value'
49
52
  },
50
53
  LastNumber: {
51
- label: 'Last number'
54
+ label: 'Last *',
55
+ description: 'Last numeric value'
52
56
  },
53
57
  Mean: {
54
- label: 'Mean'
58
+ label: 'Avg',
59
+ description: 'Average value excluding nulls'
55
60
  },
56
61
  Sum: {
57
- label: 'Sum'
62
+ label: 'Sum',
63
+ description: 'The sum of all values'
64
+ },
65
+ Min: {
66
+ label: 'Min',
67
+ description: 'Minimum value'
68
+ },
69
+ Max: {
70
+ label: 'Max',
71
+ description: 'Maximum value'
58
72
  }
59
73
  };
60
74
  const DEFAULT_CALCULATION = 'Sum';
75
+ function getCalculations(values, includeCalculations) {
76
+ const calculations = includeCalculations.reduce((initResult, calculation)=>{
77
+ initResult[calculation] = undefined;
78
+ return initResult;
79
+ }, {});
80
+ // We save these values as separate values instead of directly setting them
81
+ // in the calculations because they are needed by multiple calculations.
82
+ let nonNullCount = 0;
83
+ let sum = 0;
84
+ // We use this large function capable of performing one or more calculations
85
+ // in a single iteration of the data to minimize the performance impact of
86
+ // generating multiple calculations for large timeseries values. This is
87
+ // less optimized for certain single calculations when done in isolation (e.g.
88
+ // `Last`), but will be more performant in the more expensive cases where
89
+ // multiple values are being used (e.g. table legend).
90
+ values.forEach((tuple, i)=>{
91
+ const value = tuple[1];
92
+ if (i === 0 && 'First' in calculations) {
93
+ calculations.First = value;
94
+ }
95
+ if (i === values.length - 1 && 'Last' in calculations) {
96
+ calculations.Last = value;
97
+ }
98
+ // Handling specific to non-null values.
99
+ if (typeof value === 'number') {
100
+ nonNullCount += 1;
101
+ sum += value;
102
+ if ('FirstNumber' in calculations && calculations.FirstNumber === undefined) {
103
+ // Save the first number we see.
104
+ calculations.FirstNumber = value;
105
+ }
106
+ if ('LastNumber' in calculations) {
107
+ // Keep setting the numbers we see, which will eventually be set to the
108
+ // last number when finished iterating.
109
+ calculations.LastNumber = value;
110
+ }
111
+ if ('Min' in calculations) {
112
+ if (typeof calculations.Min !== 'number') {
113
+ // Init the first time we see a number
114
+ calculations.Min = value;
115
+ } else {
116
+ // Use lowest value once initialized
117
+ calculations.Min = Math.min(calculations.Min, value);
118
+ }
119
+ }
120
+ if ('Max' in calculations) {
121
+ if (typeof calculations.Max !== 'number') {
122
+ // Init the first time we see a number
123
+ calculations.Max = value;
124
+ } else {
125
+ // Use highest value once initialized
126
+ calculations.Max = Math.max(calculations.Max, value);
127
+ }
128
+ }
129
+ }
130
+ });
131
+ // Set calculations that require iterating over all values.
132
+ if (nonNullCount > 0 && 'Sum' in calculations) {
133
+ calculations.Sum = sum;
134
+ }
135
+ if (nonNullCount > 0 && 'Mean' in calculations) {
136
+ calculations.Mean = sum / nonNullCount;
137
+ }
138
+ return calculations;
139
+ }
140
+ function getCalculation(values, calculation) {
141
+ return getCalculations(values, [
142
+ calculation
143
+ ])[calculation];
144
+ }
61
145
  function first(values) {
62
- const tuple = values[0];
63
- return tuple === undefined ? undefined : getValue(tuple);
146
+ return getCalculation(values, 'First');
64
147
  }
65
148
  function last(values) {
66
- if (values.length <= 0) return undefined;
67
- const tuple = values[values.length - 1];
68
- return tuple === undefined ? undefined : getValue(tuple);
149
+ return getCalculation(values, 'Last');
150
+ }
151
+ function firstNumber(values) {
152
+ return getCalculation(values, 'FirstNumber');
69
153
  }
70
154
  function lastNumber(values) {
71
- const tuple = (0, _findLast.default)(values, (tuple)=>isNaN(getValue(tuple)) === false);
72
- return tuple === undefined ? undefined : getValue(tuple);
155
+ return getCalculation(values, 'LastNumber');
73
156
  }
74
157
  function mean(values) {
75
- if (values.length <= 0) return undefined;
76
- return (0, _meanBy.default)(values, getValue);
158
+ return getCalculation(values, 'Mean');
77
159
  }
78
160
  function sum(values) {
79
- if (values.length <= 0) return undefined;
80
- return (0, _sumBy.default)(values, getValue);
161
+ return getCalculation(values, 'Sum');
81
162
  }
82
- function getValue(valueTuple) {
83
- const value = valueTuple[1];
84
- if (value !== null) {
85
- return value;
86
- }
87
- // TODO: refactor utils so null can be returned and LastNotNull supported
88
- return NaN;
163
+ function min(values) {
164
+ return getCalculation(values, 'Min');
165
+ }
166
+ function max(values) {
167
+ return getCalculation(values, 'Max');
89
168
  }
@@ -30,6 +30,7 @@ _exportStar(require("./thresholds"), exports);
30
30
  _exportStar(require("./time"), exports);
31
31
  _exportStar(require("./time-series-data"), exports);
32
32
  _exportStar(require("./time-series-queries"), exports);
33
+ _exportStar(require("./units"), exports);
33
34
  _exportStar(require("./variables"), exports);
34
35
  function _exportStar(from, to) {
35
36
  Object.keys(from).forEach(function(k) {
@@ -27,11 +27,14 @@ function _export(target, all) {
27
27
  _export(exports, {
28
28
  legendPositions: ()=>legendPositions,
29
29
  legendModes: ()=>legendModes,
30
+ legendSizes: ()=>legendSizes,
30
31
  isValidLegendPosition: ()=>isValidLegendPosition,
31
32
  isValidLegendMode: ()=>isValidLegendMode,
33
+ isValidLegendSize: ()=>isValidLegendSize,
32
34
  DEFAULT_LEGEND: ()=>DEFAULT_LEGEND,
33
35
  getLegendPosition: ()=>getLegendPosition,
34
- getLegendMode: ()=>getLegendMode
36
+ getLegendMode: ()=>getLegendMode,
37
+ getLegendSize: ()=>getLegendSize
35
38
  });
36
39
  const legendPositions = [
37
40
  'Bottom',
@@ -41,15 +44,23 @@ const legendModes = [
41
44
  'List',
42
45
  'Table'
43
46
  ];
47
+ const legendSizes = [
48
+ 'Small',
49
+ 'Medium'
50
+ ];
44
51
  function isValidLegendPosition(position) {
45
52
  return legendPositions.includes(position);
46
53
  }
47
54
  function isValidLegendMode(mode) {
48
55
  return legendModes.includes(mode);
49
56
  }
57
+ function isValidLegendSize(size) {
58
+ return legendSizes.includes(size);
59
+ }
50
60
  const DEFAULT_LEGEND = {
51
61
  position: 'Bottom',
52
- mode: 'List'
62
+ mode: 'List',
63
+ size: 'Medium'
53
64
  };
54
65
  function getLegendPosition(position) {
55
66
  if (position === undefined) {
@@ -66,3 +77,9 @@ function getLegendMode(mode) {
66
77
  }
67
78
  return mode;
68
79
  }
80
+ function getLegendSize(size) {
81
+ if (!size || !isValidLegendSize(size)) {
82
+ return DEFAULT_LEGEND.size;
83
+ }
84
+ return size;
85
+ }
@@ -0,0 +1,84 @@
1
+ // Copyright 2023 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 _numbro = /*#__PURE__*/ _interopRequireDefault(require("numbro"));
29
+ const _constants = require("./constants");
30
+ const _utils = require("./utils");
31
+ function _interopRequireDefault(obj) {
32
+ return obj && obj.__esModule ? obj : {
33
+ default: obj
34
+ };
35
+ }
36
+ const DEFAULT_NUMBRO_MANTISSA = 2;
37
+ const bytesUnitKinds = [
38
+ 'Bytes'
39
+ ];
40
+ const BYTES_GROUP_CONFIG = {
41
+ label: 'Bytes',
42
+ decimal_places: true,
43
+ abbreviate: true
44
+ };
45
+ const BYTES_UNIT_CONFIG = {
46
+ // These units are powers of 1000.
47
+ // In other words, 1 KB = 1000 bytes.
48
+ Bytes: {
49
+ group: 'Bytes',
50
+ label: 'Bytes'
51
+ }
52
+ };
53
+ function formatBytes(bytes, options) {
54
+ const { abbreviate , decimal_places } = options;
55
+ if (!(0, _utils.shouldAbbreviate)(abbreviate) || Math.abs(bytes) < 1000) {
56
+ const formatterOptions = {
57
+ style: 'unit',
58
+ unit: 'byte',
59
+ unitDisplay: 'long',
60
+ useGrouping: true
61
+ };
62
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
63
+ formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
64
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
65
+ } else {
66
+ // This can happen if bytes is between -1000 and 1000
67
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
68
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
69
+ }
70
+ }
71
+ return Intl.NumberFormat('en-US', formatterOptions).format(bytes);
72
+ }
73
+ // numbro is able to add units like KB, MB, GB, etc. correctly
74
+ return (0, _numbro.default)(bytes).format({
75
+ output: 'byte',
76
+ base: 'decimal',
77
+ spaceSeparated: true,
78
+ mantissa: (0, _utils.hasDecimalPlaces)(decimal_places) ? decimal_places : DEFAULT_NUMBRO_MANTISSA,
79
+ // trimMantissa trims trailing 0s
80
+ trimMantissa: !(0, _utils.hasDecimalPlaces)(decimal_places),
81
+ // optionalMantissa excludes all the decimal places if they're all zeros
82
+ optionalMantissa: !(0, _utils.hasDecimalPlaces)(decimal_places)
83
+ });
84
+ }
@@ -0,0 +1,23 @@
1
+ // Copyright 2023 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, "MAX_SIGNIFICANT_DIGITS", {
20
+ enumerable: true,
21
+ get: ()=>MAX_SIGNIFICANT_DIGITS
22
+ });
23
+ const MAX_SIGNIFICANT_DIGITS = 3;
@@ -0,0 +1,62 @@
1
+ // Copyright 2023 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
+ DECIMAL_GROUP_CONFIG: ()=>DECIMAL_GROUP_CONFIG,
25
+ DECIMAL_UNIT_CONFIG: ()=>DECIMAL_UNIT_CONFIG,
26
+ formatDecimal: ()=>formatDecimal
27
+ });
28
+ const _constants = require("./constants");
29
+ const _utils = require("./utils");
30
+ const decimalUnitKinds = [
31
+ 'Decimal'
32
+ ];
33
+ const DECIMAL_GROUP_CONFIG = {
34
+ label: 'Decimal',
35
+ decimal_places: true,
36
+ abbreviate: true
37
+ };
38
+ const DECIMAL_UNIT_CONFIG = {
39
+ Decimal: {
40
+ group: 'Decimal',
41
+ label: 'Decimal'
42
+ }
43
+ };
44
+ function formatDecimal(value, options) {
45
+ const { abbreviate , decimal_places } = options;
46
+ const formatterOptions = {
47
+ style: 'decimal',
48
+ useGrouping: true
49
+ };
50
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
51
+ formatterOptions.notation = 'compact';
52
+ }
53
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
54
+ formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
55
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
56
+ } else {
57
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
58
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
59
+ }
60
+ }
61
+ return Intl.NumberFormat('en-US', formatterOptions).format(value);
62
+ }
@@ -0,0 +1,31 @@
1
+ // Copyright 2023 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("./utils"), exports);
18
+ _exportStar(require("./units"), exports);
19
+ _exportStar(require("./constants"), exports);
20
+ _exportStar(require("./types"), 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
+ }
@@ -0,0 +1,73 @@
1
+ // Copyright 2023 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
+ PERCENT_UNIT_CONFIG: ()=>PERCENT_UNIT_CONFIG,
26
+ formatPercent: ()=>formatPercent
27
+ });
28
+ const _constants = require("./constants");
29
+ const _utils = require("./utils");
30
+ const percentUnitKinds = [
31
+ 'Percent',
32
+ 'PercentDecimal',
33
+ '%'
34
+ ];
35
+ const PERCENT_GROUP_CONFIG = {
36
+ label: 'Percent',
37
+ decimal_places: true
38
+ };
39
+ const PERCENT_GROUP = 'Percent';
40
+ const PERCENT_UNIT_CONFIG = {
41
+ Percent: {
42
+ group: PERCENT_GROUP,
43
+ label: 'Percent (0-100)'
44
+ },
45
+ PercentDecimal: {
46
+ group: PERCENT_GROUP,
47
+ label: 'Percent (0.0-1.0)'
48
+ },
49
+ '%': {
50
+ // This option is not shown in the selector because it is a shorthand
51
+ // duplicate of `Percent`.
52
+ disableSelectorOption: true,
53
+ group: PERCENT_GROUP,
54
+ label: '%'
55
+ }
56
+ };
57
+ function formatPercent(value, { kind , decimal_places }) {
58
+ // Intl.NumberFormat translates 0 -> 0%, 0.5 -> 50%, 1 -> 100%
59
+ if (kind === 'Percent') {
60
+ value = value / 100;
61
+ }
62
+ const formatterOptions = {
63
+ style: 'percent',
64
+ useGrouping: true
65
+ };
66
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
67
+ formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
68
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
69
+ } else {
70
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
71
+ }
72
+ return Intl.NumberFormat('en-US', formatterOptions).format(value);
73
+ }
@@ -0,0 +1,105 @@
1
+ // Copyright 2023 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
+ PersesTimeToIntlTime: ()=>PersesTimeToIntlTime,
25
+ TIME_GROUP_CONFIG: ()=>TIME_GROUP_CONFIG,
26
+ TIME_UNIT_CONFIG: ()=>TIME_UNIT_CONFIG,
27
+ formatTime: ()=>formatTime
28
+ });
29
+ const _constants = require("./constants");
30
+ const _utils = require("./utils");
31
+ const timeUnitKinds = [
32
+ 'Milliseconds',
33
+ 'Seconds',
34
+ 'Minutes',
35
+ 'Hours',
36
+ 'Days',
37
+ 'Weeks',
38
+ 'Months',
39
+ 'Years'
40
+ ];
41
+ const TIME_GROUP = 'Time';
42
+ const TIME_GROUP_CONFIG = {
43
+ label: 'Time',
44
+ decimal_places: true
45
+ };
46
+ const TIME_UNIT_CONFIG = {
47
+ Milliseconds: {
48
+ group: TIME_GROUP,
49
+ label: 'Milliseconds'
50
+ },
51
+ Seconds: {
52
+ group: TIME_GROUP,
53
+ label: 'Seconds'
54
+ },
55
+ Minutes: {
56
+ group: TIME_GROUP,
57
+ label: 'Minutes'
58
+ },
59
+ Hours: {
60
+ group: TIME_GROUP,
61
+ label: 'Hours'
62
+ },
63
+ Days: {
64
+ group: TIME_GROUP,
65
+ label: 'Days'
66
+ },
67
+ Weeks: {
68
+ group: TIME_GROUP,
69
+ label: 'Weeks'
70
+ },
71
+ Months: {
72
+ group: TIME_GROUP,
73
+ label: 'Months'
74
+ },
75
+ Years: {
76
+ group: TIME_GROUP,
77
+ label: 'Years'
78
+ }
79
+ };
80
+ var PersesTimeToIntlTime;
81
+ (function(PersesTimeToIntlTime) {
82
+ PersesTimeToIntlTime["Milliseconds"] = 'millisecond';
83
+ PersesTimeToIntlTime["Seconds"] = 'second';
84
+ PersesTimeToIntlTime["Minutes"] = 'minute';
85
+ PersesTimeToIntlTime["Hours"] = 'hour';
86
+ PersesTimeToIntlTime["Days"] = 'day';
87
+ PersesTimeToIntlTime["Weeks"] = 'week';
88
+ PersesTimeToIntlTime["Months"] = 'month';
89
+ PersesTimeToIntlTime["Years"] = 'year';
90
+ })(PersesTimeToIntlTime || (PersesTimeToIntlTime = {}));
91
+ function formatTime(value, { kind , decimal_places }) {
92
+ const isMonthOrYear = kind === 'Months' || kind === 'Years';
93
+ const formatterOptions = {
94
+ style: 'unit',
95
+ unit: PersesTimeToIntlTime[kind],
96
+ unitDisplay: isMonthOrYear ? 'long' : 'narrow'
97
+ };
98
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
99
+ formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
100
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
101
+ } else {
102
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
103
+ }
104
+ return Intl.NumberFormat('en-US', formatterOptions).format(value);
105
+ }
@@ -0,0 +1,28 @@
1
+ // Copyright 2023 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 types needed across individual unit groups and the overall combined
14
+ // units.
15
+ "use strict";
16
+ Object.defineProperty(exports, "__esModule", {
17
+ value: true
18
+ });
19
+ Object.defineProperty(exports, "UNIT_GROUPS", {
20
+ enumerable: true,
21
+ get: ()=>UNIT_GROUPS
22
+ });
23
+ const UNIT_GROUPS = [
24
+ 'Time',
25
+ 'Percent',
26
+ 'Decimal',
27
+ 'Bytes'
28
+ ];