Skip to content

Commit 553610a

Browse files
committed
refactor: typing
1 parent e5abdaa commit 553610a

5 files changed

Lines changed: 56 additions & 50 deletions

File tree

src/lib/gpu/calculations.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import type {
88
IF,
99
ImpactFactors,
1010
ImpactFactorsKeys,
11-
UnorderedImpactFactors
11+
UnorderedImpactFactors,
12+
GCLC
1213
} from "../../lib/types/gpu";
14+
import { isNotExcludedCriterion } from "$lib/utils";
1315
import Average from "../../data/gpu/average_model.json" with { type: "json" };
1416
import { PlanetBoundaries } from "$lib/types/enums";
1517

@@ -217,15 +219,15 @@ export function computeTotalsPerLifeCycleStep(
217219
splitProperty[lastIndex] === "c" || splitProperty[lastIndex] === "nc"
218220
? "CTUh_".concat(splitProperty[lastIndex])
219221
: (splitProperty[lastIndex] as IF);
220-
totalsPerLifeCycleStep[endOfLife][criteria]! = sum as number;
222+
totalsPerLifeCycleStep[endOfLife][criteria as IF]! = sum as number;
221223
} else {
222224
const splitProperty = property.split("_");
223225
const lifeCycleStep = splitProperty[0] as keyof GraphicsCardLifeCycle;
224226
const criteria =
225227
splitProperty[2] === "c" || splitProperty[2] === "nc"
226228
? "CTUh_".concat(splitProperty[2])
227229
: (splitProperty[1] as IF);
228-
totalsPerLifeCycleStep[lifeCycleStep][criteria]! = sum as number;
230+
totalsPerLifeCycleStep[lifeCycleStep][criteria as IF]! = sum as number;
229231
}
230232
});
231233

