Пример #1
0
 def series_definitions(self, base_measure, base_series_definition):
     dd = super().series_definitions(base_measure, base_series_definition)
     dd['atl'] = SeriesDefinition(measure='atl',
                                  underlying_measure=base_measure)
     dd['ctl'] = SeriesDefinition(measure='ctl',
                                  underlying_measure=base_measure)
     dd['tsb'] = SeriesDefinition(measure='tsb',
                                  underlying_measure=base_measure)
     return dd
Пример #2
0
 def __init__(self,
              data_definition,
              series_definition=SeriesDefinition(),
              processor=NoOpProcessor(),
              x_axis_number=1):
     self.data_definition = data_definition
     self.series_definition = series_definition
     self.processor = processor
     self.x_axis_number = x_axis_number
Пример #3
0
    def call_resource(self, request):

        period_year = Period(pandas_period=PandasPeriod.Y_DEC,
                             aggregation=Aggregation.MEAN,
                             to_date=False,
                             incl_zeroes=False)

        series_definition = SeriesDefinition(period=period_year, rolling_definition=NoOpRoller())

        time_series_sets = list()

        for reading in ReadingEnum:
            dd = DataDefinition(measure=reading.value, day_aggregation_method=DayAggregation.MEAN)
            time_series_sets.append(TimeSeriesManager.TimeSeriesSet(data_definition=dd,
                                                                    series_definition=series_definition,
                                                                    processor=TimeSeriesProcessor.get_processor("No-op")))

        diary_time_period = TrainingDataManager().diary_time_period()
        # year summaries do need time period to be to year end
        diary_time_period.end = date(diary_time_period.end.year, 12, 31)
        tsl, errors = TimeSeriesManager().time_series_list(requested_time_period=diary_time_period, time_series_list=time_series_sets)

        if len(tsl) > 0:
            total_series = {'date': "Total"}
            for k in tsl[0].keys():
                if k != 'date':
                    entries = [d[k] for d in tsl]
                    year_count = sum([1 for e in entries if e > 0])
                    total_series[k] = 0 if year_count == 0 else sum(entries) / year_count
            tsl.append(total_series)

        for dd in tsl:
            dd['name'] = dd['date']

        response = TrainingDiaryResponse()
        [response.add_message(response.MSG_ERROR, e) for e in errors]
        response.add_data('time_series', tsl)

        return JsonResponse(data=response.as_dict())
Пример #4
0
    def call_resource(self, request):
        response = TrainingDiaryResponse()
        year = int(request.POST['year'])
        tp = TimePeriod(date(year,1,1), date(year, 12,31))

        swim_km = DataDefinition(activity='Swim', activity_type='All', equipment='All', measure='km', day_aggregation_method=DayAggregation.SUM)
        bike_km = DataDefinition(activity='Bike', activity_type='All', equipment='All', measure='km', day_aggregation_method=DayAggregation.SUM)
        run_km = DataDefinition(activity='Run', activity_type='All', equipment='All', measure='km', day_aggregation_method=DayAggregation.SUM)
        hours = DataDefinition(activity='All', activity_type='All', equipment='All', measure='hours', day_aggregation_method=DayAggregation.SUM)
        reps = DataDefinition(activity='Gym', activity_type='PressUp', equipment='All', measure='reps', day_aggregation_method=DayAggregation.SUM)
        series_defn = SeriesDefinition(period=Period(PandasPeriod(request.POST['period'])))

        summary = list()
        summary.append(TimeSeriesManager.TimeSeriesSet(data_definition=swim_km, series_definition=series_defn))
        summary.append(TimeSeriesManager.TimeSeriesSet(data_definition=bike_km, series_definition=series_defn))
        summary.append(TimeSeriesManager.TimeSeriesSet(data_definition=run_km, series_definition=series_defn))
        summary.append(TimeSeriesManager.TimeSeriesSet(data_definition=hours, series_definition=series_defn))
        summary.append(TimeSeriesManager.TimeSeriesSet(data_definition=reps, series_definition=series_defn))

        dd, errors = TimeSeriesManager().time_series_list(tp, summary)
        response.add_data('time_series', sorted(dd, key=lambda x: x['date']))
        [response.add_message(response.MSG_ERROR, error) for error in errors]

        return JsonResponse(data=response.as_dict())
