add calculation exp

This commit is contained in:
Qiuxia Fan 2022-04-13 11:44:16 +08:00
parent 2dd9df19d7
commit 5bca58b00d
4 changed files with 26 additions and 11 deletions

View File

@ -33,6 +33,7 @@ export enum Calculations {
ConvertSeconds = "convertSeconds", ConvertSeconds = "convertSeconds",
ConvertMilliseconds = "convertMilliseconds", ConvertMilliseconds = "convertMilliseconds",
MsTos = "msTos", MsTos = "msTos",
Average = "average",
} }
export enum sizeEnum { export enum sizeEnum {
XS = "XS", XS = "XS",

View File

@ -150,9 +150,7 @@ export function useSourceProcessor(
const c = (config.metricConfig && config.metricConfig[index]) || {}; const c = (config.metricConfig && config.metricConfig[index]) || {};
if (type === MetricQueryTypes.ReadMetricsValues) { if (type === MetricQueryTypes.ReadMetricsValues) {
source[m] = resp.data[keys[index]].values.values.map( source[m] = calculateExp(resp.data[keys[index]].values.values, c);
(d: { value: number }) => aggregation(d.value, c)
);
} }
if (type === MetricQueryTypes.ReadLabeledMetricsValues) { if (type === MetricQueryTypes.ReadLabeledMetricsValues) {
const resVal = Object.values(resp.data)[0] || []; const resVal = Object.values(resp.data)[0] || [];
@ -166,7 +164,6 @@ export function useSourceProcessor(
const values = item.values.values.map((d: { value: number }) => const values = item.values.values.map((d: { value: number }) =>
aggregation(Number(d.value), c) aggregation(Number(d.value), c)
); );
const indexNum = labelsIdx.findIndex((d: string) => d === item.label); const indexNum = labelsIdx.findIndex((d: string) => d === item.label);
if (labels[indexNum] && indexNum > -1) { if (labels[indexNum] && indexNum > -1) {
source[labels[indexNum]] = values; source[labels[indexNum]] = values;
@ -287,9 +284,7 @@ export function usePodsSource(
d[name] = aggregation(resp.data[key], c); d[name] = aggregation(resp.data[key], c);
} }
if (config.metricTypes[index] === MetricQueryTypes.ReadMetricsValues) { if (config.metricTypes[index] === MetricQueryTypes.ReadMetricsValues) {
d[name] = resp.data[key].values.values.map((d: { value: number }) => d[name] = calculateExp(resp.data[key].values.values, c);
aggregation(d.value, c)
);
} }
}); });
@ -322,8 +317,26 @@ export function useQueryTopologyMetrics(metrics: string[], ids: string[]) {
return { queryStr, conditions }; return { queryStr, conditions };
} }
function calculateExp(
arr: number[],
config: { calculation: string }
): (number | string)[] {
let data: (number | string)[] = arr;
switch (config.calculation) {
case Calculations.Average:
data = [arr.reduce((a, b) => a + b) / arr.length];
break;
default:
data = arr.map((d) => aggregation(d, config));
break;
}
return data;
}
export function aggregation(val: number, config: any): number | string { function aggregation(
val: number,
config: { calculation: string }
): number | string {
let data: number | string = Number(val); let data: number | string = Number(val);
switch (config.calculation) { switch (config.calculation) {

View File

@ -466,7 +466,7 @@ function setMetricConfig(index: number) {
.chart-types { .chart-types {
span { span {
display: inline-block; display: inline-block;
padding: 5px 10px; padding: 2px 10px;
border: 1px solid #ccc; border: 1px solid #ccc;
background-color: #fff; background-color: #fff;
border-right: 0; border-right: 0;

View File

@ -266,11 +266,12 @@ export const CalculationOpts = [
{ label: "Byte to KB", value: "byteToKB" }, { label: "Byte to KB", value: "byteToKB" },
{ label: "Byte to MB", value: "byteToMB" }, { label: "Byte to MB", value: "byteToMB" },
{ label: "Byte to GB", value: "byteToGB" }, { label: "Byte to GB", value: "byteToGB" },
{ label: "Average", value: "average" },
{ {
label: "Convert milliseconds to YYYY-MM-DD HH:mm:ss", label: "Milliseconds to YYYY-MM-DD HH:mm:ss",
value: "convertMilliseconds", value: "convertMilliseconds",
}, },
{ label: "Convert seconds to YYYY-MM-DD HH:mm:ss", value: "convertSeconds" }, { label: "Seconds to YYYY-MM-DD HH:mm:ss", value: "convertSeconds" },
{ label: "Precision is 2", value: "precision" }, { label: "Precision is 2", value: "precision" },
{ label: "Milliseconds to seconds", value: "msTos" }, { label: "Milliseconds to seconds", value: "msTos" },
]; ];