@perses-dev/core 0.39.0 → 0.40.1

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 (119) hide show
  1. package/dist/cjs/constants/dashboard-defaults.js +6 -2
  2. package/dist/cjs/constants/dialog.js +3 -1
  3. package/dist/cjs/constants/index.js +11 -9
  4. package/dist/cjs/index.js +12 -10
  5. package/dist/cjs/model/calculations.js +61 -49
  6. package/dist/cjs/model/index.js +28 -26
  7. package/dist/cjs/model/legend.js +39 -19
  8. package/dist/cjs/model/resource.js +9 -0
  9. package/dist/cjs/model/time-series-queries.js +3 -1
  10. package/dist/cjs/model/time.js +147 -64
  11. package/dist/cjs/model/units/bytes.js +26 -20
  12. package/dist/cjs/model/units/constants.js +3 -1
  13. package/dist/cjs/model/units/decimal.js +20 -14
  14. package/dist/cjs/model/units/index.js +13 -11
  15. package/dist/cjs/model/units/percent.js +21 -15
  16. package/dist/cjs/model/units/time.js +68 -60
  17. package/dist/cjs/model/units/types.js +3 -1
  18. package/dist/cjs/model/units/units.js +67 -43
  19. package/dist/cjs/model/units/utils.js +13 -7
  20. package/dist/cjs/model/variables.js +16 -3
  21. package/dist/cjs/utils/event.js +3 -1
  22. package/dist/cjs/utils/fetch.js +12 -4
  23. package/dist/cjs/utils/index.js +17 -14
  24. package/dist/cjs/utils/is-empty-object.js +3 -1
  25. package/dist/cjs/utils/mathjs.js +3 -1
  26. package/dist/cjs/utils/memo.js +10 -6
  27. package/dist/cjs/utils/panel-refs.js +9 -3
  28. package/dist/cjs/utils/text.js +33 -21
  29. package/dist/cjs/utils/time-series-data.js +21 -11
  30. package/dist/cjs/utils/types.js +16 -0
  31. package/dist/constants/dashboard-defaults.js.map +1 -1
  32. package/dist/constants/dialog.js.map +1 -1
  33. package/dist/constants/index.js.map +1 -1
  34. package/dist/index.js.map +1 -1
  35. package/dist/model/calculations.d.ts +9 -8
  36. package/dist/model/calculations.d.ts.map +1 -1
  37. package/dist/model/calculations.js +46 -45
  38. package/dist/model/calculations.js.map +1 -1
  39. package/dist/model/datasource.d.ts +19 -3
  40. package/dist/model/datasource.d.ts.map +1 -1
  41. package/dist/model/datasource.js.map +1 -1
  42. package/dist/model/display.d.ts +1 -1
  43. package/dist/model/display.d.ts.map +1 -1
  44. package/dist/model/display.js.map +1 -1
  45. package/dist/model/index.js.map +1 -1
  46. package/dist/model/legend.d.ts +6 -6
  47. package/dist/model/legend.js +9 -9
  48. package/dist/model/legend.js.map +1 -1
  49. package/dist/model/panels.d.ts +5 -2
  50. package/dist/model/panels.d.ts.map +1 -1
  51. package/dist/model/panels.js.map +1 -1
  52. package/dist/model/resource.d.ts +3 -2
  53. package/dist/model/resource.d.ts.map +1 -1
  54. package/dist/model/resource.js +3 -1
  55. package/dist/model/resource.js.map +1 -1
  56. package/dist/model/thresholds.d.ts +2 -2
  57. package/dist/model/thresholds.d.ts.map +1 -1
  58. package/dist/model/thresholds.js.map +1 -1
  59. package/dist/model/time-series-queries.js.map +1 -1
  60. package/dist/model/time.d.ts +3 -0
  61. package/dist/model/time.d.ts.map +1 -1
  62. package/dist/model/time.js +115 -55
  63. package/dist/model/time.js.map +1 -1
  64. package/dist/model/units/bytes.d.ts +8 -8
  65. package/dist/model/units/bytes.d.ts.map +1 -1
  66. package/dist/model/units/bytes.js +16 -16
  67. package/dist/model/units/bytes.js.map +1 -1
  68. package/dist/model/units/constants.js.map +1 -1
  69. package/dist/model/units/decimal.d.ts +8 -8
  70. package/dist/model/units/decimal.d.ts.map +1 -1
  71. package/dist/model/units/decimal.js +12 -12
  72. package/dist/model/units/decimal.js.map +1 -1
  73. package/dist/model/units/index.js.map +1 -1
  74. package/dist/model/units/percent.d.ts +7 -7
  75. package/dist/model/units/percent.d.ts.map +1 -1
  76. package/dist/model/units/percent.js +12 -12
  77. package/dist/model/units/percent.js.map +1 -1
  78. package/dist/model/units/time.d.ts +15 -15
  79. package/dist/model/units/time.d.ts.map +1 -1
  80. package/dist/model/units/time.js +56 -56
  81. package/dist/model/units/time.js.map +1 -1
  82. package/dist/model/units/types.d.ts +14 -5
  83. package/dist/model/units/types.d.ts.map +1 -1
  84. package/dist/model/units/types.js.map +1 -1
  85. package/dist/model/units/units.d.ts +30 -30
  86. package/dist/model/units/units.d.ts.map +1 -1
  87. package/dist/model/units/units.js +31 -31
  88. package/dist/model/units/units.js.map +1 -1
  89. package/dist/model/units/utils.d.ts +2 -2
  90. package/dist/model/units/utils.d.ts.map +1 -1
  91. package/dist/model/units/utils.js +4 -4
  92. package/dist/model/units/utils.js.map +1 -1
  93. package/dist/model/variables.d.ts +25 -7
  94. package/dist/model/variables.d.ts.map +1 -1
  95. package/dist/model/variables.js +3 -0
  96. package/dist/model/variables.js.map +1 -1
  97. package/dist/test/setup-tests.js.map +1 -1
  98. package/dist/utils/event.js.map +1 -1
  99. package/dist/utils/fetch.js.map +1 -1
  100. package/dist/utils/index.d.ts +2 -1
  101. package/dist/utils/index.d.ts.map +1 -1
  102. package/dist/utils/index.js +2 -1
  103. package/dist/utils/index.js.map +1 -1
  104. package/dist/utils/is-empty-object.js.map +1 -1
  105. package/dist/utils/mathjs.js.map +1 -1
  106. package/dist/utils/memo.js +2 -2
  107. package/dist/utils/memo.js.map +1 -1
  108. package/dist/utils/panel-refs.js.map +1 -1
  109. package/dist/utils/text.d.ts +5 -5
  110. package/dist/utils/text.d.ts.map +1 -1
  111. package/dist/utils/text.js +17 -17
  112. package/dist/utils/text.js.map +1 -1
  113. package/dist/utils/time-series-data.js +6 -6
  114. package/dist/utils/time-series-data.js.map +1 -1
  115. package/dist/utils/types.d.ts +2 -0
  116. package/dist/utils/types.d.ts.map +1 -0
  117. package/dist/utils/types.js +15 -0
  118. package/dist/utils/types.js.map +1 -0
  119. package/package.json +5 -4
@@ -10,6 +10,8 @@
10
10
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
- export { };
13
+ export function getMetadataProject(metadata) {
14
+ return 'project' in metadata ? metadata.project : undefined;
15
+ }
14
16
 
15
17
  //# sourceMappingURL=resource.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/model/resource.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\nexport interface Metadata {\n name: string;\n created_at?: string;\n updated_at?: string;\n version?: number;\n}\n\nexport interface ProjectMetadata extends Metadata {\n project: string;\n}\n"],"names":[],"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,WASC"}
1
+ {"version":3,"sources":["../../src/model/resource.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\nexport interface Metadata {\n name: string;\n createdAt?: string;\n updatedAt?: string;\n version?: number;\n}\n\nexport interface ProjectMetadata extends Metadata {\n project: string;\n}\n\nexport function getMetadataProject(metadata: ProjectMetadata | Metadata): string | undefined {\n return 'project' in metadata ? metadata.project : undefined;\n}\n"],"names":["getMetadataProject","metadata","project","undefined"],"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;AAajC,OAAO,SAASA,mBAAmBC,QAAoC;IACrE,OAAO,aAAaA,WAAWA,SAASC,UAAUC;AACpD"}
@@ -4,8 +4,8 @@ export interface StepOptions {
4
4
  name?: string;
5
5
  }
