Skip to content

Commit

Permalink
fix fitness aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
vcai122 committed Sep 13, 2023
1 parent 7291d8e commit 2f47197
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions backend/penndata/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def get(self, request):

class FitnessUsage(APIView):
def safe_add(self, a, b):
return a + b if a and b else (a if a else b)
return None if a is None and b is None else (a or 0) + (b or 0)

def linear_interpolate(self, before_val, after_val, before_date, current_date, after_date):
return (
Expand Down Expand Up @@ -370,14 +370,14 @@ def get_usage(self, room, date, num_samples, group_by, field):
usage = self.get_usage_on_date(room, curr, field) # usage for curr
# incorporate usage safely considering None (no data) values
usage_aggs = [
(self.safe_add(acc[0], val), acc[1] + (1 if val is not None else 0))
for acc, val in zip(usage_aggs, usage)
(self.safe_add(sum, val), count + (val is not None))
for (sum, count), val in zip(usage_aggs, usage)
]
# update min and max date if any data was logged
if any(usage):
min_date = min(min_date, curr)
max_date = max(max_date, curr)
ret = [usage_agg[0] / usage_agg[1] if usage_agg[1] else None for usage_agg in usage_aggs]
ret = [(sum / count) if count else None for (sum, count) in usage_aggs]
return ret, min_date, max_date

def get(self, request, room_id):
Expand Down

0 comments on commit 2f47197

Please sign in to comment.