Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Dec 10, 2023
1 parent 09f2c4d commit fb657f9
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 18 deletions.
4 changes: 4 additions & 0 deletions app/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ def filter(self, record: logging.LogRecord) -> bool:
else:
method = "SYNCHRONOUS"

if "scheme" not in INFLUXDB_CONFIG:
INFLUXDB_CONFIG["scheme"] = "http"

write_options = []
if "batching_options" in INFLUXDB_CONFIG:
write_options = INFLUXDB_CONFIG["batching_options"]
INFLUXDB = InfluxDB(
scheme=INFLUXDB_CONFIG["scheme"],
hostname=INFLUXDB_CONFIG["hostname"],
port=INFLUXDB_CONFIG["port"],
token=INFLUXDB_CONFIG["token"],
Expand Down
4 changes: 2 additions & 2 deletions app/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def __init__(self, path="/data"):
self.file = "config.yaml"
self.path_file = f"{self.path}/{self.file}"
self.config = {}
self.mandatory_parameters = {}
self.default_port = 5000
self.mandatory_parameters = {}
# "myelectricaldata": {
# "pdl": {
# "enable": True,
Expand Down Expand Up @@ -51,7 +51,7 @@ def __init__(self, path="/data"):
"debug": False,
"log2file": False,
"tempo": {
"enable": True,
"enable": False,
},
"myelectricaldata": {
"pdl": {
Expand Down
21 changes: 12 additions & 9 deletions app/models/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

import influxdb_client
from dateutil.tz import tzlocal
from dependencies import title, separator, separator_warning
from influxdb_client.client.util import date_utils
from influxdb_client.client.util.date_utils import DateHelper
from influxdb_client.client.write_api import ASYNCHRONOUS, SYNCHRONOUS

from dependencies import title, separator, separator_warning


class InfluxDB:

def __init__(
self,
scheme: str,
hostname: str,
port: int,
token: str,
Expand All @@ -24,6 +24,7 @@ def __init__(
):
if write_options is None:
write_options = {}
self.scheme = scheme
self.hostname = hostname
self.port = port
self.token = token
Expand Down Expand Up @@ -85,7 +86,7 @@ def connect(self):
logging.info(f"Connect to InfluxDB {self.hostname}:{self.port}")
date_utils.date_helper = DateHelper(timezone=tzlocal())
self.influxdb = influxdb_client.InfluxDBClient(
url=f"http://{self.hostname}:{self.port}",
url=f"{self.scheme}://{self.hostname}:{self.port}",
token=self.token,
org=self.org,
timeout="600000"
Expand All @@ -94,12 +95,14 @@ def connect(self):
if health.status == "pass":
title("Connection success")
else:
logging.critical([
"Impossible de se connecter à la base influxdb.",
"",
"Vous pouvez récupérer un exemple ici :",
"https://github.com/m4dm4rtig4n/enedisgateway2mqtt#configuration-file"
])
logging.critical("""
Impossible de se connecter à la base influxdb.
Vous pouvez récupérer un exemple ici :
https://github.com/m4dm4rtig4n/enedisgateway2mqtt#configuration-file
""")
exit(1)

title(f"Méthode d'importation : {self.method.upper()}")
if self.method.upper() == "ASYNCHRONOUS":
Expand Down
2 changes: 2 additions & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ art==5.7
# via myelectricaldata_import (pyproject.toml)
async-timeout==4.0.3
# via aiohttp
asyncio==3.4.3
# via myelectricaldata_import (pyproject.toml)
attrs==23.1.0
# via aiohttp
certifi==2022.9.24
Expand Down
67 changes: 66 additions & 1 deletion app/templates/usage_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from templates.models.menu import Menu
from templates.models.sidemenu import SideMenu
from templates.models.usage_point_select import UsagePointSelect
from models.stat import Stat
from init import CONFIG, DB


Expand Down Expand Up @@ -280,7 +281,8 @@ def display(self):
if hasattr(self.usage_point_config, "consumption") and self.usage_point_config.consumption:
self.generate_data("consumption")
self.consumption()
recap_consumption = self.recap(data=self.recap_consumption_data)
# recap_consumption = self.recap(data=self.recap_consumption_data)
recap_consumption = self.recapv2()
body += "<h2>Consommation</h2>"
body += str(recap_consumption)
body += '<div id="chart_daily_consumption"></div>'
Expand Down Expand Up @@ -907,3 +909,66 @@ def recap(self, data):
else:
body = "Pas de données."
return body

def recapv2(self, measurement_direction="consumption"):

idx = 0
finish = False
output_data = {
"years": {},
"linear": {}
}
body_year = ""
body_linear = ""
while not finish:
linear_data = Stat(self.usage_point_id, measurement_direction).get_year_linear(idx)
idx += 1
if linear_data["value"] == 0:
finish = True
else:
year = linear_data["end"].split("-")[0]
year_data = Stat(self.usage_point_id, measurement_direction).get_year(int(year))
output_data["years"][year] = year_data['value']
output_data["linear"][year] = {
"begin": linear_data["begin"],
"end": linear_data["end"],
"value": linear_data["value"]
}

for year, value in output_data["years"].items():
body_year += f"""
<td class="table_recap_data">
<div class='recap_years_title'>{year}</div>
<div class='recap_years_value'>{round(value / 1000)} kWh</div>
</td>
"""
for year, data in output_data["linear"].items():
last_year = str(int(year)-1)
data_last_years = 0
if last_year in output_data["linear"]:
data_last_years = round((100 * int(data["value"])) / int(output_data["linear"][last_year]["value"]) - 100, 2)
if data_last_years >= 0:
if data_last_years == 0:
data_last_years_class = "blue"
else:
data_last_years_class = "red"
data_last_years = f"+{data_last_years}"
else:
data_last_years_class = "green"
body_linear += f"""
<td class="table_recap_data">
<div class='recap_years_title'>{data["begin"]} => {data["end"]}</div>
<div class='recap_years_value'>{round(data["value"] / 1000)} kWh</div>
<div class='recap_years_value {data_last_years_class}'><b>{data_last_years}%</b></div>
</td>
"""
body = '<table class="table_recap"><tr>'
body += '<th class="table_recap_header">Annuel</th>'
body += body_year
body += "</tr>"
body += "<tr>"
body += '<th class="table_recap_header">Annuel linéaire</th>'
body += body_linear
body += "</tr>"
body += "</table>"
return body
3 changes: 2 additions & 1 deletion config.exemple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ home_assistant_ws: # FOR ENERGY TAB
token: HOME_ASSISTANT_TOKEN_GENERATE_IN_PROFILE_TABS_(BOTTOM)
url: myhomeassistant.domain.fr
ssl:
gateway: True
gateway: true
certfile: ""
keyfile: ""
influxdb:
enable: true
scheme: http
hostname: influxdb
port: 8086
token: myelectricaldata
Expand Down
6 changes: 3 additions & 3 deletions dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from InquirerPy import inquirer, prompt
from InquirerPy.base import Choice

docker_compose = "docker-compose -f dev/docker-compose.dev.yaml"
docker_compose_test = "docker-compose -f dev/docker-compose.test.yaml"
docker_compose = "docker compose -f dev/docker-compose.dev.yaml"
docker_compose_test = "docker compose -f dev/docker-compose.test.yaml"


def cmd(cmd, path="./"):
Expand Down Expand Up @@ -83,7 +83,7 @@ def run(dev=False, debug=False, test=False):
else:
compose = docker_compose
command = (
f"{compose} run -p 5000:5000 "
f"{compose} run -p 8080:5000 "
f"{mode_debug} {mode_dev} myelectricaldata_import"
)
logging.info(command)
Expand Down
2 changes: 1 addition & 1 deletion dev/docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
environment:
TZ: Europe/Paris
ports:
- '5000:5001'
- '5000:5000'
links:
- influxdb
- mosquitto
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ services:
environment:
TZ: Europe/Paris
ports:
- '5000:5000'
- 5000:5000

0 comments on commit fb657f9

Please sign in to comment.