def test_record_stat(self): now = datetime.datetime.utcnow() record_stat('app.install', self.request, value=1) # we should have only one record record = MonolithRecord.objects.get() eq_(record.key, 'app.install') eq_(record.value, json.dumps({'value': 1})) self.assertTrue(total_seconds(record.recorded - now) < 1)
def test_filter_by_key(self): record_stat('apps_added_us_free', self.request, value=3) record_stat('apps_added_uk_free', self.request, value=1) # Exact match. res = self.client.get(self.list_url, data={'key': 'apps_added_us_free'}) eq_(res.status_code, 200) data = json.loads(res.content) eq_(len(data['objects']), 1)
def test_normal_call(self): record_stat('app.install', self.request, value=2) res = self.client.get(self.list_url) eq_(res.status_code, 200) data = json.loads(res.content) eq_(len(data['objects']), 1) obj = data['objects'][0] eq_(obj['key'], 'app.install') eq_(obj['value'], {'value': 2}) # Check other fields we want to exist but ignore their value here. for field in ('recorded', 'user_hash'): assert field in obj
def test_filter_by_date(self): for id_, date in enumerate((self.last_week, self.yesterday, self.now)): record_stat('app.install', self.request, __recorded=date, value=id_) res = self.client.get(self.list_url, data={ 'end': self.now.strftime(self.date_format)}) eq_(res.status_code, 200) data = json.loads(res.content) eq_(len(data['objects']), 2) res = self.client.get(self.list_url, data={ 'start': self.yesterday.strftime(self.date_format), 'end': self.now.strftime(self.date_format)}) data = json.loads(res.content) eq_(len(data['objects']), 1)
def record_action(action, request, data=None): """Records the given action by sending it to the metrics servers. Currently this is storing the data internally in the monolith temporary table. :param action: the action related to this request. :param request: the request that triggered this call. :param data: some optional additional data about this call. """ if data is None: data = {} data['user-agent'] = request.META.get('HTTP_USER_AGENT') data['locale'] = request.LANG data['src'] = request.GET.get('src', '') record_stat(action, request, **data)
def test_filter_by_date(self): for id_, date in enumerate((self.last_week, self.yesterday, self.now)): record_stat('app.install', self.request, __recorded=date, value=id_) res = self.client.get( self.list_url, data={'end': self.now.strftime(self.date_format)}) eq_(res.status_code, 200) data = json.loads(res.content) eq_(len(data['objects']), 2) res = self.client.get(self.list_url, data={ 'start': self.yesterday.strftime(self.date_format), 'end': self.now.strftime(self.date_format) }) data = json.loads(res.content) eq_(len(data['objects']), 1)
def test_pagination(self): record_stat('app.install', self.request, value=2) record_stat('app.install', self.request, value=4) record_stat('app.install', self.request, value=6) res = self.client.get(self.list_url, data={'limit': 2}) eq_(res.status_code, 200) data = json.loads(res.content) eq_(len(data['objects']), 2) ok_(data['meta']['next'] is not None) ok_(data['meta']['previous'] is None) eq_(data['meta']['total_count'], 3) eq_(data['meta']['offset'], 0) eq_(data['meta']['limit'], 2)
def test_record_stat_without_data(self): with self.assertRaises(ValueError): record_stat('app.install', self.request)