diff --git a/src/utils/getMetricValueByName.ts b/src/utils/getMetricValueByName.ts index 693e459..a869f2a 100644 --- a/src/utils/getMetricValueByName.ts +++ b/src/utils/getMetricValueByName.ts @@ -17,6 +17,22 @@ function getFieldByName(fieldName: string): Field | undefined { } } +/** + * Finds the value field + * + * @example + * ```ts + * getValueField(series.fields); + * ``` + * + * @param fields - Fields object + * + * @returns Value field + */ +function getValueField(fields: Field[]): Field | undefined { + return fields.find((field) => field.name == "Value"); +} + export interface MetricOptions { /** * Return value when no data is found. @@ -43,7 +59,9 @@ export function getMetricValueByName( { noDataValue = null }: MetricOptions = {} ): unknown { const series = getSeriesByName(metricName); - const valueField = series ? series.fields[1] : getFieldByName(metricName); + const valueField = series + ? getValueField(series.fields) + : getFieldByName(metricName); return valueField?.state?.calcs?.last ?? noDataValue; }