def stat_json(request, stat_name): start = _parse_date(request.GET.get('start', ''), date.today() - timedelta(days=7)) end = _parse_date(request.GET.get('end', ''), date.today()) account = request.GET.get('account', '') data = {} if account: analytics = AccountAnalytics(account) else: analytics = OverallAnalytics() # if not hasattr(analytics, stat_name): # raise Http404("Stat not found.") data = { 'result': analytics.get_stats(stat_name, start_date=start, end_date=end) # 'result': getattr(analytics, stat_name)(start_date=start, end_date=end) } return HttpResponse(json.dumps(data), mimetype='application/json')
def setUp(self): from analytics.models import AccountAnalytics, OverallAnalytics from accounts.tests import setUpAccounts setUpAccounts(self) account = self.bob account2 = self.alice self.analytics = AccountAnalytics(account, redis_db=15) self.analytics2 = AccountAnalytics(account2, redis_db=15) self.overall_analytics = OverallAnalytics(redis_db=15) self.redis = self.analytics.conn self.redis.flushdb()
def setUp(self): from analytics.models import AccountAnalytics, OverallAnalytics from accounts.models import Account from accounts.tests import setUpAccounts from locations.tests import setUpLocations from resources.tests import setUpResources from enginecab.views import reindex_resources setUpAccounts(self) setUpLocations(self) setUpResources(self) reindex_resources(url=settings.TEST_SOLR_URL) self.account = Account.objects[2] self.account_analytics = AccountAnalytics( self.account, redis_db=settings.TEST_REDIS_ANALYTICS_DATABASE) self.overall_analytics = OverallAnalytics( redis_db=settings.TEST_REDIS_ANALYTICS_DATABASE) self.redis = self.account_analytics.conn self.redis.flushdb() from django.test.client import Client self.client = Client()
def stat_json(request, stat_name): start = _parse_date(request.GET.get('start', ''), date.today() - timedelta(days=7)) end = _parse_date(request.GET.get('end', ''), date.today()) account = request.GET.get('account', '') data = { } if account: analytics = AccountAnalytics(account) else: analytics = OverallAnalytics() # if not hasattr(analytics, stat_name): # raise Http404("Stat not found.") data = { 'result': analytics.get_stats(stat_name, start_date=start, end_date=end) # 'result': getattr(analytics, stat_name)(start_date=start, end_date=end) } return HttpResponse(json.dumps(data), mimetype='application/json')
class AccountStatsTestCase(MongoTestCase): def setUp(self): from analytics.models import AccountAnalytics, OverallAnalytics from accounts.tests import setUpAccounts setUpAccounts(self) account = self.bob account2 = self.alice self.analytics = AccountAnalytics(account, redis_db=15) self.analytics2 = AccountAnalytics(account2, redis_db=15) self.overall_analytics = OverallAnalytics(redis_db=15) self.redis = self.analytics.conn self.redis.flushdb() def test_tag(self): # what tags are being searched for from datetime import datetime # Add 30 for i in range(1, 31): dt = datetime(2011, 11, i) self.analytics.increment_tags("Sport and fitness", date_instance=dt, count=i) # Add 25 for i in range(1, 26): dt = datetime(2011, 11, i) self.analytics.increment_tags("Mental Health", date_instance=dt, count=i) # Add 15 for i in range(11, 26): dt = datetime(2011, 11, i) self.analytics.increment_tags("Hobbies, arts and crafts", date_instance=dt, count=i) start_date = datetime(2011, 11, 1) end_date = datetime(2011, 11, 30) result = self.analytics.top_tags(start_date, end_date) # Create a report for the full month, should contain everything we # added expected_result = [ ('Sport and fitness', 30), ('Mental Health', 25), ('Hobbies, arts and crafts', 15), ] self.assertEqual(result, expected_result) # Create another report, but only for one day - should only contain # the expected result below. start_date = datetime(2011, 11, 1) end_date = datetime(2011, 11, 1) result = self.analytics.top_tags(start_date, end_date) expected_result = [ ('Mental Health', 1), ('Sport and fitness', 1), ] self.assertEqual(result, expected_result) def test_overall_analytics(self): """ Incremement the tag stat counter for two different accounts. Check the values are right in the summary for those accounts and then check that the overall stats are correctly the sum of the accounts. """ from string import uppercase from datetime import datetime dt = datetime(2011, 11, 20) # Increment A-K for both for i in range(0, 11): self.analytics.increment_tags(uppercase[i], date_instance=dt, count=i) self.analytics2.increment_tags(uppercase[i], date_instance=dt, count=i) # Increment A-F again for account 1 for i in range(0, 6): self.analytics.increment_tags(uppercase[i], date_instance=dt, count=i) # Increment G-K again for account 2 for i in range(6, 11): self.analytics2.increment_tags(uppercase[i], date_instance=dt, count=i) account1_result = self.analytics.top_tags(dt, dt) account2_result = self.analytics2.top_tags(dt, dt) overall_result = self.overall_analytics.top_tags(dt, dt) account1_expected = [('A', 2), ('B', 2), ('C', 2), ('D', 2), ('E', 2), ('F', 2), ('G', 1), ('H', 1), ('I', 1), ('J', 1), ('K', 1), ] account2_expected = [('G', 2), ('H', 2), ('I', 2), ('J', 2), ('K', 2), ('A', 1), ('B', 1), ('C', 1), ('D', 1), ('E', 1), ('F', 1), ] overall_expected = [('A', 3), ('B', 3), ('C', 3), ('D', 3), ('E', 3), ('F', 3), ('G', 3), ('H', 3), ('I', 3), ('J', 3), ('K', 3), ] self.assertEqual(account1_result, account1_expected) self.assertEqual(account2_result, account2_expected) self.assertEqual(overall_expected, overall_result)
def increment_resource_crud(action_type, account=None): # return AccountAnalytics(account).increment_resource_crud(action_type)
def increment_api_resources(object_id, account=None): # return if not object_id: return AccountAnalytics(account).increment_api_resources(object_id)
def increment_api_locations(location, account=None): # return if not location: return AccountAnalytics(account).increment_api_locations(location)
def increment_api_queries(query, account=None): # return if not query: return AccountAnalytics(account).increment_api_queries(query)
def increment_tags(tag_name, account=None): # return if not tag_name: return AccountAnalytics(account).increment_tags(tag_name)
def process_request(self, request): """ request.META CONTENT_LENGTH -- the length of the request body (as a string). CONTENT_TYPE -- the MIME type of the request body. HTTP_ACCEPT_ENCODING -- Acceptable encodings for the response. HTTP_ACCEPT_LANGUAGE -- Acceptable languages for the response. HTTP_HOST -- The HTTP Host header sent by the client. HTTP_REFERER -- The referring page, if any. HTTP_USER_AGENT -- The client's user-agent string. QUERY_STRING -- The query string, as a single (unparsed) string. REMOTE_ADDR -- The IP address of the client. REMOTE_HOST -- The hostname of the client. REMOTE_USER -- The user authenticated by the Web server, if any. REQUEST_METHOD -- A string such as "GET" or "POST". SERVER_NAME -- The hostname of the server. SERVER_PORT -- The port of the server (as a string). """ analytics = AccountAnalytics(None) log = True META_KEYS = [ 'HTTP_USER_AGENT', ] for key in META_KEYS: value = request.META.get(key) if value: if key == 'HTTP_USER_AGENT': try: # parser.detect gives more detail- could break out into OS, browser, version stats... # simple_detect -> ('Linux', 'Chrome 5.0.307.11') agent_data = httpagentparser.detect(value) # print agent_data value = None if agent_data: value = agent_data.get('browser') if value: value = '%s %s' % (value.get( 'name', 'no name'), value.get( 'version', '').split('.')[0]) if value is None: value = 'Unknown Browser' log = False # value = httpagentparser.simple_detect(value)[1] # print httpagentparser.detect(value) # log = value != 'Unknown Browser' # agent_data = value.split() # value = '%s %s' % (' '.join(agent_data[:-1]), agent_data[-1].split('.')[0]) # print value except IndexError: pass if log: analytics.increment(key, field=value) request.META["ENGINE_LOG"] = log if log: DETAIL_PATHS = ['resource', 'accounts_detail'] try: match = resolve(request.path) # print match.url_name, match.args, match.kwargs if match.url_name in DETAIL_PATHS and 'object_id' in match.kwargs: analytics.increment(match.url_name, field=match.kwargs['object_id']) except Resolver404: pass return None
def process_request(self, request): """ request.META CONTENT_LENGTH -- the length of the request body (as a string). CONTENT_TYPE -- the MIME type of the request body. HTTP_ACCEPT_ENCODING -- Acceptable encodings for the response. HTTP_ACCEPT_LANGUAGE -- Acceptable languages for the response. HTTP_HOST -- The HTTP Host header sent by the client. HTTP_REFERER -- The referring page, if any. HTTP_USER_AGENT -- The client's user-agent string. QUERY_STRING -- The query string, as a single (unparsed) string. REMOTE_ADDR -- The IP address of the client. REMOTE_HOST -- The hostname of the client. REMOTE_USER -- The user authenticated by the Web server, if any. REQUEST_METHOD -- A string such as "GET" or "POST". SERVER_NAME -- The hostname of the server. SERVER_PORT -- The port of the server (as a string). """ analytics = AccountAnalytics(None) log = True META_KEYS = ['HTTP_USER_AGENT', ] for key in META_KEYS: value = request.META.get(key) if value: if key == 'HTTP_USER_AGENT': try: # parser.detect gives more detail- could break out into OS, browser, version stats... # simple_detect -> ('Linux', 'Chrome 5.0.307.11') agent_data = httpagentparser.detect(value) # print agent_data value = None if agent_data: value = agent_data.get('browser') if value: value = '%s %s' % (value.get('name', 'no name'), value.get('version','').split('.')[0]) if value is None: value = 'Unknown Browser' log = False # value = httpagentparser.simple_detect(value)[1] # print httpagentparser.detect(value) # log = value != 'Unknown Browser' # agent_data = value.split() # value = '%s %s' % (' '.join(agent_data[:-1]), agent_data[-1].split('.')[0]) # print value except IndexError: pass if log: analytics.increment(key, field=value) request.META["ENGINE_LOG"] = log if log: DETAIL_PATHS = ['resource', 'accounts_detail'] try: match = resolve(request.path) # print match.url_name, match.args, match.kwargs if match.url_name in DETAIL_PATHS and 'object_id' in match.kwargs: analytics.increment(match.url_name, field=match.kwargs['object_id']) except Resolver404: pass return None
class AccountStatsTestCase(MongoTestCase): def setUp(self): from analytics.models import AccountAnalytics, OverallAnalytics from accounts.tests import setUpAccounts setUpAccounts(self) account = self.bob account2 = self.alice self.analytics = AccountAnalytics(account, redis_db=15) self.analytics2 = AccountAnalytics(account2, redis_db=15) self.overall_analytics = OverallAnalytics(redis_db=15) self.redis = self.analytics.conn self.redis.flushdb() def test_tag(self): # what tags are being searched for from datetime import datetime # Add 30 for i in range(1, 31): dt = datetime(2011, 11, i) self.analytics.increment_tags("Sport and fitness", date_instance=dt, count=i) # Add 25 for i in range(1, 26): dt = datetime(2011, 11, i) self.analytics.increment_tags("Mental Health", date_instance=dt, count=i) # Add 15 for i in range(11, 26): dt = datetime(2011, 11, i) self.analytics.increment_tags("Hobbies, arts and crafts", date_instance=dt, count=i) start_date = datetime(2011, 11, 1) end_date = datetime(2011, 11, 30) result = self.analytics.top_tags(start_date, end_date) # Create a report for the full month, should contain everything we # added expected_result = [ ('Sport and fitness', 30), ('Mental Health', 25), ('Hobbies, arts and crafts', 15), ] self.assertEqual(result, expected_result) # Create another report, but only for one day - should only contain # the expected result below. start_date = datetime(2011, 11, 1) end_date = datetime(2011, 11, 1) result = self.analytics.top_tags(start_date, end_date) expected_result = [ ('Mental Health', 1), ('Sport and fitness', 1), ] self.assertEqual(result, expected_result) def test_overall_analytics(self): """ Incremement the tag stat counter for two different accounts. Check the values are right in the summary for those accounts and then check that the overall stats are correctly the sum of the accounts. """ from string import uppercase from datetime import datetime dt = datetime(2011, 11, 20) # Increment A-K for both for i in range(0, 11): self.analytics.increment_tags(uppercase[i], date_instance=dt, count=i) self.analytics2.increment_tags(uppercase[i], date_instance=dt, count=i) # Increment A-F again for account 1 for i in range(0, 6): self.analytics.increment_tags(uppercase[i], date_instance=dt, count=i) # Increment G-K again for account 2 for i in range(6, 11): self.analytics2.increment_tags(uppercase[i], date_instance=dt, count=i) account1_result = self.analytics.top_tags(dt, dt) account2_result = self.analytics2.top_tags(dt, dt) overall_result = self.overall_analytics.top_tags(dt, dt) account1_expected = [ ('A', 2), ('B', 2), ('C', 2), ('D', 2), ('E', 2), ('F', 2), ('G', 1), ('H', 1), ('I', 1), ('J', 1), ('K', 1), ] account2_expected = [ ('G', 2), ('H', 2), ('I', 2), ('J', 2), ('K', 2), ('A', 1), ('B', 1), ('C', 1), ('D', 1), ('E', 1), ('F', 1), ] overall_expected = [ ('A', 3), ('B', 3), ('C', 3), ('D', 3), ('E', 3), ('F', 3), ('G', 3), ('H', 3), ('I', 3), ('J', 3), ('K', 3), ] self.assertEqual(account1_result, account1_expected) self.assertEqual(account2_result, account2_expected) self.assertEqual(overall_expected, overall_result)