All files / src/pages/flowsheet-page/economics/schedule-selector scheduleFormatting.ts

80% Statements 20/25
76.92% Branches 10/13
25% Functions 1/4
79.16% Lines 19/24

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132            103x 103x               103x                                                                             80x           35x   106x   26x 26x   26x   26x   80x                           35x                                           80x 128x   80x 26x       80x 80x             78x                
import type {
  CompiledScheduleStep,
  CompositeSchedulePlanPayload,
} from "@/api/apiStore.gen";
import { ScheduleTimelineSourceKind } from "@/api/apiStore.gen";
 
export const DAY_LABELS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
export const TIMETABLE_AXIS_LABELS = [
  "00:00",
  "06:00",
  "12:00",
  "18:00",
  "24:00",
];
 
export const SOURCE_PALETTE = [
  {
    block: "bg-sky-500/25 text-sky-950 dark:text-sky-100",
    swatch: "bg-sky-500",
  },
  {
    block: "bg-emerald-500/25 text-emerald-950 dark:text-emerald-100",
    swatch: "bg-emerald-500",
  },
  {
    block: "bg-amber-500/25 text-amber-950 dark:text-amber-100",
    swatch: "bg-amber-500",
  },
  {
    block: "bg-rose-500/25 text-rose-950 dark:text-rose-100",
    swatch: "bg-rose-500",
  },
  {
    block: "bg-cyan-500/25 text-cyan-950 dark:text-cyan-100",
    swatch: "bg-cyan-500",
  },
];
 
export type TimetableDisplayBlock = {
  key: string;
  source_kind: CompiledScheduleStep["source_kind"];
  start_time: string;
  end_time: string;
  duration_hours: number;
  source_scenario?: number | null;
  source_scenario_name?: string;
  source_rule?: number | null;
  source_rule_label?: string;
  source_row_count: number;
};
 
export function timeRangeLabel(
  value: Pick<TimetableDisplayBlock, "start_time" | "end_time">,
) {
  return `${value.start_time.slice(0, 5)}-${value.end_time.slice(0, 5)}`;
}
 
export function timetableBlocks(
  steps: CompiledScheduleStep[],
): TimetableDisplayBlock[] {
  const blocks: TimetableDisplayBlock[] = [];
  for (const step of steps) {
    const previous = blocks.at(-1);
    if (previous && timetableBlockCanMerge(previous, step)) {
      previous.end_time = step.end_time;
      previous.duration_hours += Number(step.duration_hours);
      if (step.source_kind === ScheduleTimelineSourceKind.ScheduledSource) {
        previous.source_row_count += 1;
      }
      continue;
    }
    blocks.push({
      key: String(step.index),
      source_kind: step.source_kind,
      start_time: step.start_time,
      end_time: step.end_time,
      duration_hours: Number(step.duration_hours),
      source_scenario: step.source_scenario,
      source_scenario_name: step.source_scenario_name,
      source_rule: step.source_rule,
      source_rule_label: step.source_rule_label,
      source_row_count:
        step.source_kind === ScheduleTimelineSourceKind.ScheduledSource ? 1 : 0,
    });
  }
  return blocks;
}
 
export function scheduledBlockCount(plan: CompositeSchedulePlanPayload) {
  return DAY_LABELS.reduce((count, _label, dayIndex) => {
    const daySteps = plan.timeline.filter(
      (step) => Number(step.day) === dayIndex,
    );
    return (
      count +
      timetableBlocks(daySteps).filter(
        (block) =>
          block.source_kind === ScheduleTimelineSourceKind.ScheduledSource,
      ).length
    );
  }, 0);
}
 
export function paletteForBlock(
  plan: CompositeSchedulePlanPayload,
  block: TimetableDisplayBlock,
) {
  const sourceIndex = plan.sources.findIndex(
    (source) => source.id === block.source_scenario,
  );
  if (sourceIndex < 0) return SOURCE_PALETTE[0];
  return SOURCE_PALETTE[sourceIndex % SOURCE_PALETTE.length];
}
 
export function timeToHours(value: string) {
  const [hours = "0", minutes = "0", seconds = "0"] = value.split(":");
  return Number(hours) + Number(minutes) / 60 + Number(seconds) / 3600;
}
 
function timetableBlockCanMerge(
  block: TimetableDisplayBlock,
  step: CompiledScheduleStep,
) {
  return (
    block.end_time === step.start_time &&
    block.source_kind === step.source_kind &&
    block.source_scenario === step.source_scenario &&
    block.source_rule === step.source_rule &&
    block.source_rule_label === step.source_rule_label
  );
}