useCategorizeFrames.ts 984 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10
import { useMemo } from 'react';
import { DataFrame } from '@grafana/data';

/**
 * As we need 2 dataframes for the service map, one with nodes and one with edges we have to figure out which is which.
 * Right now we do not have any metadata for it so we just check preferredVisualisationType and then column names.
 * TODO: maybe we could use column labels to have a better way to do this
 */
export function useCategorizeFrames(series: DataFrame[]) {
  return useMemo(() => {
11
    const serviceMapFrames = series.filter((frame) => frame.meta?.preferredVisualisationType === 'nodeGraph');
12 13
    return serviceMapFrames.reduce(
      (acc, frame) => {
14
        const sourceField = frame.fields.filter((f) => f.name === 'source');
15 16 17 18 19 20 21 22 23 24 25
        if (sourceField.length) {
          acc.edges.push(frame);
        } else {
          acc.nodes.push(frame);
        }
        return acc;
      },
      { edges: [], nodes: [] } as { nodes: DataFrame[]; edges: DataFrame[] }
    );
  }, [series]);
}