Skip to content

Commit

Permalink
fix unknow value in config.yaml, force float to export value in influxdb
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Dec 4, 2022
1 parent 2a01eb2 commit 786fffc
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 93 deletions.
262 changes: 178 additions & 84 deletions app/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,104 +203,194 @@ def set_usage_point(self, usage_point_id, data):
.where(UsagePoints.usage_point_id == usage_point_id)
)
usage_points = self.session.scalars(query).one_or_none()
if "enable" in data:
enable = data["enable"]
else:
enable = True
if "name" in data:
name = data["name"]
else:
name = ""
if "cache" in data:
cache = data["cache"]
else:
cache = True
if "consumption" in data:
consumption = data["consumption"]
else:
consumption = True
if "consumption_detail" in data:
consumption_detail = data["consumption_detail"]
else:
consumption_detail = True
if "production" in data:
production = data["production"]
else:
production = False
if "production_detail" in data:
production_detail = data["production_detail"]
else:
production_detail = False
if "production_price" in data:
production_price = data["production_price"]
else:
production_price = 0
if "consumption_price_base" in data:
consumption_price_base = data["consumption_price_base"]
else:
consumption_price_base = 0
if "consumption_price_hc" in data:
consumption_price_hc = data["consumption_price_hc"]
else:
consumption_price_hc = 0
if "consumption_price_hp" in data:
consumption_price_hp = data["consumption_price_hp"]
else:
consumption_price_hp = 0
if "offpeak_hours_0" in data:
offpeak_hours_0 = data["offpeak_hours_0"]
else:
offpeak_hours_0 = ""
if "offpeak_hours_1" in data:
offpeak_hours_1 = data["offpeak_hours_1"]
else:
offpeak_hours_1 = ""
if "offpeak_hours_2" in data:
offpeak_hours_2 = data["offpeak_hours_2"]
else:
offpeak_hours_2 = ""
if "offpeak_hours_3" in data:
offpeak_hours_3 = data["offpeak_hours_3"]
else:
offpeak_hours_3 = ""
if "offpeak_hours_4" in data:
offpeak_hours_4 = data["offpeak_hours_4"]
else:
offpeak_hours_4 = ""
if "offpeak_hours_5" in data:
offpeak_hours_5 = data["offpeak_hours_5"]
else:
offpeak_hours_5 = ""
if "offpeak_hours_6" in data:
offpeak_hours_6 = data["offpeak_hours_6"]
else:
offpeak_hours_6 = ""
if "plan" in data:
plan = data["plan"]
else:
plan = "BASE"
if "refresh_addresse" in data:
refresh_addresse = data["refresh_addresse"]
else:
refresh_addresse = False
if "refresh_contract" in data:
refresh_contract = data["refresh_contract"]
else:
refresh_contract = False
if "token" in data:
token = data["token"]
else:
token = ""
progress = 0
if "progress" in data:
progress = data["progress"]
progress_status = ""
if "progress_status" in data:
progress = data["progress_status"]
# progress_status = data["progress_status"] if "progress_status" in data else progress_status = 0
if usage_points is not None:
usage_points.name = data["name"]
usage_points.cache = str2bool(data["cache"])
usage_points.consumption = str2bool(data["consumption"])
usage_points.consumption_detail = str2bool(data["consumption_detail"])
usage_points.production = str2bool(data["production"])
usage_points.production_detail = str2bool(data["production_detail"])
usage_points.production_price = data["production_price"]
usage_points.consumption_price_base = data["consumption_price_base"]
usage_points.consumption_price_hc = data["consumption_price_hc"]
usage_points.consumption_price_hp = data["consumption_price_hp"]
usage_points.offpeak_hours_0 = data["offpeak_hours_0"]
usage_points.offpeak_hours_1 = data["offpeak_hours_1"]
usage_points.offpeak_hours_2 = data["offpeak_hours_2"]
usage_points.offpeak_hours_3 = data["offpeak_hours_3"]
usage_points.offpeak_hours_4 = data["offpeak_hours_4"]
usage_points.offpeak_hours_5 = data["offpeak_hours_5"]
usage_points.offpeak_hours_6 = data["offpeak_hours_6"]
usage_points.plan = data["plan"]
usage_points.refresh_addresse = str2bool(data["refresh_addresse"])
usage_points.refresh_contract = str2bool(data["refresh_contract"])
usage_points.token = data["token"]
usage_points.progress = progress
usage_points.progress_status = progress_status
usage_points.enable = str2bool(data["enable"])
if "consumption_max_date" in data:
if not data["consumption_max_date"] or data["consumption_max_date"] is None:
usage_points.consumption_max_date = None
else:
consumption_max_date = data["consumption_max_date"]
if isinstance(consumption_max_date, datetime):
usage_points.consumption_max_date = consumption_max_date
else:
usage_points.consumption_max_date = datetime.strptime(consumption_max_date, "%Y-%m-%d")
progress_status = data["progress_status"]
consumption_max_date = None
if "consumption_max_date" in data:
if not data["consumption_max_date"] or data["consumption_max_date"] is None:
consumption_max_date = None
else:
consumption_max_date = data["consumption_max_date"]
if not isinstance(consumption_max_date, datetime):
consumption_max_date = datetime.strptime(consumption_max_date, "%Y-%m-%d")
consumption_detail_max_date = None
if "consumption_detail_max_date" in data:
if "consumption_detail_max_date" in data:
if "consumption_detail_max_date" in data:
if not data["consumption_detail_max_date"] or data["consumption_detail_max_date"] is None:
usage_points.consumption_detail_max_date = None
else:
consumption_detail_max_date = data["consumption_detail_max_date"]
if isinstance(consumption_detail_max_date, datetime):
usage_points.consumption_detail_max_date = consumption_detail_max_date
else:
usage_points.consumption_detail_max_date = datetime.strptime(consumption_detail_max_date, "%Y-%m-%d")
if "production_max_date" in data:
if not data["production_max_date"] or data["production_max_date"] is None:
usage_points.production_max_date = None
if not data["consumption_detail_max_date"] or data["consumption_detail_max_date"] is None:
consumption_detail_max_date = None
else:
production_max_date = data["production_max_date"]
if isinstance(production_max_date, datetime):
usage_points.production_max_date = production_max_date
else:
usage_points.production_max_date = datetime.strptime(production_max_date, "%Y-%m-%d")
if "production_detail_max_date" in data:
if not data["production_detail_max_date"] or data["production_detail_max_date"] is None:
usage_points.production_detail_max_date = None
consumption_detail_max_date = data["consumption_detail_max_date"]
if not isinstance(consumption_detail_max_date, datetime):
consumption_detail_max_date = datetime.strptime(consumption_detail_max_date,"%Y-%m-%d")
production_max_date = None
if "production_max_date" in data:
if not data["production_max_date"] or data["production_max_date"] is None:
production_max_date = None
else:
production_max_date = data["production_max_date"]
if not isinstance(production_max_date, datetime):
production_max_date = datetime.strptime(production_max_date, "%Y-%m-%d")
production_detail_max_date = None
if "production_detail_max_date" in data:
if not data["production_detail_max_date"] or data["production_detail_max_date"] is None:
production_detail_max_date = None
else:
production_detail_max_date = data["production_detail_max_date"]
if isinstance(production_detail_max_date, datetime):
production_detail_max_date = production_detail_max_date
else:
production_detail_max_date = data["production_detail_max_date"]
if isinstance(production_detail_max_date, datetime):
usage_points.production_detail_max_date = production_detail_max_date
else:
usage_points.production_detail_max_date = datetime.strptime(production_detail_max_date, "%Y-%m-%d")
production_detail_max_date = datetime.strptime(production_detail_max_date, "%Y-%m-%d")
if usage_points is not None:
usage_points.enable = str2bool(enable)
usage_points.name = name
usage_points.cache = str2bool(cache)
usage_points.consumption = str2bool(consumption)
usage_points.consumption_detail = str2bool(consumption_detail)
usage_points.production = str2bool(production)
usage_points.production_detail = str2bool(production_detail)
usage_points.production_price = production_price
usage_points.consumption_price_base = consumption_price_base
usage_points.consumption_price_hc = consumption_price_hc
usage_points.consumption_price_hp = consumption_price_hp
usage_points.offpeak_hours_0 = offpeak_hours_0
usage_points.offpeak_hours_1 = offpeak_hours_1
usage_points.offpeak_hours_2 = offpeak_hours_2
usage_points.offpeak_hours_3 = offpeak_hours_3
usage_points.offpeak_hours_4 = offpeak_hours_4
usage_points.offpeak_hours_5 = offpeak_hours_5
usage_points.offpeak_hours_6 = offpeak_hours_6
usage_points.offpeak_hours_6 = offpeak_hours_6
usage_points.plan = plan
usage_points.refresh_addresse = str2bool(refresh_addresse)
usage_points.refresh_contract = str2bool(refresh_contract)
usage_points.token = token
usage_points.progress = progress
usage_points.progress_status = progress_status
usage_points.consumption_max_date = consumption_max_date
usage_points.consumption_detail_max_date = consumption_detail_max_date
usage_points.production_max_date = production_max_date
usage_points.production_detail_max_date = production_detail_max_date
else:
self.session.add(
UsagePoints(
usage_point_id=usage_point_id,
# TODO : Erreur si name est vide
name=data["name"],
cache=str2bool(data["cache"]),
consumption=str2bool(data["consumption"]),
consumption_detail=str2bool(data["consumption_detail"]),
production=str2bool(data["production"]),
production_detail=str2bool(data["production_detail"]),
production_price=data["production_price"],
consumption_price_base=data["consumption_price_base"],
consumption_price_hc=data["consumption_price_hc"],
consumption_price_hp=data["consumption_price_hp"],
offpeak_hours_0=data["offpeak_hours_0"],
offpeak_hours_1=data["offpeak_hours_1"],
offpeak_hours_2=data["offpeak_hours_2"],
offpeak_hours_3=data["offpeak_hours_3"],
offpeak_hours_4=data["offpeak_hours_4"],
offpeak_hours_5=data["offpeak_hours_5"],
offpeak_hours_6=data["offpeak_hours_6"],
plan=data["plan"],
refresh_addresse=str2bool(data["refresh_addresse"]),
refresh_contract=str2bool(data["refresh_contract"]),
token=data["token"],
name=name,
cache=str2bool(cache),
consumption=str2bool(consumption),
consumption_detail=str2bool(consumption_detail),
production=str2bool(production),
production_detail=str2bool(production_detail),
production_price=production_price,
consumption_price_base=consumption_price_base,
consumption_price_hc=consumption_price_hc,
consumption_price_hp=consumption_price_hp,
offpeak_hours_0=offpeak_hours_0,
offpeak_hours_1=offpeak_hours_1,
offpeak_hours_2=offpeak_hours_2,
offpeak_hours_3=offpeak_hours_3,
offpeak_hours_4=offpeak_hours_4,
offpeak_hours_5=offpeak_hours_5,
offpeak_hours_6=offpeak_hours_6,
plan=plan,
refresh_addresse=str2bool(refresh_addresse),
refresh_contract=str2bool(refresh_contract),
token=token,
progress=progress,
progress_status=progress_status,
enable=str2bool(data["enable"]),
enable=enable,
consumption_max_date=None,
consumption_detail_max_date=None,
production_max_date=None,
Expand Down Expand Up @@ -804,8 +894,11 @@ def get_detail(self, usage_point_id, begin, end, measurement_direction="consumpt
time_delta = abs(int((begin - end).total_seconds() / 60))
total_internal = 0
for query in query_result:
# print(query)
total_internal = total_internal + query.interval
if abs(total_internal - time_delta) > 60:
total_time = abs(total_internal - time_delta)
if total_time > 300:
LOG.log(f" - {total_time}m absente du relevé.")
result["missing_data"] = True
else:
for query in query_result:
Expand Down Expand Up @@ -849,6 +942,7 @@ def insert_detail_bulk(self, data, mesure_type="consumption"):
self.session.execute(
table.__table__.delete().filter(ConsumptionDetail.date.between(begin, end))
)
print(data)
self.session.add_all(data)

def insert_detail(self, usage_point_id, date, value, interval, measure_type, blacklist=0, fail_count=0,
Expand Down
16 changes: 8 additions & 8 deletions app/models/export_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def daily(self, price, measurement_direction="consumption"):
"month": daily.date.strftime("%m"),
},
fields={
"Wh": watt,
"kWh": forceRound(kwatt, 2),
"price": forceRound(euro, 2)
"Wh": float(watt),
"kWh": float(forceRound(kwatt, 2)),
"price": float(forceRound(euro, 2))
},
)
current_month = date.strftime("%m")
Expand Down Expand Up @@ -73,11 +73,11 @@ def detail(self, price_hp, price_hc=0, measurement_direction="consumption_detail
"measure_type": detail.measure_type,
},
fields={
"W": watt,
"kW": forceRound(kwatt, 2),
"Wh": watth,
"kWh": forceRound(kwatth, 2),
"price": forceRound(euro, 2)
"W": float(watt),
"kW": float(forceRound(kwatt, 2)),
"Wh": float(watth),
"kWh": float(forceRound(kwatth, 2)),
"price": float(forceRound(euro, 2))
},
)
current_month = date.strftime("%m")
2 changes: 1 addition & 1 deletion app/models/query_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run(self, begin, end):
endpoint += "/cache"
try:
current_data = self.db.get_detail(self.usage_point_id, begin, end, self.measure_type)
current_week = datetime.datetime.now() - datetime.timedelta(days=self.max_detail + 1)
# current_week = datetime.datetime.now() - datetime.timedelta(days=self.max_detail + 1)
# last_week = False
# if current_week <= begin:
# last_week = True
Expand Down

0 comments on commit 786fffc

Please sign in to comment.