Skip to content

Commit

Permalink
Merge pull request #7 from gapitio/refactor/explicit-value-field
Browse files Browse the repository at this point in the history
refactor: explicit retrieve value field
  • Loading branch information
ZuperZee authored Feb 24, 2021
2 parents 13156f0 + 2bfec08 commit 77d90df
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/utils/getMetricValueByName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

0 comments on commit 77d90df

Please sign in to comment.