All files / src/pages/flowsheet-page/pinch-analysis/hen-generation/utils buildHENStreams.ts

100% Statements 25/25
100% Branches 11/11
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                  14x 14x   44x 11x 36x 36x 36x                   14x 53x 14x                                     14x     14x   14x             20x 20x     14x 14x         14x               14x   14x            
import * as d3 from "d3";
import { AggregatedStream, MergedStream } from "../henTypes";
import {
    buildSdeChain,
    mergeRelevantSdes,
    sortHotPriority
} from "./streams";
 
function aggregateStreams(segments, sdes): AggregatedStream[] {
  const children = segments?.children ?? [];
  if (!children || !sdes) return [];
 
  const grouped = d3.group(children, (d: any) => String(d.stream_data_entry));
  return Array.from(grouped, ([sde, segs]) => {
    const first = segs[0];
    const isHot = first.t_supply > first.t_target;
    return {
      stream_data_entry: sde,
      name: first.name,
      isHot,
      rawSegments: segs,
    };
  });
}
 
function expandBySde(sdes): Map<string, boolean> {
  const m = new Map<string, boolean>();
  (sdes ?? []).forEach((s) => m.set(String(s.id), !!s.expanded));
  return m;
}
 
export function buildStreams(
  segments,
  sdes,
  henNodes,
  streamPositions
): {
  allStreams: MergedStream[];
  expandedBySde: Map<string, boolean>;
  aggregatedStreams: AggregatedStream[];
} {
  // Builds the streams from the segments and sorts the streams for rendering.
 
  // -------------------------------------------------
  // stuff for setting up layout LATER
  // -------------------------------------------------
  // aggregate segments by SDE id
  const aggregatedStreams = aggregateStreams(segments, sdes);
 
  // get sde chains (i.e. full streams via joining sdes and their "nextStreams").
  const sdeChains = buildSdeChain(sdes ?? []);
  // connect the streams with the sde chains.
  const mergedStreams = mergeRelevantSdes(
    aggregatedStreams,
    sdeChains,
    henNodes
  );
 
  // sort hot then cold streams at first render
  const hotStreams = mergedStreams.filter((s) => s.isHot);
  const coldStreams = mergedStreams.filter((s) => !s.isHot);
 
  // sort so hot streams with more nodes are rendered first, then all cold streams.
  const sortedHot = sortHotPriority(hotStreams);
  const initialStreams = [...sortedHot, ...coldStreams];
 
  // use stream order specified by user (by dragging) or the first initial render for noe
  // Future should let user's rearranged order be persistent (save to db).
  const allStreams =
    streamPositions.length > 0 ? streamPositions : initialStreams;
 
  // -----------------------------------------------------------------------
  // Expansion state stuff (optimisitic updates for expanding/collapsing)
  // -----------------------------------------------------------------------
  // makes a map <sdeId, boolean> to show whether sde is expanded (should show segments) or not (show collapsed).
  // so iterating over this map should quickyl check if sde is expanded or not.
  // reflects server side (what is actually in db)
  const expandedBySde = expandBySde(sdes);
 
  return {
    allStreams,
    expandedBySde,
    aggregatedStreams,
  };
}