def statistics():
     requester = get_requesting_user()
     if requester is None:
         raise Unauthorized()
     elif not requester.is_administrator:
         raise Forbidden()
     else:
         yesterday = Statistic.date_yesterday()
         response_data = {'data': {}}
         for days_ago in range(30):
             date = yesterday - datetime.timedelta(days=days_ago)
             stats = Statistic.get_statistics(date)
             response_data['data'][time_format.to_iso8601(date)] = stats
     return response_data
 def statistics():
     requester = get_requesting_user()
     if requester is None:
         response = base_routes.make_not_authorized_response()
     elif not requester.is_administrator:
         response = base_routes.make_forbidden_response()
     else:
         yesterday = Statistic.date_yesterday()
         response_data = {'data': {}}
         for days_ago in range(30):
             date = yesterday - datetime.timedelta(days=days_ago)
             stats = Statistic.get_statistics(date)
             response_data['data'][time_format.to_iso8601(date)] = stats
         response = jsonify(response_data)
     return response
Beispiel #3
0
    def test_statistics(self):
        user_datas = {
            'userA': sample_userA,
            'userB': sample_userB,
        }
        user_ids, user_headers = self.create_users(user_datas)
        # userA is not an administrator so we should get forbidden.
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 403)
        # Make userA an administrator
        userA = store.session.query(User).filter(User.id == user_ids['userA']).first()
        userA.is_administrator = True
        store.session.add(userA)
        store.session.commit()
        # Now we should get stats
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 200)
        stats = json.loads(rv.data.decode('utf8'))['data']
        yesterday = datetime.datetime.utcnow().date() - datetime.timedelta(days=1)
        yesterday_string = time_format.to_iso8601(yesterday)
        self.assertEqual(stats[yesterday_string]['n_new_users'], 0)
        # Now create change the creation date of a user so it was yesterday.
        userA.date_created = datetime.datetime.utcnow() - datetime.timedelta(days=1)
        store.session.add(userA)
        store.session.commit()
        # Add force recalculation of statistics
        Statistic.update_statistics(yesterday, force=True)
        # We should get stats and see one new user yesterday.
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 200)
        stats = json.loads(rv.data.decode('utf8'))['data']
        self.assertEqual(stats[yesterday_string]['n_new_users'], 1)
        self.assertEqual(stats[yesterday_string]['n_users_started_conversation'], 0)
        self.assertEqual(stats[yesterday_string]['n_users_did_event'], 0)
        # Now lets make a conversation and event
        searchA_id, searchB_id = self.create_searches(user_ids, user_headers)
        conversation_data = self.make_conversation(
            user_headers['userA'],
            search_id=searchA_id,
            title='Trip to moon.',
            userA_id=user_ids['userA'],
            userB_id=user_ids['userB'],
        )
        conversation_id = conversation_data['id']
        share_data = self.make_share(
            user_headers['userA'],
            conversation_id,
            educator_user_id=user_ids['userA'],
            community_partner_user_id=user_ids['userB'],
            starting_in_hours=-20,
            force_past_events=True,
        )
        share_id = share_data['id']
        eventA_id = share_data['events'][0]['id']
        # Pretend conversation was created yesterday.
        conversation = store.session.query(Conversation)
        conversation = conversation.filter(Conversation.id == conversation_id)
        conversation = conversation.first()
        conversation.date_created -= datetime.timedelta(hours=24)
        store.session.add(conversation)
        store.session.commit()
        # Stats should be appropriately changed.
        Statistic.update_statistics(yesterday, force=True)
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 200)
        stats = json.loads(rv.data.decode('utf8'))['data']
        self.assertEqual(stats[yesterday_string]['n_new_users'], 1)
        self.assertEqual(stats[yesterday_string]['n_users_started_conversation'], 1)
        # FIXME(seanastephens): What is this assertion and why does it fail?
        # self.assertEqual(stats[yesterday_string]['n_users_did_event'], 2)

        # Make sure check_statistics doesn't break
        Statistic.check_statistics()
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 200)
        stats = json.loads(rv.data.decode('utf8'))['data']
        self.assertEqual(len(stats.keys()), 30)
Beispiel #4
0
    def test_statistics(self):
        user_datas = {
            'userA': sample_userA,
            'userB': sample_userB,
        }
        user_ids, user_headers = self.create_users(user_datas)
        # userA is not an administrator so we should get forbidden.
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 403)
        # Make userA an administrator
        userA = store.session.query(User).filter(
            User.id == user_ids['userA']).first()
        userA.is_administrator = True
        store.session.add(userA)
        store.session.commit()
        # Now we should get stats
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 200)
        stats = json.loads(rv.data.decode('utf8'))['data']
        yesterday = datetime.datetime.utcnow().date() - datetime.timedelta(
            days=1)
        yesterday_string = time_format.to_iso8601(yesterday)
        self.assertEqual(stats[yesterday_string]['n_new_users'], 0)
        # Now create change the creation date of a user so it was yesterday.
        userA.date_created = datetime.datetime.utcnow() - datetime.timedelta(
            days=1)
        store.session.add(userA)
        store.session.commit()
        # Add force recalculation of statistics
        Statistic.update_statistics(yesterday, force=True)
        # We should get stats and see one new user yesterday.
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 200)
        stats = json.loads(rv.data.decode('utf8'))['data']
        self.assertEqual(stats[yesterday_string]['n_new_users'], 1)
        self.assertEqual(
            stats[yesterday_string]['n_users_started_conversation'], 0)
        self.assertEqual(stats[yesterday_string]['n_users_did_event'], 0)
        # Now lets make a conversation and event
        searchA_id, searchB_id = self.create_searches(user_ids, user_headers)
        conversation_data = self.make_conversation(
            user_headers['userA'],
            search_id=searchA_id,
            title='Trip to moon.',
            userA_id=user_ids['userA'],
            userB_id=user_ids['userB'],
        )
        conversation_id = conversation_data['id']
        share_data = self.make_share(
            user_headers['userA'],
            conversation_id,
            educator_user_id=user_ids['userA'],
            community_partner_user_id=user_ids['userB'],
            starting_in_hours=-20,
            force_past_events=True,
        )
        share_id = share_data['id']
        eventA_id = share_data['events'][0]['id']
        # Pretend conversation was created yesterday.
        conversation = store.session.query(Conversation)
        conversation = conversation.filter(Conversation.id == conversation_id)
        conversation = conversation.first()
        conversation.date_created -= datetime.timedelta(hours=24)
        store.session.add(conversation)
        store.session.commit()
        # Stats should be appropriately changed.
        Statistic.update_statistics(yesterday, force=True)
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 200)
        stats = json.loads(rv.data.decode('utf8'))['data']
        self.assertEqual(stats[yesterday_string]['n_new_users'], 1)
        self.assertEqual(
            stats[yesterday_string]['n_users_started_conversation'], 1)
        # FIXME(seanastephens): What is this assertion and why does it fail?
        # self.assertEqual(stats[yesterday_string]['n_users_did_event'], 2)

        # Make sure check_statistics doesn't break
        Statistic.check_statistics()
        rv = self.app.get('/api/statistics', headers=user_headers['userA'])
        self.assertEqual(rv.status_code, 200)
        stats = json.loads(rv.data.decode('utf8'))['data']
        self.assertEqual(len(stats.keys()), 30)
Beispiel #5
0
def do_work():
    logger.info('Running do_work')
    reminder.send_reminders()
    Statistic.check_statistics()
def do_work():
    logger.info('Running do_work')
    reminder.send_reminders()
    Statistic.check_statistics()