Пример #5
0
 def series_definitions(self, base_measure, base_series_definition):
     dd = super().series_definitions(base_measure, base_series_definition)
     dd['annual_ed_num'] = SeriesDefinition(measure='annual_ed_num', underlying_measure=base_measure)
     dd['annual_plus_one'] = SeriesDefinition(measure='annual_plus_one', underlying_measure=base_measure)
     return dd
Пример #6
0
 def series_definitions(self, base_measure, base_series_definition):
     dd = super().series_definitions(base_measure, base_series_definition)
     dd['ed_num'] = SeriesDefinition(measure='ed_num', underlying_measure=base_measure)
     dd['plus_one'] = SeriesDefinition(measure='plus_one', underlying_measure=base_measure)
     dd['contributor'] = SeriesDefinition(measure='contributor', underlying_measure=base_measure)
     return dd
Пример #7
0
 def series_definitions(self, base_measure, base_series_definition):
     dd = super().series_definitions(base_measure, base_series_definition)
     dd[self.ED_NUM] = SeriesDefinition(measure=self.ED_NUM, underlying_measure=base_measure)
     dd[self.PLUS_ONE] = SeriesDefinition(measure=self.PLUS_ONE, underlying_measure=base_measure)
     return dd
Пример #8
0
    def call_resource(self, request):
        response = TrainingDiaryResponse()
        tms = TimeSeriesManager()
        graph = request.POST['graph']
        activity = request.POST['activity']
        year_str = self._normalise_year_str(request.POST['year'])
        yr_title = year_str if year_str != 'Total' else "All Time"

        if year_str == 'Total':
            tdm = TrainingDataManager()
            tp = tdm.diary_time_period()
        else:
            year = int(year_str)
            tp = TimePeriod(date(year,1,1), date(year,12,31))

        tss_list = list()
        if graph == 'tss':
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=DataDefinition(activity='All' if activity == "Total" else activity,
                                                                                           activity_type='All',
                                                                                           equipment='All',
                                                                                           measure='tss',
                                                                                           day_aggregation_method=DayAggregation.SUM),
                                                            processor=TSBProcessor(7, 7, 42, 42)))
        elif graph == 'duration':
            duration_defn = DataDefinition(activity='All' if activity == "Total" else activity,
                                           activity_type='All',
                                           equipment='All',
                                           measure='hours',
                                           day_aggregation_method=DayAggregation.SUM)
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=duration_defn))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=duration_defn, series_definition=SeriesDefinition(Period(), RollingDefinition(7, Aggregation.SUM))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=duration_defn, series_definition=SeriesDefinition(Period(PandasPeriod.Y_DEC, to_date=True))))
        elif graph == 'km':
            km_defn = DataDefinition(activity='All' if activity == "Total" else activity,
                                     activity_type='All',
                                     equipment='All',
                                     measure='km',
                                     day_aggregation_method=DayAggregation.SUM)
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=km_defn))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=km_defn, series_definition=SeriesDefinition(Period(), RollingDefinition(7, Aggregation.SUM))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=km_defn, series_definition=SeriesDefinition(Period(PandasPeriod.Y_DEC, to_date=True))))
        elif graph == 'reading':
            reading_defn = DataDefinition(activity='All',
                                          activity_type='All',
                                          equipment='All',
                                          measure=activity,
                                          day_aggregation_method=DayAggregation.MEAN)
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=reading_defn))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=reading_defn, series_definition=SeriesDefinition(Period(), RollingDefinition(7, Aggregation.MEAN))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=reading_defn, series_definition=SeriesDefinition(Period(), RollingDefinition(31, Aggregation.MEAN))))
        elif graph == 'bike':
            bike_defn = DataDefinition(activity='Bike',
                                       activity_type='All',
                                       equipment='All' if activity == 'Total' else activity,
                                       measure='km',
                                       day_aggregation_method=DayAggregation.SUM)
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=bike_defn))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=bike_defn,
                                                            series_definition=SeriesDefinition(Period(), RollingDefinition(7, Aggregation.SUM))))
            tss_list.append(TimeSeriesManager.TimeSeriesSet(data_definition=bike_defn,
                                                            series_definition=SeriesDefinition(Period(PandasPeriod.Y_DEC, Aggregation.SUM, to_date=True))))

        if len(tss_list) > 0:
            values = tms.time_series_graph(requested_time_period=tp, time_series_list=tss_list)
        else:
            values = {'title': "No Data"}

        response.add_data('chart_title', f"{yr_title} {values['title']}")
        response.add_data('time_series', values)
        return JsonResponse(data=response.as_dict())
