Changes in ow/models/user.py [bed4f06:2f8a48f] in OpenWorkouts-current


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/models/user.py

    rbed4f06 r2f8a48f  
    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.