useCategorizeFrames.ts 864 B

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