def reload_period_from_analytics(cls, period, verbose=False): """Replace the stats for the given period from Google Analytics.""" counts = googleanalytics.pageviews_by_document( *period_dates(period), verbose=verbose) if counts: # Close any existing connections because our load balancer times # them out at 5 minutes and the GA calls take forever. close_old_connections() # Delete and remake the rows: # Horribly inefficient until # http://code.djangoproject.com/ticket/9519 is fixed. # cls.objects.filter(period=period).delete() # Instead, we use raw SQL! cursor = connection.cursor() cursor.execute( 'DELETE FROM `dashboards_wikidocumentvisits`' ' WHERE `period` = %s', [period]) # Now we create them again with fresh data. for doc_id, visits in counts.iteritems(): cls.objects.create(document=Document(pk=doc_id), visits=visits, period=period) else: # Don't erase interesting data if there's nothing to replace it: log.warning('Google Analytics returned no interesting data,' ' so I kept what I had.')
def reload_period_from_analytics(cls, period, verbose=False): """Replace the stats for the given period from Google Analytics.""" counts = googleanalytics.pageviews_by_document(*period_dates(period), verbose=verbose) if counts: # Close any existing connections because our load balancer times # them out at 5 minutes and the GA calls take forever. close_old_connections() # Delete and remake the rows: # Horribly inefficient until # http://code.djangoproject.com/ticket/9519 is fixed. # cls.objects.filter(period=period).delete() # Instead, we use raw SQL! cursor = connection.cursor() cursor.execute( "DELETE FROM `dashboards_wikidocumentvisits`" " WHERE `period` = %s", [period] ) # Now we create them again with fresh data. for doc_id, visits in counts.items(): cls.objects.create(document=Document(pk=doc_id), visits=visits, period=period) else: # Don't erase interesting data if there's nothing to replace it: log.warning("Google Analytics returned no interesting data," " so I kept what I had.")
def reload_period_from_analytics(cls, period, verbose=False): """Replace the stats for the given period from Google Analytics.""" counts = googleanalytics.pageviews_by_document(*period_dates(period), verbose=verbose) if counts: # Delete and remake the rows: # Horribly inefficient until # http://code.djangoproject.com/ticket/9519 is fixed. cls.objects.filter(period=period).delete() for doc_id, visits in counts.iteritems(): cls.objects.create(document=Document(pk=doc_id), visits=visits, period=period) else: # Don't erase interesting data if there's nothing to replace it: log.warning("Google Analytics returned no interesting data," " so I kept what I had.")
def reload_period_from_analytics(cls, period): """Replace the stats for the given period from Google Analytics.""" counts = googleanalytics.pageviews_by_document(*period_dates(period)) if counts: # Delete and remake the rows: # Horribly inefficient until # http://code.djangoproject.com/ticket/9519 is fixed. cls.objects.filter(period=period).delete() for doc_id, visits in counts.iteritems(): cls.objects.create(document=Document(pk=doc_id), visits=visits, period=period) else: # Don't erase interesting data if there's nothing to replace it: log.warning('Google Analytics returned no interesting data,' ' so I kept what I had.')
def test_pageviews_by_document(self, _build_request): """Test googleanalytics.pageviews_by_document().""" execute = _build_request.return_value.get.return_value.execute execute.return_value = PAGEVIEWS_BY_DOCUMENT_RESPONSE # Add some documents that match the response data. documents = [] for i in range(1, 6): documents.append(ApprovedRevisionFactory(document__slug='doc-%s' % i).document) pageviews = googleanalytics.pageviews_by_document( date(2013, 01, 16), date(2013, 01, 16)) eq_(5, len(pageviews)) eq_(1, pageviews[documents[0].pk]) eq_(2, pageviews[documents[1].pk]) eq_(10, pageviews[documents[2].pk]) eq_(39, pageviews[documents[3].pk]) eq_(46, pageviews[documents[4].pk])
def test_pageviews_by_document(self, _build_request): """Test googleanalytics.pageviews_by_document().""" execute = _build_request.return_value.get.return_value.execute execute.return_value = PAGEVIEWS_BY_DOCUMENT_RESPONSE # Add some documents that match the response data. documents = [] for i in range(1, 6): documents.append(ApprovedRevisionFactory(document__slug='doc-%s' % i).document) pageviews = googleanalytics.pageviews_by_document( date(2013, 1, 16), date(2013, 1, 16)) eq_(5, len(pageviews)) eq_(1, pageviews[documents[0].pk]) eq_(2, pageviews[documents[1].pk]) eq_(10, pageviews[documents[2].pk]) eq_(39, pageviews[documents[3].pk]) eq_(46, pageviews[documents[4].pk])