The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/dimensions/scalar.ts

61 lines
1.5 KiB

import { DataFrame, Field } from '@grafana/data';
import { ScalarDimensionConfig, ScalarDimensionMode } from '@grafana/schema';
import { DimensionSupplier } from './types';
import { findField, getLastNotNullFieldValue } from './utils';
//---------------------------------------------------------
// Scalar dimension
//---------------------------------------------------------
export function getScalarDimension(
frame: DataFrame | undefined,
config: ScalarDimensionConfig
): DimensionSupplier<number> {
return getScalarDimensionForField(findField(frame, config?.field), config);
}
export function getScalarDimensionForField(
field: Field | undefined,
cfg: ScalarDimensionConfig
): DimensionSupplier<number> {
if (!field) {
const v = cfg.fixed ?? 0;
return {
isAssumed: Boolean(cfg.field?.length) || !cfg.fixed,
fixed: v,
value: () => v,
get: () => v,
};
}
//mod mode as default
let validated = (value: number) => {
return value % cfg.max;
};
//capped mode
if (cfg.mode === ScalarDimensionMode.Clamped) {
validated = (value: number) => {
if (value < cfg.min) {
return cfg.min;
}
if (value > cfg.max) {
return cfg.max;
}
return value;
};
}
const get = (i: number) => {
const v = field.values[i];
if (v === null || typeof v !== 'number') {
return 0;
}
return validated(v);
};
return {
field,
get,
value: () => getLastNotNullFieldValue(field),
};
}