feat: add Avg calculations (#66)

This commit is contained in:
Fine0830 2022-04-14 18:15:28 +08:00 committed by GitHub
parent 69a9c6de13
commit 26db1ec23e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 15 deletions

View File

@ -34,6 +34,8 @@ export enum Calculations {
ConvertMilliseconds = "convertMilliseconds", ConvertMilliseconds = "convertMilliseconds",
MsTos = "msTos", MsTos = "msTos",
Average = "average", Average = "average",
PercentageAvg = "percentageAvg",
ApdexAvg = "apdexAvg",
} }
export enum sizeEnum { export enum sizeEnum {
XS = "XS", XS = "XS",

View File

@ -17,16 +17,21 @@
import { MetricQueryTypes, Calculations } from "./data"; import { MetricQueryTypes, Calculations } from "./data";
export function useListConfig(config: any, index: string) { export function useListConfig(config: any, index: string) {
const i = Number(index); const i = Number(index);
const types = [
Calculations.Average,
Calculations.ApdexAvg,
Calculations.PercentageAvg,
];
const calculation = const calculation =
config.metricConfig && config.metricConfig &&
config.metricConfig[i] && config.metricConfig[i] &&
config.metricConfig[i].calculation; config.metricConfig[i].calculation;
const line = const line =
config.metricTypes[i] === MetricQueryTypes.ReadMetricsValues && config.metricTypes[i] === MetricQueryTypes.ReadMetricsValues &&
calculation !== Calculations.Average; !types.includes(calculation);
const isAvg = const isAvg =
config.metricTypes[i] === MetricQueryTypes.ReadMetricsValues && config.metricTypes[i] === MetricQueryTypes.ReadMetricsValues &&
calculation === Calculations.Average; types.includes(calculation);
return { return {
isLinear: line, isLinear: line,
isAvg, isAvg,

View File

@ -278,14 +278,20 @@ export function usePodsSource(
} }
const data = pods.map((d: Instance | any, idx: number) => { const data = pods.map((d: Instance | any, idx: number) => {
config.metrics.map((name: string, index: number) => { config.metrics.map((name: string, index: number) => {
const c = (config.metricConfig && config.metricConfig[index]) || {}; const c: any = (config.metricConfig && config.metricConfig[index]) || {};
const key = name + idx + index; const key = name + idx + index;
if (config.metricTypes[index] === MetricQueryTypes.ReadMetricsValue) { if (config.metricTypes[index] === MetricQueryTypes.ReadMetricsValue) {
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] = {}; d[name] = {};
if (c.calculation === Calculations.Average) { if (
[
Calculations.Average,
Calculations.ApdexAvg,
Calculations.PercentageAvg,
].includes(c.calculation)
) {
d[name]["avg"] = calculateExp(resp.data[key].values.values, c); d[name]["avg"] = calculateExp(resp.data[key].values.values, c);
} }
d[name]["values"] = resp.data[key].values.values.map( d[name]["values"] = resp.data[key].values.values.map(
@ -324,18 +330,22 @@ export function useQueryTopologyMetrics(metrics: string[], ids: string[]) {
return { queryStr, conditions }; return { queryStr, conditions };
} }
function calculateExp( function calculateExp(
arr: any[], arr: { value: number }[],
config: { calculation: string } config: { calculation: string }
): (number | string)[] { ): (number | string)[] {
const sum = arr
.map((d: { value: number }) => d.value)
.reduce((a, b) => a + b);
let data: (number | string)[] = []; let data: (number | string)[] = [];
switch (config.calculation) { switch (config.calculation) {
case Calculations.Average: case Calculations.Average:
data = [ data = [(sum / arr.length).toFixed(2)];
( break;
arr.map((d: { value: number }) => d.value).reduce((a, b) => a + b) / case Calculations.PercentageAvg:
arr.length data = [(sum / arr.length / 100).toFixed(2)];
).toFixed(2), break;
]; case Calculations.ApdexAvg:
data = [(sum / arr.length / 10000).toFixed(2)];
break; break;
default: default:
data = arr.map((d) => aggregation(d.value, config)); data = arr.map((d) => aggregation(d.value, config));
@ -354,6 +364,9 @@ export function aggregation(
case Calculations.Percentage: case Calculations.Percentage:
data = (val / 100).toFixed(2); data = (val / 100).toFixed(2);
break; break;
case Calculations.PercentageAvg:
data = (val / 100).toFixed(2);
break;
case Calculations.ByteToKB: case Calculations.ByteToKB:
data = (val / 1024).toFixed(2); data = (val / 1024).toFixed(2);
break; break;
@ -364,7 +377,10 @@ export function aggregation(
data = (val / 1024 / 1024 / 1024).toFixed(2); data = (val / 1024 / 1024 / 1024).toFixed(2);
break; break;
case Calculations.Apdex: case Calculations.Apdex:
data = (val / 10000).toFixed(2); data = val / 10000;
break;
case Calculations.ApdexAvg:
data = val / 10000;
break; break;
case Calculations.ConvertSeconds: case Calculations.ConvertSeconds:
data = dayjs(val).format("YYYY-MM-DD HH:mm:ss"); data = dayjs(val).format("YYYY-MM-DD HH:mm:ss");

View File

@ -263,10 +263,12 @@ export const TextColors: { [key: string]: string } = {
export const CalculationOpts = [ export const CalculationOpts = [
{ label: "Percentage", value: "percentage" }, { label: "Percentage", value: "percentage" },
{ label: "Apdex", value: "apdex" }, { label: "Apdex", value: "apdex" },
{ label: "Avg-preview", value: "average" },
{ label: "Percentage + Avg-preview", value: "percentageAvg" },
{ label: "Apdex + Avg-preview", value: "apdexAvg" },
{ 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: "Milliseconds to YYYY-MM-DD HH:mm:ss", label: "Milliseconds to YYYY-MM-DD HH:mm:ss",
value: "convertMilliseconds", value: "convertMilliseconds",

View File

@ -23,7 +23,7 @@ limitations under the License. -->
}" }"
> >
{{ singleVal.toFixed(2) }} {{ singleVal.toFixed(2) }}
<span class="unit" v-show="config.showUnit"> <span class="unit" v-show="config.showUnit && unit">
{{ decodeURIComponent(unit) }} {{ decodeURIComponent(unit) }}
</span> </span>
</div> </div>
@ -55,7 +55,9 @@ const metricConfig = computed(() => props.config.metricConfig || []);
const key = computed(() => Object.keys(props.data)[0]); const key = computed(() => Object.keys(props.data)[0]);
const singleVal = computed(() => Number(props.data[key.value])); const singleVal = computed(() => Number(props.data[key.value]));
const unit = computed( const unit = computed(
() => metricConfig.value[0] && encodeURIComponent(metricConfig.value[0].unit) () =>
metricConfig.value[0] &&
encodeURIComponent(metricConfig.value[0].unit || "")
); );
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>