Skip to content

Commit 40d3377

Browse files
committed
fix: compute and display total co2 and energy ref #185
1 parent 5170db5 commit 40d3377

File tree

1 file changed

+74
-12
lines changed
  • frontend/src/routes/projects/dryruns/[dry_run]/[resource]

1 file changed

+74
-12
lines changed

frontend/src/routes/projects/dryruns/[dry_run]/[resource]/+page.svelte

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
// import { displayAlert } from '$utils/alerts-utils';
2525
import type { DryRunMetrics, DryRun, MetricsWithTimeStamps } from '$typesdefinitions';
2626
27+
// Extended type to include carbontracker data
28+
interface ExtendedDryRunMetrics extends DryRunMetrics {
29+
carbontracker?: {
30+
fetchCarbontrackerData?: {
31+
co2eq: number;
32+
energy: number;
33+
};
34+
};
35+
}
36+
2737
export let data;
2838
2939
let loadingFinished = false;
@@ -50,7 +60,8 @@
5060
let allStepNames: string[] = [];
5161
5262
$: selectedStep = 'Total';
53-
$: reactiveStepsList = $stepsList;
63+
let reactiveStepsList: ExtendedDryRunMetrics[];
64+
$: reactiveStepsList = ($stepsList as ExtendedDryRunMetrics[]) || [];
5465
5566
function gotoOverview(): void {
5667
selectedStep = 'Total';
@@ -73,6 +84,8 @@
7384
let networkTotalReceived = 'N/A';
7485
let networkTotalTransmitted = 'N/A';
7586
let pipelineDuration = 'N/A';
87+
let totalCO2 = 'N/A';
88+
let totalEnergy = 'N/A';
7689
7790
function formatDuration(seconds: number): string {
7891
const hours = Math.floor(seconds / 3600);
@@ -108,9 +121,35 @@
108121
pipelineDuration = formatDuration(totalDuration);
109122
}
110123
124+
function computeTotalCarbonMetrics(): void {
125+
let co2Sum = 0;
126+
let energySum = 0;
127+
128+
if (reactiveStepsList) {
129+
reactiveStepsList.forEach((step) => {
130+
if (step.carbontracker?.fetchCarbontrackerData) {
131+
if (step.carbontracker.fetchCarbontrackerData.co2eq) {
132+
co2Sum += step.carbontracker.fetchCarbontrackerData.co2eq;
133+
}
134+
if (step.carbontracker.fetchCarbontrackerData.energy) {
135+
energySum += step.carbontracker.fetchCarbontrackerData.energy;
136+
}
137+
}
138+
});
139+
}
140+
141+
totalCO2 = co2Sum > 0 ? co2Sum.toFixed(3) : 'N/A';
142+
totalEnergy = energySum > 0 ? energySum.toFixed(6) : 'N/A';
143+
}
144+
145+
// Reactive statement to compute carbon metrics when reactiveStepsList changes
146+
$: if (reactiveStepsList && reactiveStepsList.length > 0) {
147+
computeTotalCarbonMetrics();
148+
}
149+
111150
let dryRunPhaseMessage: string | null;
112151
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, consistent-return
113-
const getMetricsResponse = async () => {
152+
const getMetricsResponse = async (): Promise<DryRunMetrics[]> => {
114153
const dryrunVariables = {
115154
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
116155
dryRunId: data.resource
@@ -125,10 +164,16 @@
125164
const body = `${(error as Error).message}`;
126165
// await displayAlert(title, body, 10_000);
127166
console.log(title, body);
167+
return [];
128168
}
129169
};
130170
131-
async function getCarbontrackerDataResponse(input: any): Promise<any> {
171+
async function getCarbontrackerDataResponse(input: any): Promise<{
172+
fetchCarbontrackerData?: {
173+
co2eq: number;
174+
energy: number;
175+
};
176+
} | undefined> {
132177
const inputData = {
133178
input: {
134179
data: {
@@ -145,7 +190,12 @@
145190
console.log('inputData:', inputData);
146191
try {
147192
const response = await requestGraphQLClient(getCarbontrackerDataQuery, inputData);
148-
return response;
193+
return response as {
194+
fetchCarbontrackerData?: {
195+
co2eq: number;
196+
energy: number;
197+
};
198+
};
149199
} catch (error) {
150200
const title = 'Internal error!';
151201
const body = `${(error as Error).message}`;
@@ -209,12 +259,15 @@
209259
cumulativeNetworkData = result.cumulativeNetworkData;
210260
currentNetworkData = result.currentNetworkData;
211261
logs = result.logs;
212-
//console.log('dryRunId:', $selectedDryRunName);
213-
const carbontracker_data = [];
262+
const carbontrackerData: Array<{
263+
nodeId: string;
264+
stepName: string;
265+
carbonData: { fetchCarbontrackerData?: { co2eq: number; energy: number } } | undefined;
266+
}> = [];
214267
for (const step of metricsResponse) {
215268
if (step.type === 'Pod') {
216269
const carbonResponse = await getCarbontrackerDataResponse(step.metrics.cpuUsageSecondsTotal);
217-
carbontracker_data.push({
270+
carbontrackerData.push({
218271
nodeId: step.id,
219272
stepName: step.displayName,
220273
carbonData: carbonResponse
@@ -225,10 +278,10 @@
225278
226279
// Merge carbontracker data with stepsList
227280
$stepsList = $stepsList.map(step => {
228-
const carbonData = carbontracker_data.find(carbon => carbon.nodeId === step.id);
281+
const carbonData = carbontrackerData.find(carbon => carbon.nodeId === step.id);
229282
return {
230283
...step,
231-
carbontracker: carbonData ? carbonData.carbonData : null
284+
carbontracker: carbonData ? carbonData.carbonData : undefined
232285
};
233286
});
234287
@@ -238,8 +291,9 @@
238291
metrics: metricsResponse,
239292
allstepnames: allStepNames,
240293
selectedDryRunName: $selectedDryRunName,
241-
carbontracker_data
294+
carbontrackerData
242295
};
296+
243297
console.log('responses:', responses);
244298
return responses;
245299
};
@@ -671,8 +725,16 @@
671725
<tr>
672726
<td>Duration</td>
673727
<td> {pipelineDuration} </td>
674-
</tr></tbody
675-
>
728+
</tr>
729+
<tr>
730+
<td>Total CO2</td>
731+
<td> {totalCO2} g </td>
732+
</tr>
733+
<tr>
734+
<td>Total Energy</td>
735+
<td> {totalEnergy} kWh </td>
736+
</tr>
737+
</tbody>
676738
</table>
677739
</div>
678740

0 commit comments

Comments
 (0)