diff --git a/device/environmental_impact/calculator.py b/device/environmental_impact/calculator.py deleted file mode 100644 index dd79432..0000000 --- a/device/environmental_impact/calculator.py +++ /dev/null @@ -1,10 +0,0 @@ -from dataclasses import dataclass - - -@dataclass -class EnvironmentalImpact: - carbon_saved: float - - -def get_device_environmental_impact() -> EnvironmentalImpact: - return EnvironmentalImpact(carbon_saved=225.0) diff --git a/device/templates/details.html b/device/templates/details.html index 0a843b7..e881bc4 100644 --- a/device/templates/details.html +++ b/device/templates/details.html @@ -29,16 +29,16 @@ - {% if dpps %} - - {% endif %} + {% if dpps %} + + {% endif %} @@ -244,15 +244,15 @@ {% endfor %} -
+ +
- + - {% trans 'Export to PDF' %} - + {% trans 'Export to PDF' %} +
- {% comment %}
Environmental Impact Assessment
{% endcomment %}
@@ -267,40 +267,59 @@
- {% comment %}
+ +
+
+
+
+ +
+
Carbon Consumed
+

{{ impact.co2_emissions }}

+

kg CO₂e consumed

+
+
+
+ +
-
Whatever other metric we might wanna show
+
Additional Impact Metric

85%

whatever

-
{% endcomment %} -
-
-
Impact Details
+
+
-
- - - - - - - -
Manufacturing Impact Avoided - {{ impact.carbon_saved }} kg CO₂e -
- Based on average laptop manufacturing emissions -
-
+
+
+
+
+
Impact Details
+
+ + + + + + + +
Manufacturing Impact Avoided + {{ impact.carbon_saved }} kg CO₂e +
+ Based on average laptop manufacturing emissions +
+
-
-
Calculation Method
- Based on industry standards X Y and Z +
+
Calculation Method
+ Based on industry standards X Y and Z +
+
@@ -308,22 +327,22 @@
{% if dpps %} -
-
{% trans 'List of dpps' %}
-
- {% for d in dpps %} -
-
- {{ d.timestamp }} - {{ d.type }} +
+
{% trans 'List of dpps' %}
+
+ {% for d in dpps %} +
+
+ {{ d.timestamp }} + {{ d.type }} +
+

+ {{ d.signature }} +

-

- {{ d.signature }} -

-
- {% endfor %} + {% endfor %} +
-
{% endif %}
{% endblock %} diff --git a/device/urls.py b/device/urls.py index 78922c1..9d38ff5 100644 --- a/device/urls.py +++ b/device/urls.py @@ -9,8 +9,5 @@ urlpatterns = [ path("/", views.DetailsView.as_view(), name="details"), path("/annotation/add", views.AddAnnotationView.as_view(), name="add_annotation"), path("/document/add", views.AddDocumentView.as_view(), name="add_document"), - path("/public/", views.PublicDeviceWebView.as_view(), name="device_web"), - path('/export-environmental-impact-pdf/', - views.ExportEnvironmentalImpactPDF.as_view(), name='export_environmental_impact_pdf'), - + path("/public/", views.PublicDeviceWebView.as_view(), name="device_web") ] diff --git a/device/views.py b/device/views.py index c8ab8ed..13bb273 100644 --- a/device/views.py +++ b/device/views.py @@ -14,7 +14,7 @@ from evidence.models import Annotation from lot.models import LotTag from device.models import Device from device.forms import DeviceFormSet -from device.environmental_impact.calculator import get_device_environmental_impact +from environmental_impact.calculator import get_device_environmental_impact if settings.DPP: from dpp.models import Proof from dpp.api_dlt import PROOF_TYPE @@ -115,7 +115,7 @@ class DetailsView(DashboardView, TemplateView): 'object': self.object, 'snapshot': self.object.get_last_evidence(), 'lot_tags': lot_tags, - 'impact': get_device_environmental_impact(), + 'impact': get_device_environmental_impact(self.object), 'dpps': dpps, }) return context @@ -177,10 +177,6 @@ class PublicDeviceWebView(TemplateView): return JsonResponse(device_data) -class ExportEnvironmentalImpactPDF(DashboardView, TemplateView): - pass - - class AddAnnotationView(DashboardView, CreateView): template_name = "new_annotation.html" title = _("New annotation") diff --git a/dhub/settings.py b/dhub/settings.py index 2aca54e..682299e 100644 --- a/dhub/settings.py +++ b/dhub/settings.py @@ -89,6 +89,7 @@ INSTALLED_APPS = [ "dashboard", "admin", "api", + "environmental_impact" ] DPP = config("DPP", default=False, cast=bool) diff --git a/device/environmental_impact/__init__.py b/environmental_impact/__init__.py similarity index 100% rename from device/environmental_impact/__init__.py rename to environmental_impact/__init__.py diff --git a/environmental_impact/admin.py b/environmental_impact/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/environmental_impact/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/environmental_impact/apps.py b/environmental_impact/apps.py new file mode 100644 index 0000000..5d0d987 --- /dev/null +++ b/environmental_impact/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EnvironmentalImpactConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "environmental_impact" \ No newline at end of file diff --git a/environmental_impact/calculator.py b/environmental_impact/calculator.py new file mode 100644 index 0000000..a348b6b --- /dev/null +++ b/environmental_impact/calculator.py @@ -0,0 +1,30 @@ +from dataclasses import dataclass +from device.models import Device + + +@dataclass +class EnvironmentalImpact: + carbon_saved: float = 0.0 + co2_emissions: float = 0.0 + + +def get_device_environmental_impact(device: Device) -> EnvironmentalImpact: + avg_watts = 40 # Arbitrary laptop average consumption + power_on_hours = get_power_on_hours_from(device) + energy_kwh = (power_on_hours * avg_watts) / 1000 + # CO2 emissions based on global average electricity mix + co2_per_kwh = 0.475 + co2_emissions = energy_kwh * co2_per_kwh + return EnvironmentalImpact(co2_emissions=co2_emissions) + + +def get_power_on_hours_from(device: Device) -> int: + storage_components = device.components[9] + str_time = storage_components.get('time of used', -1) + uptime_in_hours = convert_str_time_to_hours(str_time) + return uptime_in_hours + + +def convert_str_time_to_hours(time_str: str) -> int: + multipliers = {'y': 365 * 24, 'd': 24, 'h': 1} + return sum(int(part[:-1]) * multipliers[part[-1]] for part in time_str.split()) diff --git a/environmental_impact/migrations/__init__.py b/environmental_impact/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/environmental_impact/models.py b/environmental_impact/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/environmental_impact/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/environmental_impact/tests.py b/environmental_impact/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/environmental_impact/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/environmental_impact/views.py b/environmental_impact/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/environmental_impact/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.