@@ -293,38 +295,26 @@ export function tidy(card: GraphicsCardImpactFactors): TidyImpactFactor[] {
293295
export function tidyTotals(impactFactors: GraphicsCardLifeCycle): TidyImpactFactor[] {
294296
const lifeCycleSteps = Object.keys(impactFactors).filter((key) => typeof key === "string");
295297
const tidiedImpactFactors = lifeCycleSteps.flatMap((lifeCycleStep) => {
296-
const criterias = Object.keys(impactFactors[lifeCycleStep]).filter(
298+
const criterias = Object.keys(impactFactors[lifeCycleStep as GCLC]).filter(
297299
(key) => typeof key === "string"
298300
);
299301
return criterias.map((criteria) => {
300302
return {
301303
impactCriterion: criteria,
302-
lifeCycleStep,
303-
value: impactFactors[lifeCycleStep][criteria]
304+
lifeCycleStep: lifeCycleStep
305+
.replace("endOfLife", "End-of-life")
306+
.replace(/^./, (char) => char.toUpperCase()),
307+
value: impactFactors[lifeCycleStep as GCLC][criteria as IF]!
304308
};
305309
});
306310
});
307311
return tidiedImpactFactors;
308312
}
309313

310-
function isNotExcludedCriterion(value: string) {
311-
if (
312-
value === "GWPb" ||
313-
value === "GWPf" ||
314-
value === "GWPlu" ||
315-
value === "LU" ||
316-
value === "MIPS" ||
317-
value === "DEEE" ||
318-
value === "TPE"
319-
) {
320-
return false;
321-
}
322-
return true;
323-
}
324314
export function computePlanetBoundaries(impactFactors: ImpactFactors): ImpactFactors {
325315
const ignoredCriteria = ["GWPb", "GWPf", "GWPlu", "LU", "MIPS", "DEEE", "TPE"];
326316
const planetBoundaries = Object.assign({}, impactFactors);
327-
ignoredCriteria.forEach((criterion) => delete planetBoundaries[criterion]);
317+
ignoredCriteria.forEach((criterion) => delete planetBoundaries[criterion as IF]);
328318

329319
const criteria = Object.keys(impactFactors).filter(isNotExcludedCriterion);
330320

@@ -384,10 +374,10 @@ export function tidyPlanetBoundaries(impactFactors: ImpactFactors): TidyRatio[]
384374
);
385375
const tidiedFactors = criteria.flatMap((criterion) => {
386376
const ratio: TidyRatio = {
387-
totalImpactFactor: impactFactors[criterion],
377+
totalImpactFactor: impactFactors[criterion as IF]!,
388378
impactCriterion: criterion,
389-
ratioNumber: planetBoundariesValues[criterion],
390-
ratioPercentage: (planetBoundariesValues[criterion] / totalPerInhabitant) * 100
379+
ratioNumber: planetBoundariesValues[criterion as IF]!,
380+
ratioPercentage: (planetBoundariesValues[criterion as IF]! / totalPerInhabitant) * 100
391381
};
392382
return ratio;
393383
});

src/lib/gpu/gpu.svelte.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import type {
77
TidyRatio
88
} from "$lib/types/gpu.d.ts";
99

10-
import { LifeCycleSteps, Scopes } from "$lib/types/enums";
10+
import { LifeCycleSteps } from "$lib/types/enums";
1111
import GraphicsCards from "../../data/gpu/gpus.json";
1212
import GraphicsCardsImpactFactors from "../../data/gpu/gpus_impact_factors.json";
1313

1414
import { renderHorizontalBarPlot, renderStackedBarPlot } from "$lib/plots";
1515
import {
1616
computeImpacts,
17-
computePlanetBoundaries,
1817
computeTotalsPerCriteria,
1918
computeTotalsPerLifeCycleStep,
2019
tidy,
@@ -83,13 +82,13 @@ export class Card {
8382
}
8483

8584
updatePlotPerCriteria() {
86-
const lcSteps = ["manufacturing", "transport", "use", "endOfLife"];
85+
const lcSteps = Object.values(LifeCycleSteps).filter((lcstep) => typeof lcstep === "string");
8786
const source = "criteria";
8887
renderStackedBarPlot(
8988
source,
9089
1000,
9190
600,
92-
this.tidyTotals,
91+
this.tidyTotals!,
9392
lcSteps,
9493
"impactCriterion",
9594
"value",
@@ -112,7 +111,7 @@ export class Card {
112111
source,
113112
1000,
114113
600,
115-
filteredImpactFactors,
114+
filteredImpactFactors!,
116115
components,
117116
"impactCriterion",
118117
"value",
@@ -126,7 +125,7 @@ export class Card {
126125
source,
127126
1000,
128127
600,
129-
this.tidyRatiosPerPlanetBoundary,
128+
this.tidyRatiosPerPlanetBoundary!,
130129
"ratioNumber",
131130
"impactCriterion",
132131
false
@@ -136,7 +135,7 @@ export class Card {
136135
source,
137136
1000,
138137
600,
139-
this.tidyRatiosPerPlanetBoundary,
138+
this.tidyRatiosPerPlanetBoundary!,
140139
"ratioPercentage",
141140
"impactCriterion",
142141
false

src/lib/plots.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
import type { Data } from "@observablehq/plot";
2-
import type { ImpactFactorShare, OrderedImpactFactors } from "./types/pcr-cloud";
2+
import type { TidyImpactFactor, TidyRatio } from "./types/gpu";
33
import { select } from "d3";
44
import { axisX, axisY, barX, dot, plot, ruleX, ruleY } from "@observablehq/plot";
55

6-
interface ImpactFactor {
7-
impactCriterion: string;
8-
amount: number;
9-
}
10-
11-
interface ImpactFactorWithScope {
12-
scope: string;
13-
amount: number;
14-
}
15-
166
interface Axes {
177
x: string;
188
y: string;
199
}
2010

21-
export function assignAxes(impactFactor: ImpactFactor | ImpactFactorWithScope) {
11+
export function assignAxes(impactFactor: TidyImpactFactor) {
2212
const keys = Object.keys(impactFactor);
2313
const axes: Axes = { x: keys[1], y: keys[0] };
2414
return axes;
@@ -28,7 +18,7 @@ export function renderHorizontalBarPlot(
2818
source: string,
2919
width: number,
3020
height: number,
31-
impactFactors: ImpactFactor[] | ImpactFactorWithScope[],
21+
impactFactors: TidyImpactFactor[] | TidyRatio[],
3222
xLabel: string,
3323
yLabel: string,
3424
lollipop: boolean
@@ -53,6 +43,7 @@ export function renderHorizontalBarPlot(
5343
const barMarks = [
5444
ruleX([0]),
5545
axisX({ tickSpacing: 100 }),
46+
axisY({ labelAnchor: "top" }),
5647
barX(impactFactors, {
5748
x: xLabel,
5849
y: yLabel,
@@ -64,7 +55,9 @@ export function renderHorizontalBarPlot(
6455
const barPlot = plot({
6556
width: width,
6657
height: height,
67-
y: { grid: true },
58+
style: { overflow: "visible" },
59+
y: { grid: true, label: "Criterion" },
60+
x: { label: "Ratio" },
6861
marks: lollipop ? lollipopMarks : barMarks
6962
});
7063
div.append(barPlot);
@@ -84,24 +77,25 @@ export function renderStackedBarPlot(
8477
source: string,
8578
width: number,
8679
height: number,
87-
impactFactors: ImpactFactorShare[] | OrderedImpactFactors,
80+
impactFactors: TidyImpactFactor[] | TidyRatio[],
8881
domains: string[],
8982
yLabel: string,
9083
xLabel: string,
9184
domainColor: string
9285
) {
9386
let div = document.querySelector(`#impact-factors-plot-${source}`);
9487
div?.firstChild?.remove();
88+
9589
if (div) {
9690
const barPlot = plot({
9791
width: width,
9892
height: height,
9993
className: "plot",
100-
color: { legend: true, domain: domains },
94+
color: { legend: "swatches", domain: domains },
10195
x: { percent: true },
10296
marks: [
10397
axisY({ fontSize: 12, label: null, marginLeft: 60 }),
104-
axisX({ marginBottom: 48 }),
98+
axisX({ marginBottom: 48, label: "Value (%)" }),
10599
barX(impactFactors as Data, {
106100
y: yLabel,
107101
x: xLabel,

src/lib/types/gpu.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export declare type GraphicsCardLifeCycle = {
144144
endOfLife: ImpactFactors;
145145
};
146146

147+
export declare type GCLC = keyof GraphicsCardLifeCycle;
148+
147149
export declare type ImpactFactors = {
148150
ADPe?: number;
149151
ADPf?: number;
@@ -171,4 +173,10 @@ export declare type ImpactFactors = {
171173

172174
export declare type IF = keyof ImpactFactors;
173175

176+
export declare type ImpactCriteria = {
177+
name: string;
178+
acronym: ImpactCriterionAcronym;
179+
unit: string;
180+
};
181+
174182
declare module "gpu";

src/lib/utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function convertTableToCSV(table: HTMLTableElement) {
1515
export function downloadToCSV(nodeId: string) {
1616
const table = document.getElementById(nodeId)?.getElementsByTagName("table")[0];
1717
const csvRaw = convertTableToCSV(table!);
18-
const csvFile = new File([csvRaw], { type: "text/csv" });
18+
const csvFile = new File([csvRaw], "text/csv");
1919
const temporaryLink = document.createElement("a");
2020
const url = window.URL.createObjectURL(csvFile);
2121

@@ -44,9 +44,24 @@ export async function downloadToPNG(nodeId: string) {
4444
window.URL.revokeObjectURL(url);
4545
}
4646

47-
export function isNotATransport(value) {
47+
export function isNotATransport(value: string) {
4848
if (value === "transport_boat" || value === "transport_truck" || value === "transport_plane") {
4949
return false;
5050
}
5151
return true;
5252
}
53+
54+
export function isNotExcludedCriterion(value: string) {
55+
if (
56+
value === "GWPb" ||
57+
value === "GWPf" ||
58+
value === "GWPlu" ||
59+
value === "LU" ||
60+
value === "MIPS" ||
61+
value === "DEEE" ||
62+
value === "TPE"
63+
) {
64+
return false;
65+
}
66+
return true;
67+
}

0 commit comments

Comments
 (0)