6
6
  export interface ThresholdOptions {
7
- mode?: 'Percent' | 'Absolute';
8
- default_color?: string;
7
+ mode?: 'percent' | 'absolute';
8
+ defaultColor?: string;
9
9
  max?: number;
10
10
  steps?: StepOptions[];
11
11
  }
@@ -1 +1 @@
1
- {"version":3,"file":"thresholds.d.ts","sourceRoot":"","sources":["../../src/model/thresholds.ts"],"names":[],"mappings":"AAaA,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,SAAS,GAAG,UAAU,CAAC;IAC9B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;CACvB;AAED,MAAM,WAAW,qBAAqB;IACpC,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB"}
1
+ {"version":3,"file":"thresholds.d.ts","sourceRoot":"","sources":["../../src/model/thresholds.ts"],"names":[],"mappings":"AAaA,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,SAAS,GAAG,UAAU,CAAC;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;CACvB;AAED,MAAM,WAAW,qBAAqB;IACpC,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/model/thresholds.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\nexport interface StepOptions {\n value: number;\n color?: string;\n name?: string;\n}\n\nexport interface ThresholdOptions {\n mode?: 'Percent' | 'Absolute';\n default_color?: string;\n max?: number; // is this same as the max in GaugeChartOptions? can we remove?\n steps?: StepOptions[];\n}\n\nexport interface ThresholdColorPalette {\n defaultColor: string;\n palette: string[];\n}\n"],"names":[],"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,WAgBC"}
1
+ {"version":3,"sources":["../../src/model/thresholds.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\nexport interface StepOptions {\n value: number;\n color?: string;\n name?: string;\n}\n\nexport interface ThresholdOptions {\n mode?: 'percent' | 'absolute';\n defaultColor?: string;\n max?: number; // is this same as the max in GaugeChartOptions? can we remove?\n steps?: StepOptions[];\n}\n\nexport interface ThresholdColorPalette {\n defaultColor: string;\n palette: string[];\n}\n"],"names":[],"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,WAgBC"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/model/time-series-queries.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 { UnknownSpec } from './definitions';\nimport { QueryDefinition } from './query';\nimport { UnixTimeMs } from './time';\n\nexport type TimeSeriesQueryDefinition<PluginSpec = UnknownSpec> = QueryDefinition<'TimeSeriesQuery', PluginSpec>;\n\nexport type TimeSeriesValueTuple = [timestamp: UnixTimeMs, value: number | null];\n\nexport function isTimeSeriesValueTuple(data: TimeSeriesValueTuple): data is TimeSeriesValueTuple {\n if (data.length !== 2) return false;\n return true;\n}\n\nexport type Labels = Record<string, string>;\n"],"names":["isTimeSeriesValueTuple","data","length"],"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;AAUjC,OAAO,SAASA,sBAAsB,CAACC,IAA0B,EAAgC;IAC/F,IAAIA,IAAI,CAACC,MAAM,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACpC,OAAO,IAAI,CAAC;AACd,CAAC"}
1
+ {"version":3,"sources":["../../src/model/time-series-queries.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 { UnknownSpec } from './definitions';\nimport { QueryDefinition } from './query';\nimport { UnixTimeMs } from './time';\n\nexport type TimeSeriesQueryDefinition<PluginSpec = UnknownSpec> = QueryDefinition<'TimeSeriesQuery', PluginSpec>;\n\nexport type TimeSeriesValueTuple = [timestamp: UnixTimeMs, value: number | null];\n\nexport function isTimeSeriesValueTuple(data: TimeSeriesValueTuple): data is TimeSeriesValueTuple {\n if (data.length !== 2) return false;\n return true;\n}\n\nexport type Labels = Record<string, string>;\n"],"names":["isTimeSeriesValueTuple","data","length"],"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;AAUjC,OAAO,SAASA,uBAAuBC,IAA0B;IAC/D,IAAIA,KAAKC,WAAW,GAAG,OAAO;IAC9B,OAAO;AACT"}
@@ -39,6 +39,9 @@ export declare function parseDurationString(durationString: string): Duration;
39
39
  * Returns true if the given string is a valid DurationString.
40
40
  */
41
41
  export declare function isDurationString(maybeDuration: string): maybeDuration is DurationString;
42
+ export declare function intervalToPrometheusDuration(timeRange: AbsoluteTimeRange): Duration;
43
+ export declare function msToPrometheusDuration(durationInMs: number): Duration;
44
+ export declare function formatDuration(duration: Duration): DurationString;
42
45
  /**
43
46
  * Round interval to clearer increments
44
47
  */
@@ -1 +1 @@
1
- {"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../src/model/time.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,QAAQ,EAAO,MAAM,UAAU,CAAC;AAEzC,oBAAY,UAAU,GAAG,MAAM,CAAC;AAEhC,oBAAY,cAAc,GAAG,MAAM,GAAG,MAAM,CAAC;AAE7C,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,IAAI,CAAC;IACZ,GAAG,EAAE,IAAI,CAAC;CACX;AAED,MAAM,WAAW,iBAAiB;IAEhC,GAAG,CAAC,EAAE,IAAI,CAAC;IACX,YAAY,EAAE,cAAc,CAAC;CAC9B;AAED,oBAAY,cAAc,GAAG,iBAAiB,GAAG,iBAAiB,CAAC;AAEnE;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,cAAc,GAAG,SAAS,IAAI,iBAAiB,CAE7F;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,cAAc,GAAG,SAAS,IAAI,iBAAiB,CAE7F;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,iBAAiB,GAAG,iBAAiB,CAOnF;AAED,aAAK,0BAA0B,GAAG,GAAG,MAAM,IAAI,CAAC;AAChD,aAAK,qBAAqB,GAAG,GAAG,MAAM,GAAG,CAAC;AAC1C,aAAK,qBAAqB,GAAG,GAAG,MAAM,GAAG,CAAC;AAC1C,aAAK,mBAAmB,GAAG,GAAG,MAAM,GAAG,CAAC;AACxC,aAAK,kBAAkB,GAAG,GAAG,MAAM,GAAG,CAAC;AACvC,aAAK,m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AC,SAAS,EAAE,iBAAiB,EAAE,KAAK,EAAE,MAAM,UAI7E"}
@@ -11,6 +11,12 @@
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
13
  import { sub } from 'date-fns';
14
+ const ONE_SECOND_IN_MS = 1000;
15
+ const ONE_MINUTE_IN_MS = 60000;
16
+ const ONE_HOUR_IN_MS = 3600000;
17
+ const ONE_DAY_IN_MS = 86400000; // assuming a day has always 24h
18
+ const ONE_WEEK_IN_MS = 604800000; // assuming a week has always 7d
19
+ const ONE_YEAR_IN_MS = 31536000000; // assuming a year has always 365d
14
20
  /**
15
21
  * Determine whether a given time range is relative
16
22
  */ export function isRelativeTimeRange(timeRange) {
@@ -24,8 +30,8 @@ import { sub } from 'date-fns';
24
30
  /**
25
31
  * Returns an absolute time range from a RelativeTimeRange.
26
32
  */ export function toAbsoluteTimeRange(timeRange) {
27
- var _end;
28
- const end = (_end = timeRange.end) !== null && _end !== void 0 ? _end : new Date();
33
+ var _timeRange_end;
34
+ const end = (_timeRange_end = timeRange.end) !== null && _timeRange_end !== void 0 ? _timeRange_end : new Date();
29
35
  return {
30
36
  start: sub(end, parseDurationString(timeRange.pastDuration)),
31
37
  end
@@ -40,15 +46,15 @@ const DURATION_REGEX = /^(?:(\d+)y)?(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?
40
46
  if (matches === null) {
41
47
  throw new Error(`Invalid duration string '${durationString}'`);
42
48
  }
43
- var ref, ref1, ref2, ref3, ref4, ref5, ref6;
49
+ var _matches_, _matches_1, _matches_2, _matches_3, _matches_4, _matches_5, _matches_6;
44
50
  return {
45
- years: parseInt((ref = matches[1]) !== null && ref !== void 0 ? ref : '0'),
51
+ years: parseInt((_matches_ = matches[1]) !== null && _matches_ !== void 0 ? _matches_ : '0'),
46
52
  months: 0,
47
- weeks: parseInt((ref1 = matches[2]) !== null && ref1 !== void 0 ? ref1 : '0'),
48
- days: parseInt((ref2 = matches[3]) !== null && ref2 !== void 0 ? ref2 : '0'),
49
- hours: parseInt((ref3 = matches[4]) !== null && ref3 !== void 0 ? ref3 : '0'),
50
- minutes: parseInt((ref4 = matches[5]) !== null && ref4 !== void 0 ? ref4 : '0'),
51
- seconds: parseInt((ref5 = matches[6]) !== null && ref5 !== void 0 ? ref5 : '0') + parseInt((ref6 = matches[7]) !== null && ref6 !== void 0 ? ref6 : '0') / 1000
53
+ weeks: parseInt((_matches_1 = matches[2]) !== null && _matches_1 !== void 0 ? _matches_1 : '0'),
54
+ days: parseInt((_matches_2 = matches[3]) !== null && _matches_2 !== void 0 ? _matches_2 : '0'),
55
+ hours: parseInt((_matches_3 = matches[4]) !== null && _matches_3 !== void 0 ? _matches_3 : '0'),
56
+ minutes: parseInt((_matches_4 = matches[5]) !== null && _matches_4 !== void 0 ? _matches_4 : '0'),
57
+ seconds: parseInt((_matches_5 = matches[6]) !== null && _matches_5 !== void 0 ? _matches_5 : '0') + parseInt((_matches_6 = matches[7]) !== null && _matches_6 !== void 0 ? _matches_6 : '0') / 1000
52
58
  };
53
59
  }
54
60
  /**
@@ -57,6 +63,60 @@ const DURATION_REGEX = /^(?:(\d+)y)?(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?
57
63
  if (maybeDuration === '') return false;
58
64
  return DURATION_REGEX.test(maybeDuration);
59
65
  }
66
+ export function intervalToPrometheusDuration(timeRange) {
67
+ const durationInMs = timeRange.end.valueOf() - timeRange.start.valueOf();
68
+ return msToPrometheusDuration(durationInMs);
69
+ }
70
+ export function msToPrometheusDuration(durationInMs) {
71
+ const years = Math.trunc(durationInMs / ONE_YEAR_IN_MS);
72
+ if (years > 0) durationInMs -= years * ONE_YEAR_IN_MS;
73
+ const weeks = Math.trunc(durationInMs / ONE_WEEK_IN_MS);
74
+ if (weeks > 0) durationInMs -= weeks * ONE_WEEK_IN_MS;
75
+ const days = Math.trunc(durationInMs / ONE_DAY_IN_MS);
76
+ if (days > 0) durationInMs -= days * ONE_DAY_IN_MS;
77
+ const hours = Math.trunc(durationInMs / ONE_HOUR_IN_MS);
78
+ if (hours > 0) durationInMs -= hours * ONE_HOUR_IN_MS;
79
+ const minutes = Math.trunc(durationInMs / ONE_MINUTE_IN_MS);
80
+ if (minutes > 0) durationInMs -= minutes * ONE_MINUTE_IN_MS;
81
+ return {
82
+ years: years,
83
+ months: 0,
84
+ weeks: weeks,
85
+ days: days,
86
+ hours: hours,
87
+ minutes: minutes,
88
+ seconds: durationInMs / 1000
89
+ };
90
+ }
91
+ export function formatDuration(duration) {
92
+ const result = [];
93
+ if (duration.years) {
94
+ result.push(`${duration.years}y`);
95
+ }
96
+ if (duration.weeks) {
97
+ result.push(`${duration.weeks}w`);
98
+ }
99
+ if (duration.days) {
100
+ result.push(`${duration.days}d`);
101
+ }
102
+ if (duration.hours) {
103
+ result.push(`${duration.hours}h`);
104
+ }
105
+ if (duration.minutes) {
106
+ result.push(`${duration.minutes}m`);
107
+ }
108
+ if (duration.seconds) {
109
+ const seconds = Math.trunc(duration.seconds);
110
+ if (seconds) {
111
+ result.push(`${seconds}s`);
112
+ }
113
+ const ms = (duration.seconds - seconds) * 1000;
114
+ if (ms) {
115
+ result.push(`${ms}ms`);
116
+ }
117
+ }
118
+ return result.join('');
119
+ }
60
120
  const DEFAULT_STEP_MS = 15000;
61
121
  const ROUNDED_STEP_INTERVALS = [
62
122
  // max: 0.015s
@@ -97,140 +157,140 @@ const ROUNDED_STEP_INTERVALS = [
97
157
  },
98
158
  // max: 1.5s
99
159
  {
100
- maxMs: 1500,
101
- roundedStepMs: 1000,
160
+ maxMs: ONE_SECOND_IN_MS * 1.5,
161
+ roundedStepMs: ONE_SECOND_IN_MS,
102
162
  display: '1s'
103
163
  },
104
164
  // max: 3.5s
105
165
  {
106
- maxMs: 3500,
107
- roundedStepMs: 2000,
166
+ maxMs: ONE_SECOND_IN_MS * 3.5,
167
+ roundedStepMs: ONE_SECOND_IN_MS * 2,
108
168
  display: '2s'
109
169
  },
110
170
  // max: 7.5s
111
171
  {
112
- maxMs: 7500,
113
- roundedStepMs: 5000,
172
+ maxMs: ONE_SECOND_IN_MS * 7.5,
173
+ roundedStepMs: ONE_SECOND_IN_MS * 5,
114
174
  display: '5s'
115
175
  },
116
176
  // max: 12.5s
117
177
  {
118
- maxMs: 12500,
119
- roundedStepMs: 10000,
178
+ maxMs: ONE_SECOND_IN_MS * 12.5,
179
+ roundedStepMs: ONE_SECOND_IN_MS * 10,
120
180
  display: '10s'
121
181
  },
122
182
  // max: 17.5s
123
183
  {
124
- maxMs: 17500,
125
- roundedStepMs: 15000,
184
+ maxMs: ONE_SECOND_IN_MS * 17.5,
185
+ roundedStepMs: ONE_SECOND_IN_MS * 15,
126
186
  display: '15s'
127
187
  },
128
188
  // max: 25s
129
189
  {
130
- maxMs: 25000,
131
- roundedStepMs: 20000,
190
+ maxMs: ONE_SECOND_IN_MS * 25,
191
+ roundedStepMs: ONE_SECOND_IN_MS * 20,
132
192
  display: '20s'
133
193
  },
134
194
  // max: 45s
135
195
  {
136
- maxMs: 45000,
137
- roundedStepMs: 30000,
196
+ maxMs: ONE_SECOND_IN_MS * 45,
197
+ roundedStepMs: ONE_SECOND_IN_MS * 30,
138
198
  display: '30s'
139
199
  },
140
200
  // max: 1.5m
141
201
  {
142
- maxMs: 90000,
143
- roundedStepMs: 60000,
202
+ maxMs: ONE_MINUTE_IN_MS * 1.5,
203
+ roundedStepMs: ONE_MINUTE_IN_MS,
144
204
  display: '1m'
145
205
  },
146
206
  // max: 3.5m
147
207
  {
148
- maxMs: 210000,
149
- roundedStepMs: 120000,
208
+ maxMs: ONE_MINUTE_IN_MS * 3.5,
209
+ roundedStepMs: ONE_MINUTE_IN_MS * 2,
150
210
  display: '2m'
151
211
  },
152
212
  // max: 7.5m
153
213
  {
154
- maxMs: 450000,
155
- roundedStepMs: 300000,
214
+ maxMs: ONE_MINUTE_IN_MS * 7.5,
215
+ roundedStepMs: ONE_MINUTE_IN_MS * 5,
156
216
  display: '5m'
157
217
  },
158
218
  // max: 12.5m
159
219
  {
160
- maxMs: 750000,
161
- roundedStepMs: 600000,
220
+ maxMs: ONE_MINUTE_IN_MS * 12.5,
221
+ roundedStepMs: ONE_MINUTE_IN_MS * 10,
162
222
  display: '10m'
163
223
  },
164
224
  // max: 12.5m
165
225
  {
166
- maxMs: 1050000,
167
- roundedStepMs: 900000,
226
+ maxMs: ONE_MINUTE_IN_MS * 12.5,
227
+ roundedStepMs: ONE_MINUTE_IN_MS * 15,
168
228
  display: '15m'
169
229
  },
170
230
  // max: 25m
171
231
  {
172
- maxMs: 1500000,
173
- roundedStepMs: 1200000,
232
+ maxMs: ONE_MINUTE_IN_MS * 25,
233
+ roundedStepMs: ONE_MINUTE_IN_MS * 20,
174
234
  display: '20m'
175
235
  },
176
236
  // max: 45m
177
237
  {
178
- maxMs: 2700000,
179
- roundedStepMs: 1800000,
238
+ maxMs: ONE_MINUTE_IN_MS * 45,
239
+ roundedStepMs: ONE_MINUTE_IN_MS * 30,
180
240
  display: '30m'
181
241
  },
182
242
  // max: 1.5h
183
243
  {
184
- maxMs: 5400000,
185
- roundedStepMs: 3600000,
244
+ maxMs: ONE_HOUR_IN_MS * 1.5,
245
+ roundedStepMs: ONE_HOUR_IN_MS,
186
246
  display: '1h'
187
247
  },
188
248
  // max: 2.5h
189
249
  {
190
- maxMs: 9000000,
191
- roundedStepMs: 7200000,
250
+ maxMs: ONE_HOUR_IN_MS * 2.5,
251
+ roundedStepMs: ONE_HOUR_IN_MS * 2,
192
252
  display: '2h'
193
253
  },
194
254
  // max: 4.5h
195
255
  {
196
- maxMs: 16200000,
197
- roundedStepMs: 10800000,
256
+ maxMs: ONE_HOUR_IN_MS * 4.5,
257
+ roundedStepMs: ONE_HOUR_IN_MS * 3,
198
258
  display: '3h'
199
259
  },
200
260
  // max: 9h
201
261
  {
202
- maxMs: 32400000,
203
- roundedStepMs: 21600000,
262
+ maxMs: ONE_HOUR_IN_MS * 9,
263
+ roundedStepMs: ONE_HOUR_IN_MS * 6,
204
264
  display: '6h'
205
265
  },
206
266
  // max: 1d
207
267
  {
208
- maxMs: 86400000,
209
- roundedStepMs: 43200000,
268
+ maxMs: ONE_DAY_IN_MS,
269
+ roundedStepMs: ONE_HOUR_IN_MS * 12,
210
270
  display: '12h'
211
271
  },
212
272
  // max: 1w
213
273
  {
214
- maxMs: 604800000,
215
- roundedStepMs: 86400000,
274
+ maxMs: ONE_WEEK_IN_MS,
275
+ roundedStepMs: ONE_DAY_IN_MS,
216
276
  display: '1d'
217
277
  },
218
278
  // max: 3w
219
279
  {
220
- maxMs: 1814400000,
221
- roundedStepMs: 604800000,
280
+ maxMs: ONE_WEEK_IN_MS * 3,
281
+ roundedStepMs: ONE_WEEK_IN_MS,
222
282
  display: '1w'
223
283
  },
224
284
  // max: 6w
225
285
  {
226
- maxMs: 3628800000,
227
- roundedStepMs: 2592000000,
286
+ maxMs: ONE_WEEK_IN_MS * 6,
287
+ roundedStepMs: ONE_DAY_IN_MS * 30,
228
288
  display: '30d'
229
289
  },
230
290
  // max: 2y
231
291
  {
232
- maxMs: 63072000000,
233
- roundedStepMs: 31536000000,
292
+ maxMs: ONE_YEAR_IN_MS * 2,
293
+ roundedStepMs: ONE_YEAR_IN_MS,
234
294
  display: '1y'
235
295
  }
236
296
  ];
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/model/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 { Duration, sub } from 'date-fns';\n\nexport type UnixTimeMs = number;\n\nexport type DateTimeFormat = number | string;\n\nexport interface AbsoluteTimeRange {\n start: Date;\n end: Date;\n}\n\nexport interface RelativeTimeRange {\n // End date or undefined if relative to the current Date\n end?: Date;\n pastDuration: DurationString;\n}\n\nexport type TimeRangeValue = AbsoluteTimeRange | RelativeTimeRange;\n\n/**\n * Determine whether a given time range is relative\n */\nexport function isRelativeTimeRange(timeRange: TimeRangeValue): timeRange is RelativeTimeRange {\n return (timeRange as RelativeTimeRange).pastDuration !== undefined;\n}\n\n/**\n * Determine whether a given time range is absolute\n */\nexport function isAbsoluteTimeRange(timeRange: TimeRangeValue): timeRange is AbsoluteTimeRange {\n return (timeRange as AbsoluteTimeRange).start !== undefined && (timeRange as AbsoluteTimeRange).end !== undefined;\n}\n\n/**\n * Returns an absolute time range from a RelativeTimeRange.\n */\nexport function toAbsoluteTimeRange(timeRange: RelativeTimeRange): AbsoluteTimeRange {\n const end = timeRange.end ?? new Date();\n\n return {\n start: sub(end, parseDurationString(timeRange.pastDuration)),\n end,\n };\n}\n\ntype MillisecondsDurationString = `${number}ms`;\ntype SecondsDurationString = `${number}s`;\ntype MinutesDurationString = `${number}m`;\ntype HoursDurationString = `${number}h`;\ntype DaysDurationString = `${number}d`;\ntype WeeksDurationString = `${number}w`;\ntype YearsDurationString = `${number}y`;\n\nexport type DurationString = Exclude<\n `${YearsDurationString | ''}${WeeksDurationString | ''}${DaysDurationString | ''}${HoursDurationString | ''}${\n | MinutesDurationString\n | ''}${SecondsDurationString | ''}${MillisecondsDurationString | ''}`,\n ''\n>;\n\nconst DURATION_REGEX = /^(?:(\\d+)y)?(?:(\\d+)w)?(?:(\\d+)d)?(?:(\\d+)h)?(?:(\\d+)m)?(?:(\\d+)s)?(?:(\\d+)ms)?$/;\n\n/**\n * Parses a DurationString into a Duration object with numeric values that can\n * be used to do Date math. Throws if not a valid duration string.\n */\nexport function parseDurationString(durationString: string): Duration {\n const matches = DURATION_REGEX.exec(durationString);\n if (matches === null) {\n throw new Error(`Invalid duration string '${durationString}'`);\n }\n\n return {\n years: parseInt(matches[1] ?? '0'),\n months: 0,\n weeks: parseInt(matches[2] ?? '0'),\n days: parseInt(matches[3] ?? '0'),\n hours: parseInt(matches[4] ?? '0'),\n minutes: parseInt(matches[5] ?? '0'),\n seconds: parseInt(matches[6] ?? '0') + parseInt(matches[7] ?? '0') / 1000,\n };\n}\n\n/**\n * Returns true if the given string is a valid DurationString.\n */\nexport function isDurationString(maybeDuration: string): maybeDuration is DurationString {\n if (maybeDuration === '') return false;\n return DURATION_REGEX.test(maybeDuration);\n}\n\nconst DEFAULT_STEP_MS = 15000;\n\nconst ROUNDED_STEP_INTERVALS = [\n // max: 0.015s\n { maxMs: 15, roundedStepMs: 10, display: '0.01s' },\n // max: 0.035s\n { maxMs: 35, roundedStepMs: 20, display: '0.02s' },\n // max: 0.075s\n { maxMs: 75, roundedStepMs: 50, display: '0.05s' },\n // max: 0.15s\n { maxMs: 150, roundedStepMs: 100, display: '0.1s' },\n // max: 0.35s\n { maxMs: 350, roundedStepMs: 200, display: '0.2s' },\n // max: 0.75s\n { maxMs: 750, roundedStepMs: 500, display: '0.5s' },\n // max: 1.5s\n { maxMs: 1500, roundedStepMs: 1000, display: '1s' },\n // max: 3.5s\n { maxMs: 3500, roundedStepMs: 2000, display: '2s' },\n // max: 7.5s\n { maxMs: 7500, roundedStepMs: 5000, display: '5s' },\n // max: 12.5s\n { maxMs: 12500, roundedStepMs: 10000, display: '10s' },\n // max: 17.5s\n { maxMs: 17500, roundedStepMs: 15000, display: '15s' },\n // max: 25s\n { maxMs: 25000, roundedStepMs: 20000, display: '20s' },\n // max: 45s\n { maxMs: 45000, roundedStepMs: 30000, display: '30s' },\n // max: 1.5m\n { maxMs: 90000, roundedStepMs: 60000, display: '1m' },\n // max: 3.5m\n { maxMs: 210000, roundedStepMs: 120000, display: '2m' },\n // max: 7.5m\n { maxMs: 450000, roundedStepMs: 300000, display: '5m' },\n // max: 12.5m\n { maxMs: 750000, roundedStepMs: 600000, display: '10m' },\n // max: 12.5m\n { maxMs: 1050000, roundedStepMs: 900000, display: '15m' },\n // max: 25m\n { maxMs: 1500000, roundedStepMs: 1200000, display: '20m' },\n // max: 45m\n { maxMs: 2700000, roundedStepMs: 1800000, display: '30m' },\n // max: 1.5h\n { maxMs: 5400000, roundedStepMs: 3600000, display: '1h' },\n // max: 2.5h\n { maxMs: 9000000, roundedStepMs: 7200000, display: '2h' },\n // max: 4.5h\n { maxMs: 16200000, roundedStepMs: 10800000, display: '3h' },\n // max: 9h\n { maxMs: 32400000, roundedStepMs: 21600000, display: '6h' },\n // max: 1d\n { maxMs: 86400000, roundedStepMs: 43200000, display: '12h' },\n // max: 1w\n { maxMs: 604800000, roundedStepMs: 86400000, display: '1d' },\n // max: 3w\n { maxMs: 1814400000, roundedStepMs: 604800000, display: '1w' },\n // max: 6w\n { maxMs: 3628800000, roundedStepMs: 2592000000, display: '30d' },\n // max: 2y\n { maxMs: 63072000000, roundedStepMs: 31536000000, display: '1y' },\n];\n\n/**\n * Round interval to clearer increments\n */\nexport function roundStepInterval(stepMs: number) {\n for (const { maxMs, roundedStepMs } of ROUNDED_STEP_INTERVALS) {\n if (stepMs < maxMs) {\n return roundedStepMs;\n }\n }\n return DEFAULT_STEP_MS;\n}\n\n/**\n * Gets a suggested step/interval size for a time range based on the width of a visual component.\n */\nexport function getSuggestedStepMs(timeRange: AbsoluteTimeRange, width: number) {\n const queryRangeMs = timeRange.end.valueOf() - timeRange.start.valueOf();\n const stepMs = Math.floor(queryRangeMs / width);\n return roundStepInterval(stepMs);\n}\n"],"names":["sub","isRelativeTimeRange","timeRange","pastDuration","undefined","isAbsoluteTimeRange","start","end","toAbsoluteTimeRange","Date","parseDurationString","DURATION_REGEX","durationString","matches","exec","Error","years","parseInt","months","weeks","days","hours","minutes","seconds","isDurationString","maybeDuration","test","DEFAULT_STEP_MS","ROUNDED_STEP_INTERVALS","maxMs","roundedStepMs","display","roundStepInterval","stepMs","getSuggestedStepMs","width","queryRangeMs","valueOf","Math","floor"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE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E,QAAQ;QAAEC,OAAO,EAAE,IAAI;KAAE;IAC5D,UAAU;IACV;QAAEF,KAAK,EAAE,UAAU;QAAEC,aAAa,EAAE,SAAS;QAAEC,OAAO,EAAE,IAAI;KAAE;IAC9D,UAAU;IACV;QAAEF,KAAK,EAAE,UAAU;QAAEC,aAAa,EAAE,UAAU;QAAEC,OAAO,EAAE,KAAK;KAAE;IAChE,UAAU;IACV;QAAEF,KAAK,EAAE,WAAW;QAAEC,aAAa,EAAE,WAAW;QAAEC,OAAO,EAAE,IAAI;KAAE;CAClE,AAAC;AAEF;;CAEC,GACD,OAAO,SAASC,iBAAiB,CAACC,MAAc,EAAE;IAChD,KAAK,MAAM,EAAEJ,KAAK,CAAA,EAAEC,aAAa,CAAA,EAAE,IAAIF,sBAAsB,CAAE;QAC7D,IAAIK,MAAM,GAAGJ,KAAK,EAAE;YAClB,OAAOC,aAAa,CAAC;QACvB,CAAC;IACH,CAAC;IACD,OAAOH,eAAe,CAAC;AACzB,CAAC;AAED;;CAEC,GACD,OAAO,SAASO,kBAAkB,CAAChC,SAA4B,EAAEiC,KAAa,EAAE;IAC9E,MAAMC,YAAY,GAAGlC,SAAS,CAACK,GAAG,CAAC8B,OAAO,EAAE,GAAGnC,SAAS,CAACI,KAAK,CAAC+B,OAAO,EAAE,AAAC;IACzE,MAAMJ,MAAM,GAAGK,IAAI,CAACC,KAAK,CAACH,YAAY,GAAGD,KAAK,CAAC,AAAC;IAChD,OAAOH,iBAAiB,CAACC,MAAM,CAAC,CAAC;AACnC,CAAC"}
1
+ {"version":3,"sources":["../../src/model/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 { Duration, sub } from 'date-fns';\n\nconst ONE_SECOND_IN_MS = 1000;\nconst ONE_MINUTE_IN_MS = 60000;\nconst ONE_HOUR_IN_MS = 3600000;\nconst ONE_DAY_IN_MS = 86400000; // assuming a day has always 24h\nconst ONE_WEEK_IN_MS = 604800000; // assuming a week has always 7d\nconst ONE_YEAR_IN_MS = 31536000000; // assuming a year has always 365d\n\nexport type UnixTimeMs = number;\n\nexport type DateTimeFormat = number | string;\n\nexport interface AbsoluteTimeRange {\n start: Date;\n end: Date;\n}\n\nexport interface RelativeTimeRange {\n // End date or undefined if relative to the current Date\n end?: Date;\n pastDuration: DurationString;\n}\n\nexport type TimeRangeValue = AbsoluteTimeRange | RelativeTimeRange;\n\n/**\n * Determine whether a given time range is relative\n */\nexport function isRelativeTimeRange(timeRange: TimeRangeValue): timeRange is RelativeTimeRange {\n return (timeRange as RelativeTimeRange).pastDuration !== undefined;\n}\n\n/**\n * Determine whether a given time range is absolute\n */\nexport function isAbsoluteTimeRange(timeRange: TimeRangeValue): timeRange is AbsoluteTimeRange {\n return (timeRange as AbsoluteTimeRange).start !== undefined && (timeRange as AbsoluteTimeRange).end !== undefined;\n}\n\n/**\n * Returns an absolute time range from a RelativeTimeRange.\n */\nexport function toAbsoluteTimeRange(timeRange: RelativeTimeRange): AbsoluteTimeRange {\n const end = timeRange.end ?? new Date();\n\n return {\n start: sub(end, parseDurationString(timeRange.pastDuration)),\n end,\n };\n}\n\ntype MillisecondsDurationString = `${number}ms`;\ntype SecondsDurationString = `${number}s`;\ntype MinutesDurationString = `${number}m`;\ntype HoursDurationString = `${number}h`;\ntype DaysDurationString = `${number}d`;\ntype WeeksDurationString = `${number}w`;\ntype YearsDurationString = `${number}y`;\n\nexport type DurationString = Exclude<\n `${YearsDurationString | ''}${WeeksDurationString | ''}${DaysDurationString | ''}${HoursDurationString | ''}${\n | MinutesDurationString\n | ''}${SecondsDurationString | ''}${MillisecondsDurationString | ''}`,\n ''\n>;\n\nconst DURATION_REGEX = /^(?:(\\d+)y)?(?:(\\d+)w)?(?:(\\d+)d)?(?:(\\d+)h)?(?:(\\d+)m)?(?:(\\d+)s)?(?:(\\d+)ms)?$/;\n\n/**\n * Parses a DurationString into a Duration object with numeric values that can\n * be used to do Date math. Throws if not a valid duration string.\n */\nexport function parseDurationString(durationString: string): Duration {\n const matches = DURATION_REGEX.exec(durationString);\n if (matches === null) {\n throw new Error(`Invalid duration string '${durationString}'`);\n }\n\n return {\n years: parseInt(matches[1] ?? '0'),\n months: 0,\n weeks: parseInt(matches[2] ?? '0'),\n days: parseInt(matches[3] ?? '0'),\n hours: parseInt(matches[4] ?? '0'),\n minutes: parseInt(matches[5] ?? '0'),\n seconds: parseInt(matches[6] ?? '0') + parseInt(matches[7] ?? '0') / 1000,\n };\n}\n\n/**\n * Returns true if the given string is a valid DurationString.\n */\nexport function isDurationString(maybeDuration: string): maybeDuration is DurationString {\n if (maybeDuration === '') return false;\n return DURATION_REGEX.test(maybeDuration);\n}\n\nexport function intervalToPrometheusDuration(timeRange: AbsoluteTimeRange): Duration {\n const durationInMs = timeRange.end.valueOf() - timeRange.start.valueOf();\n return msToPrometheusDuration(durationInMs);\n}\n\nexport function msToPrometheusDuration(durationInMs: number): Duration {\n const years = Math.trunc(durationInMs / ONE_YEAR_IN_MS);\n if (years > 0) durationInMs -= years * ONE_YEAR_IN_MS;\n const weeks = Math.trunc(durationInMs / ONE_WEEK_IN_MS);\n if (weeks > 0) durationInMs -= weeks * ONE_WEEK_IN_MS;\n const days = Math.trunc(durationInMs / ONE_DAY_IN_MS);\n if (days > 0) durationInMs -= days * ONE_DAY_IN_MS;\n const hours = Math.trunc(durationInMs / ONE_HOUR_IN_MS);\n if (hours > 0) durationInMs -= hours * ONE_HOUR_IN_MS;\n const minutes = Math.trunc(durationInMs / ONE_MINUTE_IN_MS);\n if (minutes > 0) durationInMs -= minutes * ONE_MINUTE_IN_MS;\n\n return {\n years: years,\n months: 0,\n weeks: weeks,\n days: days,\n hours: hours,\n minutes: minutes,\n seconds: durationInMs / 1000,\n };\n}\n\nexport function formatDuration(duration: Duration): DurationString {\n const result: string[] = [];\n if (duration.years) {\n result.push(`${duration.years}y`);\n }\n if (duration.weeks) {\n result.push(`${duration.weeks}w`);\n }\n if (duration.days) {\n result.push(`${duration.days}d`);\n }\n if (duration.hours) {\n result.push(`${duration.hours}h`);\n }\n if (duration.minutes) {\n result.push(`${duration.minutes}m`);\n }\n if (duration.seconds) {\n const seconds = Math.trunc(duration.seconds);\n if (seconds) {\n result.push(`${seconds}s`);\n }\n const ms = (duration.seconds - seconds) * 1000;\n if (ms) {\n result.push(`${ms}ms`);\n }\n }\n return result.join('') as DurationString;\n}\n\nconst DEFAULT_STEP_MS = 15000;\n\nconst ROUNDED_STEP_INTERVALS = [\n // max: 0.015s\n { maxMs: 15, roundedStepMs: 10, display: '0.01s' },\n // max: 0.035s\n { maxMs: 35, roundedStepMs: 20, display: '0.02s' },\n // max: 0.075s\n { maxMs: 75, roundedStepMs: 50, display: '0.05s' },\n // max: 0.15s\n { maxMs: 150, roundedStepMs: 100, display: '0.1s' },\n // max: 0.35s\n { maxMs: 350, roundedStepMs: 200, display: '0.2s' },\n // max: 0.75s\n { maxMs: 750, roundedStepMs: 500, display: '0.5s' },\n // max: 1.5s\n { maxMs: ONE_SECOND_IN_MS * 1.5, roundedStepMs: ONE_SECOND_IN_MS, display: '1s' },\n // max: 3.5s\n { maxMs: ONE_SECOND_IN_MS * 3.5, roundedStepMs: ONE_SECOND_IN_MS * 2, display: '2s' },\n // max: 7.5s\n { maxMs: ONE_SECOND_IN_MS * 7.5, roundedStepMs: ONE_SECOND_IN_MS * 5, display: '5s' },\n // max: 12.5s\n { maxMs: ONE_SECOND_IN_MS * 12.5, roundedStepMs: ONE_SECOND_IN_MS * 10, display: '10s' },\n // max: 17.5s\n { maxMs: ONE_SECOND_IN_MS * 17.5, roundedStepMs: ONE_SECOND_IN_MS * 15, display: '15s' },\n // max: 25s\n { maxMs: ONE_SECOND_IN_MS * 25, roundedStepMs: ONE_SECOND_IN_MS * 20, display: '20s' },\n // max: 45s\n { maxMs: ONE_SECOND_IN_MS * 45, roundedStepMs: ONE_SECOND_IN_MS * 30, display: '30s' },\n // max: 1.5m\n { maxMs: ONE_MINUTE_IN_MS * 1.5, roundedStepMs: ONE_MINUTE_IN_MS, display: '1m' },\n // max: 3.5m\n { maxMs: ONE_MINUTE_IN_MS * 3.5, roundedStepMs: ONE_MINUTE_IN_MS * 2, display: '2m' },\n // max: 7.5m\n { maxMs: ONE_MINUTE_IN_MS * 7.5, roundedStepMs: ONE_MINUTE_IN_MS * 5, display: '5m' },\n // max: 12.5m\n { maxMs: ONE_MINUTE_IN_MS * 12.5, roundedStepMs: ONE_MINUTE_IN_MS * 10, display: '10m' },\n // max: 12.5m\n { maxMs: ONE_MINUTE_IN_MS * 12.5, roundedStepMs: ONE_MINUTE_IN_MS * 15, display: '15m' },\n // max: 25m\n { maxMs: ONE_MINUTE_IN_MS * 25, roundedStepMs: ONE_MINUTE_IN_MS * 20, display: '20m' },\n // max: 45m\n { maxMs: ONE_MINUTE_IN_MS * 45, roundedStepMs: ONE_MINUTE_IN_MS * 30, display: '30m' },\n // max: 1.5h\n { maxMs: ONE_HOUR_IN_MS * 1.5, roundedStepMs: ONE_HOUR_IN_MS, display: '1h' },\n // max: 2.5h\n { maxMs: ONE_HOUR_IN_MS * 2.5, roundedStepMs: ONE_HOUR_IN_MS * 2, display: '2h' },\n // max: 4.5h\n { maxMs: ONE_HOUR_IN_MS * 4.5, roundedStepMs: ONE_HOUR_IN_MS * 3, display: '3h' },\n // max: 9h\n { maxMs: ONE_HOUR_IN_MS * 9, roundedStepMs: ONE_HOUR_IN_MS * 6, display: '6h' },\n // max: 1d\n { maxMs: ONE_DAY_IN_MS, roundedStepMs: ONE_HOUR_IN_MS * 12, display: '12h' },\n // max: 1w\n { maxMs: ONE_WEEK_IN_MS, roundedStepMs: ONE_DAY_IN_MS, display: '1d' },\n // max: 3w\n { maxMs: ONE_WEEK_IN_MS * 3, roundedStepMs: ONE_WEEK_IN_MS, display: '1w' },\n // max: 6w\n { maxMs: ONE_WEEK_IN_MS * 6, roundedStepMs: ONE_DAY_IN_MS * 30, display: '30d' },\n // max: 2y\n { maxMs: ONE_YEAR_IN_MS * 2, roundedStepMs: ONE_YEAR_IN_MS, display: '1y' },\n];\n\n/**\n * Round interval to clearer increments\n */\nexport function roundStepInterval(stepMs: number) {\n for (const { maxMs, roundedStepMs } of ROUNDED_STEP_INTERVALS) {\n if (stepMs < maxMs) {\n return roundedStepMs;\n }\n }\n return DEFAULT_STEP_MS;\n}\n\n/**\n * Gets a suggested step/interval size for a time range based on the width of a visual component.\n */\nexport function getSuggestedStepMs(timeRange: AbsoluteTimeRange, width: number) {\n const queryRangeMs = timeRange.end.valueOf() - timeRange.start.valueOf();\n const stepMs = Math.floor(queryRangeMs / width);\n return roundStepInterval(stepMs);\n}\n"],"names":["sub","ONE_SECOND_IN_MS","ONE_MINUTE_IN_MS","ONE_HOUR_IN_MS","ONE_DAY_IN_MS","ONE_WEEK_IN_MS","ONE_YEAR_IN_MS","isRelativeTimeRange","timeRange","pastDuration","undefined","isAbsoluteTimeRange","start","end","toAbsoluteTimeRange","Date","parseDurationString","DURATION_REGEX","durationString","matches","exec","Error","years","parseInt","months","weeks","days","hours","minutes","seconds","isDurationString","maybeDuration","test","intervalToPrometheusDuration","durationInMs","valueOf","msToPrometheusDuration","Math","trunc","formatDuration","duration","result","push","ms","join","DEFAULT_STEP_MS","ROUNDED_STEP_INTERVALS","maxMs","roundedStepMs","display","roundStepInterval","stepMs","getSuggestedStepMs","width","queryRangeMs","floor"],"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;A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IAAK;IAChF,YAAY;IACZ;QAAEF,OAAO5C,iBAAiB;QAAK6C,eAAe7C,iBAAiB;QAAG8C,SAAS;IAAK;IAChF,UAAU;IACV;QAAEF,OAAO5C,iBAAiB;QAAG6C,eAAe7C,iBAAiB;QAAG8C,SAAS;IAAK;IAC9E,UAAU;IACV;QAAEF,OAAO3C;QAAe4C,eAAe7C,iBAAiB;QAAI8C,SAAS;IAAM;IAC3E,UAAU;IACV;QAAEF,OAAO1C;QAAgB2C,eAAe5C;QAAe6C,SAAS;IAAK;IACrE,UAAU;IACV;QAAEF,OAAO1C,iBAAiB;QAAG2C,eAAe3C;QAAgB4C,SAAS;IAAK;IAC1E,UAAU;IACV;QAAEF,OAAO1C,iBAAiB;QAAG2C,eAAe5C,gBAAgB;QAAI6C,SAAS;IAAM;IAC/E,UAAU;IACV;QAAEF,OAAOzC,iBAAiB;QAAG0C,eAAe1C;QAAgB2C,SAAS;IAAK;CAC3E;AAED;;CAEC,GACD,OAAO,SAASC,kBAAkBC,MAAc;IAC9C,KAAK,MAAM,EAAEJ,MAAK,EAAEC,cAAa,EAAE,IAAIF,uBAAwB;QAC7D,IAAIK,SAASJ,OAAO;YAClB,OAAOC;QACT;IACF;IACA,OAAOH;AACT;AAEA;;CAEC,GACD,OAAO,SAASO,mBAAmB5C,SAA4B,EAAE6C,KAAa;IAC5E,MAAMC,eAAe9C,UAAUK,IAAIsB,YAAY3B,UAAUI,MAAMuB;IAC/D,MAAMgB,SAASd,KAAKkB,MAAMD,eAAeD;IACzC,OAAOH,kBAAkBC;AAC3B"}
@@ -1,13 +1,13 @@
1
1
  import { UnitGroupConfig, UnitConfig } from './types';
2
- declare const bytesUnitKinds: readonly ["Bytes"];
3
- declare type BytesUnitKind = (typeof bytesUnitKinds)[number];
4
- export declare type BytesUnitOptions = {
5
- kind: BytesUnitKind;
6
- decimal_places?: number;
7
- abbreviate?: boolean;
2
+ declare const bytesUnits: readonly ["bytes"];
3
+ declare type BytesUnit = (typeof bytesUnits)[number];
4
+ export declare type BytesFormatOptions = {
5
+ unit: BytesUnit;
6
+ decimalPlaces?: number;
7
+ shortValues?: boolean;
8
8
  };
9
9
  export declare const BYTES_GROUP_CONFIG: UnitGroupConfig;
10
- export declare const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnitKind, UnitConfig>>;
11
- export declare function formatBytes(bytes: number, { abbreviate, decimal_places }: BytesUnitOptions): string;
10
+ export declare const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnit, UnitConfig>>;
11
+ export declare function formatBytes(bytes: number, { shortValues, decimalPlaces }: BytesFormatOptions): string;
12
12
  export {};
13
13
  //# sourceMappingURL=bytes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../../src/model/units/bytes.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAatD,QAAA,MAAM,cAAc,oBAAqB,CAAC;AAC1C,aAAK,aAAa,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AACrD,oBAAY,gBAAgB,GAAG;IAC7B,IAAI,EAAE,aAAa,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AACF,eAAO,MAAM,kBAAkB,EAAE,eAIhC,CAAC;AACF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,aAAa,EAAE,UAAU,CAAC,CAKzE,CAAC;AAEF,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,cAAc,EAAE,EAAE,gBAAgB,UAmC1F"}
1
+ {"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../../src/model/units/bytes.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAatD,QAAA,MAAM,UAAU,oBAAqB,CAAC;AACtC,aAAK,SAAS,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC;AAC7C,oBAAY,kBAAkB,GAAG;IAC/B,IAAI,EAAE,SAAS,CAAC;IAChB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AACF,eAAO,MAAM,kBAAkB,EAAE,eAIhC,CAAC;AACF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,CAKrE,CAAC;AAEF,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,aAAa,EAAE,EAAE,kBAAkB,UAmC5F"}
@@ -12,7 +12,7 @@
12
12
  // limitations under the License.
13
13
  import numbro from 'numbro';
14
14
  import { MAX_SIGNIFICANT_DIGITS } from './constants';
15
- import { hasDecimalPlaces, limitDecimalPlaces, shouldAbbreviate } from './utils';
15
+ import { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';
16
16
  /**
17
17
  * We consider the units for bytes to be powers of 1000.
18
18
  * In other words:
@@ -20,52 +20,52 @@ import { hasDecimalPlaces, limitDecimalPlaces, shouldAbbreviate } from './utils'
20
20
  * 1 MB = 1,000,000 bytes (1000^2 bytes)
21
21
  * etc.
22
22
  */ const DEFAULT_NUMBRO_MANTISSA = 2;
23
- const bytesUnitKinds = [
24
- 'Bytes'
23
+ const bytesUnits = [
24
+ 'bytes'
25
25
  ];
26
26
  export const BYTES_GROUP_CONFIG = {
27
27
  label: 'Bytes',
28
- decimal_places: true,
29
- abbreviate: true
28
+ decimalPlaces: true,
29
+ shortValues: true
30
30
  };
31
31
  export const BYTES_UNIT_CONFIG = {
32
- Bytes: {
32
+ bytes: {
33
33
  group: 'Bytes',
34
34
  label: 'Bytes'
35
35
  }
36
36
  };
37
- export function formatBytes(bytes, { abbreviate , decimal_places }) {
37
+ export function formatBytes(bytes, { shortValues , decimalPlaces }) {
38
38
  // If we're showing the entire value, we can use Intl.NumberFormat.
39
- if (!shouldAbbreviate(abbreviate) || Math.abs(bytes) < 1000) {
39
+ if (!shouldShortenValues(shortValues) || Math.abs(bytes) < 1000) {
40
40
  const formatterOptions = {
41
41
  style: 'unit',
42
42
  unit: 'byte',
43
43
  unitDisplay: 'long',
44
44
  useGrouping: true
45
45
  };
46
- if (hasDecimalPlaces(decimal_places)) {
47
- formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimal_places);
48
- formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);
46
+ if (hasDecimalPlaces(decimalPlaces)) {
47
+ formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);
48
+ formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);
49
49
  } else {
50
50
  // This can happen if bytes is between -1000 and 1000
51
- if (shouldAbbreviate(abbreviate)) {
51
+ if (shouldShortenValues(shortValues)) {
52
52
  formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
53
53
  }
54
54
  }
55
55
  const formatter = Intl.NumberFormat('en-US', formatterOptions);
56
56
  return formatter.format(bytes);
57
57
  }
58
- // If we're showing the "abbreviated" value, we use numbro.
58
+ // If we're showing the shorten value, we use numbro.
59
59
  // numbro is able to add units like KB, MB, GB, etc. correctly.
60
60
  return numbro(bytes).format({
61
61
  output: 'byte',
62
62
  base: 'decimal',
63
63
  spaceSeparated: true,
64
- mantissa: hasDecimalPlaces(decimal_places) ? decimal_places : DEFAULT_NUMBRO_MANTISSA,
64
+ mantissa: hasDecimalPlaces(decimalPlaces) ? decimalPlaces : DEFAULT_NUMBRO_MANTISSA,
65
65
  // trimMantissa trims trailing 0s
66
- trimMantissa: !hasDecimalPlaces(decimal_places),
66
+ trimMantissa: !hasDecimalPlaces(decimalPlaces),
67
67
  // optionalMantissa excludes all the decimal places if they're all zeros
68
- optionalMantissa: !hasDecimalPlaces(decimal_places)
68
+ optionalMantissa: !hasDecimalPlaces(decimalPlaces)
69
69
  });
70
70
  }
71
71
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/model/units/bytes.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 numbro from 'numbro';\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces, shouldAbbreviate } from './utils';\n\n/**\n * We consider the units for bytes to be powers of 1000.\n * In other words:\n * 1 KB = 1000 bytes (1000^1 bytes)\n * 1 MB = 1,000,000 bytes (1000^2 bytes)\n * etc.\n */\n\nconst DEFAULT_NUMBRO_MANTISSA = 2;\n\nconst bytesUnitKinds = ['Bytes'] as const;\ntype BytesUnitKind = (typeof bytesUnitKinds)[number];\nexport type BytesUnitOptions = {\n kind: BytesUnitKind;\n decimal_places?: number;\n abbreviate?: boolean;\n};\nexport const BYTES_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Bytes',\n decimal_places: true,\n abbreviate: true,\n};\nexport const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnitKind, UnitConfig>> = {\n Bytes: {\n group: 'Bytes',\n label: 'Bytes',\n },\n};\n\nexport function formatBytes(bytes: number, { abbreviate, decimal_places }: BytesUnitOptions) {\n // If we're showing the entire value, we can use Intl.NumberFormat.\n if (!shouldAbbreviate(abbreviate) || Math.abs(bytes) < 1000) {\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'unit',\n unit: 'byte',\n unitDisplay: 'long',\n useGrouping: true,\n };\n\n if (hasDecimalPlaces(decimal_places)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimal_places);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);\n } else {\n // This can happen if bytes is between -1000 and 1000\n if (shouldAbbreviate(abbreviate)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n const formatter = Intl.NumberFormat('en-US', formatterOptions);\n return formatter.format(bytes);\n }\n\n // If we're showing the \"abbreviated\" value, we use numbro.\n // numbro is able to add units like KB, MB, GB, etc. correctly.\n return numbro(bytes).format({\n output: 'byte',\n base: 'decimal',\n spaceSeparated: true,\n mantissa: hasDecimalPlaces(decimal_places) ? decimal_places : DEFAULT_NUMBRO_MANTISSA,\n // trimMantissa trims trailing 0s\n trimMantissa: !hasDecimalPlaces(decimal_places),\n // optionalMantissa excludes all the decimal places if they're all zeros\n optionalMantissa: !hasDecimalPlaces(decimal_places),\n });\n}\n"],"names":["numbro","MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","shouldAbbreviate","DEFAULT_NUMBRO_MANTISSA","bytesUnitKinds","BYTES_GROUP_CONFIG","label","decimal_places","abbreviate","BYTES_UNIT_CONFIG","Bytes","group","formatBytes","bytes","Math","abs","formatterOptions","style","unit","unitDisplay","useGrouping","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","formatter","Intl","NumberFormat","format","output","base","spaceSeparated","mantissa","trimMantissa","optionalMantissa"],"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,OAAOA,MAAM,MAAM,QAAQ,CAAC;AAE5B,SAASC,sBAAsB,QAAQ,aAAa,CAAC;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,EAAEC,gBAAgB,QAAQ,SAAS,CAAC;AAEjF;;;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AOf,MAAM,CAACe,KAAK,CAAC,CAACc,MAAM,CAAC;QAC1BC,MAAM,EAAE,MAAM;QACdC,IAAI,EAAE,SAAS;QACfC,cAAc,EAAE,IAAI;QACpBC,QAAQ,EAAE/B,gBAAgB,CAACO,cAAc,CAAC,GAAGA,cAAc,GAAGJ,uBAAuB;QACrF,iCAAiC;QACjC6B,YAAY,EAAE,CAAChC,gBAAgB,CAACO,cAAc,CAAC;QAC/C,wEAAwE;QACxE0B,gBAAgB,EAAE,CAACjC,gBAAgB,CAACO,cAAc,CAAC;KACpD,CAAC,CAAC;AACL,CAAC"}
1
+ {"version":3,"sources":["../../../src/model/units/bytes.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 numbro from 'numbro';\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';\n\n/**\n * We consider the units for bytes to be powers of 1000.\n * In other words:\n * 1 KB = 1000 bytes (1000^1 bytes)\n * 1 MB = 1,000,000 bytes (1000^2 bytes)\n * etc.\n */\n\nconst DEFAULT_NUMBRO_MANTISSA = 2;\n\nconst bytesUnits = ['bytes'] as const;\ntype BytesUnit = (typeof bytesUnits)[number];\nexport type BytesFormatOptions = {\n unit: BytesUnit;\n decimalPlaces?: number;\n shortValues?: boolean;\n};\nexport const BYTES_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Bytes',\n decimalPlaces: true,\n shortValues: true,\n};\nexport const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnit, UnitConfig>> = {\n bytes: {\n group: 'Bytes',\n label: 'Bytes',\n },\n};\n\nexport function formatBytes(bytes: number, { shortValues, decimalPlaces }: BytesFormatOptions) {\n // If we're showing the entire value, we can use Intl.NumberFormat.\n if (!shouldShortenValues(shortValues) || Math.abs(bytes) < 1000) {\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'unit',\n unit: 'byte',\n unitDisplay: 'long',\n useGrouping: true,\n };\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n // This can happen if bytes is between -1000 and 1000\n if (shouldShortenValues(shortValues)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n const formatter = Intl.NumberFormat('en-US', formatterOptions);\n return formatter.format(bytes);\n }\n\n // If we're showing the shorten value, we use numbro.\n // numbro is able to add units like KB, MB, GB, etc. correctly.\n return numbro(bytes).format({\n output: 'byte',\n base: 'decimal',\n spaceSeparated: true,\n mantissa: hasDecimalPlaces(decimalPlaces) ? decimalPlaces : DEFAULT_NUMBRO_MANTISSA,\n // trimMantissa trims trailing 0s\n trimMantissa: !hasDecimalPlaces(decimalPlaces),\n // optionalMantissa excludes all the decimal places if they're all zeros\n optionalMantissa: !hasDecimalPlaces(decimalPlaces),\n });\n}\n"],"names":["numbro","MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","shouldShortenValues","DEFAULT_NUMBRO_MANTISSA","bytesUnits","BYTES_GROUP_CONFIG","label","decimalPlaces","shortValues","BYTES_UNIT_CONFIG","bytes","group","formatBytes","Math","abs","formatterOptions","style","unit","unitDisplay","useGrouping","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","formatter","Intl","NumberFormat","format","output","base","spaceSeparated","mantissa","trimMantissa","optionalMantissa"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEA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CAAChC,iBAAiBO;IACtC;AACF"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/model/units/constants.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\n// Common constants needed across individual unit groups and the overall\n// combined units.\nexport const MAX_SIGNIFICANT_DIGITS = 3;\n"],"names":["MAX_SIGNIFICANT_DIGITS"],"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,wEAAwE;AACxE,kBAAkB;AAClB,OAAO,MAAMA,sBAAsB,GAAG,CAAC,CAAC"}
1
+ {"version":3,"sources":["../../../src/model/units/constants.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\n// Common constants needed across individual unit groups and the overall\n// combined units.\nexport const MAX_SIGNIFICANT_DIGITS = 3;\n"],"names":["MAX_SIGNIFICANT_DIGITS"],"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,wEAAwE;AACxE,kBAAkB;AAClB,OAAO,MAAMA,yBAAyB,EAAE"}
@@ -1,13 +1,13 @@
1
1
  import { UnitGroupConfig, UnitConfig } from './types';
2
- declare const decimalUnitKinds: readonly ["Decimal"];
3
- declare type DecimalUnitKind = (typeof decimalUnitKinds)[number];
4
- export declare type DecimalUnitOptions = {
5
- kind: DecimalUnitKind;
6
- decimal_places?: number;
7
- abbreviate?: boolean;
2
+ declare const decimalUnits: readonly ["decimal"];
3
+ declare type DecimalUnit = (typeof decimalUnits)[number];
4
+ export declare type DecimalFormatOptions = {
5
+ unit: DecimalUnit;
6
+ decimalPlaces?: number;
7
+ shortValues?: boolean;
8
8
  };
9
9
  export declare const DECIMAL_GROUP_CONFIG: UnitGroupConfig;
10
- export declare const DECIMAL_UNIT_CONFIG: Readonly<Record<DecimalUnitKind, UnitConfig>>;
11
- export declare function formatDecimal(value: number, { abbreviate, decimal_places }: DecimalUnitOptions): string;
10
+ export declare const DECIMAL_UNIT_CONFIG: Readonly<Record<DecimalUnit, UnitConfig>>;
11
+ export declare function formatDecimal(value: number, { shortValues, decimalPlaces }: DecimalFormatOptions): string;
12
12
  export {};
13
13
  //# sourceMappingURL=decimal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"decimal.d.ts","sourceRoot":"","sources":["../../../src/model/units/decimal.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,QAAA,MAAM,gBAAgB,sBAAuB,CAAC;AAC9C,aAAK,eAAe,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AACzD,oBAAY,kBAAkB,GAAG;IAC/B,IAAI,EAAE,eAAe,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE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