briangann/grafana-datatable-panel

View on GitHub
src/transformations.ts

Summary

Maintainability
A
0 mins
Test Coverage
F
0%
import { DataFrame, DataTransformerConfig, DataTransformerID, transformDataFrame } from '@grafana/data';
import { lastValueFrom } from 'rxjs';
import { TransformationOptions } from 'types';

export const transformationIDMapping = {
  [TransformationOptions.TimeSeriesToColumns]: DataTransformerID.joinByField,
  [TransformationOptions.TimeSeriesToRows]: DataTransformerID.seriesToRows,
  [TransformationOptions.Table]: DataTransformerID.timeSeriesTable,
};

export function transformData(
  data: DataFrame[],
  transformation: keyof typeof transformationIDMapping,
  options: DataTransformerConfig['options'] = {}
): Promise<DataFrame[]> {
  const transformationID = transformationIDMapping[transformation];
  return lastValueFrom(
    transformDataFrame(
      [
        {
          id: transformationID,
          options,
        },
      ],
      data
    )
  );
}
export function getDataFramesFields(dataFrames: DataFrame[]): string[] {
  return dataFrames.reduce<string[]>((acc, df) => {
    df.fields.map((field) => {
      if (!acc.includes(field.name)) {
        acc.push(field.name);
      }
    });
    return acc;
  }, []);
}