-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbenchmark.html
639 lines (514 loc) · 26.9 KB
/
benchmark.html
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<title>Carbon Footprint Benchmarking Tool</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="js/carbon-footprint-modeling-tool.js"></script>
<link rel="stylesheet" href="css/style.css">
<style>
html, body {
margin:0;
padding:0;
height:100%;
}
h1 {
color: #333;
text-align: center;
font-size: 24px; /* Adjust size for better mobile readability */
}
form, div#description {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 500px;
margin: 0 auto;
width: 100%; /* Make form width responsive */
}
span#expandedDescription {
margin-top:10px;
display: none;
}
a#expandedDescriptionLink {
color: grey;
}
input, select, textarea {
width: 100%; /* Use percentage-based width for responsiveness */
box-sizing: border-box; /* Ensure padding is included in width calculation */
}
div#form {
font-family: Arial, sans-serif !important;
display: none;
}
#benchmarkForm button.addButton, #benchmarkForm button[type="submit"] {
font-family: Arial, sans-serif;
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
font-weight: bold;
font-size: 18px;
width: 100%; /* Make the submit button full width on small screens */
}
#benchmarkForm button.addButton {
background-color: white;
color: black;
border: solid 1px grey;
font-weight: normal;
margin-bottom: 20px;
width:200px;
padding: 5px 10px;
font-size: 16px;
border-radius: 2px;
}
div[id="header"] {
text-align: center;
}
div[id="arena"] {
clear: right;
display: flex;
margin: auto;
width: max-content;
height: 90%;
}
div[name="sample"], div[name="reference"] {
width: 200px;
margin: 20px;
align-self: flex-end;
}
div[name="sample"] div[name="bar"] {
border: solid;
}
div[name="reference"] div[name="bar"] {
border: solid;
border-color: lightgray;
background-color: lightgray;
text-align: center;
justify-content: center;
display: flex;
align-items: center;
}
div[name="sample"] div[name="foot"], div[name="reference"] div[name="foot"] {
min-height: 100px;
}
label, input, select {
display: block;
margin-bottom: 10px;
}
.form-group {
margin-bottom: 10px;
}
.input-section {
margin-bottom: 15px;
padding: 10px;
cursor: grab;
background-color: rgb(237, 237, 237);
padding: 15px;
}
.input-section.dragging {
opacity: 0.5;
}
div#footer {
padding-top: 20px;
text-align: center;
color: grey;
font-size: 12px;
a {
color: grey;
}
}
.closeButton {
float: right;
cursor: pointer;
background: none;
border: none;
padding: 0;
font: inherit;
cursor: pointer;
outline: inherit;
color: grey;
}
.tooltip {
position: relative;
display: inline-block;
cursor: help;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 200px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
<script>
let url = new URL(location.href);
// the preferred parameter is scenario_ids[]; ids[] is retained for backward compatibility
let scenario_ids = [...url.searchParams.getAll("scenario_ids[]"), ...url.searchParams.getAll("ids[]")];
let scenarios = url.searchParams.getAll("scenarios[]").map(base64 => {
let scenario = {};
scenario["scenario_id"] = null;
scenario["base64"] = base64;
base64 = decodeURIComponent(base64);
scenario["scenario"] = JSON.parse(atob(base64));
return scenario;
});
let reference_scenario_base64 = url.searchParams.get("reference_scenario");
let reference_scenarios = [];
if (reference_scenario_base64) {
let reference_scenario = {};
reference_scenario["scenario_id"] = null;
reference_scenario["base64"] = reference_scenario_base64;
reference_scenario_base64 = decodeURIComponent(reference_scenario_base64);
reference_scenario["scenario"] = JSON.parse(atob(reference_scenario_base64));
reference_scenarios.push(reference_scenario);
};
let reference_ids = [...url.searchParams.getAll("reference_ids[]"), url.searchParams.get("reference_id")].filter(value => value !== null && value !== "");
let source_ids = url.searchParams.getAll("source_ids[]");
let primary_sources = [];
if (source_ids) {
// TODO: replace data source of autocompletejs with API call
let request2 = new XMLHttpRequest();
request2.open("GET", "./data/sources.json", false);
request2.setRequestHeader('Cache-Control', 'no-cache');
request2.send(null)
let sources_json = JSON.parse(request2.responseText);
primary_sources = source_ids.map(key => (key in sources_json ? sources_json[key] : null));
}
// optional parameter to override emission type
var emission_type_parameter = url.searchParams.get("emission_type");
// optional parameter to add page title
var title_parameter = url.searchParams.get("title");
// optional parameter to multiply scenarios by factor
var factor_parameter = url.searchParams.get("factor");
window.addEventListener('load', function () {
document.getElementById('expandedDescriptionLink').onclick = function(e) {
e.preventDefault();
var expandedDescription = document.querySelector('#expandedDescription');
expandedDescription.style.display = 'block';
this.style.display = 'none';
};
// fallback to sample scenario if no id parameter
if (!scenario_ids.length && !scenarios.length) {
// window.location.href = "benchmark.html?scenario_ids[]=scenario-8f35af7c-ee5b-42aa-b538-371b126b3d24&scenario_ids[]=scenario-725b3ff2-294b-4cfc-81a3-fc460ee61fdc"
document.getElementById("form").style.display = "block";
}
// fetch the scenarios which are to be compared
for (const scenario_id of scenario_ids) {
let request = new XMLHttpRequest();
request.open("GET", "./scenarios/" + scenario_id + ".json", false);
request.setRequestHeader('Cache-Control', 'no-cache');
request.send(null)
let scenario = {};
scenario["scenario_id"] = scenario_id;
scenario["scenario"] = JSON.parse(request.responseText);
scenarios.push(scenario);
}
for (const reference_id of reference_ids) {
let request = new XMLHttpRequest();
request.open("GET", "./scenarios/" + reference_id + ".json", false);
request.setRequestHeader('Cache-Control', 'no-cache');
request.send(null)
let scenario = {};
scenario["scenario_id"] = reference_id;
scenario["scenario"] = JSON.parse(request.responseText);
reference_scenarios.push(scenario);
}
let arena = document.getElementById("arena");
// identify max emission value
let emissions = [];
// calculate emissions per scenario, consider primary source if provided
let i = 0;
for (const { scenario_id: scenario_id, base64: base64, scenario: scenario } of scenarios) {
let emission = {};
emission["scenario_id"] = scenario_id;
emission["emission"] = totalEmissions(scenario, factor_parameter || null, primary_sources[i]);
emissions.push(emission);
i++;
}
let all_emissions = emissions.reduce((acc, { scenario_id, emission }) => {
acc[scenario_id] = emission;
return acc;
}, {});
if (Object.keys(reference_scenarios).length > 0) {
for (const reference_scenario of reference_scenarios) {
all_emissions[reference_scenario["scenario_id"]] = totalEmissions(reference_scenario["scenario"], null);
}
}
let best_unit = emission_type_parameter || bestTotalEmissionType(all_emissions);
if (!best_unit) {
console.error("No common emission type available");
alert("Error: No common emission type available");
return
}
let emissions_of_best_unit = [];
emissions.map(function({ scenario_id, emission }) {
emissions_of_best_unit.push(emission[best_unit]);
});
// compute calibration factor
let calibration_factor = window.screen.availHeight/2/Math.max(...emissions_of_best_unit);
let j = 0;
for (const { scenario_id: scenario_id, base64: base64, scenario: scenario } of scenarios) {
let sample = document.querySelector(".template[name='sample']").cloneNode(true);
sample.classList.remove("template");
let title = sample.querySelector("a[name='title']");
title.innerHTML = scenario.title;
let urlParameters = {};
if (scenario_id) {
urlParameters["scenario_id"] = scenario_id;
} else {
urlParameters["scenario"] = encodeURIComponent(base64);
}
urlParameters["emission_type"] = best_unit;
urlParameters["source_id"] = (source_ids[j] || "");
if (factor_parameter !== '' && factor_parameter !== null && factor_parameter != 1) {
urlParameters["factor"] = factor_parameter;
}
title.setAttribute("href", "show.html?" + Object.entries(urlParameters).map(x=>x.join("=")).join("&"));
let emissions = totalEmissions(scenario, factor_parameter || null, primary_sources[j]);
let bar = sample.querySelector("div[name='bar']");
bar.style.height = emissions[best_unit]*calibration_factor + "px";
let total_emission = sample.querySelector("span[name='total_emission']");
total_emission.innerHTML = formatTotalEmissions(emissions[best_unit], best_unit);
arena.appendChild(sample);
j++;
}
// add comparison(s) with baseline energy source(s) if provided
for (const { scenario_id: scenario_id, base64: base64, scenario: scenario } of reference_scenarios) {
let sample = document.querySelector(".template[name='reference']").cloneNode(true);
sample.classList.remove("template");
let bar = sample.querySelector("div[name='bar']");
let last_scenario = scenarios[scenarios.length-1].scenario;
let last_primary_source = primary_sources[scenarios.length-1];
let emissions = totalEmissions(last_scenario, factor_parameter || null, last_primary_source);
let reference_emission = totalEmissions(scenario, null);
let reference_emissions_of_best_unit = reference_emission[best_unit];
let reference_factor = (emissions[best_unit]/reference_emissions_of_best_unit).toFixed(2);
bar.style.height = emissions[best_unit]*calibration_factor + "px";
sample.querySelector("span[name='reference_factor']").innerHTML = reference_factor + "x"
arena.appendChild(sample);
let title = sample.querySelector("a[name='title']");
title.innerHTML = scenario["title"];
let urlParameters = {};
if (scenario_id) {
urlParameters["scenario_id"] = scenario_id;
} else {
urlParameters["scenario"] = encodeURIComponent(base64);
}
urlParameters["emission_type"] = best_unit;
if (reference_factor !== '' && reference_factor !== null && reference_factor != 1) {
urlParameters["factor"] = reference_factor;
}
title.setAttribute("href", "show.html?" + Object.entries(urlParameters).map(x=>x.join("=")).join("&"));
}
if (title_parameter) {
document.getElementById("title").appendChild(document.createTextNode(decodeURIComponent(title_parameter)));
}
});
let draggedElement;
let originalY;
function addInput() {
const container = document.getElementById('inputContainer');
const newInputSection = document.createElement('div');
newInputSection.className = 'input-section';
newInputSection.setAttribute('draggable', 'true');
newInputSection.setAttribute('ondragstart', 'drag(event)');
newInputSection.setAttribute('ondragover', 'allowDrop(event)');
newInputSection.setAttribute('ondrop', 'drop(event)');
newInputSection.innerHTML = `
<div class="form-group">
<button onclick="removeInput(this)" class="closeButton">X</button>
<label>Scenario ID <span class="tooltip">?⃝<span class="tooltiptext">Enter the scenario ID you would like to benchmark.</span>:</label>
<input type="text" name="scenario_ids[]" required><br/>
<label>Source <span class="tooltip">?⃝<span class="tooltiptext">Optionally, select a carbon factor for the electricity source to override the scenario's default.</span>(optional):</label>
<select id="source_ids" name="source_ids[]">
<option value="">--- Select source ---</option>
<option value="electricity-austria-2020">Austria (2020)</option>
<option value="electricity-belgium-2020">Belgium (2020)</option>
<option value="electricity-bulgaria-2020">Bulgaria (2020)</option>
<option value="electricity-croatia-2020">Croatia (2020)</option>
<option value="electricity-cyprus-2020">Cyprus (2020)</option>
<option value="electricity-czechia-2020">Czechia (2020)</option>
<option value="electricity-denmark-2020">Denmark (2020)</option>
<option value="electricity-estonia-2020">Estonia (2020)</option>
<option value="electricity-eu-27-2020">EU-27 (2020)</option>
<option value="electricity-finland-2020">Finland (2020)</option>
<option value="electricity-france-2020">France (2020)</option>
<option value="electricity-germany-2020">Germany (2020)</option>
<option value="electricity-greece-2020">Greece (2020)</option>
<option value="electricity-hungary-2020">Hungary (2020)</option>
<option value="electricity-ireland-2020">Ireland (2020)</option>
<option value="electricity-italy-2020">Italy (2020)</option>
<option value="electricity-latvia-2020">Latvia (2020)</option>
<option value="electricity-lithuania-2020">Lithuania (2020)</option>
<option value="electricity-luxembourg-2020">Luxembourg (2020)</option>
<option value="electricity-malta-2020">Malta (2020)</option>
<option value="electricity-netherlands-2020">Netherlands (2020)</option>
<option value="electricity-poland-2020">Poland (2020)</option>
<option value="electricity-portugal-2020">Portugal (2020)</option>
<option value="electricity-romania-2020">Romania (2020)</option>
<option value="electricity-slovakia-2020">Slovakia (2020)</option>
<option value="electricity-slovenia-2020">Slovenia (2020)</option>
<option value="electricity-spain-2020">Spain (2020)</option>
<option value="electricity-sweden-2020">Sweden (2020)</option>
<option value="electricity-usa-2020">USA (2020)</option>
</select>
</div>
`;
container.appendChild(newInputSection);
}
function removeInput(element) {
const inputSection = element.parentElement.parentElement;
inputSection.remove();
}
function toggleReferenceFields(input) {
const referenceId = document.querySelector('input[name="reference_id"]');
const referenceScenario = document.querySelector('textarea[name="reference_scenario"]');
if (input === referenceId) {
referenceScenario.value = '';
} else {
referenceId.value = '';
}
}
function drag(event) {
draggedElement = event.target;
draggedElement.classList.add('dragging');
originalY = draggedElement.getBoundingClientRect().top;
}
function allowDrop(event) {
event.preventDefault();
}
function drop(event) {
event.preventDefault(); // Prevent default behavior
const dropTarget = event.target.closest('.input-section');
if (dropTarget) {
// Insert the dragged element before or after the drop target
if (event.clientY < originalY) {
dropTarget.parentNode.insertBefore(draggedElement, dropTarget);
} else {
dropTarget.parentNode.insertBefore(draggedElement, dropTarget.nextSibling);
}
} else {
const container = document.getElementById('inputContainer');
container.appendChild(draggedElement); // Append to the end if not dropped on a valid target
}
draggedElement.classList.remove('dragging');
}
</script>
</head>
<body>
<div id="form">
<h1>Carbon Emission Scenario Benchmarking</h1>
<div id="description">
Compare and contextualize different emission scenarios: <a href="#" id="expandedDescriptionLink">...more</a>
<span id="expandedDescription">
Carbon emission studies can be complex and significantly influenced by specific factors, such as the energy mix of the electricity grid used. Our <a href="https://github.com/borisruf/carbon-footprint-modeling-tool?tab=readme-ov-file#carbon-footprint-modeling-tool" target="_blank">open data model</a> for carbon footprint scenarios addresses these complexities by offering a transparent and flexible approach to emissions analysis.<br/><br/>
Comparing different scenarios and putting their emissions into context can still be challenging. This benchmark view aims to simplify the process. Use the form below, or <a href="benchmark.html?ids[]=gpt-ruf-mortas-token&ids[]=mistral-small-ruf-mortas-token&factor=10000000&source_ids[]=electricity-eu-27-2020&source_ids[]=electricity-eu-27-2020&title=GPT-3.5%20vs%20Mistral%20Small%20(1M%20tokens)&reference_id=diesel" target="_blank">try an example</a>.
</span>
</div>
<br/>
<form id="benchmarkForm" action="benchmark.html" method="GET">
<div id="inputContainer">
<div class="input-section" draggable="true" ondragstart="drag(event)" ondragover="allowDrop(event)" ondrop="drop(event)">
<div class="form-group">
<label for="scenario_ids[]">Scenario ID <span class="tooltip">?⃝<span class="tooltiptext">Enter the scenario ID you would like to benchmark.</span>:</label>
<input type="text" name="scenario_ids[]" required><br/>
<label for="source_ids[]">Source <span class="tooltip">?⃝<span class="tooltiptext">Optionally, select a carbon factor for the electricity source to override the scenario's default.</span>(optional):</label>
<select id="source_ids" name="source_ids[]">
<option value="">--- Select source ---</option>
<option value="electricity-austria-2020">Austria (2020)</option>
<option value="electricity-belgium-2020">Belgium (2020)</option>
<option value="electricity-bulgaria-2020">Bulgaria (2020)</option>
<option value="electricity-croatia-2020">Croatia (2020)</option>
<option value="electricity-cyprus-2020">Cyprus (2020)</option>
<option value="electricity-czechia-2020">Czechia (2020)</option>
<option value="electricity-denmark-2020">Denmark (2020)</option>
<option value="electricity-estonia-2020">Estonia (2020)</option>
<option value="electricity-eu-27-2020">EU-27 (2020)</option>
<option value="electricity-finland-2020">Finland (2020)</option>
<option value="electricity-france-2020">France (2020)</option>
<option value="electricity-germany-2020">Germany (2020)</option>
<option value="electricity-greece-2020">Greece (2020)</option>
<option value="electricity-hungary-2020">Hungary (2020)</option>
<option value="electricity-ireland-2020">Ireland (2020)</option>
<option value="electricity-italy-2020">Italy (2020)</option>
<option value="electricity-latvia-2020">Latvia (2020)</option>
<option value="electricity-lithuania-2020">Lithuania (2020)</option>
<option value="electricity-luxembourg-2020">Luxembourg (2020)</option>
<option value="electricity-malta-2020">Malta (2020)</option>
<option value="electricity-netherlands-2020">Netherlands (2020)</option>
<option value="electricity-poland-2020">Poland (2020)</option>
<option value="electricity-portugal-2020">Portugal (2020)</option>
<option value="electricity-romania-2020">Romania (2020)</option>
<option value="electricity-slovakia-2020">Slovakia (2020)</option>
<option value="electricity-slovenia-2020">Slovenia (2020)</option>
<option value="electricity-spain-2020">Spain (2020)</option>
<option value="electricity-sweden-2020">Sweden (2020)</option>
<option value="electricity-usa-2020">USA (2020)</option>
</select>
<!-- Remove button is not included here -->
</div>
</div>
</div>
<button type="button" class="addButton" onclick="addInput()">Add scenario</button>
<div class="form-group">
<label>Factor <span class="tooltip">?⃝<span class="tooltiptext">Optionally, choose a multiplier that is applied to all scenarios.</span>(optional):</label>
<input type="number" name="factor">
</div>
<label for="title">Title (optional):</label>
<input type="text" name="title">
<label style="font-weight:bold">Reference <span class="tooltip">?⃝<span class="tooltiptext">Optionally, add a reference scenario of a basic and relatable emission generating activity (e.g. burning diesel) to contextualize the result.</span>(optional)</label>
<div class="input-section">
<div class="form-group">
<label>Scenario ID <span class="tooltip">?⃝<span class="tooltiptext">E.g., "diesel"</span>:</label>
<input type="text" name="reference_id" oninput="toggleReferenceFields(this)">
</div>
<div class="form-group">
<label>Encoded scenario <span class="tooltip">?⃝<span class="tooltiptext">Alternatively, enter an encoded scenario from a URL generated using the "Share via Link" feature available for each scenario in its footer.</span>:</label>
<textarea name="reference_scenario" oninput="toggleReferenceFields(this)"></textarea>
</div>
</div>
<button type="submit">Compare</button>
</form>
<div id="footer">
Based on the carbon footprint data model. <a href="https://github.com/borisruf/carbon-footprint-modeling-tool">Learn more</a>
</div>
</div>
<div id="header">
<h1><span name="title" id="title"></span></h1>
</div>
<div id="arena"></div>
<div id="footer"></div>
<!--------------------------------------------------------------------------------------------------------->
<div class="templates">
<!------------------------------------------------------------------------------------------------->
<div class="data template" name="sample" data-type="object">
<span name="total_emission"></span>
<div class="data" name="bar"></div>
<div name="foot"><a name="title"></a></div>
</div>
<div class="data template" name="reference" data-type="object">
<div class="data" name="bar"><span name="reference_factor"></span></div>
<div name="foot"><a name="title"></a></div>
</div>
</div>
</body>
</html>