Skip to content

Commit

Permalink
export account status to mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Nov 30, 2022
1 parent 30ebc92 commit 0db02dc
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
Create Date: 2022-11-21 00:40:16.407209
"""
from alembic import op
import sqlalchemy as sa

from alembic import op
from sqlalchemy.sql import true

# revision identifiers, used by Alembic.
revision = '09d887b265a5'
Expand All @@ -17,8 +17,12 @@


def upgrade() -> None:
pass
op.add_column('usage_points', sa.Column('progress', sa.Integer, nullable=False, server_default="0"))
op.add_column('usage_points', sa.Column('progress_status', sa.Text, nullable=False, server_default=""))
op.add_column('usage_points', sa.Column('enable', sa.Boolean, nullable=False, server_default=true()))


def downgrade() -> None:
pass
op.drop_column('usage_points', 'progress')
op.drop_column('usage_points', 'progress_status')
op.drop_column('usage_points', 'enable')

This file was deleted.

29 changes: 29 additions & 0 deletions app/db_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ class UsagePoints(Base):
nullable=False,
default=True
)
consentement_expiration = Column(DateTime,
nullable=True,
)
call_number = Column(Integer,
nullable=True,
)
quota_reached = Column(Boolean,
nullable=True,
)
quota_limit = Column(Integer,
nullable=True,
)
quota_reset_at = Column(DateTime,
nullable=True,
)
last_call = Column(DateTime,
nullable=True,
)
ban = Column(Boolean,
nullable=True,
)

relation_addressess = relationship("Addresses", back_populates="usage_point")
relation_contract = relationship("Contracts", back_populates="usage_point")
Expand All @@ -135,6 +156,7 @@ def __repr__(self):
return f"UsagePoints(" \
f"usage_point_id={self.usage_point_id!r}, " \
f"name={self.name!r}, " \
f"cache={self.cache!r}, " \
f"consumption={self.consumption!r}, " \
f"consumption_detail={self.consumption_detail!r}, " \
f"production={self.production!r}, " \
Expand All @@ -157,6 +179,13 @@ def __repr__(self):
f"progress={self.progress!r}, " \
f"progress_status={self.progress_status!r}, " \
f"enable={self.enable!r}, " \
f"consentement_expiration={self.consentement_expiration!r}, " \
f"call_number={self.call_number!r}, " \
f"quota_reached={self.quota_reached!r}, " \
f"quota_limit={self.quota_limit!r}, " \
f"quota_reset_at={self.quota_reset_at!r}, " \
f"last_call={self.last_call!r}, " \
f"ban={self.ban!r}, " \
f")"


Expand Down
23 changes: 1 addition & 22 deletions app/models/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,22 @@ def reset_all_data(self):
Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).reset()
app.LOG.title(f"[{self.usage_point_id}] Reset de la consommation détaillée.")
Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).reset()
app.LOG.title(f"[{self.usage_point_id}] Reset de la production journalière.")
Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).reset()
app.LOG.title(f"[{self.usage_point_id}] Reset de la production détaillée.")
Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).reset()
return {
Expand All @@ -76,30 +72,25 @@ def reset_data(self, target, date):
app.LOG.title(f"[{self.usage_point_id}] Reset de la consommation journalière du {date}:")
result["consumption"] = Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
usage_point_id=self.usage_point_id
).reset(date)
elif target == "consumption_detail":
app.LOG.title(f"[{self.usage_point_id}] Reset de la consommation détaillé du {date}:")
result["consumption_detail"] = Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).reset(date)
elif target == "production":
app.LOG.title(f"[{self.usage_point_id}] Reset de la production journalière du {date}:")
result["production"] = Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).reset(date)
elif target == "production_detail":
app.LOG.title(f"[{self.usage_point_id}] Reset de la production détaillé du {date}:")
result["production_detail"] = Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).reset(date)
else:
Expand Down Expand Up @@ -129,23 +120,20 @@ def fetch(self, target, date):
result["consumption"] = Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).fetch(date)
elif target == "consumption_detail":
if hasattr(self.usage_point_config, "consumption_detail") and self.usage_point_config.consumption_detail:
app.LOG.title(f"[{self.usage_point_id}] Importation de la consommation détaillé du {date}:")
result["consumption_detail"] = Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).fetch(date)
elif target == "production":
if hasattr(self.usage_point_config, "production") and self.usage_point_config.production:
app.LOG.title(f"[{self.usage_point_id}] Importation de la production journalière du {date}:")
result["production"] = Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).fetch(date)
elif target == "production_detail":
Expand All @@ -154,7 +142,6 @@ def fetch(self, target, date):
result["production_detail"] = Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).fetch(date)
else:
Expand Down Expand Up @@ -192,23 +179,20 @@ def blacklist(self, target, date):
result["consumption"] = Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).blacklist(date, True)
if target == "consumption_detail":
if hasattr(self.usage_point_config, "consumption_detail") and self.usage_point_config.consumption_detail:
app.LOG.title(f"[{self.usage_point_id}] Blacklist de la consommation détaillé du {date}:")
result["consumption_detail"] = Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).blacklist(date, True)
elif target == "production":
if hasattr(self.usage_point_config, "production") and self.usage_point_config.production:
app.LOG.title(f"[{self.usage_point_id}] Blacklist de la production journalière du {date}:")
result["production"] = Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).blacklist(date, True)
elif target == "production_detail":
Expand All @@ -217,7 +201,6 @@ def blacklist(self, target, date):
result["production_detail"] = Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).blacklist(date, True)
else:
Expand Down Expand Up @@ -247,23 +230,20 @@ def whitelist(self, target, date):
result["consumption"] = Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).blacklist(date, False)
elif target == "consumption_detail":
if hasattr(self.usage_point_config, "consumption_detail") and self.usage_point_config.consumption_detail:
app.LOG.title(f"[{self.usage_point_id}] Whitelist de la consommation détaillé du {date}:")
result["consumption_detail"] = Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
).blacklist(date, False)
elif target == "production":
if hasattr(self.usage_point_config, "production") and self.usage_point_config.production:
app.LOG.title(f"[{self.usage_point_id}] Whitelist de la production journalière du {date}:")
result["production"] = Daily(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).blacklist(date, False)
elif target == "production_detail":
Expand All @@ -272,7 +252,6 @@ def whitelist(self, target, date):
result["production_detail"] = Detail(
headers=self.headers,
usage_point_id=self.usage_point_id,
config=self.usage_point_config,
measure_type="production"
).blacklist(date, False)
else:
Expand Down
40 changes: 33 additions & 7 deletions app/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import os
from datetime import datetime, timedelta

from sqlalchemy import (create_engine, delete, inspect, select)
from sqlalchemy.orm import sessionmaker

from config import MAX_IMPORT_TRY
from db_schema import (
Config,
Contracts,
Expand All @@ -13,13 +17,9 @@
ConsumptionDetail,
ProductionDetail,
)
from dependencies import str2bool
from models.config import get_version
from models.log import Log
from sqlalchemy import (create_engine, delete, inspect, select)
from sqlalchemy.orm import sessionmaker

from config import MAX_IMPORT_TRY
from dependencies import str2bool

LOG = Log()

Expand Down Expand Up @@ -275,7 +275,31 @@ def progress(self, usage_point_id, increment):
usage_points = self.session.scalars(query).one_or_none()
usage_points.progress = usage_points.progress + increment

## ----------------------------------------------------------------------------------------------------------------
def usage_point_update(self,
usage_point_id,
consentement_expiration=datetime.now(),
call_number=0,
quota_reached=False,
quota_limit=None,
quota_reset_at=None,
last_call=None,
ban=None
):
query = (
select(UsagePoints)
.where(UsagePoints.usage_point_id == usage_point_id)
)
usage_points = self.session.scalars(query).one_or_none()
usage_points.consentement_expiration = consentement_expiration
usage_points.call_number = call_number
usage_points.quota_reached = quota_reached
usage_points.quota_limit = quota_limit
usage_points.quota_reset_at = quota_reset_at
usage_points.last_call = last_call
usage_points.ban = ban

## ----------------------------------------------------------------------------------------------------------------

## ADDRESSES
## ----------------------------------------------------------------------------------------------------------------
def get_addresse(self, usage_point_id):
Expand Down Expand Up @@ -581,7 +605,8 @@ def get_daily(self, usage_point_id, begin, end, measurement_direction="consumpti
}
return result

def insert_daily(self, usage_point_id, date, value, blacklist=0, fail_count=0, measurement_direction="consumption"):
def insert_daily(self, usage_point_id, date, value, blacklist=0, fail_count=0,
measurement_direction="consumption"):
if measurement_direction == "consumption":
table = ConsumptionDaily
relation = UsagePoints.relation_consumption_daily
Expand Down Expand Up @@ -919,5 +944,6 @@ def get_detail_date_range(self, usage_point_id):
"end": self.get_detail_first_date(usage_point_id)
}


os.system("cd /app; alembic upgrade head")
Database().init_database()
17 changes: 17 additions & 0 deletions app/models/export_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ def __init__(self, usage_point_id, measurement_direction="consumption"):
self.measurement_direction = measurement_direction
self.date_format = "%Y-%m-%d"

def status(self):
app.LOG.title(f"[{self.usage_point_id}] Status du compte.")
usage_point_id_config = app.DB.get_usage_point(self.usage_point_id)
# consentement_expiration_date = usage_point_id_config.consentement_expiration.strftime("%Y-%m-%d %H:%M:%S")
consentement_expiration = {
f"{self.usage_point_id}/status/consentement_expiration": usage_point_id_config.consentement_expiration.strftime("%Y-%m-%d %H:%M:%S"),
f"{self.usage_point_id}/status/call_number": usage_point_id_config.call_number,
f"{self.usage_point_id}/status/quota_reached": usage_point_id_config.quota_reached,
f"{self.usage_point_id}/status/quota_limit": usage_point_id_config.quota_limit,
f"{self.usage_point_id}/status/quota_reset_at": usage_point_id_config.quota_reset_at.strftime("%Y-%m-%d %H:%M:%S"),
f"{self.usage_point_id}/status/last_call": usage_point_id_config.last_call.strftime("%Y-%m-%d %H:%M:%S"),
f"{self.usage_point_id}/status/ban": usage_point_id_config.ban
}
app.MQTT.publish_multiple(consentement_expiration)
app.LOG.log(" => Finish")


def contract(self):
app.LOG.title(f"[{self.usage_point_id}] Exportation de données dans MQTT.")

Expand Down
Loading

0 comments on commit 0db02dc

Please sign in to comment.