@perses-dev/core 0.53.0-beta.1 → 0.53.0-beta.3
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.
- package/dist/cjs/model/base-metadata.js +16 -0
- package/dist/cjs/model/profile-data.js +1 -5
- package/dist/cjs/model/units/bits.js +105 -0
- package/dist/cjs/model/units/date.js +297 -0
- package/dist/cjs/model/units/temperature.js +64 -0
- package/dist/cjs/model/units/throughput.js +22 -1
- package/dist/cjs/model/units/time.js +16 -2
- package/dist/cjs/model/units/units.js +39 -3
- package/dist/model/base-metadata.d.ts +17 -0
- package/dist/model/base-metadata.d.ts.map +1 -0
- package/dist/model/base-metadata.js +15 -0
- package/dist/model/base-metadata.js.map +1 -0
- package/dist/model/log-data.d.ts +2 -9
- package/dist/model/log-data.d.ts.map +1 -1
- package/dist/model/log-data.js.map +1 -1
- package/dist/model/profile-data.d.ts +2 -1
- package/dist/model/profile-data.d.ts.map +1 -1
- package/dist/model/profile-data.js +1 -5
- package/dist/model/profile-data.js.map +1 -1
- package/dist/model/time-series-data.d.ts +3 -9
- package/dist/model/time-series-data.d.ts.map +1 -1
- package/dist/model/time-series-data.js.map +1 -1
- package/dist/model/trace-data.d.ts +5 -2
- package/dist/model/trace-data.d.ts.map +1 -1
- package/dist/model/trace-data.js.map +1 -1
- package/dist/model/units/bits.d.ts +12 -0
- package/dist/model/units/bits.d.ts.map +1 -0
- package/dist/model/units/bits.js +81 -0
- package/dist/model/units/bits.js.map +1 -0
- package/dist/model/units/date.d.ts +28 -0
- package/dist/model/units/date.d.ts.map +1 -0
- package/dist/model/units/date.js +278 -0
- package/dist/model/units/date.js.map +1 -0
- package/dist/model/units/temperature.d.ts +11 -0
- package/dist/model/units/temperature.d.ts.map +1 -0
- package/dist/model/units/temperature.js +45 -0
- package/dist/model/units/temperature.js.map +1 -0
- package/dist/model/units/throughput.d.ts +1 -1
- package/dist/model/units/throughput.d.ts.map +1 -1
- package/dist/model/units/throughput.js +22 -1
- package/dist/model/units/throughput.js.map +1 -1
- package/dist/model/units/time.d.ts +3 -1
- package/dist/model/units/time.d.ts.map +1 -1
- package/dist/model/units/time.js +16 -2
- package/dist/model/units/time.js.map +1 -1
- package/dist/model/units/types.d.ts +1 -1
- package/dist/model/units/types.d.ts.map +1 -1
- package/dist/model/units/types.js.map +1 -1
- package/dist/model/units/units.d.ts +26 -1
- package/dist/model/units/units.d.ts.map +1 -1
- package/dist/model/units/units.js +30 -3
- package/dist/model/units/units.js.map +1 -1
- package/dist/schema/datasource.d.ts +4 -4
- package/dist/schema/metadata.d.ts +2 -2
- package/dist/schema/role.d.ts +4 -4
- package/dist/schema/rolebinding.d.ts +4 -4
- package/dist/schema/secret.d.ts +8 -8
- package/dist/schema/variable.d.ts +8 -8
- package/package.json +1 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// Copyright 2025 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
|
+
const DATE_GROUP = 'Date';
|
|
14
|
+
export const DATE_GROUP_CONFIG = {
|
|
15
|
+
label: 'Date & Time'
|
|
16
|
+
};
|
|
17
|
+
export const DATE_UNIT_CONFIG = {
|
|
18
|
+
'datetime-iso': {
|
|
19
|
+
group: DATE_GROUP,
|
|
20
|
+
label: 'DateTime (GMT)'
|
|
21
|
+
},
|
|
22
|
+
'datetime-us': {
|
|
23
|
+
group: DATE_GROUP,
|
|
24
|
+
label: 'DateTime (US-East)'
|
|
25
|
+
},
|
|
26
|
+
'datetime-local': {
|
|
27
|
+
group: DATE_GROUP,
|
|
28
|
+
label: 'DateTime (Browser Local)'
|
|
29
|
+
},
|
|
30
|
+
'date-iso': {
|
|
31
|
+
group: DATE_GROUP,
|
|
32
|
+
label: 'Date (GMT)'
|
|
33
|
+
},
|
|
34
|
+
'date-us': {
|
|
35
|
+
group: DATE_GROUP,
|
|
36
|
+
label: 'Date (US-East)'
|
|
37
|
+
},
|
|
38
|
+
'date-local': {
|
|
39
|
+
group: DATE_GROUP,
|
|
40
|
+
label: 'Date (Browser Local)'
|
|
41
|
+
},
|
|
42
|
+
'time-local': {
|
|
43
|
+
group: DATE_GROUP,
|
|
44
|
+
label: 'Time (Browser Local)'
|
|
45
|
+
},
|
|
46
|
+
'time-iso': {
|
|
47
|
+
group: DATE_GROUP,
|
|
48
|
+
label: 'Time (GMT)'
|
|
49
|
+
},
|
|
50
|
+
'time-us': {
|
|
51
|
+
group: DATE_GROUP,
|
|
52
|
+
label: 'Time (US-East)'
|
|
53
|
+
},
|
|
54
|
+
'relative-time': {
|
|
55
|
+
group: DATE_GROUP,
|
|
56
|
+
label: 'Relative Time'
|
|
57
|
+
},
|
|
58
|
+
'unix-timestamp': {
|
|
59
|
+
group: DATE_GROUP,
|
|
60
|
+
label: 'Unix Timestamp (s)'
|
|
61
|
+
},
|
|
62
|
+
'unix-timestamp-ms': {
|
|
63
|
+
group: DATE_GROUP,
|
|
64
|
+
label: 'Unix Timestamp (ms)'
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Converts a numeric value to a Date object.
|
|
69
|
+
* Handles both Unix timestamps (seconds) and millisecond timestamps.
|
|
70
|
+
*/ function valueToDate(value) {
|
|
71
|
+
// Timestamp detection logic with special case handling
|
|
72
|
+
// Main threshold stays at 10 billion to maintain existing behavior
|
|
73
|
+
// Handle special edge cases explicitly
|
|
74
|
+
// Special case: negative timestamps
|
|
75
|
+
if (value < 0) {
|
|
76
|
+
// For negative values, check magnitude to distinguish seconds vs milliseconds
|
|
77
|
+
// Large negative values (> 1 billion in magnitude) are likely milliseconds
|
|
78
|
+
// Small negative values are likely seconds (rare edge case)
|
|
79
|
+
if (Math.abs(value) > 10000000000) {
|
|
80
|
+
return new Date(value); // milliseconds
|
|
81
|
+
} else {
|
|
82
|
+
return new Date(value * 1000); // seconds
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Special case: year 9999 in seconds (~253402300799)
|
|
86
|
+
// This is a very specific edge case for far-future timestamps
|
|
87
|
+
if (value >= 250000000000 && value <= 260000000000) {
|
|
88
|
+
// Check if this looks like year 9999 in seconds
|
|
89
|
+
const asSeconds = new Date(value * 1000);
|
|
90
|
+
const year = asSeconds.getUTCFullYear();
|
|
91
|
+
if (year >= 9999) {
|
|
92
|
+
return asSeconds; // seconds
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const SECONDS_THRESHOLD = 10000000000; // ~year 2286 - original threshold
|
|
96
|
+
if (value < SECONDS_THRESHOLD) {
|
|
97
|
+
// Assume it's in seconds
|
|
98
|
+
return new Date(value * 1000);
|
|
99
|
+
} else {
|
|
100
|
+
// Assume it's in milliseconds
|
|
101
|
+
return new Date(value);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Formats a relative time string using the Intl.RelativeTimeFormat API.
|
|
106
|
+
*/ function formatRelativeTime(date, referenceTime, locale) {
|
|
107
|
+
const referenceDate = new Date(referenceTime);
|
|
108
|
+
const diffMs = date.getTime() - referenceDate.getTime();
|
|
109
|
+
const units = [
|
|
110
|
+
{
|
|
111
|
+
unit: 'year',
|
|
112
|
+
ms: 1000 * 60 * 60 * 24 * 365
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
unit: 'month',
|
|
116
|
+
ms: 1000 * 60 * 60 * 24 * 30
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
unit: 'week',
|
|
120
|
+
ms: 1000 * 60 * 60 * 24 * 7
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
unit: 'day',
|
|
124
|
+
ms: 1000 * 60 * 60 * 24
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
unit: 'hour',
|
|
128
|
+
ms: 1000 * 60 * 60
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
unit: 'minute',
|
|
132
|
+
ms: 1000 * 60
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
unit: 'second',
|
|
136
|
+
ms: 1000
|
|
137
|
+
}
|
|
138
|
+
];
|
|
139
|
+
for (const { unit, ms } of units){
|
|
140
|
+
// Determine the value for the current unit, ensuring it's an integer for Intl.RelativeTimeFormat
|
|
141
|
+
const value = Math.round(diffMs / ms);
|
|
142
|
+
// If the absolute value is 1 or more, use this unit
|
|
143
|
+
if (Math.abs(value) >= 1) {
|
|
144
|
+
const rtf = new Intl.RelativeTimeFormat(locale, {
|
|
145
|
+
numeric: 'auto'
|
|
146
|
+
});
|
|
147
|
+
return rtf.format(value, unit);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// If less than a second, show "now" or "0 seconds"
|
|
151
|
+
const rtf = new Intl.RelativeTimeFormat(locale, {
|
|
152
|
+
numeric: 'auto'
|
|
153
|
+
});
|
|
154
|
+
return rtf.format(0, 'second');
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Gets the browser's preferred locale with comprehensive fallbacks.
|
|
158
|
+
*/ const getBrowserLocale = ()=>{
|
|
159
|
+
if (typeof navigator !== 'undefined') {
|
|
160
|
+
if (navigator.language) return navigator.language;
|
|
161
|
+
if (navigator.languages && navigator.languages.length > 0) {
|
|
162
|
+
const firstLanguage = navigator.languages[0];
|
|
163
|
+
if (firstLanguage) return firstLanguage;
|
|
164
|
+
}
|
|
165
|
+
// Legacy fallbacks for older browsers
|
|
166
|
+
const nav = navigator;
|
|
167
|
+
if (nav.userLanguage) return nav.userLanguage;
|
|
168
|
+
if (nav.browserLanguage) return nav.browserLanguage;
|
|
169
|
+
if (nav.systemLanguage) return nav.systemLanguage;
|
|
170
|
+
}
|
|
171
|
+
// Node.js or server-side fallback, or if navigator is not available/empty
|
|
172
|
+
return Intl.DateTimeFormat().resolvedOptions().locale || 'en-US';
|
|
173
|
+
};
|
|
174
|
+
export function formatDate(value, options = {}) {
|
|
175
|
+
const systemTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
176
|
+
const { unit = 'datetime-local', locale = getBrowserLocale(), timeZone = systemTimeZone, referenceTime = Date.now() } = options;
|
|
177
|
+
// Handle raw timestamp display
|
|
178
|
+
if (unit === 'unix-timestamp') {
|
|
179
|
+
// Ensure it's in seconds. If it looks like milliseconds, convert.
|
|
180
|
+
const timestamp = value > 1000000000000 ? Math.floor(value / 1000) : value;
|
|
181
|
+
return timestamp.toString();
|
|
182
|
+
}
|
|
183
|
+
if (unit === 'unix-timestamp-ms') {
|
|
184
|
+
// Ensure it's in milliseconds. If it looks like seconds, convert.
|
|
185
|
+
// Use 100 billion as threshold - values < 100B are likely seconds, >= 100B are likely milliseconds
|
|
186
|
+
// This distinguishes between 999999999 (seconds) and 999999999000 (milliseconds)
|
|
187
|
+
const MILLISECONDS_THRESHOLD = 100000000000; // 100 billion
|
|
188
|
+
const timestamp = value < MILLISECONDS_THRESHOLD ? value * 1000 : value;
|
|
189
|
+
return Math.floor(timestamp).toString();
|
|
190
|
+
}
|
|
191
|
+
const date = valueToDate(value);
|
|
192
|
+
// Handle relative time
|
|
193
|
+
if (unit === 'relative-time') {
|
|
194
|
+
return formatRelativeTime(date, referenceTime, locale);
|
|
195
|
+
}
|
|
196
|
+
// Configure Intl.DateTimeFormat options based on unit
|
|
197
|
+
const formatOptions = {
|
|
198
|
+
timeZone
|
|
199
|
+
};
|
|
200
|
+
switch(unit){
|
|
201
|
+
case 'datetime-iso':
|
|
202
|
+
// datetime-iso should ALWAYS show GMT/UTC time
|
|
203
|
+
return date.toISOString();
|
|
204
|
+
case 'datetime-us':
|
|
205
|
+
{
|
|
206
|
+
// datetime-us should ALWAYS show date in US Eastern timezone
|
|
207
|
+
formatOptions.timeZone = 'America/New_York';
|
|
208
|
+
formatOptions.year = 'numeric';
|
|
209
|
+
formatOptions.month = '2-digit';
|
|
210
|
+
formatOptions.day = '2-digit';
|
|
211
|
+
formatOptions.hour = '2-digit';
|
|
212
|
+
formatOptions.minute = '2-digit';
|
|
213
|
+
formatOptions.second = '2-digit';
|
|
214
|
+
formatOptions.hour12 = true; // 12-hour format with AM/PM
|
|
215
|
+
const formatter = new Intl.DateTimeFormat('en-US', formatOptions);
|
|
216
|
+
return formatter.format(date);
|
|
217
|
+
}
|
|
218
|
+
case 'datetime-local':
|
|
219
|
+
// datetime-local should use the browser's local timezone (detected automatically)
|
|
220
|
+
// Don't override timeZone - let it use the detected system timezone
|
|
221
|
+
formatOptions.year = 'numeric';
|
|
222
|
+
formatOptions.month = '2-digit';
|
|
223
|
+
formatOptions.day = '2-digit';
|
|
224
|
+
formatOptions.hour = '2-digit';
|
|
225
|
+
formatOptions.minute = '2-digit';
|
|
226
|
+
formatOptions.second = '2-digit';
|
|
227
|
+
formatOptions.hour12 = false; // 24-hour format
|
|
228
|
+
break;
|
|
229
|
+
case 'date-iso':
|
|
230
|
+
// date-iso should ALWAYS show GMT/UTC date
|
|
231
|
+
return date.toISOString().split('T')[0];
|
|
232
|
+
case 'date-us':
|
|
233
|
+
{
|
|
234
|
+
// date-us should ALWAYS show date in US Eastern timezone
|
|
235
|
+
formatOptions.timeZone = 'America/New_York';
|
|
236
|
+
formatOptions.year = 'numeric';
|
|
237
|
+
formatOptions.month = '2-digit';
|
|
238
|
+
formatOptions.day = '2-digit';
|
|
239
|
+
const formatter = new Intl.DateTimeFormat('en-US', formatOptions);
|
|
240
|
+
return formatter.format(date);
|
|
241
|
+
}
|
|
242
|
+
case 'date-local':
|
|
243
|
+
formatOptions.year = 'numeric';
|
|
244
|
+
formatOptions.month = '2-digit';
|
|
245
|
+
formatOptions.day = '2-digit';
|
|
246
|
+
break;
|
|
247
|
+
case 'time-local':
|
|
248
|
+
formatOptions.hour = '2-digit';
|
|
249
|
+
formatOptions.minute = '2-digit';
|
|
250
|
+
formatOptions.second = '2-digit';
|
|
251
|
+
formatOptions.hour12 = false; // 24-hour format
|
|
252
|
+
break;
|
|
253
|
+
case 'time-iso':
|
|
254
|
+
// time-iso should ALWAYS show GMT/UTC time
|
|
255
|
+
return date.toISOString().split('T')[1].replace('Z', '');
|
|
256
|
+
case 'time-us':
|
|
257
|
+
{
|
|
258
|
+
// time-us should show time in US-East timezone (Eastern Time)
|
|
259
|
+
formatOptions.timeZone = 'America/New_York';
|
|
260
|
+
formatOptions.hour = '2-digit';
|
|
261
|
+
formatOptions.minute = '2-digit';
|
|
262
|
+
formatOptions.second = '2-digit';
|
|
263
|
+
formatOptions.hour12 = true;
|
|
264
|
+
return new Intl.DateTimeFormat('en-US', formatOptions).format(date);
|
|
265
|
+
}
|
|
266
|
+
default:
|
|
267
|
+
{
|
|
268
|
+
// This ensures that all DateUnits are handled at compile time.
|
|
269
|
+
const exhaustive = unit;
|
|
270
|
+
throw new Error(`Unknown date unit: ${exhaustive}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// For all other units, use Intl.DateTimeFormat with the specified locale and options
|
|
274
|
+
const formatter = new Intl.DateTimeFormat(locale, formatOptions);
|
|
275
|
+
return formatter.format(date);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
//# sourceMappingURL=date.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/date.ts"],"sourcesContent":["// Copyright 2025 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { UnitGroupConfig, UnitConfig } from './types';\n\ntype DateUnits =\n | 'datetime-iso'\n | 'datetime-us'\n | 'datetime-local'\n | 'date-iso'\n | 'date-us'\n | 'date-local'\n | 'time-local'\n | 'time-iso'\n | 'time-us'\n | 'relative-time'\n | 'unix-timestamp'\n | 'unix-timestamp-ms';\n\nexport type DateFormatOptions = {\n unit?: DateUnits;\n /**\n * The locale to use for formatting. Defaults to the system's locale.\n */\n locale?: string;\n /**\n * The timezone to use for formatting. Defaults to the user's local timezone.\n */\n timeZone?: string;\n /**\n * For relative time formatting, the reference time to compare against.\n * Defaults to current time.\n */\n referenceTime?: number;\n /**\n * This property is not used for date formatting, but is included for\n * compatibility with the FormatControls UI component.\n */\n decimalPlaces?: number;\n};\n\nconst DATE_GROUP = 'Date';\n\nexport const DATE_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Date & Time',\n};\n\nexport const DATE_UNIT_CONFIG: Readonly<Record<DateUnits, UnitConfig>> = {\n 'datetime-iso': {\n group: DATE_GROUP,\n label: 'DateTime (GMT)',\n },\n 'datetime-us': {\n group: DATE_GROUP,\n label: 'DateTime (US-East)',\n },\n 'datetime-local': {\n group: DATE_GROUP,\n label: 'DateTime (Browser Local)',\n },\n 'date-iso': {\n group: DATE_GROUP,\n label: 'Date (GMT)',\n },\n 'date-us': {\n group: DATE_GROUP,\n label: 'Date (US-East)',\n },\n 'date-local': {\n group: DATE_GROUP,\n label: 'Date (Browser Local)',\n },\n 'time-local': {\n group: DATE_GROUP,\n label: 'Time (Browser Local)',\n },\n 'time-iso': {\n group: DATE_GROUP,\n label: 'Time (GMT)',\n },\n 'time-us': {\n group: DATE_GROUP,\n label: 'Time (US-East)',\n },\n 'relative-time': {\n group: DATE_GROUP,\n label: 'Relative Time',\n },\n 'unix-timestamp': {\n group: DATE_GROUP,\n label: 'Unix Timestamp (s)',\n },\n 'unix-timestamp-ms': {\n group: DATE_GROUP,\n label: 'Unix Timestamp (ms)',\n },\n};\n\n/**\n * Converts a numeric value to a Date object.\n * Handles both Unix timestamps (seconds) and millisecond timestamps.\n */\nfunction valueToDate(value: number): Date {\n // Timestamp detection logic with special case handling\n // Main threshold stays at 10 billion to maintain existing behavior\n // Handle special edge cases explicitly\n\n // Special case: negative timestamps\n if (value < 0) {\n // For negative values, check magnitude to distinguish seconds vs milliseconds\n // Large negative values (> 1 billion in magnitude) are likely milliseconds\n // Small negative values are likely seconds (rare edge case)\n if (Math.abs(value) > 10000000000) {\n return new Date(value); // milliseconds\n } else {\n return new Date(value * 1000); // seconds\n }\n }\n\n // Special case: year 9999 in seconds (~253402300799)\n // This is a very specific edge case for far-future timestamps\n if (value >= 250000000000 && value <= 260000000000) {\n // Check if this looks like year 9999 in seconds\n const asSeconds = new Date(value * 1000);\n const year = asSeconds.getUTCFullYear();\n if (year >= 9999) {\n return asSeconds; // seconds\n }\n }\n\n const SECONDS_THRESHOLD = 10000000000; // ~year 2286 - original threshold\n\n if (value < SECONDS_THRESHOLD) {\n // Assume it's in seconds\n return new Date(value * 1000);\n } else {\n // Assume it's in milliseconds\n return new Date(value);\n }\n}\n\n/**\n * Formats a relative time string using the Intl.RelativeTimeFormat API.\n */\nfunction formatRelativeTime(date: Date, referenceTime: number, locale: string): string {\n const referenceDate = new Date(referenceTime);\n const diffMs = date.getTime() - referenceDate.getTime();\n\n const units = [\n { unit: 'year', ms: 1000 * 60 * 60 * 24 * 365 },\n { unit: 'month', ms: 1000 * 60 * 60 * 24 * 30 },\n { unit: 'week', ms: 1000 * 60 * 60 * 24 * 7 },\n { unit: 'day', ms: 1000 * 60 * 60 * 24 },\n { unit: 'hour', ms: 1000 * 60 * 60 },\n { unit: 'minute', ms: 1000 * 60 },\n { unit: 'second', ms: 1000 },\n ] as const;\n\n for (const { unit, ms } of units) {\n // Determine the value for the current unit, ensuring it's an integer for Intl.RelativeTimeFormat\n const value = Math.round(diffMs / ms);\n // If the absolute value is 1 or more, use this unit\n if (Math.abs(value) >= 1) {\n const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });\n return rtf.format(value, unit);\n }\n }\n\n // If less than a second, show \"now\" or \"0 seconds\"\n const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });\n return rtf.format(0, 'second');\n}\n\n/**\n * Gets the browser's preferred locale with comprehensive fallbacks.\n */\nconst getBrowserLocale = (): string => {\n if (typeof navigator !== 'undefined') {\n if (navigator.language) return navigator.language;\n if (navigator.languages && navigator.languages.length > 0) {\n const firstLanguage = navigator.languages[0];\n if (firstLanguage) return firstLanguage;\n }\n // Legacy fallbacks for older browsers\n const nav = navigator as Navigator & {\n userLanguage?: string;\n browserLanguage?: string;\n systemLanguage?: string;\n };\n if (nav.userLanguage) return nav.userLanguage;\n if (nav.browserLanguage) return nav.browserLanguage;\n if (nav.systemLanguage) return nav.systemLanguage;\n }\n // Node.js or server-side fallback, or if navigator is not available/empty\n return Intl.DateTimeFormat().resolvedOptions().locale || 'en-US';\n};\n\nexport function formatDate(value: number, options: DateFormatOptions = {}): string {\n const systemTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;\n\n const {\n unit = 'datetime-local',\n locale = getBrowserLocale(),\n timeZone = systemTimeZone,\n referenceTime = Date.now(),\n } = options;\n\n // Handle raw timestamp display\n if (unit === 'unix-timestamp') {\n // Ensure it's in seconds. If it looks like milliseconds, convert.\n const timestamp = value > 1000000000000 ? Math.floor(value / 1000) : value;\n return timestamp.toString();\n }\n\n if (unit === 'unix-timestamp-ms') {\n // Ensure it's in milliseconds. If it looks like seconds, convert.\n // Use 100 billion as threshold - values < 100B are likely seconds, >= 100B are likely milliseconds\n // This distinguishes between 999999999 (seconds) and 999999999000 (milliseconds)\n const MILLISECONDS_THRESHOLD = 100000000000; // 100 billion\n const timestamp = value < MILLISECONDS_THRESHOLD ? value * 1000 : value;\n return Math.floor(timestamp).toString();\n }\n\n const date = valueToDate(value);\n\n // Handle relative time\n if (unit === 'relative-time') {\n return formatRelativeTime(date, referenceTime, locale);\n }\n\n // Configure Intl.DateTimeFormat options based on unit\n const formatOptions: Intl.DateTimeFormatOptions = {\n timeZone, // This will be overridden for specific units that need different timezones\n };\n\n switch (unit) {\n case 'datetime-iso':\n // datetime-iso should ALWAYS show GMT/UTC time\n return date.toISOString();\n\n case 'datetime-us': {\n // datetime-us should ALWAYS show date in US Eastern timezone\n formatOptions.timeZone = 'America/New_York';\n formatOptions.year = 'numeric';\n formatOptions.month = '2-digit';\n formatOptions.day = '2-digit';\n formatOptions.hour = '2-digit';\n formatOptions.minute = '2-digit';\n formatOptions.second = '2-digit';\n formatOptions.hour12 = true; // 12-hour format with AM/PM\n const formatter = new Intl.DateTimeFormat('en-US', formatOptions);\n return formatter.format(date);\n }\n\n case 'datetime-local':\n // datetime-local should use the browser's local timezone (detected automatically)\n // Don't override timeZone - let it use the detected system timezone\n formatOptions.year = 'numeric';\n formatOptions.month = '2-digit';\n formatOptions.day = '2-digit';\n formatOptions.hour = '2-digit';\n formatOptions.minute = '2-digit';\n formatOptions.second = '2-digit';\n formatOptions.hour12 = false; // 24-hour format\n break;\n\n case 'date-iso':\n // date-iso should ALWAYS show GMT/UTC date\n return date.toISOString().split('T')[0]!;\n\n case 'date-us': {\n // date-us should ALWAYS show date in US Eastern timezone\n formatOptions.timeZone = 'America/New_York';\n formatOptions.year = 'numeric';\n formatOptions.month = '2-digit';\n formatOptions.day = '2-digit';\n const formatter = new Intl.DateTimeFormat('en-US', formatOptions);\n return formatter.format(date);\n }\n\n case 'date-local':\n formatOptions.year = 'numeric';\n formatOptions.month = '2-digit';\n formatOptions.day = '2-digit';\n break;\n\n case 'time-local':\n formatOptions.hour = '2-digit';\n formatOptions.minute = '2-digit';\n formatOptions.second = '2-digit';\n formatOptions.hour12 = false; // 24-hour format\n break;\n\n case 'time-iso':\n // time-iso should ALWAYS show GMT/UTC time\n return date.toISOString().split('T')[1]!.replace('Z', '');\n\n case 'time-us': {\n // time-us should show time in US-East timezone (Eastern Time)\n formatOptions.timeZone = 'America/New_York';\n formatOptions.hour = '2-digit';\n formatOptions.minute = '2-digit';\n formatOptions.second = '2-digit';\n formatOptions.hour12 = true;\n return new Intl.DateTimeFormat('en-US', formatOptions).format(date);\n }\n\n default: {\n // This ensures that all DateUnits are handled at compile time.\n const exhaustive: never = unit;\n throw new Error(`Unknown date unit: ${exhaustive}`);\n }\n }\n\n // For all other units, use Intl.DateTimeFormat with the specified locale and options\n const formatter = new Intl.DateTimeFormat(locale, formatOptions);\n return formatter.format(date);\n}\n"],"names":["DATE_GROUP","DATE_GROUP_CONFIG","label","DATE_UNIT_CONFIG","group","valueToDate","value","Math","abs","Date","asSeconds","year","getUTCFullYear","SECONDS_THRESHOLD","formatRelativeTime","date","referenceTime","locale","referenceDate","diffMs","getTime","units","unit","ms","round","rtf","Intl","RelativeTimeFormat","numeric","format","getBrowserLocale","navigator","language","languages","length","firstLanguage","nav","userLanguage","browserLanguage","systemLanguage","DateTimeFormat","resolvedOptions","formatDate","options","systemTimeZone","timeZone","now","timestamp","floor","toString","MILLISECONDS_THRESHOLD","formatOptions","toISOString","month","day","hour","minute","second","hour12","formatter","split","replace","exhaustive","Error"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAwCjC,MAAMA,aAAa;AAEnB,OAAO,MAAMC,oBAAqC;IAChDC,OAAO;AACT,EAAE;AAEF,OAAO,MAAMC,mBAA4D;IACvE,gBAAgB;QACdC,OAAOJ;QACPE,OAAO;IACT;IACA,eAAe;QACbE,OAAOJ;QACPE,OAAO;IACT;IACA,kBAAkB;QAChBE,OAAOJ;QACPE,OAAO;IACT;IACA,YAAY;QACVE,OAAOJ;QACPE,OAAO;IACT;IACA,WAAW;QACTE,OAAO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J,cAAcK,MAAM,GAAG;gBACvBL,cAAcM,MAAM,GAAG;gBACvBN,cAAcO,MAAM,GAAG;gBACvB,OAAO,IAAIhC,KAAKc,cAAc,CAAC,SAASW,eAAetB,MAAM,CAACd;YAChE;QAEA;YAAS;gBACP,+DAA+D;gBAC/D,MAAM+C,aAAoBxC;gBAC1B,MAAM,IAAIyC,MAAM,CAAC,mBAAmB,EAAED,YAAY;YACpD;IACF;IAEA,qFAAqF;IACrF,MAAMH,YAAY,IAAIjC,KAAKc,cAAc,CAACvB,QAAQkC;IAClD,OAAOQ,UAAU9B,MAAM,CAACd;AAC1B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UnitConfig, UnitGroupConfig } from './types';
|
|
2
|
+
type TemperatureUnits = 'celsius' | 'fahrenheit';
|
|
3
|
+
export type TemperatureFormatOptions = {
|
|
4
|
+
unit: TemperatureUnits;
|
|
5
|
+
decimalPlaces?: number;
|
|
6
|
+
};
|
|
7
|
+
export declare const TEMPERATURE_GROUP_CONFIG: UnitGroupConfig;
|
|
8
|
+
export declare const TEMPERATURE_UNIT_CONFIG: Readonly<Record<TemperatureUnits, UnitConfig>>;
|
|
9
|
+
export declare const formatTemperature: (value: number, { unit, decimalPlaces }: TemperatureFormatOptions) => string;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=temperature.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temperature.d.ts","sourceRoot":"","sources":["../../../src/model/units/temperature.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAKtD,KAAK,gBAAgB,GAAG,SAAS,GAAG,YAAY,CAAC;AAEjD,MAAM,MAAM,wBAAwB,GAAG;IACrC,IAAI,EAAE,gBAAgB,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,eAGtC,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,gBAAgB,EAAE,UAAU,CAAC,CASlF,CAAC;AAEF,eAAO,MAAM,iBAAiB,UAAW,MAAM,2BAA2B,wBAAwB,KAAG,MAepG,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Copyright 2025 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
|
+
import { MAX_SIGNIFICANT_DIGITS } from './constants';
|
|
14
|
+
import { hasDecimalPlaces, limitDecimalPlaces } from './utils';
|
|
15
|
+
const TEMPERATURE_GROUP = 'Temperature';
|
|
16
|
+
export const TEMPERATURE_GROUP_CONFIG = {
|
|
17
|
+
label: TEMPERATURE_GROUP,
|
|
18
|
+
decimalPlaces: true
|
|
19
|
+
};
|
|
20
|
+
export const TEMPERATURE_UNIT_CONFIG = {
|
|
21
|
+
celsius: {
|
|
22
|
+
group: TEMPERATURE_GROUP,
|
|
23
|
+
label: 'Celsius (°C)'
|
|
24
|
+
},
|
|
25
|
+
fahrenheit: {
|
|
26
|
+
group: TEMPERATURE_GROUP,
|
|
27
|
+
label: 'Fahrenheit (°F)'
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export const formatTemperature = (value, { unit, decimalPlaces })=>{
|
|
31
|
+
const formatterOptions = {
|
|
32
|
+
unit,
|
|
33
|
+
style: 'unit'
|
|
34
|
+
};
|
|
35
|
+
if (hasDecimalPlaces(decimalPlaces)) {
|
|
36
|
+
formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
37
|
+
formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
38
|
+
} else {
|
|
39
|
+
formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
|
|
40
|
+
}
|
|
41
|
+
const locals = unit === 'celsius' ? 'en-GB' : 'en-US';
|
|
42
|
+
return Intl.NumberFormat(locals, formatterOptions).format(value);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=temperature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/temperature.ts"],"sourcesContent":["// Copyright 2025 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitConfig, UnitGroupConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces } from './utils';\n\nconst TEMPERATURE_GROUP = 'Temperature';\n\ntype TemperatureUnits = 'celsius' | 'fahrenheit';\n\nexport type TemperatureFormatOptions = {\n unit: TemperatureUnits;\n decimalPlaces?: number;\n};\n\nexport const TEMPERATURE_GROUP_CONFIG: UnitGroupConfig = {\n label: TEMPERATURE_GROUP,\n decimalPlaces: true,\n};\n\nexport const TEMPERATURE_UNIT_CONFIG: Readonly<Record<TemperatureUnits, UnitConfig>> = {\n celsius: {\n group: TEMPERATURE_GROUP,\n label: 'Celsius (°C)',\n },\n fahrenheit: {\n group: TEMPERATURE_GROUP,\n label: 'Fahrenheit (°F)',\n },\n};\n\nexport const formatTemperature = (value: number, { unit, decimalPlaces }: TemperatureFormatOptions): string => {\n const formatterOptions: Intl.NumberFormatOptions = {\n unit,\n style: 'unit',\n };\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n\n const locals = unit === 'celsius' ? 'en-GB' : 'en-US';\n return Intl.NumberFormat(locals, formatterOptions).format(value);\n};\n"],"names":["MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","TEMPERATURE_GROUP","TEMPERATURE_GROUP_CONFIG","label","decimalPlaces","TEMPERATURE_UNIT_CONFIG","celsius","group","fahrenheit","formatTemperature","value","unit","formatterOptions","style","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","locals","Intl","NumberFormat","format"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ,UAAU;AAE/D,MAAMC,oBAAoB;AAS1B,OAAO,MAAMC,2BAA4C;IACvDC,OAAOF;IACPG,eAAe;AACjB,EAAE;AAEF,OAAO,MAAMC,0BAA0E;IACrFC,SAAS;QACPC,OAAON;QACPE,OAAO;IACT;IACAK,YAAY;QACVD,OAAON;QACPE,OAAO;IACT;AACF,EAAE;AAEF,OAAO,MAAMM,oBAAoB,CAACC,OAAe,EAAEC,IAAI,EAAEP,aAAa,EAA4B;IAChG,MAAMQ,mBAA6C;QACjDD;QACAE,OAAO;IACT;IAEA,IAAId,iBAAiBK,gBAAgB;QACnCQ,iBAAiBE,qBAAqB,GAAGd,mBAAmBI;QAC5DQ,iBAAiBG,qBAAqB,GAAGf,mBAAmBI;IAC9D,OAAO;QACLQ,iBAAiBI,wBAAwB,GAAGlB;IAC9C;IAEA,MAAMmB,SAASN,SAAS,YAAY,UAAU;IAC9C,OAAOO,KAAKC,YAAY,CAACF,QAAQL,kBAAkBQ,MAAM,CAACV;AAC5D,EAAE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { UnitGroupConfig, UnitConfig } from './types';
|
|
2
|
-
type ThroughputUnit = 'bits/sec' | 'bytes/sec' | 'decbytes/sec' | 'counts/sec' | 'events/sec' | 'messages/sec' | 'ops/sec' | 'packets/sec' | 'reads/sec' | 'records/sec' | 'requests/sec' | 'rows/sec' | 'writes/sec';
|
|
2
|
+
type ThroughputUnit = 'bits/sec' | 'decbits/sec' | 'bytes/sec' | 'decbytes/sec' | 'counts/sec' | 'events/sec' | 'messages/sec' | 'ops/sec' | 'packets/sec' | 'reads/sec' | 'records/sec' | 'requests/sec' | 'rows/sec' | 'writes/sec';
|
|
3
3
|
export type ThroughputFormatOptions = {
|
|
4
4
|
unit?: ThroughputUnit;
|
|
5
5
|
decimalPlaces?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throughput.d.ts","sourceRoot":"","sources":["../../../src/model/units/throughput.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"throughput.d.ts","sourceRoot":"","sources":["../../../src/model/units/throughput.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,KAAK,cAAc,GACf,UAAU,GACV,aAAa,GACb,WAAW,GACX,cAAc,GACd,YAAY,GACZ,YAAY,GACZ,cAAc,GACd,SAAS,GACT,aAAa,GACb,WAAW,GACX,aAAa,GACb,cAAc,GACd,UAAU,GACV,YAAY,CAAC;AACjB,MAAM,MAAM,uBAAuB,GAAG;IACpC,IAAI,CAAC,EAAE,cAAc,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AACF,eAAO,MAAM,uBAAuB,EAAE,eAGrC,CAAC;AAEF,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,MAAM,CAAC,cAAc,EAAE,UAAU,CAAC,CA0D/E,CAAC;AAEF,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,aAAa,EAAE,EAAE,uBAAuB,GAAG,MAAM,CA0CrH"}
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// See the License for the specific language governing permissions and
|
|
12
12
|
// limitations under the License.
|
|
13
13
|
import { formatBytes } from './bytes';
|
|
14
|
+
import { formatBits } from './bits';
|
|
14
15
|
import { MAX_SIGNIFICANT_DIGITS } from './constants';
|
|
15
16
|
import { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';
|
|
16
17
|
export const THROUGHPUT_GROUP_CONFIG = {
|
|
@@ -21,7 +22,11 @@ const THROUGHPUT_GROUP = 'Throughput';
|
|
|
21
22
|
export const THROUGHPUT_UNIT_CONFIG = {
|
|
22
23
|
'bits/sec': {
|
|
23
24
|
group: THROUGHPUT_GROUP,
|
|
24
|
-
label: 'Bits/sec'
|
|
25
|
+
label: 'Bits/sec (IEC)'
|
|
26
|
+
},
|
|
27
|
+
'decbits/sec': {
|
|
28
|
+
group: THROUGHPUT_GROUP,
|
|
29
|
+
label: 'Bits/sec (SI)'
|
|
25
30
|
},
|
|
26
31
|
'bytes/sec': {
|
|
27
32
|
group: THROUGHPUT_GROUP,
|
|
@@ -74,6 +79,22 @@ export const THROUGHPUT_UNIT_CONFIG = {
|
|
|
74
79
|
};
|
|
75
80
|
export function formatThroughput(value, { unit, shortValues, decimalPlaces }) {
|
|
76
81
|
// special case for data throughput
|
|
82
|
+
if (unit === 'bits/sec') {
|
|
83
|
+
const denominator = Math.abs(value) < 1024 ? 'sec' : 's';
|
|
84
|
+
return formatBits(value, {
|
|
85
|
+
unit: 'bits',
|
|
86
|
+
shortValues,
|
|
87
|
+
decimalPlaces
|
|
88
|
+
}) + '/' + denominator;
|
|
89
|
+
}
|
|
90
|
+
if (unit === 'decbits/sec') {
|
|
91
|
+
const denominator = Math.abs(value) < 1000 ? 'sec' : 's';
|
|
92
|
+
return formatBits(value, {
|
|
93
|
+
unit: 'decbits',
|
|
94
|
+
shortValues,
|
|
95
|
+
decimalPlaces
|
|
96
|
+
}) + '/' + denominator;
|
|
97
|
+
}
|
|
77
98
|
if (unit === 'decbytes/sec') {
|
|
78
99
|
const denominator = Math.abs(value) < 1000 ? 'sec' : 's';
|
|
79
100
|
return formatBytes(value, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/model/units/throughput.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { formatBytes } from './bytes';\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';\n\ntype ThroughputUnit =\n | 'bits/sec'\n | 'bytes/sec'\n | 'decbytes/sec'\n | 'counts/sec'\n | 'events/sec'\n | 'messages/sec'\n | 'ops/sec'\n | 'packets/sec'\n | 'reads/sec'\n | 'records/sec'\n | 'requests/sec'\n | 'rows/sec'\n | 'writes/sec';\nexport type ThroughputFormatOptions = {\n unit?: ThroughputUnit;\n decimalPlaces?: number;\n shortValues?: boolean;\n};\nexport const THROUGHPUT_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Throughput',\n decimalPlaces: true,\n};\nconst THROUGHPUT_GROUP = 'Throughput';\nexport const THROUGHPUT_UNIT_CONFIG: Readonly<Record<ThroughputUnit, UnitConfig>> = {\n 'bits/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Bits/sec',\n },\n 'bytes/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Bytes/sec (IEC)',\n },\n 'decbytes/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Bytes/sec (SI)',\n },\n\n 'counts/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Counts/sec',\n },\n 'events/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Events/sec',\n },\n 'messages/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Messages/sec',\n },\n 'ops/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Ops/sec',\n },\n 'packets/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Packets/sec',\n },\n 'reads/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Reads/sec',\n },\n 'requests/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Requests/sec',\n },\n 'records/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Records/sec',\n },\n 'rows/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Rows/sec',\n },\n 'writes/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Writes/sec',\n },\n};\n\nexport function formatThroughput(value: number, { unit, shortValues, decimalPlaces }: ThroughputFormatOptions): string {\n // special case for data throughput\n if (unit === 'decbytes/sec') {\n const denominator = Math.abs(value) < 1000 ? 'sec' : 's';\n return formatBytes(value, { unit: 'decbytes', shortValues, decimalPlaces }) + '/' + denominator;\n }\n\n if (unit === 'bytes/sec') {\n const denominator = Math.abs(value) < 1024 ? 'sec' : 's';\n return formatBytes(value, { unit: 'bytes', shortValues, decimalPlaces }) + '/' + denominator;\n }\n\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'decimal',\n useGrouping: true,\n };\n\n if (shouldShortenValues(shortValues)) {\n formatterOptions.notation = 'compact';\n }\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n if (shouldShortenValues(shortValues)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n\n const formatter = Intl.NumberFormat('en-US', formatterOptions);\n return formatter.format(value) + ' ' + unit;\n}\n"],"names":["formatBytes","MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","shouldShortenValues","THROUGHPUT_GROUP_CONFIG","label","decimalPlaces","THROUGHPUT_GROUP","THROUGHPUT_UNIT_CONFIG","group","formatThroughput","value","unit","shortValues","denominator","Math","abs","formatterOptions","style","useGrouping","notation","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","formatter","Intl","NumberFormat","format"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,WAAW,QAAQ,UAAU;AACtC,SAASC,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,EAAEC,mBAAmB,QAAQ,UAAU;
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/throughput.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { formatBytes } from './bytes';\nimport { formatBits } from './bits';\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';\n\ntype ThroughputUnit =\n | 'bits/sec'\n | 'decbits/sec'\n | 'bytes/sec'\n | 'decbytes/sec'\n | 'counts/sec'\n | 'events/sec'\n | 'messages/sec'\n | 'ops/sec'\n | 'packets/sec'\n | 'reads/sec'\n | 'records/sec'\n | 'requests/sec'\n | 'rows/sec'\n | 'writes/sec';\nexport type ThroughputFormatOptions = {\n unit?: ThroughputUnit;\n decimalPlaces?: number;\n shortValues?: boolean;\n};\nexport const THROUGHPUT_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Throughput',\n decimalPlaces: true,\n};\nconst THROUGHPUT_GROUP = 'Throughput';\nexport const THROUGHPUT_UNIT_CONFIG: Readonly<Record<ThroughputUnit, UnitConfig>> = {\n 'bits/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Bits/sec (IEC)',\n },\n 'decbits/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Bits/sec (SI)',\n },\n 'bytes/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Bytes/sec (IEC)',\n },\n 'decbytes/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Bytes/sec (SI)',\n },\n\n 'counts/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Counts/sec',\n },\n 'events/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Events/sec',\n },\n 'messages/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Messages/sec',\n },\n 'ops/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Ops/sec',\n },\n 'packets/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Packets/sec',\n },\n 'reads/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Reads/sec',\n },\n 'requests/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Requests/sec',\n },\n 'records/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Records/sec',\n },\n 'rows/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Rows/sec',\n },\n 'writes/sec': {\n group: THROUGHPUT_GROUP,\n label: 'Writes/sec',\n },\n};\n\nexport function formatThroughput(value: number, { unit, shortValues, decimalPlaces }: ThroughputFormatOptions): string {\n // special case for data throughput\n if (unit === 'bits/sec') {\n const denominator = Math.abs(value) < 1024 ? 'sec' : 's';\n return formatBits(value, { unit: 'bits', shortValues, decimalPlaces }) + '/' + denominator;\n }\n\n if (unit === 'decbits/sec') {\n const denominator = Math.abs(value) < 1000 ? 'sec' : 's';\n return formatBits(value, { unit: 'decbits', shortValues, decimalPlaces }) + '/' + denominator;\n }\n\n if (unit === 'decbytes/sec') {\n const denominator = Math.abs(value) < 1000 ? 'sec' : 's';\n return formatBytes(value, { unit: 'decbytes', shortValues, decimalPlaces }) + '/' + denominator;\n }\n\n if (unit === 'bytes/sec') {\n const denominator = Math.abs(value) < 1024 ? 'sec' : 's';\n return formatBytes(value, { unit: 'bytes', shortValues, decimalPlaces }) + '/' + denominator;\n }\n\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'decimal',\n useGrouping: true,\n };\n\n if (shouldShortenValues(shortValues)) {\n formatterOptions.notation = 'compact';\n }\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n if (shouldShortenValues(shortValues)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n\n const formatter = Intl.NumberFormat('en-US', formatterOptions);\n return formatter.format(value) + ' ' + unit;\n}\n"],"names":["formatBytes","formatBits","MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","shouldShortenValues","THROUGHPUT_GROUP_CONFIG","label","decimalPlaces","THROUGHPUT_GROUP","THROUGHPUT_UNIT_CONFIG","group","formatThroughput","value","unit","shortValues","denominator","Math","abs","formatterOptions","style","useGrouping","notation","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","formatter","Intl","NumberFormat","format"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,WAAW,QAAQ,UAAU;AACtC,SAASC,UAAU,QAAQ,SAAS;AACpC,SAASC,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,EAAEC,mBAAmB,QAAQ,UAAU;AAsBpF,OAAO,MAAMC,0BAA2C;IACtDC,OAAO;IACPC,eAAe;AACjB,EAAE;AACF,MAAMC,mBAAmB;AACzB,OAAO,MAAMC,yBAAuE;IAClF,YAAY;QACVC,OAAOF;QACPF,OAAO;IACT;IACA,eAAe;QACbI,OAAOF;QACPF,OAAO;IACT;IACA,aAAa;QACXI,OAAOF;QACPF,OAAO;IACT;IACA,gBAAgB;QACdI,OAAOF;QACPF,OAAO;IACT;IAEA,cAAc;QACZI,OAAOF;QACPF,OAAO;IACT;IACA,cAAc;QACZI,OAAOF;QACPF,OAAO;IACT;IACA,gBAAgB;QACdI,OAAOF;QACPF,OAAO;IACT;IACA,WAAW;QACTI,OAAOF;QACPF,OAAO;IACT;IACA,eAAe;QACbI,OAAOF;QACPF,OAAO;IACT;IACA,aAAa;QACXI,OAAOF;QACPF,OAAO;IACT;IACA,gBAAgB;QACdI,OAAOF;QACPF,OAAO;IACT;IACA,eAAe;QACbI,OAAOF;QACPF,OAAO;IACT;IACA,YAAY;QACVI,OAAOF;QACPF,OAAO;IACT;IACA,cAAc;QACZI,OAAOF;QACPF,OAAO;IACT;AACF,EAAE;AAEF,OAAO,SAASK,iBAAiBC,KAAa,EAAE,E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imeUnits = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years';
|
|
2
|
+
type TimeUnits = 'nanoseconds' | 'microseconds' | 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years';
|
|
3
3
|
export type TimeFormatOptions = {
|
|
4
4
|
unit?: TimeUnits;
|
|
5
5
|
decimalPlaces?: number;
|
|
@@ -7,6 +7,8 @@ export type TimeFormatOptions = {
|
|
|
7
7
|
export declare const TIME_GROUP_CONFIG: UnitGroupConfig;
|
|
8
8
|
export declare const TIME_UNIT_CONFIG: Readonly<Record<TimeUnits, UnitConfig>>;
|
|
9
9
|
export declare enum PersesTimeToIntlTime {
|
|
10
|
+
nanoseconds = "nanosecond",
|
|
11
|
+
microseconds = "microsecond",
|
|
10
12
|
milliseconds = "millisecond",
|
|
11
13
|
seconds = "second",
|
|
12
14
|
minutes = "minute",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../../src/model/units/time.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,KAAK,SAAS,
|
|
1
|
+
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../../src/model/units/time.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,KAAK,SAAS,GACV,aAAa,GACb,cAAc,GACd,cAAc,GACd,SAAS,GACT,SAAS,GACT,OAAO,GACP,MAAM,GACN,OAAO,GACP,QAAQ,GACR,OAAO,CAAC;AACZ,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,eAG/B,CAAC;AACF,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,CAyCpE,CAAC;AAIF,oBAAY,oBAAoB;IAC9B,WAAW,eAAe;IAC1B,YAAY,gBAAgB;IAC5B,YAAY,gBAAgB;IAC5B,OAAO,WAAW;IAClB,OAAO,WAAW;IAClB,KAAK,SAAS;IACd,IAAI,QAAQ;IACZ,KAAK,SAAS;IACd,MAAM,UAAU;IAChB,KAAK,SAAS;CACf;AA4DD,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,iBAAiB,GAAG,MAAM,CAoB5F"}
|
package/dist/model/units/time.js
CHANGED
|
@@ -18,6 +18,14 @@ export const TIME_GROUP_CONFIG = {
|
|
|
18
18
|
decimalPlaces: true
|
|
19
19
|
};
|
|
20
20
|
export const TIME_UNIT_CONFIG = {
|
|
21
|
+
nanoseconds: {
|
|
22
|
+
group: TIME_GROUP,
|
|
23
|
+
label: 'Nanoseconds'
|
|
24
|
+
},
|
|
25
|
+
microseconds: {
|
|
26
|
+
group: TIME_GROUP,
|
|
27
|
+
label: 'Microseconds'
|
|
28
|
+
},
|
|
21
29
|
milliseconds: {
|
|
22
30
|
group: TIME_GROUP,
|
|
23
31
|
label: 'Milliseconds'
|
|
@@ -54,6 +62,8 @@ export const TIME_UNIT_CONFIG = {
|
|
|
54
62
|
// Mapping of time units to what Intl.NumberFormat formatter expects
|
|
55
63
|
// https://v8.dev/features/intl-numberformat#units
|
|
56
64
|
export var PersesTimeToIntlTime = /*#__PURE__*/ function(PersesTimeToIntlTime) {
|
|
65
|
+
PersesTimeToIntlTime["nanoseconds"] = "nanosecond";
|
|
66
|
+
PersesTimeToIntlTime["microseconds"] = "microsecond";
|
|
57
67
|
PersesTimeToIntlTime["milliseconds"] = "millisecond";
|
|
58
68
|
PersesTimeToIntlTime["seconds"] = "second";
|
|
59
69
|
PersesTimeToIntlTime["minutes"] = "minute";
|
|
@@ -77,7 +87,9 @@ export var PersesTimeToIntlTime = /*#__PURE__*/ function(PersesTimeToIntlTime) {
|
|
|
77
87
|
hours: 3600,
|
|
78
88
|
minutes: 60,
|
|
79
89
|
seconds: 1,
|
|
80
|
-
milliseconds: 0.001
|
|
90
|
+
milliseconds: 0.001,
|
|
91
|
+
microseconds: 0.000001,
|
|
92
|
+
nanoseconds: 0.000000001
|
|
81
93
|
};
|
|
82
94
|
const LARGEST_TO_SMALLEST_TIME_UNITS = [
|
|
83
95
|
'years',
|
|
@@ -87,7 +99,9 @@ const LARGEST_TO_SMALLEST_TIME_UNITS = [
|
|
|
87
99
|
'hours',
|
|
88
100
|
'minutes',
|
|
89
101
|
'seconds',
|
|
90
|
-
'milliseconds'
|
|
102
|
+
'milliseconds',
|
|
103
|
+
'microseconds',
|
|
104
|
+
'nanoseconds'
|
|
91
105
|
];
|
|
92
106
|
/**
|
|
93
107
|
* Choose the first time unit that produces a number greater than 1, starting from the biggest time unit.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/model/units/time.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces } from './utils';\n\ntype TimeUnits
|
|
1
|
+
{"version":3,"sources":["../../../src/model/units/time.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces } from './utils';\n\ntype TimeUnits =\n | 'nanoseconds'\n | 'microseconds'\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'weeks'\n | 'months'\n | 'years';\nexport type TimeFormatOptions = {\n unit?: TimeUnits;\n decimalPlaces?: number;\n};\nconst TIME_GROUP = 'Time';\nexport const TIME_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Time',\n decimalPlaces: true,\n};\nexport const TIME_UNIT_CONFIG: Readonly<Record<TimeUnits, UnitConfig>> = {\n nanoseconds: {\n group: TIME_GROUP,\n label: 'Nanoseconds',\n },\n microseconds: {\n group: TIME_GROUP,\n label: 'Microseconds',\n },\n milliseconds: {\n group: TIME_GROUP,\n label: 'Milliseconds',\n },\n seconds: {\n group: TIME_GROUP,\n label: 'Seconds',\n },\n minutes: {\n group: TIME_GROUP,\n label: 'Minutes',\n },\n hours: {\n group: TIME_GROUP,\n label: 'Hours',\n },\n days: {\n group: TIME_GROUP,\n label: 'Days',\n },\n weeks: {\n group: TIME_GROUP,\n label: 'Weeks',\n },\n months: {\n group: TIME_GROUP,\n label: 'Months',\n },\n years: {\n group: TIME_GROUP,\n label: 'Years',\n },\n};\n\n// Mapping of time units to what Intl.NumberFormat formatter expects\n// https://v8.dev/features/intl-numberformat#units\nexport enum PersesTimeToIntlTime {\n nanoseconds = 'nanosecond',\n microseconds = 'microsecond',\n milliseconds = 'millisecond',\n seconds = 'second',\n minutes = 'minute',\n hours = 'hour',\n days = 'day',\n weeks = 'week',\n months = 'month',\n years = 'year',\n}\n\n/**\n * Note: This conversion will not be exactly accurate for months and years,\n * due variations in the lengths of months (i.e. 28 - 31 days) and years (i.e. leap years).\n * For precision with months and years, we would need more complex algorithms and/or external libraries.\n * However, we expect that measurements in months and years will be rare.\n */\nconst TIME_UNITS_IN_SECONDS: Record<TimeUnits, number> = {\n years: 31536000, // 365 days\n months: 2592000, // 30 days\n weeks: 604800, // 7 days\n days: 86400,\n hours: 3600,\n minutes: 60,\n seconds: 1,\n milliseconds: 0.001,\n microseconds: 0.000001,\n nanoseconds: 0.000000001,\n};\n\nconst LARGEST_TO_SMALLEST_TIME_UNITS: TimeUnits[] = [\n 'years',\n 'months',\n 'weeks',\n 'days',\n 'hours',\n 'minutes',\n 'seconds',\n 'milliseconds',\n 'microseconds',\n 'nanoseconds',\n];\n\n/**\n * Choose the first time unit that produces a number greater than 1, starting from the biggest time unit.\n */\nfunction getValueAndKindForNaturalNumbers(value: number, unit: TimeUnits): { value: number; unit: TimeUnits } {\n const valueInSeconds = value * TIME_UNITS_IN_SECONDS[unit];\n\n // Initialize for TS\n const largestTimeUnit = LARGEST_TO_SMALLEST_TIME_UNITS[0] || 'years';\n let timeUnit: TimeUnits = largestTimeUnit;\n let valueInTimeUnit: number = valueInSeconds / TIME_UNITS_IN_SECONDS[largestTimeUnit];\n\n for (timeUnit of LARGEST_TO_SMALLEST_TIME_UNITS) {\n valueInTimeUnit = valueInSeconds / TIME_UNITS_IN_SECONDS[timeUnit];\n if (valueInTimeUnit >= 1) {\n return { value: valueInTimeUnit, unit: timeUnit };\n }\n }\n\n // If we didn't find a time unit, we have to settle for the smallest time unit (which is the last time unit).\n return { value: valueInTimeUnit, unit: timeUnit };\n}\n\nfunction isMonthOrYear(unit: TimeUnits): boolean {\n return unit === 'months' || unit === 'years';\n}\n\nexport function formatTime(value: number, { unit, decimalPlaces }: TimeFormatOptions): string {\n if (value === 0) return '0s';\n\n const results = getValueAndKindForNaturalNumbers(value, unit ?? 'seconds');\n\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'unit',\n unit: PersesTimeToIntlTime[results.unit],\n unitDisplay: isMonthOrYear(results.unit) ? 'long' : 'narrow',\n };\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n\n const formatter = Intl.NumberFormat('en-US', formatterOptions);\n return formatter.format(results.value);\n}\n"],"names":["MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","TIME_GROUP","TIME_GROUP_CONFIG","label","decimalPlaces","TIME_UNIT_CONFIG","nanoseconds","group","microseconds","milliseconds","seconds","minutes","hours","days","weeks","months","years","PersesTimeToIntlTime","TIME_UNITS_IN_SECONDS","LARGEST_TO_SMALLEST_TIME_UNITS","getValueAndKindForNaturalNumbers","value","unit","valueInSeconds","largestTimeUnit","timeUnit","valueInTimeUnit","isMonthOrYear","formatTime","results","formatterOptions","style","unitDisplay","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","formatter","Intl","NumberFormat","format"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ,UAAU;AAiB/D,MAAMC,aAAa;AACnB,OAAO,MAAMC,oBAAqC;IAChDC,OAAO;IACPC,eAAe;AACjB,EAAE;AACF,OAAO,MAAMC,mBAA4D;IACvEC,aAAa;QACXC,OAAON;QACPE,OAAO;IACT;IACAK,cAAc;QACZD,OAAON;QACPE,OAAO;IACT;IACAM,cAAc;QACZF,OAAON;QACPE,OAAO;IACT;IACAO,SAAS;QACPH,OAAON;QACPE,O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AU,GAAG,OAAO;IAExB,MAAMQ,UAAUT,iCAAiCC,OAAOC,QAAQ;IAEhE,MAAMQ,mBAA6C;QACjDC,OAAO;QACPT,MAAML,oBAAoB,CAACY,QAAQP,IAAI,CAAC;QACxCU,aAAaL,cAAcE,QAAQP,IAAI,IAAI,SAAS;IACtD;IAEA,IAAIvB,iBAAiBK,gBAAgB;QACnC0B,iBAAiBG,qBAAqB,GAAGjC,mBAAmBI;QAC5D0B,iBAAiBI,qBAAqB,GAAGlC,mBAAmBI;IAC9D,OAAO;QACL0B,iBAAiBK,wBAAwB,GAAGrC;IAC9C;IAEA,MAAMsC,YAAYC,KAAKC,YAAY,CAAC,SAASR;IAC7C,OAAOM,UAAUG,MAAM,CAACV,QAAQR,KAAK;AACvC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Duration } from 'date-fns';
|
|
2
2
|
import { AbsoluteTimeRange, DurationString } from '../time';
|
|
3
3
|
import { FormatOptions } from './units';
|
|
4
|
-
export type UnitGroup = 'Time' | 'Percent' | 'Decimal' | 'Bytes' | 'Throughput' | 'Currency';
|
|
4
|
+
export type UnitGroup = 'Time' | 'Percent' | 'Decimal' | 'Bytes' | 'Bits' | 'Throughput' | 'Currency' | 'Temperature' | 'Date';
|
|
5
5
|
/**
|
|
6
6
|
* Configuration for rendering units that are part of a group.
|
|
7
7
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/model/units/types.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,MAAM,MAAM,SAAS,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/model/units/types.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE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