Exemple #1
0
    def test_from_url(self):
        """Verify question returned from valid URL."""
        q = question(save=True)

        eq_(q, Question.from_url('/en-US/questions/%s' % q.id))
        eq_(q, Question.from_url('/es/questions/%s' % q.id))
        eq_(q, Question.from_url('/questions/%s' % q.id))
Exemple #2
0
    def test_from_url(self):
        """Verify question returned from valid URL."""
        q = QuestionFactory()

        eq_(q, Question.from_url('/en-US/questions/%s' % q.id))
        eq_(q, Question.from_url('/es/questions/%s' % q.id))
        eq_(q, Question.from_url('/questions/%s' % q.id))
Exemple #3
0
    def test_from_url(self):
        """Verify question returned from valid URL."""
        q = question(save=True)

        eq_(q, Question.from_url('/en-US/questions/%s' % q.id))
        eq_(q, Question.from_url('/es/questions/%s' % q.id))
        eq_(q, Question.from_url('/questions/%s' % q.id))
Exemple #4
0
    def test_from_url(self):
        """Verify question returned from valid URL."""
        q = QuestionFactory()

        eq_(q, Question.from_url("/en-US/questions/%s" % q.id))
        eq_(q, Question.from_url("/es/questions/%s" % q.id))
        eq_(q, Question.from_url("/questions/%s" % q.id))
Exemple #5
0
    def test_from_invalid_url(self):
        """Verify question returned from valid URL."""
        q = question(save=True)

        eq_(None, Question.from_url('/en-US/questions/%s/edit' % q.id))
        eq_(None, Question.from_url('/en-US/kb/%s' % q.id))
        eq_(None, Question.from_url('/random/url'))
        eq_(None, Question.from_url('/en-US/questions/stats'))
Exemple #6
0
    def test_from_invalid_url(self):
        """Verify question returned from valid URL."""
        q = QuestionFactory()

        eq_(None, Question.from_url('/en-US/questions/%s/edit' % q.id))
        eq_(None, Question.from_url('/en-US/kb/%s' % q.id))
        eq_(None, Question.from_url('/random/url'))
        eq_(None, Question.from_url('/en-US/questions/dashboard/metrics'))
Exemple #7
0
    def test_from_invalid_url(self):
        """Verify question returned from valid URL."""
        q = QuestionFactory()

        eq_(None, Question.from_url("/en-US/questions/%s/edit" % q.id))
        eq_(None, Question.from_url("/en-US/kb/%s" % q.id))
        eq_(None, Question.from_url("/random/url"))
        eq_(None, Question.from_url("/en-US/questions/dashboard/metrics"))
Exemple #8
0
    def test_from_invalid_url(self):
        """Verify question returned from valid URL."""
        q = question(save=True)

        eq_(None, Question.from_url('/en-US/questions/%s/edit' % q.id))
        eq_(None, Question.from_url('/en-US/kb/%s' % q.id))
        eq_(None, Question.from_url('/random/url'))
        eq_(None, Question.from_url('/en-US/questions/dashboard/metrics'))
Exemple #9
0
    def test_from_invalid_url(self):
        """Verify question returned from valid URL."""
        q = question(save=True)

        eq_(None, Question.from_url('/en-US/questions/{0!s}/edit'.format(q.id)))
        eq_(None, Question.from_url('/en-US/kb/{0!s}'.format(q.id)))
        eq_(None, Question.from_url('/random/url'))
        eq_(None, Question.from_url('/en-US/questions/dashboard/metrics'))
Exemple #10
0
def pageviews_by_question(start_date, end_date):
    """Return the number of pageviews by question in a given date range.

    Returns a dict with pageviews for each document:
        {question_id>: <pageviews>,
         1: 42,
         7: 1337,...}
    """
    counts = {}
    request = _build_request()
    start_index = 1
    max_results = 10000

    while True:  # To deal with pagination

        @retry_503
        def _make_request():
            return request.get(
                ids='ga:' + profile_id,
                start_date=str(start_date),
                end_date=str(end_date),
                metrics='ga:pageviews',
                dimensions='ga:pagePath',
                filters='ga:pagePathLevel2==/questions/',
                max_results=max_results,
                start_index=start_index).execute()

        results = _make_request()

        for result in results['rows']:
            path = result[0]
            pageviews = int(result[1])
            question_id = Question.from_url(path, id_only=True)
            if not question_id:
                continue

            # The same question can appear multiple times due to url params
            # and locale.
            counts[question_id] = counts.get(question_id, 0) + pageviews

        # Move to next page of results.
        start_index += max_results
        if start_index > results['totalResults']:
            break

    return counts
