Skip to content

Commit

Permalink
feat(utils): calculate total miles and cost per mile (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lokidokicoki committed Mar 1, 2023
1 parent 8cfc3f9 commit 30c6a79
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions jalopy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ def stats(self, vehicle):
avg_mpg = 0
avg_km_per_litre = 0
avg_l100 = 0
total_miles = 0
prev_miles = -1
counts = [{"id": x.uid, "name": x.name, "count": 0} for x in types]

for record in records:
if prev_miles == -1:
prev_miles = record.odometer
total_miles += record.odometer - prev_miles
prev_miles = record.odometer

count = next(x for x in counts if x["id"] == record.record_type_id)
count["count"] = count["count"] + 1

if record.record_type_id == 1:
eff = self.calculate_economy(record)

Expand All @@ -66,4 +74,6 @@ def stats(self, vehicle):
"avg_km_per_litre": avg_km_per_litre,
"avg_l100": avg_l100,
"total_cost": total_cost,
"total_miles": total_miles,
"cpm": total_cost / total_miles,
}

0 comments on commit 30c6a79

Please sign in to comment.