Add algorithm docs
This commit is contained in:
parent
8f206340f3
commit
6ee0184f66
|
@ -1,6 +1,6 @@
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
|
||||||
<div class="tab-pane fade show active" id="environmental_impact">
|
<div class="tab-pane fade" id="environmental_impact">
|
||||||
<h5 class="card-title">{% trans 'Environmental Impact Details' %}</h5>
|
<h5 class="card-title">{% trans 'Environmental Impact Details' %}</h5>
|
||||||
<hr />
|
<hr />
|
||||||
|
|
||||||
|
@ -17,4 +17,21 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-8">{{ impact.co2_emissions|default:'0.0' }} kg</div>
|
<div class="col-sm-8">{{ impact.co2_emissions|default:'0.0' }} kg</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<button class="btn btn-outline-primary" type="button" data-bs-toggle="collapse" data-bs-target="#docsCollapse" aria-expanded="false" aria-controls="docsCollapse">
|
||||||
|
{% trans 'Read about the algorithm insights' %}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div class="collapse mt-3" id="docsCollapse">
|
||||||
|
<div class="card card-body">
|
||||||
|
<div class="markdown-content">
|
||||||
|
{{ impact.docs|safe }}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- MathJax for LaTeX rendering -->
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
|
||||||
|
|
8
environmental_impact/algorithms/docs_renderer.py
Normal file
8
environmental_impact/algorithms/docs_renderer.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import markdown
|
||||||
|
|
||||||
|
|
||||||
|
def render_docs(file_path):
|
||||||
|
with open(file_path, 'r') as file:
|
||||||
|
markdown_content = file.read()
|
||||||
|
html_content = markdown.markdown(markdown_content)
|
||||||
|
return html_content
|
|
@ -1,6 +1,8 @@
|
||||||
|
import os
|
||||||
from device.models import Device
|
from device.models import Device
|
||||||
from ..algorithm_interface import EnvironmentImpactAlgorithm
|
from ..algorithm_interface import EnvironmentImpactAlgorithm
|
||||||
from environmental_impact.models import EnvironmentalImpact
|
from environmental_impact.models import EnvironmentalImpact
|
||||||
|
from ..docs_renderer import render_docs
|
||||||
|
|
||||||
|
|
||||||
class DummyEnvironmentalImpactAlgorithm(EnvironmentImpactAlgorithm):
|
class DummyEnvironmentalImpactAlgorithm(EnvironmentImpactAlgorithm):
|
||||||
|
@ -12,7 +14,10 @@ class DummyEnvironmentalImpactAlgorithm(EnvironmentImpactAlgorithm):
|
||||||
power_on_hours = self.get_power_on_hours_from(device)
|
power_on_hours = self.get_power_on_hours_from(device)
|
||||||
energy_kwh = (power_on_hours * avg_watts) / 1000
|
energy_kwh = (power_on_hours * avg_watts) / 1000
|
||||||
co2_emissions = energy_kwh * co2_per_kwh
|
co2_emissions = energy_kwh * co2_per_kwh
|
||||||
return EnvironmentalImpact(co2_emissions=co2_emissions)
|
current_dir = os.path.dirname(__file__)
|
||||||
|
docs_path = os.path.join(current_dir, 'docs.md')
|
||||||
|
docs = render_docs(docs_path)
|
||||||
|
return EnvironmentalImpact(co2_emissions=co2_emissions, docs=docs)
|
||||||
|
|
||||||
def get_power_on_hours_from(self, device: Device) -> int:
|
def get_power_on_hours_from(self, device: Device) -> int:
|
||||||
# TODO how do I check if the device is a legacy workbench? Is there a better way?
|
# TODO how do I check if the device is a legacy workbench? Is there a better way?
|
||||||
|
@ -22,7 +27,8 @@ class DummyEnvironmentalImpactAlgorithm(EnvironmentImpactAlgorithm):
|
||||||
str_time = storage_components.get('time of used', -1)
|
str_time = storage_components.get('time of used', -1)
|
||||||
else:
|
else:
|
||||||
str_time = ""
|
str_time = ""
|
||||||
uptime_in_hours = self.convert_str_time_to_hours(str_time, is_legacy_workbench)
|
uptime_in_hours = self.convert_str_time_to_hours(
|
||||||
|
str_time, is_legacy_workbench)
|
||||||
return uptime_in_hours
|
return uptime_in_hours
|
||||||
|
|
||||||
def convert_str_time_to_hours(self, time_str: str, is_legacy_workbench: bool) -> int:
|
def convert_str_time_to_hours(self, time_str: str, is_legacy_workbench: bool) -> int:
|
||||||
|
|
|
@ -6,3 +6,4 @@ from django.db import models
|
||||||
class EnvironmentalImpact:
|
class EnvironmentalImpact:
|
||||||
carbon_saved: float = 0.0
|
carbon_saved: float = 0.0
|
||||||
co2_emissions: float = 0.0
|
co2_emissions: float = 0.0
|
||||||
|
docs: str = ""
|
||||||
|
|
Loading…
Reference in a new issue