123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { DataFrame, PanelData, Field, getFieldDisplayName, ReducerID } from '@grafana/data';
- import { config } from '@grafana/runtime';
- import {
- getColorDimension,
- getScaledDimension,
- getTextDimension,
- getResourceDimension,
- ColorDimensionConfig,
- DimensionSupplier,
- ResourceDimensionConfig,
- ScaleDimensionConfig,
- TextDimensionConfig,
- ScalarDimensionConfig,
- } from 'app/features/dimensions';
- import { getScalarDimension } from './scalar';
- export function getColorDimensionFromData(
- data: PanelData | undefined,
- cfg: ColorDimensionConfig
- ): DimensionSupplier<string> {
- if (data?.series && cfg.field) {
- for (const frame of data.series) {
- const d = getColorDimension(frame, cfg, config.theme2);
- if (!d.isAssumed || data.series.length === 1) {
- return d;
- }
- }
- }
- return getColorDimension(undefined, cfg, config.theme2);
- }
- export function getScaleDimensionFromData(
- data: PanelData | undefined,
- cfg: ScaleDimensionConfig
- ): DimensionSupplier<number> {
- if (data?.series && cfg.field) {
- for (const frame of data.series) {
- const d = getScaledDimension(frame, cfg);
- if (!d.isAssumed || data.series.length === 1) {
- return d;
- }
- }
- }
- return getScaledDimension(undefined, cfg);
- }
- export function getScalarDimensionFromData(
- data: PanelData | undefined,
- cfg: ScalarDimensionConfig
- ): DimensionSupplier<number> {
- if (data?.series && cfg.field) {
- for (const frame of data.series) {
- const d = getScalarDimension(frame, cfg);
- if (!d.isAssumed || data.series.length === 1) {
- return d;
- }
- }
- }
- return getScalarDimension(undefined, cfg);
- }
- export function getResourceDimensionFromData(
- data: PanelData | undefined,
- cfg: ResourceDimensionConfig
- ): DimensionSupplier<string> {
- if (data?.series && cfg.field) {
- for (const frame of data.series) {
- const d = getResourceDimension(frame, cfg);
- if (!d.isAssumed || data.series.length === 1) {
- return d;
- }
- }
- }
- return getResourceDimension(undefined, cfg);
- }
- export function getTextDimensionFromData(
- data: PanelData | undefined,
- cfg: TextDimensionConfig
- ): DimensionSupplier<string> {
- if (data?.series && cfg.field) {
- for (const frame of data.series) {
- const d = getTextDimension(frame, cfg);
- if (!d.isAssumed || data.series.length === 1) {
- return d;
- }
- }
- }
- return getTextDimension(undefined, cfg);
- }
- export function findField(frame?: DataFrame, name?: string): Field | undefined {
- if (!frame || !name?.length) {
- return undefined;
- }
- for (const field of frame.fields) {
- if (name === field.name) {
- return field;
- }
- const disp = getFieldDisplayName(field, frame);
- if (name === disp) {
- return field;
- }
- }
- return undefined;
- }
- export function findFieldIndex(frame?: DataFrame, name?: string): number | undefined {
- if (!frame || !name?.length) {
- return undefined;
- }
- for (let i = 0; i < frame.fields.length; i++) {
- const field = frame.fields[i];
- if (name === field.name) {
- return i;
- }
- const disp = getFieldDisplayName(field, frame);
- if (name === disp) {
- return i;
- }
- }
- return undefined;
- }
- export function getLastNotNullFieldValue<T>(field: Field): T {
- const calcs = field.state?.calcs;
- if (calcs) {
- const v = calcs[ReducerID.lastNotNull];
- if (v != null) {
- return v as T;
- }
- }
- const data = field.values;
- let idx = data.length - 1;
- while (idx >= 0) {
- const v = data.get(idx--);
- if (v != null) {
- return v;
- }
- }
- return undefined as any;
- }
|