Example #1
0
    def reload_from_analytics(cls):
        """Update the stats from Google Analytics."""
        from kitsune.sumo import googleanalytics
        counts = googleanalytics.pageviews_by_question(
            settings.GA_START_DATE, date.today())
        if counts:
            for question_id, visits in counts.iteritems():
                # We are trying to minimize db calls here. Let's try to update
                # first, that will be the common case.
                num = cls.objects.filter(
                    question_id=question_id).update(visits=visits)

                # If we were able to update, we are done.
                if num > 0:
                    continue

                # If it doesn't exist yet, create it.
                try:
                    cls.objects.create(
                        question_id=question_id, visits=visits)
                except IntegrityError:
                    # The question doesn't exist anymore, move on.
                    continue
        else:
            log.warning('Google Analytics returned no interesting data,'
                        ' so I kept what I had.')
Example #2
0
    def reload_from_analytics(cls, verbose=False):
        """Update the stats from Google Analytics."""
        from kitsune.sumo import googleanalytics
        counts = googleanalytics.pageviews_by_question(
            settings.GA_START_DATE, date.today(), verbose=verbose)
        if counts:
            for question_id, visits in counts.iteritems():
                # We are trying to minimize db calls here. Let's try to update
                # first, that will be the common case.
                num = cls.uncached.filter(
                    question_id=question_id).update(visits=visits)

                # If we were able to update, we are done.
                if num > 0:
                    continue
                # If it doesn't exist yet, create it.
                try:
                    # For some reason unbeknowest to me,
                    # IntegrityError doesn't get kicked up in the
                    # QuestionVisits tests when running the tests with
                    # a clean db. So to deal with that, we explicitly
                    # check the db to see if the question exists.
                    # This happens with Django 1.4.6 and South
                    # 0.8.2. If we update either, we might want to see
                    # if this is still a problem.
                    Question.uncached.get(pk=question_id)
                    cls.uncached.create(
                        question_id=question_id, visits=visits)
                except (Question.DoesNotExist, IntegrityError):
                    # The question doesn't exist anymore, move on.
                    continue
        else:
            log.warning('Google Analytics returned no interesting data,'
                        ' so I kept what I had.')
Example #3
0
    def reload_from_analytics(cls):
        """Update the stats from Google Analytics."""
        from kitsune.sumo import googleanalytics
        counts = googleanalytics.pageviews_by_question(settings.GA_START_DATE,
                                                       date.today())
        if counts:
            for question_id, visits in counts.iteritems():
                # We are trying to minimize db calls here. Let's try to update
                # first, that will be the common case.
                num = cls.objects.filter(question_id=question_id).update(
                    visits=visits)

                # If we were able to update, we are done.
                if num > 0:
                    continue

                # If it doesn't exist yet, create it.
                try:
                    cls.objects.create(question_id=question_id, visits=visits)
                except IntegrityError:
                    # The question doesn't exist anymore, move on.
                    continue
        else:
            log.warning('Google Analytics returned no interesting data,'
                        ' so I kept what I had.')
    def test_pageviews_by_question(self, _build_request):
        """Test googleanalytics.pageviews_by_question()."""
        execute = _build_request.return_value.get.return_value.execute
        execute.return_value = PAGEVIEWS_BY_QUESTION_RESPONSE

        pageviews = googleanalytics.pageviews_by_question(
            date(2013, 01, 16), date(2013, 01, 16))

        eq_(3, len(pageviews))
        eq_(3, pageviews[1])
        eq_(2, pageviews[2])
        eq_(11, pageviews[3])
Example #5
0
    def test_pageviews_by_question(self, _build_request):
        """Test googleanalytics.pageviews_by_question()."""
        execute = _build_request.return_value.get.return_value.execute
        execute.return_value = PAGEVIEWS_BY_QUESTION_RESPONSE

        pageviews = googleanalytics.pageviews_by_question(
            date(2013, 1, 16), date(2013, 1, 16))

        eq_(3, len(pageviews))
        eq_(3, pageviews[1])
        eq_(2, pageviews[2])
        eq_(11, pageviews[3])
Example #6
0
    def reload_from_analytics(cls, verbose=False):
        """Update the stats from Google Analytics."""
        from kitsune.sumo import googleanalytics

        today = date.today()
        one_year_ago = today - timedelta(days=365)
        counts = googleanalytics.pageviews_by_question(one_year_ago,
                                                       today,
                                                       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()

            for question_id, visits in counts.items():
                # We are trying to minimize db calls here. Let's try to update
                # first, that will be the common case.
                num = cls.objects.filter(question_id=question_id).update(
                    visits=visits)

                # If we were able to update, we are done.
                if num > 0:
                    continue
                # If it doesn't exist yet, create it.
                try:
                    # For some reason unbeknowest to me,
                    # IntegrityError doesn't get kicked up in the
                    # QuestionVisits tests when running the tests with
                    # a clean db. So to deal with that, we explicitly
                    # check the db to see if the question exists.
                    # This happens with Django 1.4.6 and South
                    # 0.8.2. If we update either, we might want to see
                    # if this is still a problem.
                    Question.objects.get(pk=question_id)
                    cls.objects.create(question_id=question_id, visits=visits)
                except (Question.DoesNotExist, IntegrityError):
                    # The question doesn't exist anymore, move on.
                    continue
        else:
            log.warning("Google Analytics returned no interesting data,"
                        " so I kept what I had.")