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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | 103x 103x 103x | import type { EconomicsResultLineRead, UnitOption } from "@/api/apiStore.gen";
import { numericField, stringField } from "./valueParsing";
export const HEAT_AND_COOLING_RESOURCE_KEY = "annual_heat_and_cooling_duty";
export const THERMAL_DUTY_RESOURCE_SECTIONS = [
{
key: "annual_heating_duty",
label: "Annual Heating Duty",
},
{
key: "annual_cooling_duty",
label: "Annual Cooling Duty",
},
] as const;
export const RESOURCE_BREAKDOWN_SECTIONS = [
{
key: "annual_work",
label: "Annual Work",
},
{
key: "raw_material_consumption",
label: "Raw material consumption",
},
{
key: "product_output",
label: "Product output",
},
{
key: "waste_disposal",
label: "Waste disposal",
},
] as const;
export type ResourceBreakdownSection = {
key: string;
label: string;
rows: EconomicsResultLineRead[];
selectedUnit: string;
};
export type ResourceQuantityValue = {
quantity: number;
unit: string;
};
export function resourceBreakdownCategory(line: EconomicsResultLineRead) {
return stringField(line.resource_breakdown_category);
}
export function resourceBreakdownRows(lines: EconomicsResultLineRead[]) {
const grouped = new Map<string, EconomicsResultLineRead[]>();
for (const line of lines) {
Iif (line.group !== "operating_lines") continue;
const category = resourceBreakdownCategory(line);
Iif (!category) continue;
grouped.set(category, [...(grouped.get(category) ?? []), line]);
}
return grouped;
}
export function resourceSectionFromDefinition({
section,
grouped,
selectedResourceUnit,
}: {
section: { key: string; label: string };
grouped: Map<string, EconomicsResultLineRead[]>;
selectedResourceUnit: (
sectionKey: string,
rows: EconomicsResultLineRead[],
) => string;
}): ResourceBreakdownSection {
const rows = grouped.get(section.key) ?? [];
return {
key: section.key,
label: section.label,
rows,
selectedUnit: selectedResourceUnit(section.key, rows),
};
}
export function resourceQuantitySummaryValues(
lines: EconomicsResultLineRead[],
selectedUnit = "",
) {
Iif (lines.length === 0) return [];
if (selectedUnit) {
const selectedQuantity = sumResourceQuantities(lines, selectedUnit);
if (selectedQuantity != null) {
return [{ quantity: selectedQuantity, unit: selectedUnit }];
}
}
const byUnit = new Map<string, number>();
for (const line of lines) {
const quantity = numericField(line.annual_basis_quantity);
const unit = annualResourceUnit(stringField(line.annual_basis_unit));
Iif (quantity == null || !unit) continue;
byUnit.set(unit, (byUnit.get(unit) ?? 0) + quantity);
}
return Array.from(byUnit.entries()).map(([unit, quantity]) => ({
quantity,
unit,
}));
}
export function resourceQuantityValue(
line: EconomicsResultLineRead,
selectedUnit = "",
): ResourceQuantityValue | null {
if (selectedUnit) {
const selectedQuantity = convertedResourceQuantity(line, selectedUnit);
if (selectedQuantity != null) {
return { quantity: selectedQuantity, unit: selectedUnit };
}
}
const quantity = numericField(line.annual_basis_quantity);
const unit = annualResourceUnit(stringField(line.annual_basis_unit));
Iif (quantity == null || !unit) return null;
return { quantity, unit };
}
export function sumResourceQuantities(
lines: EconomicsResultLineRead[],
unit: string,
) {
let total = 0;
for (const line of lines) {
const quantity = convertedResourceQuantity(line, unit);
Iif (quantity == null) return null;
total += quantity;
}
return total;
}
export function sumResourceLineAmounts(lines: EconomicsResultLineRead[]) {
return lines.reduce(
(total, line) => total + (numericField(line.amount) ?? 0),
0,
);
}
export function sharedResourceQuantityOptions(
lines: EconomicsResultLineRead[],
) {
Iif (lines.length === 0) return [];
const [firstLine, ...remainingLines] = lines;
return resourceQuantityOptions(firstLine).filter((option) =>
remainingLines.every((line) =>
resourceQuantityOptions(line).some(
(candidate) => candidate.value === option.value,
),
),
);
}
export function defaultResourceQuantityUnit(lines: EconomicsResultLineRead[]) {
const options = sharedResourceQuantityOptions(lines);
if (options.length === 0) {
return annualResourceUnit(stringField(lines[0]?.annual_basis_unit));
}
const currentUnit = annualResourceUnit(
stringField(lines[0]?.annual_basis_unit),
);
return options.some((option) => option.value === currentUnit)
? currentUnit
: options[0].value;
}
export function annualResourceUnit(unit: string) {
Iif (!unit) return "";
return unit.includes("/year") || unit.includes("/yr") ? unit : `${unit}/year`;
}
function convertedResourceQuantity(
line: EconomicsResultLineRead,
unit: string,
) {
const quantities = line.annual_basis_quantities_by_unit ?? {};
return numericField(quantities[unit]);
}
function resourceQuantityOptions(line: EconomicsResultLineRead): UnitOption[] {
return line.annual_basis_unit_options ?? [];
}
|