Exemplo n.º 1
0
def export_step_count_records_csv(users, filename, start_date, end_date):
    dates_diff = end_date - start_date
    dates = [
        start_date + timedelta(days=offset)
        for offset in range(dates_diff.days)
    ]

    rows = []
    for _user in users:
        row = [_user.username]
        day_service = DayService(user=_user)
        for _date in dates:
            start = day_service.get_start_of_day(_date)
            end = start + timedelta(days=1)
            if _user.date_joined < end:
                query = StepCount.objects.filter(user=_user,
                                                 start__range=[start, end])
                row.append(query.count())
            else:
                row.append(None)
        rows.append(row)

    headers = [''] + [d.strftime('%Y-%m-%d') for d in dates]
    rows = [headers] + rows
    f = open(filename, 'w')
    writer = csv.writer(f)
    writer.writerows(rows)
    f.close()
Exemplo n.º 2
0
 def update_message_receipts(self, date):
     if not self.user:
         return
     day_service = DayService(user=self.user)
     messages = PushMessage.objects.filter(
         recipient=self.user,
         created__gte=day_service.get_start_of_day(date),
         created__lte=day_service.get_end_of_day(date)).all()
     for message in messages:
         message.update_message_receipts()
Exemplo n.º 3
0
 def __get_time_range(self, time_range):
     if time_range not in self.TIME_RANGES:
         raise RuntimeError('time range not found')
     else:
         service = DayService(user=self.user)
         today = service.get_current_date()
         start_date = today - timedelta(days=time_range.offset)
         return [
             service.get_start_of_day(start_date),
             service.get_end_of_day(today)
         ]
Exemplo n.º 4
0
def export_activity_logs(username,
                         filename=None,
                         directory=None,
                         start=None,
                         end=None):
    if not directory:
        directory = './'
    if not filename:
        filename = '%s.activity_logs.csv' % (username)

    activity_log_query = ActivityLog.objects.filter(
        user__username=username
    ) \
    .order_by('start') \
    .prefetch_related('type')

    day_service = DayService(username=username)
    if start:
        start_datetime = day_service.get_start_of_day(start)
        activity_log_query = activity_log_query.filter(
            start__gte=start_datetime)
    if end:
        end_datetime = day_service.get_end_of_day(end)
        activity_log_query = activity_log_query.filter(start__lte=end_datetime)

    rows = []
    headers = [
        'Participant ID', 'Activity ID', 'Study Day', 'Date', 'Start Time',
        'Timezone', 'Activity Type ID', 'Activity Type Title',
        'Activity Duration', 'Activity Vigorous'
    ]
    rows.append(headers)

    for activity_log in activity_log_query.all():
        rows.append([
            username, activity_log.id, 'study day',
            activity_log.start.astimezone(
                pytz.timezone('America/Los_Angeles')).strftime('%Y-%m-%d'),
            activity_log.start.astimezone(pytz.timezone(
                'America/Los_Angeles')).strftime('%Y-%m-%d %H:%M:%S'),
            'timezone', activity_log.type.id, activity_log.type.title,
            activity_log.duration, activity_log.vigorous
        ])

    _file = open(path.join(directory, filename), 'w')
    writer = csv.writer(_file)
    writer.writerows(rows)
    _file.close()
Exemplo n.º 5
0
    def suggestion_time_category_available_at(self, time):
        category = self.suggestion_time_category_at(time)
        day_service = DayService(user=self.__user)

        query = WalkingSuggestionDecision.objects.filter(
            user=self.__user,
            test=False,
            time__range=[
                day_service.get_start_of_day(time),
                day_service.get_end_of_day(time)
            ])
        tags = [decision.category for decision in query.all()]

        if category in tags:
            raise self.Unavailable('Time already taken')
        else:
            return category
Exemplo n.º 6
0
    def get_adherence_during(self, start, end):
        if not self.user:
            return []
        metrics = {}
        adherence_metrics = AdherenceMetric.objects.filter(
            user=self.user, date__range=[start, end]).all()
        for metric in adherence_metrics:
            if metric.date not in metrics:
                metrics[metric.date] = {}
            metrics[metric.date][metric.category] = metric.value

        messages = {}
        day_service = DayService(user=self.user)
        adherence_messages = AdherenceMessage.objects.filter(
            user=self.user,
            created__range=[
                day_service.get_start_of_day(start),
                day_service.get_end_of_day(end)
            ]).all()
        for message in adherence_messages:
            message_date = day_service.get_date(message.created)
            if message_date not in messages:
                messages[message_date] = []
            messages[message_date].append({
                'category': message.category,
                'body': message.body
            })

        summaries = []
        _dates = [
            end - timedelta(days=offset)
            for offset in range((end - start).days + 1)
        ]
        for _date in _dates:
            _metrics = {}
            if _date in metrics:
                _metrics = metrics[_date]
            _messages = []
            if _date in messages:
                _messages = messages[_date]
            summaries.append({
                'date': _date,
                'metrics': _metrics,
                'messages': _messages
            })
        return summaries
Exemplo n.º 7
0
 def get_study_start_datetime(self):
     study_start_date = self.get_study_start_date()
     if study_start_date:
         day_service = DayService(user=self.user)
         return day_service.get_start_of_day(study_start_date)
     return None
Exemplo n.º 8
0
 def was_updated_on(self, date):
     day_service = DayService(user=self.__user)
     start = day_service.get_start_of_day(date)
     end = day_service.get_end_of_day(date)
     return self.account.was_updated_between(start, end)