Very initial impleentation of co2 consumption

This commit is contained in:
Sergio Giménez Antón 2024-12-17 09:58:20 +01:00
parent 60ccbec369
commit f9c9c9dd7c
13 changed files with 119 additions and 68 deletions

View file

@ -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)

View file

@ -38,7 +38,7 @@
<a class="nav-link" href="{% url 'device:device_web' object.id %}" target="_blank">Web</a>
</li>
<li class="nav-item">
<a href="#enviromental_impact" class="nav-link" data-bs-toggle="tab" data-bs-target="#enviromental_impact">{% trans 'Enviromental impact' %}</a>
<a href="#environmental_impact" class="nav-link" data-bs-toggle="tab" data-bs-target="#environmental_impact">{% trans 'Environmental impact' %}</a>
</li>
</ul>
</div>
@ -244,15 +244,15 @@
{% endfor %}
</div>
</div>
<div class="tab-pane fade" id="enviromental_impact">
<div class="tab-pane fade" id="environmental_impact">
<div class="container-fluid py-3">
<div class="d-flex justify-content-end mb-3">
<a href="{% url 'device:export_environmental_impact_pdf' object.pk %}" class="btn btn-success">
<a class="btn btn-success">
<i class="bi bi-file-earmark-pdf"></i>
{% trans 'Export to PDF' %}
</a>
</div>
{% comment %} <h5 class="card-title text-success">Environmental Impact Assessment</h5> {% endcomment %}
<div class="row g-4 mb-4">
<div class="col-md-4">
@ -267,22 +267,39 @@
</div>
</div>
</div>
{% comment %} <div class="col-md-4">
<div class="col-md-4">
<div class="card h-100 border-danger">
<div class="card-body text-center">
<div class="mb-3">
<i class="bi bi-cloud-fill text-danger" style="font-size: 2rem;"></i>
</div>
<h5 class="card-title text-danger">Carbon Consumed</h5>
<h2 class="mb-2">{{ impact.co2_emissions }}</h2>
<p class="card-text text-muted">kg CO₂e consumed</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card h-100 border-success">
<div class="card-body text-center">
<div class="mb-3">
<i class="bi bi-recycle text-success" style="font-size: 2rem;"></i>
</div>
<h5 class="card-title text-success">Whatever other metric we might wanna show</h5>
<h5 class="card-title text-success">Additional Impact Metric</h5>
<h2 class="mb-2">85%</h2>
<p class="card-text text-muted">whatever</p>
</div>
</div>
</div> {% endcomment %}
</div>
</div>
<div class="row">
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title">Impact Details</h5>
<div class="table-responsive">
<table class="table table-bordered">
<tbody>
@ -306,6 +323,8 @@
</div>
</div>
</div>
</div>
</div>
{% if dpps %}
<div class="tab-pane fade" id="dpps">

View file

@ -9,8 +9,5 @@ urlpatterns = [
path("<str:pk>/", views.DetailsView.as_view(), name="details"),
path("<str:pk>/annotation/add", views.AddAnnotationView.as_view(), name="add_annotation"),
path("<str:pk>/document/add", views.AddDocumentView.as_view(), name="add_document"),
path("<str:pk>/public/", views.PublicDeviceWebView.as_view(), name="device_web"),
path('<str:pk>/export-environmental-impact-pdf/',
views.ExportEnvironmentalImpactPDF.as_view(), name='export_environmental_impact_pdf'),
path("<str:pk>/public/", views.PublicDeviceWebView.as_view(), name="device_web")
]

View file

@ -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")

View file

@ -89,6 +89,7 @@ INSTALLED_APPS = [
"dashboard",
"admin",
"api",
"environmental_impact"
]
DPP = config("DPP", default=False, cast=bool)

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class EnvironmentalImpactConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "environmental_impact"

View file

@ -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())

View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.