Changeset ed7e9d7 in OpenWorkouts-current for ow/models/user.py


Ignore:
Timestamp:
Jan 29, 2019, 12:41:01 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
1183d5a, 22eb5de
Parents:
26220ba (diff), bd8eeb4 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merged patches from darcs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/models/user.py

    r26220ba red7e9d7  
    234234                        timedelta())
    235235        }
     236
     237    @property
     238    def yearly_stats(self):
     239        """
     240        Return per-month stats for the last 12 months
     241        """
     242        # set the boundaries for looking for workouts afterwards,
     243        # we need the current date as the "end date" and one year
     244        # ago from that date. Then we set the start at the first
     245        # day of that month.
     246        end = datetime.now(timezone.utc)
     247        start = (end - timedelta(days=365)).replace(day=1)
     248
     249        # build the stats, populating first the dict with empty values
     250        # for each month.
     251        stats = {}
     252        for days in range((end - start).days):
     253            day = (start + timedelta(days=days)).date()
     254            if (day.year, day.month) not in stats.keys():
     255                stats[(day.year, day.month)] = {
     256                    'workouts': 0,
     257                    'time': timedelta(0),
     258                    'distance': Decimal(0),
     259                    'elevation': Decimal(0),
     260                    'sports': {}
     261                }
     262
     263        # now loop over workouts, filtering and then adding stats to the
     264        # proper place
     265        for workout in self.workouts():
     266            if start.date() <= workout.start.date() <= end.date():
     267                # less typing, avoid long lines
     268                month = stats[
     269                    (workout.start.date().year, workout.start.date().month)]
     270                month['workouts'] += 1
     271                month['time'] += workout.duration or timedelta(seconds=0)
     272                month['distance'] += workout.distance or Decimal(0)
     273                month['elevation'] += workout.uphill or Decimal(0)
     274                if workout.sport not in month['sports']:
     275                    month['sports'][workout.sport] = {
     276                        'workouts': 0,
     277                        'time': timedelta(seconds=0),
     278                        'distance': Decimal(0),
     279                        'elevation': Decimal(0),
     280                    }
     281                month['sports'][workout.sport]['workouts'] += 1
     282                month['sports'][workout.sport]['time'] += (
     283                    workout.duration or timedelta(0))
     284                month['sports'][workout.sport]['distance'] += (
     285                    workout.distance or Decimal(0))
     286                month['sports'][workout.sport]['elevation'] += (
     287                    workout.uphill or Decimal(0))
     288
     289        return stats
Note: See TracChangeset for help on using the changeset viewer.