@perses-dev/core 0.34.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,67 +23,146 @@ function _export(target, all) {
23
23
  _export(exports, {
24
24
  CalculationsMap: ()=>CalculationsMap,
25
25
  CALCULATIONS_CONFIG: ()=>CALCULATIONS_CONFIG,
26
- DEFAULT_CALCULATION: ()=>DEFAULT_CALCULATION
26
+ DEFAULT_CALCULATION: ()=>DEFAULT_CALCULATION,
27
+ getCalculations: ()=>getCalculations,
28
+ getCalculation: ()=>getCalculation
27
29
  });
28
- const _findLast = /*#__PURE__*/ _interopRequireDefault(require("lodash/findLast"));
29
- const _meanBy = /*#__PURE__*/ _interopRequireDefault(require("lodash/meanBy"));
30
- const _sumBy = /*#__PURE__*/ _interopRequireDefault(require("lodash/sumBy"));
31
- function _interopRequireDefault(obj) {
32
- return obj && obj.__esModule ? obj : {
33
- default: obj
34
- };
35
- }
36
30
  const CalculationsMap = {
37
31
  First: first,
38
32
  Last: last,
33
+ FirstNumber: firstNumber,
39
34
  LastNumber: lastNumber,
40
35
  Mean: mean,
41
- Sum: sum
36
+ Sum: sum,
37
+ Min: min,
38
+ Max: max
42
39
  };
43
40
  const CALCULATIONS_CONFIG = {
44
41
  First: {
45
- label: 'First'
42
+ label: 'First',
43
+ description: 'First value'
46
44
  },
47
45
  Last: {
48
- label: 'Last'
46
+ label: 'Last',
47
+ description: 'Last value'
48
+ },
49
+ FirstNumber: {
50
+ label: 'First *',
51
+ description: 'First numeric value'
49
52
  },
50
53
  LastNumber: {
51
- label: 'Last number'
54
+ label: 'Last *',
55
+ description: 'Last numeric value'
52
56
  },
53
57
  Mean: {
54
- label: 'Mean'
58
+ label: 'Average',
59
+ description: 'Average value excluding nulls'
55
60
  },
56
61
  Sum: {
57
- label: 'Sum'
62
+ label: 'Sum',
63
+ description: 'The sum of all values'
64
+ },
65
+ Min: {
66
+ label: 'Min',
67
+ description: 'Minimum value'
68
+ },
69
+ Max: {
70
+ label: 'Max',
71
+ description: 'Maximum value'
58
72
  }
59
73
  };
60
74
  const DEFAULT_CALCULATION = 'Sum';
75
+ function getCalculations(values, includeCalculations) {
76
+ const calculations = includeCalculations.reduce((initResult, calculation)=>{
77
+ initResult[calculation] = undefined;
78
+ return initResult;
79
+ }, {});
80
+ // We save these values as separate values instead of directly setting them
81
+ // in the calculations because they are needed by multiple calculations.
82
+ let nonNullCount = 0;
83
+ let sum = 0;
84
+ // We use this large function capable of performing one or more calculations
85
+ // in a single iteration of the data to minimize the performance impact of
86
+ // generating multiple calculations for large timeseries values. This is
87
+ // less optimized for certain single calculations when done in isolation (e.g.
88
+ // `Last`), but will be more performant in the more expensive cases where
89
+ // multiple values are being used (e.g. table legend).
90
+ values.forEach((tuple, i)=>{
91
+ const value = tuple[1];
92
+ if (i === 0 && 'First' in calculations) {
93
+ calculations.First = value;
94
+ }
95
+ if (i === values.length - 1 && 'Last' in calculations) {
96
+ calculations.Last = value;
97
+ }
98
+ // Handling specific to non-null values.
99
+ if (typeof value === 'number') {
100
+ nonNullCount += 1;
101
+ sum += value;
102
+ if ('FirstNumber' in calculations && calculations.FirstNumber === undefined) {
103
+ // Save the first number we see.
104
+ calculations.FirstNumber = value;
105
+ }
106
+ if ('LastNumber' in calculations) {
107
+ // Keep setting the numbers we see, which will eventually be set to the
108
+ // last number when finished iterating.
109
+ calculations.LastNumber = value;
110
+ }
111
+ if ('Min' in calculations) {
112
+ if (typeof calculations.Min !== 'number') {
113
+ // Init the first time we see a number
114
+ calculations.Min = value;
115
+ } else {
116
+ // Use lowest value once initialized
117
+ calculations.Min = Math.min(calculations.Min, value);
118
+ }
119
+ }
120
+ if ('Max' in calculations) {
121
+ if (typeof calculations.Max !== 'number') {
122
+ // Init the first time we see a number
123
+ calculations.Max = value;
124
+ } else {
125
+ // Use highest value once initialized
126
+ calculations.Max = Math.max(calculations.Max, value);
127
+ }
128
+ }
129
+ }
130
+ });
131
+ // Set calculations that require iterating over all values.
132
+ if (nonNullCount > 0 && 'Sum' in calculations) {
133
+ calculations.Sum = sum;
134
+ }
135
+ if (nonNullCount > 0 && 'Mean' in calculations) {
136
+ calculations.Mean = sum / nonNullCount;
137
+ }
138
+ return calculations;
139
+ }
140
+ function getCalculation(values, calculation) {
141
+ return getCalculations(values, [
142
+ calculation
143
+ ])[calculation];
144
+ }
61
145
  function first(values) {
62
- const tuple = values[0];
63
- return tuple === undefined ? undefined : getValue(tuple);
146
+ return getCalculation(values, 'First');
64
147
  }
65
148
  function last(values) {
66
- if (values.length <= 0) return undefined;
67
- const tuple = values[values.length - 1];
68
- return tuple === undefined ? undefined : getValue(tuple);
149
+ return getCalculation(values, 'Last');
150
+ }
151
+ function firstNumber(values) {
152
+ return getCalculation(values, 'FirstNumber');
69
153
  }
70
154
  function lastNumber(values) {
71
- const tuple = (0, _findLast.default)(values, (tuple)=>isNaN(getValue(tuple)) === false);
72
- return tuple === undefined ? undefined : getValue(tuple);
155
+ return getCalculation(values, 'LastNumber');
73
156
  }
74
157
  function mean(values) {
75
- if (values.length <= 0) return undefined;
76
- return (0, _meanBy.default)(values, getValue);
158
+ return getCalculation(values, 'Mean');
77
159
  }
78
160
  function sum(values) {
79
- if (values.length <= 0) return undefined;
80
- return (0, _sumBy.default)(values, getValue);
161
+ return getCalculation(values, 'Sum');
81
162
  }
82
- function getValue(valueTuple) {
83
- const value = valueTuple[1];
84
- if (value !== null) {
85
- return value;
86
- }
87
- // TODO: refactor utils so null can be returned and LastNotNull supported
88
- return NaN;
163
+ function min(values) {
164
+ return getCalculation(values, 'Min');
165
+ }
166
+ function max(values) {
167
+ return getCalculation(values, 'Max');
89
168
  }
@@ -2,20 +2,44 @@ import { TimeSeriesValueTuple } from '@perses-dev/core';
2
2
  export declare const CalculationsMap: {
3
3
  First: typeof first;
4
4
  Last: typeof last;
5
+ FirstNumber: typeof firstNumber;
5
6
  LastNumber: typeof lastNumber;
6
7
  Mean: typeof mean;
7
8
  Sum: typeof sum;
9
+ Min: typeof min;
10
+ Max: typeof max;
8
11
  };
9
12
  export declare type CalculationType = keyof typeof CalculationsMap;
10
13
  export declare type CalculationConfig = {
11
14
  label: string;
15
+ description: string;
12
16
  };
13
17
  export declare const CALCULATIONS_CONFIG: Readonly<Record<CalculationType, CalculationConfig>>;
14
18
  export declare const DEFAULT_CALCULATION: CalculationType;
15
- declare function first(values: TimeSeriesValueTuple[]): number | undefined;
16
- declare function last(values: TimeSeriesValueTuple[]): number | undefined;
17
- declare function lastNumber(values: TimeSeriesValueTuple[]): number | undefined;
18
- declare function mean(values: TimeSeriesValueTuple[]): number | undefined;
19
- declare function sum(values: TimeSeriesValueTuple[]): number | undefined;
19
+ declare type CalculationValue = number | null | undefined;
20
+ /**
21
+ * Calculate a multiple values for a set of time series data.
22
+ *
23
+ * @param values - Array of time series data.
24
+ * @param includeCalculations - Array of calculations to include.
25
+ */
26
+ export declare function getCalculations<IncludeCalcs extends CalculationType[]>(values: TimeSeriesValueTuple[], includeCalculations: IncludeCalcs): Record<Extract<CalculationType, IncludeCalcs[number]>, CalculationValue>;
27
+ /**
28
+ * Calculate a single value for a set of time series data.
29
+ *
30
+ * Use `getCalculations` instead if you need multiple calculations.
31
+ *
32
+ * @param values - Array of time series data.
33
+ * @param calculation - Name of the calculation to calculate.
34
+ */
35
+ export declare function getCalculation(values: TimeSeriesValueTuple[], calculation: CalculationType): CalculationValue;
36
+ declare function first(values: TimeSeriesValueTuple[]): CalculationValue;
37
+ declare function last(values: TimeSeriesValueTuple[]): CalculationValue;
38
+ declare function firstNumber(values: TimeSeriesValueTuple[]): CalculationValue;
39
+ declare function lastNumber(values: TimeSeriesValueTuple[]): CalculationValue;
40
+ declare function mean(values: TimeSeriesValueTuple[]): CalculationValue;
41
+ declare function sum(values: TimeSeriesValueTuple[]): CalculationValue;
42
+ declare function min(values: TimeSeriesValueTuple[]): CalculationValue;
43
+ declare function max(values: TimeSeriesValueTuple[]): CalculationValue;
20
44
  export {};
21
45
  //# sourceMappingURL=calculations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"calculations.d.ts","sourceRoot":"","sources":["../../src/model/calculations.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAGxD,eAAO,MAAM,eAAe;;;;;;CAM3B,CAAC;AAEF,oBAAY,eAAe,GAAG,MAAM,OAAO,eAAe,CAAC;AAE3D,oBAAY,iBAAiB,GAAG;IAC9B,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AACF,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,iBAAiB,CAAC,CAgB3E,CAAC;AAEX,eAAO,MAAM,mBAAmB,EAAE,eAAuB,CAAC;AAE1D,iBAAS,KAAK,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,GAAG,SAAS,CAGjE;AAED,iBAAS,IAAI,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,GAAG,SAAS,CAKhE;AAED,iBAAS,UAAU,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,GAAG,SAAS,CAGtE;AAED,iBAAS,IAAI,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,GAAG,SAAS,CAGhE;AAED,iBAAS,GAAG,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,GAAG,SAAS,CAG/D"}
1
+ {"version":3,"file":"calculations.d.ts","sourceRoot":"","sources":["../../src/model/calculations.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAExD,eAAO,MAAM,eAAe;;;;;;;;;CAS3B,CAAC;AAEF,oBAAY,eAAe,GAAG,MAAM,OAAO,eAAe,CAAC;AAE3D,oBAAY,iBAAiB,GAAG;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,iBAAiB,CAAC,CAiC3E,CAAC;AAEX,eAAO,MAAM,mBAAmB,EAAE,eAAuB,CAAC;AAE1D,aAAK,gBAAgB,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;AAElD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,YAAY,SAAS,eAAe,EAAE,EACpE,MAAM,EAAE,oBAAoB,EAAE,EAC9B,mBAAmB,EAAE,YAAY,GAChC,MAAM,CAGP,OAAO,CAAC,eAAe,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC,EAC9C,gBAAgB,CACjB,CA2EA;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,oBAAoB,EAAE,EAAE,WAAW,EAAE,eAAe,oBAE1F;AAED,iBAAS,KAAK,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,gBAAgB,CAE/D;AAED,iBAAS,IAAI,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,gBAAgB,CAE9D;AAED,iBAAS,WAAW,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,gBAAgB,CAErE;AAED,iBAAS,UAAU,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,gBAAgB,CAEpE;AAED,iBAAS,IAAI,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,gBAAgB,CAE9D;AAED,iBAAS,GAAG,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,gBAAgB,CAE7D;AAED,iBAAS,GAAG,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,gBAAgB,CAE7D;AAED,iBAAS,GAAG,CAAC,MAAM,EAAE,oBAAoB,EAAE,GAAG,gBAAgB,CAE7D"}
@@ -10,63 +10,156 @@
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
- import findLast from 'lodash/findLast';
14
- import meanBy from 'lodash/meanBy';
15
- import sumBy from 'lodash/sumBy';
16
- // TODO: move this file and calculations.test.ts to @perses-dev/core
17
13
  export const CalculationsMap = {
18
14
  First: first,
19
15
  Last: last,
16
+ FirstNumber: firstNumber,
20
17
  LastNumber: lastNumber,
21
18
  Mean: mean,
22
- Sum: sum
19
+ Sum: sum,
20
+ Min: min,
21
+ Max: max
23
22
  };
24
23
  export const CALCULATIONS_CONFIG = {
25
24
  First: {
26
- label: 'First'
25
+ label: 'First',
26
+ description: 'First value'
27
27
  },
28
28
  Last: {
29
- label: 'Last'
29
+ label: 'Last',
30
+ description: 'Last value'
31
+ },
32
+ FirstNumber: {
33
+ label: 'First *',
34
+ description: 'First numeric value'
30
35
  },
31
36
  LastNumber: {
32
- label: 'Last number'
37
+ label: 'Last *',
38
+ description: 'Last numeric value'
33
39
  },
34
40
  Mean: {
35
- label: 'Mean'
41
+ label: 'Average',
42
+ description: 'Average value excluding nulls'
36
43
  },
37
44
  Sum: {
38
- label: 'Sum'
45
+ label: 'Sum',
46
+ description: 'The sum of all values'
47
+ },
48
+ Min: {
49
+ label: 'Min',
50
+ description: 'Minimum value'
51
+ },
52
+ Max: {
53
+ label: 'Max',
54
+ description: 'Maximum value'
39
55
  }
40
56
  };
41
57
  export const DEFAULT_CALCULATION = 'Sum';
58
+ /**
59
+ * Calculate a multiple values for a set of time series data.
60
+ *
61
+ * @param values - Array of time series data.
62
+ * @param includeCalculations - Array of calculations to include.
63
+ */ export function getCalculations(values, includeCalculations) {
64
+ const calculations = includeCalculations.reduce((initResult, calculation)=>{
65
+ initResult[calculation] = undefined;
66
+ return initResult;
67
+ }, {});
68
+ // We save these values as separate values instead of directly setting them
69
+ // in the calculations because they are needed by multiple calculations.
70
+ let nonNullCount = 0;
71
+ let sum = 0;
72
+ // We use this large function capable of performing one or more calculations
73
+ // in a single iteration of the data to minimize the performance impact of
74
+ // generating multiple calculations for large timeseries values. This is
75
+ // less optimized for certain single calculations when done in isolation (e.g.
76
+ // `Last`), but will be more performant in the more expensive cases where
77
+ // multiple values are being used (e.g. table legend).
78
+ values.forEach((tuple, i)=>{
79
+ const value = tuple[1];
80
+ if (i === 0 && 'First' in calculations) {
81
+ calculations.First = value;
82
+ }
83
+ if (i === values.length - 1 && 'Last' in calculations) {
84
+ calculations.Last = value;
85
+ }
86
+ // Handling specific to non-null values.
87
+ if (typeof value === 'number') {
88
+ nonNullCount += 1;
89
+ sum += value;
90
+ if ('FirstNumber' in calculations && calculations.FirstNumber === undefined) {
91
+ // Save the first number we see.
92
+ calculations.FirstNumber = value;
93
+ }
94
+ if ('LastNumber' in calculations) {
95
+ // Keep setting the numbers we see, which will eventually be set to the
96
+ // last number when finished iterating.
97
+ calculations.LastNumber = value;
98
+ }
99
+ if ('Min' in calculations) {
100
+ if (typeof calculations.Min !== 'number') {
101
+ // Init the first time we see a number
102
+ calculations.Min = value;
103
+ } else {
104
+ // Use lowest value once initialized
105
+ calculations.Min = Math.min(calculations.Min, value);
106
+ }
107
+ }
108
+ if ('Max' in calculations) {
109
+ if (typeof calculations.Max !== 'number') {
110
+ // Init the first time we see a number
111
+ calculations.Max = value;
112
+ } else {
113
+ // Use highest value once initialized
114
+ calculations.Max = Math.max(calculations.Max, value);
115
+ }
116
+ }
117
+ }
118
+ });
119
+ // Set calculations that require iterating over all values.
120
+ if (nonNullCount > 0 && 'Sum' in calculations) {
121
+ calculations.Sum = sum;
122
+ }
123
+ if (nonNullCount > 0 && 'Mean' in calculations) {
124
+ calculations.Mean = sum / nonNullCount;
125
+ }
126
+ return calculations;
127
+ }
128
+ /**
129
+ * Calculate a single value for a set of time series data.
130
+ *
131
+ * Use `getCalculations` instead if you need multiple calculations.
132
+ *
133
+ * @param values - Array of time series data.
134
+ * @param calculation - Name of the calculation to calculate.
135
+ */ export function getCalculation(values, calculation) {
136
+ return getCalculations(values, [
137
+ calculation
138
+ ])[calculation];
139
+ }
42
140
  function first(values) {
43
- const tuple = values[0];
44
- return tuple === undefined ? undefined : getValue(tuple);
141
+ return getCalculation(values, 'First');
45
142
  }
46
143
  function last(values) {
47
- if (values.length <= 0) return undefined;
48
- const tuple = values[values.length - 1];
49
- return tuple === undefined ? undefined : getValue(tuple);
144
+ return getCalculation(values, 'Last');
145
+ }
146
+ function firstNumber(values) {
147
+ return getCalculation(values, 'FirstNumber');
50
148
  }
51
149
  function lastNumber(values) {
52
- const tuple = findLast(values, (tuple)=>isNaN(getValue(tuple)) === false);
53
- return tuple === undefined ? undefined : getValue(tuple);
150
+ return getCalculation(values, 'LastNumber');
54
151
  }
55
152
  function mean(values) {
56
- if (values.length <= 0) return undefined;
57
- return meanBy(values, getValue);
153
+ return getCalculation(values, 'Mean');
58
154
  }
59
155
  function sum(values) {
60
- if (values.length <= 0) return undefined;
61
- return sumBy(values, getValue);
156
+ return getCalculation(values, 'Sum');
62
157
  }
63
- function getValue(valueTuple) {
64
- const value = valueTuple[1];
65
- if (value !== null) {
66
- return value;
67
- }
68
- // TODO: refactor utils so null can be returned and LastNotNull supported
69
- return NaN;
158
+ function min(values) {
159
+ return getCalculation(values, 'Min');
160
+ }
161
+ function max(values) {
162
+ return getCalculation(values, 'Max');
70
163
  }
71
164
 
72
165
  //# sourceMappingURL=calculations.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/model/calculations.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 findLast from 'lodash/findLast';\nimport meanBy from 'lodash/meanBy';\nimport sumBy from 'lodash/sumBy';\nimport { TimeSeriesValueTuple } from '@perses-dev/core';\n\n// TODO: move this file and calculations.test.ts to @perses-dev/core\nexport const CalculationsMap = {\n First: first,\n Last: last,\n LastNumber: lastNumber,\n Mean: mean,\n Sum: sum,\n};\n\nexport type CalculationType = keyof typeof CalculationsMap;\n\nexport type CalculationConfig = {\n label: string;\n};\nexport const CALCULATIONS_CONFIG: Readonly<Record<CalculationType, CalculationConfig>> = {\n First: {\n label: 'First',\n },\n Last: {\n label: 'Last',\n },\n LastNumber: {\n label: 'Last number',\n },\n Mean: {\n label: 'Mean',\n },\n Sum: {\n label: 'Sum',\n },\n} as const;\n\nexport const DEFAULT_CALCULATION: CalculationType = 'Sum';\n\nfunction first(values: TimeSeriesValueTuple[]): number | undefined {\n const tuple = values[0];\n return tuple === undefined ? undefined : getValue(tuple);\n}\n\nfunction last(values: TimeSeriesValueTuple[]): number | undefined {\n if (values.length <= 0) return undefined;\n\n const tuple = values[values.length - 1];\n return tuple === undefined ? undefined : getValue(tuple);\n}\n\nfunction lastNumber(values: TimeSeriesValueTuple[]): number | undefined {\n const tuple = findLast(values, (tuple) => isNaN(getValue(tuple)) === false);\n return tuple === undefined ? undefined : getValue(tuple);\n}\n\nfunction mean(values: TimeSeriesValueTuple[]): number | undefined {\n if (values.length <= 0) return undefined;\n return meanBy(values, getValue);\n}\n\nfunction sum(values: TimeSeriesValueTuple[]): number | undefined {\n if (values.length <= 0) return undefined;\n return sumBy(values, getValue);\n}\n\nfunction getValue(valueTuple: TimeSeriesValueTuple) {\n const value = valueTuple[1];\n if (value !== null) {\n return value;\n }\n // TODO: refactor utils so null can be returned and LastNotNull supported\n return NaN;\n}\n"],"names":["findLast","meanBy","sumBy","CalculationsMap","First","first","Last","last","LastNumber","lastNumber","Mean","mean","Sum","sum","CALCULATIONS_CONFIG","label","DEFAULT_CALCULATION","values","tuple","undefined","getValue","length","isNaN","valueTuple","value","NaN"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oE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lB,QAAQ,CAACiB,MAAM,EAAE,CAACC,KAAK,GAAKI,KAAK,CAACF,QAAQ,CAACF,KAAK,CAAC,CAAC,KAAK,KAAK,CAAC,AAAC;IAC5E,OAAOA,KAAK,KAAKC,SAAS,GAAGA,SAAS,GAAGC,QAAQ,CAACF,KAAK,CAAC,CAAC;AAC3D,CAAC;AAED,SAASP,IAAI,CAACM,MAA8B,EAAsB;IAChE,IAAIA,MAAM,CAACI,MAAM,IAAI,CAAC,EAAE,OAAOF,SAAS,CAAC;IACzC,OAAOlB,MAAM,CAACgB,MAAM,EAAEG,QAAQ,CAAC,CAAC;AAClC,CAAC;AAED,SAASP,GAAG,CAACI,MAA8B,EAAsB;IAC/D,IAAIA,MAAM,CAACI,MAAM,IAAI,CAAC,EAAE,OAAOF,SAAS,CAAC;IACzC,OAAOjB,KAAK,CAACe,MAAM,EAAEG,QAAQ,CAAC,CAAC;AACjC,CAAC;AAED,SAASA,QAAQ,CAACG,UAAgC,EAAE;IAClD,MAAMC,KAAK,GAAGD,UAAU,CAAC,CAAC,CAAC,AAAC;IAC5B,IAAIC,KAAK,KAAK,IAAI,EAAE;QAClB,OAAOA,KAAK,CAAC;IACf,CAAC;IACD,yEAAyE;IACzE,OAAOC,GAAG,CAAC;AACb,CAAC"}
1
+ {"version":3,"sources":["../../src/model/calculations.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 { TimeSeriesValueTuple } from '@perses-dev/core';\n\nexport const CalculationsMap = {\n First: first,\n Last: last,\n FirstNumber: firstNumber,\n LastNumber: lastNumber,\n Mean: mean,\n Sum: sum,\n Min: min,\n Max: max,\n};\n\nexport type CalculationType = keyof typeof CalculationsMap;\n\nexport type CalculationConfig = {\n label: string;\n description: string;\n};\n\nexport const CALCULATIONS_CONFIG: Readonly<Record<CalculationType, CalculationConfig>> = {\n First: {\n label: 'First',\n description: 'First value',\n },\n Last: {\n label: 'Last',\n description: 'Last value',\n },\n FirstNumber: {\n label: 'First *',\n description: 'First numeric value',\n },\n LastNumber: {\n label: 'Last *',\n description: 'Last numeric value',\n },\n Mean: {\n label: 'Average',\n description: 'Average value excluding nulls',\n },\n Sum: {\n label: 'Sum',\n description: 'The sum of all values',\n },\n Min: {\n label: 'Min',\n description: 'Minimum value',\n },\n Max: {\n label: 'Max',\n description: 'Maximum value',\n },\n} as const;\n\nexport const DEFAULT_CALCULATION: CalculationType = 'Sum';\n\ntype CalculationValue = number | null | undefined;\n\n/**\n * Calculate a multiple values for a set of time series data.\n *\n * @param values - Array of time series data.\n * @param includeCalculations - Array of calculations to include.\n */\nexport function getCalculations<IncludeCalcs extends CalculationType[]>(\n values: TimeSeriesValueTuple[],\n includeCalculations: IncludeCalcs\n): Record<\n // This extract combined with the generics above keeps the key of the returned\n // record to *just* the specified calculations.\n Extract<CalculationType, IncludeCalcs[number]>,\n CalculationValue\n> {\n const calculations = includeCalculations.reduce((initResult, calculation) => {\n initResult[calculation] = undefined;\n return initResult;\n }, {} as Record<string, CalculationValue>);\n\n // We save these values as separate values instead of directly setting them\n // in the calculations because they are needed by multiple calculations.\n let nonNullCount = 0;\n let sum = 0;\n\n // We use this large function capable of performing one or more calculations\n // in a single iteration of the data to minimize the performance impact of\n // generating multiple calculations for large timeseries values. This is\n // less optimized for certain single calculations when done in isolation (e.g.\n // `Last`), but will be more performant in the more expensive cases where\n // multiple values are being used (e.g. table legend).\n values.forEach((tuple, i) => {\n const value = tuple[1];\n\n if (i === 0 && 'First' in calculations) {\n calculations.First = value;\n }\n if (i === values.length - 1 && 'Last' in calculations) {\n calculations.Last = value;\n }\n\n // Handling specific to non-null values.\n if (typeof value === 'number') {\n nonNullCount += 1;\n sum += value;\n\n if ('FirstNumber' in calculations && calculations.FirstNumber === undefined) {\n // Save the first number we see.\n calculations.FirstNumber = value;\n }\n\n if ('LastNumber' in calculations) {\n // Keep setting the numbers we see, which will eventually be set to the\n // last number when finished iterating.\n calculations.LastNumber = value;\n }\n\n if ('Min' in calculations) {\n if (typeof calculations.Min !== 'number') {\n // Init the first time we see a number\n calculations.Min = value;\n } else {\n // Use lowest value once initialized\n calculations.Min = Math.min(calculations.Min, value);\n }\n }\n\n if ('Max' in calculations) {\n if (typeof calculations.Max !== 'number') {\n // Init the first time we see a number\n calculations.Max = value;\n } else {\n // Use highest value once initialized\n calculations.Max = Math.max(calculations.Max, value);\n }\n }\n }\n });\n\n // Set calculations that require iterating over all values.\n if (nonNullCount > 0 && 'Sum' in calculations) {\n calculations.Sum = sum;\n }\n\n if (nonNullCount > 0 && 'Mean' in calculations) {\n calculations.Mean = sum / nonNullCount;\n }\n\n return calculations;\n}\n\n/**\n * Calculate a single value for a set of time series data.\n *\n * Use `getCalculations` instead if you need multiple calculations.\n *\n * @param values - Array of time series data.\n * @param calculation - Name of the calculation to calculate.\n */\nexport function getCalculation(values: TimeSeriesValueTuple[], calculation: CalculationType) {\n return getCalculations(values, [calculation])[calculation];\n}\n\nfunction first(values: TimeSeriesValueTuple[]): CalculationValue {\n return getCalculation(values, 'First');\n}\n\nfunction last(values: TimeSeriesValueTuple[]): CalculationValue {\n return getCalculation(values, 'Last');\n}\n\nfunction firstNumber(values: TimeSeriesValueTuple[]): CalculationValue {\n return getCalculation(values, 'FirstNumber');\n}\n\nfunction lastNumber(values: TimeSeriesValueTuple[]): CalculationValue {\n return getCalculation(values, 'LastNumber');\n}\n\nfunction mean(values: TimeSeriesValueTuple[]): CalculationValue {\n return getCalculation(values, 'Mean');\n}\n\nfunction sum(values: TimeSeriesValueTuple[]): CalculationValue {\n return getCalculation(values, 'Sum');\n}\n\nfunction min(values: TimeSeriesValueTuple[]): CalculationValue {\n return getCalculation(values, 'Min');\n}\n\nfunction max(values: TimeSeriesValueTuple[]): CalculationValue {\n return getCalculation(values, 'Max');\n}\n"],"names":["CalculationsMap","First","first","Last","last","FirstNumber","firstNumber","LastNumber","lastNumber","Mean","mean","Sum","sum","Min","min","Max","max","CALCULATIONS_CONFIG","label","description","DEFAULT_CALCULATION","getCalculations","values","includeCalculations","calculations","reduce","initResult","calculation","undefined","nonNullCount","forEach","tuple","i","value","length","Math","getCalculation"],"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;AAIjC,OAAO,MAAMA,eAAe,GAAG;IAC7BC,KAAK,EAAEC,KAAK;IACZC,IAAI,EAAEC,IAAI;IACVC,WAAW,EAAEC,WAAW;IACxBC,UAAU,EAAEC,UAAU;IACtBC,IAAI,EAAEC,IAAI;IACVC,GAAG,EAAEC,GAAG;IACRC,GAAG,EAAEC,GAAG;IACRC,GAAG,EAAEC,GAAG;CACT,CAAC;AASF,OAAO,MAAMC,mBAAmB,GAAyD;IACvFhB,KAAK,EAAE;QACLiB,KAAK,EAAE,OAAO;QACdC,WAAW,EAAE,aAAa;KAC3B;IACDhB,IAAI,EAAE;QACJe,KAAK,EAAE,MAAM;QACbC,WAAW,EAAE,YAAY;KAC1B;IACDd,WAAW,EAAE;QACXa,KAAK,EAAE,SAAS;QAChBC,WAAW,EAAE,qBAAqB;KACnC;IACDZ,UAAU,EAAE;QACVW,KAAK,EAAE,QAAQ;QACfC,WAAW,EAAE,oBAAoB;KAClC;IACDV,IAAI,EAAE;QACJS,KAAK,EAAE,SAAS;QAChBC,WAAW,EAAE,+BAA+B;KAC7C;IACDR,GAAG,EAAE;QACHO,KAAK,EAAE,KAAK;QACZC,WAAW,EAAE,uBAAuB;KACrC;IACDN,GA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CAOC,GACD,OAAO,SAASY,cAAc,CAACd,MAA8B,EAAEK,WAA4B,EAAE;IAC3F,OAAON,eAAe,CAACC,MAAM,EAAE;QAACK,WAAW;KAAC,CAAC,CAACA,WAAW,CAAC,CAAC;AAC7D,CAAC;AAED,SAASzB,KAAK,CAACoB,MAA8B,EAAoB;IAC/D,OAAOc,cAAc,CAACd,MAAM,EAAE,OAAO,CAAC,CAAC;AACzC,CAAC;AAED,SAASlB,IAAI,CAACkB,MAA8B,EAAoB;IAC9D,OAAOc,cAAc,CAACd,MAAM,EAAE,MAAM,CAAC,CAAC;AACxC,CAAC;AAED,SAAShB,WAAW,CAACgB,MAA8B,EAAoB;IACrE,OAAOc,cAAc,CAACd,MAAM,EAAE,aAAa,CAAC,CAAC;AAC/C,CAAC;AAED,SAASd,UAAU,CAACc,MAA8B,EAAoB;IACpE,OAAOc,cAAc,CAACd,MAAM,EAAE,YAAY,CAAC,CAAC;AAC9C,CAAC;AAED,SAASZ,IAAI,CAACY,MAA8B,EAAoB;IAC9D,OAAOc,cAAc,CAACd,MAAM,EAAE,MAAM,CAAC,CAAC;AACxC,CAAC;AAED,SAASV,GAAG,CAACU,MAA8B,EAAoB;IAC7D,OAAOc,cAAc,CAACd,MAAM,EAAE,KAAK,CAAC,CAAC;AACvC,CAAC;AAED,SAASR,GAAG,CAACQ,MAA8B,EAAoB;IAC7D,OAAOc,cAAc,CAACd,MAAM,EAAE,KAAK,CAAC,CAAC;AACvC,CAAC;AAED,SAASN,GAAG,CAACM,MAA8B,EAAoB;IAC7D,OAAOc,cAAc,CAACd,MAAM,EAAE,KAAK,CAAC,CAAC;AACvC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perses-dev/core",
3
- "version": "0.34.0",
3
+ "version": "0.35.0",
4
4
  "description": "Core functionality consumed by both the Perses UI and plugins",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/perses/perses/blob/main/README.md",