def update_l10n_metric(): """Calculate new l10n coverage numbers and save. L10n coverage is a measure of the amount of translations that are up to date, weighted by the number of visits for each locale. The "algorithm" (see Bug 727084): SUMO visits = Total SUMO visits for the last 30 days; Total translated = 0; For each locale { Total up to date = Total up to date + ((Number of up to date articles in the en-US top 50 visited)/50 ) * (Visitors for that locale / SUMO visits)); } An up to date article is any of the following: * An en-US article (by definition it is always up to date) * The latest en-US revision has been translated * There are only new revisions with TYPO_SIGNIFICANCE not translated * There is only one revision of MEDIUM_SIGNIFICANCE not translated """ if settings.STAGE: # Let's be nice to GA and skip on stage. return # Get the top 60 visited articles. We will only use the top 50 # but a handful aren't localizable so we get some extras. top_60_docs = _get_top_docs(60) # Get the visits to each locale in the last 30 days. end = date.today() - timedelta(days=1) # yesterday start = end - timedelta(days=30) locale_visits = googleanalytics.visitors_by_locale(start, end) # Total visits. total_visits = sum(locale_visits.itervalues()) # Calculate the coverage. coverage = 0 for locale, visits in locale_visits.iteritems(): if locale == settings.WIKI_DEFAULT_LANGUAGE: num_docs = MAX_DOCS_UP_TO_DATE up_to_date_docs = MAX_DOCS_UP_TO_DATE else: up_to_date_docs, num_docs = _get_up_to_date_count( top_60_docs, locale) if num_docs and total_visits: coverage += ((float(up_to_date_docs) / num_docs) * (float(visits) / total_visits)) # Save the value to Metric table. metric_kind = MetricKind.objects.get(code=L10N_METRIC_CODE) day = date.today() Metric.objects.create( kind=metric_kind, start=day, end=day + timedelta(days=1), value=int(coverage * 100)) # Store as a % int.
def test_visitors_by_locale(self, _build_request): """Test googleanalytics.visits_by_locale().""" execute = _build_request.return_value.get.return_value.execute execute.return_value = VISITORS_BY_LOCALE_RESPONSE visits = googleanalytics.visitors_by_locale( date(2013, 01, 16), date(2013, 01, 16)) eq_(57, len(visits)) eq_(221447, visits['en-US']) eq_(24432, visits['es'])
def test_visitors_by_locale(self, _build_request): """Test googleanalytics.visits_by_locale().""" execute = _build_request.return_value.get.return_value.execute execute.return_value = VISITORS_BY_LOCALE_RESPONSE visits = googleanalytics.visitors_by_locale( date(2013, 1, 16), date(2013, 1, 16)) eq_(58, len(visits)) eq_(221447, visits['en-US']) eq_(24432, visits['es'])
def get_locales_by_visit(start_date, end_date): """Get a list of (locale, visits) tuples sorted descending by visits.""" cache_key = "locales_sorted_by_visits:{start}:{end}".format( start=start_date, end=end_date) sorted_locales = cache.get(cache_key) if sorted_locales is None: try: results = visitors_by_locale(start_date, end_date) locales_and_visits = list(results.items()) sorted_locales = list( reversed(sorted(locales_and_visits, key=lambda x: x[1]))) cache.add(cache_key, sorted_locales, settings.CACHE_LONG_TIMEOUT) except (GoogleAPIError, Oauth2Error, OpenSSLError): # Just return all locales with 0s for visits. log.exception("Something went wrong getting visitors by locale " "from Google Analytics. Nobody got a 500 though.") sorted_locales = [(lang, 0) for lang in settings.SUMO_LANGUAGES] return sorted_locales
def get_locales_by_visit(start_date, end_date): """Get a list of (locale, visits) tuples sorted descending by visits.""" cache_key = 'locales_sorted_by_visits:{start}:{end}'.format( start=start_date, end=end_date) sorted_locales = cache.get(cache_key) if sorted_locales is None: try: results = visitors_by_locale(start_date, end_date) locales_and_visits = results.items() sorted_locales = list(reversed(sorted( locales_and_visits, key=lambda x: x[1]))) cache.add(cache_key, sorted_locales, CACHE_TIMEOUT) except (GoogleAPIError, Oauth2Error, OpenSSLError): # Just return all locales with 0s for visits. log.exception('Something went wrong getting visitors by locale ' 'from Google Analytics. Nobody got a 500 though.') sorted_locales = [(l, 0) for l in settings.SUMO_LANGUAGES] return sorted_locales