Home | History | Annotate | Download | only in common
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "components/translate/core/common/translate_metrics.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/metrics/histogram.h"
      9 
     10 namespace translate {
     11 
     12 namespace {
     13 
     14 // Constant string values to indicate UMA names. All entries should have
     15 // a corresponding index in MetricsNameIndex and an entry in |kMetricsEntries|.
     16 const char kRenderer4LanguageDetection[] = "Renderer4.LanguageDetection";
     17 const char kTranslateContentLanguage[] = "Translate.ContentLanguage";
     18 const char kTranslateHtmlLang[] = "Translate.HtmlLang";
     19 const char kTranslateLanguageVerification[] = "Translate.LanguageVerification";
     20 const char kTranslateTimeToBeReady[] = "Translate.TimeToBeReady";
     21 const char kTranslateTimeToLoad[] = "Translate.TimeToLoad";
     22 const char kTranslateTimeToTranslate[] = "Translate.TimeToTranslate";
     23 const char kTranslateUserActionDuration[] = "Translate.UserActionDuration";
     24 const char kTranslatePageScheme[] = "Translate.PageScheme";
     25 const char kTranslateSimilarLanguageMatch[] = "Translate.SimilarLanguageMatch";
     26 
     27 const char kSchemeHttp[] = "http";
     28 const char kSchemeHttps[] = "https";
     29 
     30 struct MetricsEntry {
     31   MetricsNameIndex index;
     32   const char* const name;
     33 };
     34 
     35 // This entry table should be updated when new UMA items are added.
     36 const MetricsEntry kMetricsEntries[] = {
     37     {UMA_LANGUAGE_DETECTION, kRenderer4LanguageDetection},
     38     {UMA_CONTENT_LANGUAGE, kTranslateContentLanguage},
     39     {UMA_HTML_LANG, kTranslateHtmlLang},
     40     {UMA_LANGUAGE_VERIFICATION, kTranslateLanguageVerification},
     41     {UMA_TIME_TO_BE_READY, kTranslateTimeToBeReady},
     42     {UMA_TIME_TO_LOAD, kTranslateTimeToLoad},
     43     {UMA_TIME_TO_TRANSLATE, kTranslateTimeToTranslate},
     44     {UMA_USER_ACTION_DURATION, kTranslateUserActionDuration},
     45     {UMA_PAGE_SCHEME, kTranslatePageScheme},
     46     {UMA_SIMILAR_LANGUAGE_MATCH, kTranslateSimilarLanguageMatch}, };
     47 
     48 COMPILE_ASSERT(arraysize(kMetricsEntries) == UMA_MAX,
     49                arraysize_of_kMetricsEntries_should_be_UMA_MAX);
     50 
     51 LanguageCheckType GetLanguageCheckMetric(const std::string& provided_code,
     52                                          const std::string& revised_code) {
     53   if (provided_code.empty())
     54     return LANGUAGE_NOT_PROVIDED;
     55   else if (provided_code == revised_code)
     56     return LANGUAGE_VALID;
     57   return LANGUAGE_INVALID;
     58 }
     59 
     60 }  // namespace
     61 
     62 void ReportContentLanguage(const std::string& provided_code,
     63                            const std::string& revised_code) {
     64   UMA_HISTOGRAM_ENUMERATION(kTranslateContentLanguage,
     65                             GetLanguageCheckMetric(provided_code, revised_code),
     66                             LANGUAGE_MAX);
     67 }
     68 
     69 void ReportHtmlLang(const std::string& provided_code,
     70                     const std::string& revised_code) {
     71   UMA_HISTOGRAM_ENUMERATION(kTranslateHtmlLang,
     72                             GetLanguageCheckMetric(provided_code, revised_code),
     73                             LANGUAGE_MAX);
     74 }
     75 
     76 void ReportLanguageVerification(LanguageVerificationType type) {
     77   UMA_HISTOGRAM_ENUMERATION(kTranslateLanguageVerification,
     78                             type,
     79                             LANGUAGE_VERIFICATION_MAX);
     80 }
     81 
     82 void ReportTimeToBeReady(double time_in_msec) {
     83   UMA_HISTOGRAM_MEDIUM_TIMES(
     84       kTranslateTimeToBeReady,
     85       base::TimeDelta::FromMicroseconds(time_in_msec * 1000.0));
     86 }
     87 
     88 void ReportTimeToLoad(double time_in_msec) {
     89   UMA_HISTOGRAM_MEDIUM_TIMES(
     90       kTranslateTimeToLoad,
     91       base::TimeDelta::FromMicroseconds(time_in_msec * 1000.0));
     92 }
     93 
     94 void ReportTimeToTranslate(double time_in_msec) {
     95   UMA_HISTOGRAM_MEDIUM_TIMES(
     96       kTranslateTimeToTranslate,
     97       base::TimeDelta::FromMicroseconds(time_in_msec * 1000.0));
     98 }
     99 
    100 void ReportUserActionDuration(base::TimeTicks begin, base::TimeTicks end) {
    101   UMA_HISTOGRAM_LONG_TIMES(kTranslateUserActionDuration, end - begin);
    102 }
    103 
    104 void ReportPageScheme(const std::string& scheme) {
    105   SchemeType type = SCHEME_OTHERS;
    106   if (scheme == kSchemeHttp)
    107     type = SCHEME_HTTP;
    108   else if (scheme == kSchemeHttps)
    109     type = SCHEME_HTTPS;
    110   UMA_HISTOGRAM_ENUMERATION(kTranslatePageScheme, type, SCHEME_MAX);
    111 }
    112 
    113 void ReportLanguageDetectionTime(base::TimeTicks begin, base::TimeTicks end) {
    114   UMA_HISTOGRAM_MEDIUM_TIMES(kRenderer4LanguageDetection, end - begin);
    115 }
    116 
    117 void ReportSimilarLanguageMatch(bool match) {
    118   UMA_HISTOGRAM_BOOLEAN(kTranslateSimilarLanguageMatch, match);
    119 }
    120 
    121 const char* GetMetricsName(MetricsNameIndex index) {
    122   for (size_t i = 0; i < arraysize(kMetricsEntries); ++i) {
    123     if (kMetricsEntries[i].index == index)
    124       return kMetricsEntries[i].name;
    125   }
    126   NOTREACHED();
    127   return NULL;
    128 }
    129 
    130 }  // namespace translate
    131