Skip to content

Commit

Permalink
Fix: Inconsistency in statistics summaries (#282)
Browse files Browse the repository at this point in the history
* - Fix for double addition for first item in a year
- Fix for only single month being returned in a year summary
- Fix for no data being returned. This happens when there has no been "driving" days in the date range requested.

* Fix up lint issues

* Update type hint

---------

Co-authored-by: GitOldGrumpy <[email protected]>
  • Loading branch information
GitOldGrumpy and GitOldGrumpy authored Jan 9, 2024
1 parent 2623fc3 commit 66ad51e
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions mytoyota/models/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ async def get_summary(
from_date: date,
to_date: date,
summary_type: SummaryType = SummaryType.MONTHLY,
) -> Optional[List[Summary]]:
) -> List[Summary]:
"""Return a Daily, Weekly, Monthly or Yearly summary between the provided dates.
All but Daily can return a partial time range. For example if the summary_type is weekly
Expand Down Expand Up @@ -264,8 +264,8 @@ async def get_summary(
resp = await self._api.get_trips_endpoint(
self.vin, from_date, to_date, summary=True, limit=1, offset=0
)
if resp.payload is None:
return None
if resp.payload is None or len(resp.payload.summary) == 0:
return []

# Convert to response
if summary_type == SummaryType.DAILY:
Expand Down Expand Up @@ -424,17 +424,22 @@ def _generate_yearly_summaries(self, summary, to_date: date) -> List[Summary]:
build_summary = copy.copy(summary[0].summary)
start_date = date(day=1, month=summary[0].month, year=summary[0].year)

for month, next_month in zip(summary, summary[1:] + [None]):
summary_month = date(day=1, month=month.month, year=month.year)
add_with_none(build_hdc, month.hdc)
build_summary += month.summary

if next_month is None or next_month.year != month.year:
end_date = min(to_date, date(day=31, month=12, year=summary_month.year))
ret.append(Summary(build_summary, self._metric, start_date, end_date, build_hdc))
if next_month:
start_date = date(day=1, month=next_month.month, year=next_month.year)
build_hdc = copy.copy(next_month.hdc)
build_summary = copy.copy(next_month.summary)
if len(summary) == 1:
ret.append(Summary(build_summary, self._metric, start_date, to_date, build_hdc))
else:
for month, next_month in zip(summary[1:], summary[2:] + [None]):
summary_month = date(day=1, month=month.month, year=month.year)
add_with_none(build_hdc, month.hdc)
build_summary += month.summary

if next_month is None or next_month.year != month.year:
end_date = min(to_date, date(day=31, month=12, year=summary_month.year))
ret.append(
Summary(build_summary, self._metric, start_date, end_date, build_hdc)
)
if next_month:
start_date = date(day=1, month=next_month.month, year=next_month.year)
build_hdc = copy.copy(next_month.hdc)
build_summary = copy.copy(next_month.summary)

return ret

0 comments on commit 66ad51e

Please sign in to comment.