Changeset 63df989 in OpenWorkouts-current


Ignore:
Timestamp:
Feb 4, 2019, 12:32:07 PM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
5cf5787
Parents:
1d2acd4
Message:

Added a method to the User model that returns workouts stats for the last 12

months, but on a per-week basis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/models/user.py

    r1d2acd4 r63df989  
    99
    1010from ow.catalog import get_catalog, reindex_object
    11 from ow.utilities import get_week_days
     11from ow.utilities import get_week_days, get_month_week_number
    1212
    1313
     
    292292
    293293        return stats
     294
     295    @property
     296    def weekly_year_stats(self):
     297        """
     298        Return per-week stats for the last 12 months
     299        """
     300        # set the boundaries for looking for workouts afterwards,
     301        # we need the current date as the "end date" and one year
     302        # ago from that date. Then we set the start at the first
     303        # day of that month.
     304        end = datetime.now(timezone.utc)
     305        start = (end - timedelta(days=365)).replace(day=1)
     306
     307        stats = {}
     308
     309        # first initialize the stats dict
     310        for days in range((end - start).days):
     311            day = (start + timedelta(days=days)).date()
     312            week = day.isocalendar()[1]
     313            month_week = get_month_week_number(day)
     314            key = (day.year, day.month, week, month_week)
     315            if key not in stats.keys():
     316                stats[key] = {
     317                    'workouts': 0,
     318                    'time': timedelta(0),
     319                    'distance': Decimal(0),
     320                    'elevation': Decimal(0),
     321                    'sports': {}
     322                }
     323
     324        # now loop over the workouts, filtering and then adding stats
     325        # to the proper place
     326        for workout in self.workouts():
     327            if start.date() <= workout.start.date() <= end.date():
     328                # less typing, avoid long lines
     329                start_date = workout.start.date()
     330                week = start_date.isocalendar()[1]
     331                month_week = get_month_week_number(start_date)
     332                week = stats[(start_date.year,
     333                              start_date.month,
     334                              week,
     335                              month_week)]
     336
     337                week['workouts'] += 1
     338                week['time'] += workout.duration or timedelta(seconds=0)
     339                week['distance'] += workout.distance or Decimal(0)
     340                week['elevation'] += workout.uphill or Decimal(0)
     341                if workout.sport not in week['sports']:
     342                    week['sports'][workout.sport] = {
     343                        'workouts': 0,
     344                        'time': timedelta(seconds=0),
     345                        'distance': Decimal(0),
     346                        'elevation': Decimal(0),
     347                    }
     348                week['sports'][workout.sport]['workouts'] += 1
     349                week['sports'][workout.sport]['time'] += (
     350                    workout.duration or timedelta(0))
     351                week['sports'][workout.sport]['distance'] += (
     352                    workout.distance or Decimal(0))
     353                week['sports'][workout.sport]['elevation'] += (
     354                    workout.uphill or Decimal(0))
     355
     356        return stats
Note: See TracChangeset for help on using the changeset viewer.