Alerting: Use optional chaining for accessing frames (#61814)

This commit is contained in:
Gilles De Mey 2023-01-28 18:39:23 +01:00 committed by GitHub
parent 82e8ad8a0f
commit 13159d03ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -1,11 +1,20 @@
import { DataFrame, Labels, roundDecimals } from '@grafana/data'; import { DataFrame, Labels, roundDecimals } from '@grafana/data';
/**
* `frame.fields` could be an empty array
*
* TypeScript will NOT complain about it when accessing items via index signatures.
* Make sure to check for empty array or use optional chaining!
*
* see https://github.com/Microsoft/TypeScript/issues/13778
*/
const getSeriesName = (frame: DataFrame): string => { const getSeriesName = (frame: DataFrame): string => {
return frame.name ?? formatLabels(frame.fields[0].labels ?? {}); return frame.name ?? formatLabels(frame.fields[0]?.labels ?? {});
}; };
const getSeriesValue = (frame: DataFrame) => { const getSeriesValue = (frame: DataFrame) => {
const value = frame.fields[0].values.get(0); const value = frame.fields[0]?.values.get(0);
if (Number.isFinite(value)) { if (Number.isFinite(value)) {
return roundDecimals(value, 5); return roundDecimals(value, 5);