Exemple #11
0
 def test_from_url_id_only(self):
     """Verify question returned from valid URL."""
     # When requesting the id, the existence of the question isn't checked.
     eq_(123, Question.from_url('/en-US/questions/123', id_only=True))
     eq_(234, Question.from_url('/es/questions/234', id_only=True))
     eq_(345, Question.from_url('/questions/345', id_only=True))
Exemple #12
0
 def test_from_url_id_only(self):
     """Verify question returned from valid URL."""
     # When requesting the id, the existence of the question isn't checked.
     eq_(123, Question.from_url("/en-US/questions/123", id_only=True))
     eq_(234, Question.from_url("/es/questions/234", id_only=True))
     eq_(345, Question.from_url("/questions/345", id_only=True))
Exemple #13
0
def pageviews_by_question(start_date, end_date, verbose=False):
    """Return the number of pageviews by question in a given date range.

    Returns a dict with pageviews for each document:
        {question_id>: <pageviews>,
         1: 42,
         7: 1337,...}
    """
    counts = {}
    request = _build_request()
    max_results = 10000

    end_date_step = end_date

    while True:  # To reduce the size of result set request 3 months at a time
        start_date_step = end_date_step - timedelta(90)

        if start_date_step < start_date:
            start_date_step = start_date

        if verbose:
            print 'Fetching data for {0!s} to {1!s}:'.format(start_date_step,
                                                   end_date_step)

        start_index = 1

        while True:  # To deal with pagination

            @retry_503
            def _make_request():
                return request.get(
                    ids='ga:' + profile_id,
                    start_date=str(start_date_step),
                    end_date=str(end_date_step),
                    metrics='ga:pageviews',
                    dimensions='ga:pagePath',
                    filters='ga:pagePathLevel2==/questions/',
                    max_results=max_results,
                    start_index=start_index).execute()

            results = _make_request()

            if verbose:
                d = (max_results - 1
                     if start_index + max_results - 1 < results['totalResults']
                     else results['totalResults'] - start_index)
                print '- Got {0!s} of {1!s} results.'.format(start_index + d,
                                                   results['totalResults'])

            for result in results['rows']:
                path = result[0]
                pageviews = int(result[1])
                question_id = Question.from_url(path, id_only=True)
                if not question_id:
                    continue

                # The same question can appear multiple times due to url params
                # and locale.
                counts[question_id] = counts.get(question_id, 0) + pageviews

            # Move to next page of results.
            start_index += max_results
            if start_index > results['totalResults']:
                break

        end_date_step = start_date_step - timedelta(1)

        if start_date_step == start_date or end_date_step < start_date:
            break

    return counts
def pageviews_by_question(start_date, end_date, verbose=False):
    """Return the number of pageviews by question in a given date range.

    Returns a dict with pageviews for each document:
        {question_id>: <pageviews>,
         1: 42,
         7: 1337,...}
    """
    counts = {}
    request = _build_request()
    max_results = 10000

    end_date_step = end_date

    while True:  # To reduce the size of result set request 3 months at a time
        start_date_step = end_date_step - timedelta(90)

        if start_date_step < start_date:
            start_date_step = start_date

        if verbose:
            print 'Fetching data for %s to %s:' % (start_date_step,
                                                   end_date_step)

        start_index = 1

        while True:  # To deal with pagination

            @retry_503
            def _make_request():
                return request.get(ids='ga:' + profile_id,
                                   start_date=str(start_date_step),
                                   end_date=str(end_date_step),
                                   metrics='ga:pageviews',
                                   dimensions='ga:pagePath',
                                   filters='ga:pagePathLevel2==/questions/',
                                   max_results=max_results,
                                   start_index=start_index).execute()

            results = _make_request()

            if verbose:
                d = (max_results - 1 if start_index + max_results -
                     1 < results['totalResults'] else results['totalResults'] -
                     start_index)
                print '- Got %s of %s results.' % (start_index + d,
                                                   results['totalResults'])

            for result in results['rows']:
                path = result[0]
                pageviews = int(result[1])
                question_id = Question.from_url(path, id_only=True)
                if not question_id:
                    continue

                # The same question can appear multiple times due to url params
                # and locale.
                counts[question_id] = counts.get(question_id, 0) + pageviews

            # Move to next page of results.
            start_index += max_results
            if start_index > results['totalResults']:
                break

        end_date_step = start_date_step - timedelta(1)

        if start_date_step == start_date or end_date_step < start_date:
            break

    return counts