Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix chargelog sum #1503

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions packages/control/chargelog/chargelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,34 +293,44 @@ def get_log_data(request: Dict):

# wenn wir hier ankommen, passt der Eintrag zum Filter
log_data["entries"].append(entry)

if len(log_data["entries"]) > 0:
# Summen bilden
duration = "00:00"
range_charged = 0
mode = 0
power = 0
costs = 0
for entry in log_data["entries"]:
duration = timecheck.duration_sum(
duration, entry["time"]["time_charged"])
range_charged += entry["data"]["range_charged"]
mode += entry["data"]["imported_since_mode_switch"]
power += entry["data"]["power"]
costs += entry["data"]["costs"]
power = power / len(log_data["entries"])
log_data["totals"] = {
"time_charged": duration,
"range_charged": range_charged,
"imported_since_mode_switch": mode,
"power": power,
"costs": costs,
}
log_data["totals"] = get_totals_of_filtered_log_data(log_data)
except Exception:
log.exception("Fehler im Ladelog-Modul")
return log_data


def get_totals_of_filtered_log_data(log_data: Dict) -> Dict:
def get_sum(entry_name: str) -> float:
sum = 0
try:
for entry in log_data["entries"]:
sum += entry["data"][entry_name]
return sum
except Exception:
return None
if len(log_data["entries"]) > 0:
# Summen bilden
duration_sum = "00:00"
try:
for entry in log_data["entries"]:
duration_sum = timecheck.duration_sum(
duration_sum, entry["time"]["time_charged"])
except Exception:
duration_sum = None
range_charged_sum = get_sum("range_charged")
mode_sum = get_sum("imported_since_mode_switch")
power_sum = get_sum("power")
costs_sum = get_sum("costs")
power_sum = power_sum / len(log_data["entries"])
return {
"time_charged": duration_sum,
"range_charged": range_charged_sum,
"imported_since_mode_switch": mode_sum,
"power": power_sum,
"costs": costs_sum,
}


def calculate_charge_cost(cp, create_log_entry: bool = False):
content = get_todays_daily_log()
try:
Expand Down