Пример #9
0
    def call_resource(self, request):
        dd, errors = self._process_data(request.POST['json'])

        dd_keys = set([d for d in dd.keys()])

        data_definition = DataDefinition(activity=dd['activity'],
                                         activity_type=dd['activity_type'],
                                         equipment=dd['equipment'],
                                         measure=dd['measure'],
                                         day_aggregation_method=DayAggregation(dd['day_aggregation']),
                                         day_type=dd['day_type'],
                                         day_of_week=dd['day_of_week'],
                                         month=dd['month'],
                                         interpolation=dd['interpolation'])

        dd_keys.remove('activity')
        dd_keys.remove('activity_type')
        dd_keys.remove('equipment')
        dd_keys.remove('measure')
        dd_keys.remove('day_aggregation')
        dd_keys.remove('day_type')
        dd_keys.remove('day_of_week')
        dd_keys.remove('month')
        dd_keys.remove('interpolation')

        period = Period(pandas_period=PandasPeriod(dd['period']), aggregation=Aggregation(dd['period_aggregation']),
                        to_date=dd['to_date'] == 'yes',
                        incl_zeroes=dd['period_include_zeroes'] == 'yes')
        dd_keys.remove('period')
        dd_keys.remove('period_aggregation')
        dd_keys.remove('to_date')
        dd_keys.remove('period_include_zeroes')

        if dd['rolling'] == 'yes':
            rolling_definition = RollingDefinition(periods=int(dd['number_of_rolling_periods']), aggregation=Aggregation(dd['rolling_aggregation']),
                                                   incl_zeros=dd['rolling_include_zeroes'] == 'yes')
        else:
            rolling_definition = NoOpRoller()
        dd_keys.remove('number_of_rolling_periods')
        dd_keys.remove('rolling_aggregation')
        dd_keys.remove('rolling_include_zeroes')
        dd_keys.remove('rolling')

        series_definition = SeriesDefinition(period=period, rolling_definition=rolling_definition)

        processor = self.get_processor(dd)
        dd_keys.remove('processor_type')

        response = TrainingDiaryResponse()
        [response.add_message(response.MSG_ERROR, e) for e in errors]

        diary_time_period = TrainingDataManager().diary_time_period()
        data_tp = TimePeriod(diary_time_period.start if dd['series_start'] is None else dd['series_start'],
                             diary_time_period.end if dd['series_end'] is None else dd['series_end'])
        dd_keys.remove('series_start')
        dd_keys.remove('series_end')

        x_axis_number = 1
        if 'x_axis_number' in dd:
            x_axis_number = dd['x_axis_number']
            dd_keys.remove('x_axis_number')
        tss = TimeSeriesManager.TimeSeriesSet(data_definition, series_definition=series_definition, processor=processor, x_axis_number=x_axis_number)

        ts = TimeSeriesManager().time_series_graph(data_tp, [tss])
        response.add_data('time_series', ts)

        if len(dd_keys) > 0:
            response.add_message(response.MSG_WARNING, f"The following data was not used: {' ,'.join(dd_keys)}")

        return JsonResponse(data=response.as_dict())