Commit 40c92451 authored by martis's avatar martis Committed by Commit bot

Removed translate MetricsNameIndex.

The enum is unnecessary and is only used in tests. I've introduced an internal
namespace, but I'm happy to change if there's a more idiomatic approach.

BUG=712922

Review-Url: https://codereview.chromium.org/2825943002
Cr-Commit-Position: refs/heads/master@{#467276}
parent b48de553
......@@ -15,10 +15,8 @@
namespace translate {
namespace {
namespace metrics_internal {
// Constant string values to indicate UMA names. All entries should have
// a corresponding index in MetricsNameIndex and an entry in |kMetricsEntries|.
const char kRenderer4LanguageDetection[] = "Renderer4.LanguageDetection";
const char kTranslateContentLanguage[] = "Translate.ContentLanguage";
const char kTranslateHtmlLang[] = "Translate.HtmlLang";
......@@ -32,28 +30,9 @@ const char kTranslateSimilarLanguageMatch[] = "Translate.SimilarLanguageMatch";
const char kTranslateLanguageDetectionConflict[] =
"Translate.LanguageDetectionConflict";
struct MetricsEntry {
MetricsNameIndex index;
const char* const name;
};
// This entry table should be updated when new UMA items are added.
const MetricsEntry kMetricsEntries[] = {
{UMA_LANGUAGE_DETECTION, kRenderer4LanguageDetection},
{UMA_CONTENT_LANGUAGE, kTranslateContentLanguage},
{UMA_HTML_LANG, kTranslateHtmlLang},
{UMA_LANGUAGE_VERIFICATION, kTranslateLanguageVerification},
{UMA_TIME_TO_BE_READY, kTranslateTimeToBeReady},
{UMA_TIME_TO_LOAD, kTranslateTimeToLoad},
{UMA_TIME_TO_TRANSLATE, kTranslateTimeToTranslate},
{UMA_USER_ACTION_DURATION, kTranslateUserActionDuration},
{UMA_PAGE_SCHEME, kTranslatePageScheme},
{UMA_SIMILAR_LANGUAGE_MATCH, kTranslateSimilarLanguageMatch},
{UMA_LANGUAGE_DETECTION_CONFLICT, kTranslateLanguageDetectionConflict},
};
static_assert(arraysize(kMetricsEntries) == UMA_MAX,
"kMetricsEntries should have UMA_MAX elements");
} // namespace metrics_internal
namespace {
// Page languages for which we track CLD3 language conflicts.
const char* kLanguageDetectionConflictPageLangs[] = {
......@@ -72,43 +51,44 @@ LanguageCheckType GetLanguageCheckMetric(const std::string& provided_code,
void ReportContentLanguage(const std::string& provided_code,
const std::string& revised_code) {
UMA_HISTOGRAM_ENUMERATION(kTranslateContentLanguage,
UMA_HISTOGRAM_ENUMERATION(metrics_internal::kTranslateContentLanguage,
GetLanguageCheckMetric(provided_code, revised_code),
LANGUAGE_MAX);
}
void ReportHtmlLang(const std::string& provided_code,
const std::string& revised_code) {
UMA_HISTOGRAM_ENUMERATION(kTranslateHtmlLang,
UMA_HISTOGRAM_ENUMERATION(metrics_internal::kTranslateHtmlLang,
GetLanguageCheckMetric(provided_code, revised_code),
LANGUAGE_MAX);
}
void ReportLanguageVerification(LanguageVerificationType type) {
UMA_HISTOGRAM_ENUMERATION(kTranslateLanguageVerification, type,
LANGUAGE_VERIFICATION_MAX);
UMA_HISTOGRAM_ENUMERATION(metrics_internal::kTranslateLanguageVerification,
type, LANGUAGE_VERIFICATION_MAX);
}
void ReportTimeToBeReady(double time_in_msec) {
UMA_HISTOGRAM_MEDIUM_TIMES(kTranslateTimeToBeReady,
UMA_HISTOGRAM_MEDIUM_TIMES(metrics_internal::kTranslateTimeToBeReady,
base::TimeDelta::FromMicroseconds(
static_cast<int64_t>(time_in_msec * 1000.0)));
}
void ReportTimeToLoad(double time_in_msec) {
UMA_HISTOGRAM_MEDIUM_TIMES(kTranslateTimeToLoad,
UMA_HISTOGRAM_MEDIUM_TIMES(metrics_internal::kTranslateTimeToLoad,
base::TimeDelta::FromMicroseconds(
static_cast<int64_t>(time_in_msec * 1000.0)));
}
void ReportTimeToTranslate(double time_in_msec) {
UMA_HISTOGRAM_MEDIUM_TIMES(kTranslateTimeToTranslate,
UMA_HISTOGRAM_MEDIUM_TIMES(metrics_internal::kTranslateTimeToTranslate,
base::TimeDelta::FromMicroseconds(
static_cast<int64_t>(time_in_msec * 1000.0)));
}
void ReportUserActionDuration(base::TimeTicks begin, base::TimeTicks end) {
UMA_HISTOGRAM_LONG_TIMES(kTranslateUserActionDuration, end - begin);
UMA_HISTOGRAM_LONG_TIMES(metrics_internal::kTranslateUserActionDuration,
end - begin);
}
void ReportPageScheme(const std::string& scheme) {
......@@ -117,15 +97,18 @@ void ReportPageScheme(const std::string& scheme) {
type = SCHEME_HTTP;
else if (scheme == url::kHttpsScheme)
type = SCHEME_HTTPS;
UMA_HISTOGRAM_ENUMERATION(kTranslatePageScheme, type, SCHEME_MAX);
UMA_HISTOGRAM_ENUMERATION(metrics_internal::kTranslatePageScheme, type,
SCHEME_MAX);
}
void ReportLanguageDetectionTime(base::TimeTicks begin, base::TimeTicks end) {
UMA_HISTOGRAM_MEDIUM_TIMES(kRenderer4LanguageDetection, end - begin);
UMA_HISTOGRAM_MEDIUM_TIMES(metrics_internal::kRenderer4LanguageDetection,
end - begin);
}
void ReportSimilarLanguageMatch(bool match) {
UMA_HISTOGRAM_BOOLEAN(kTranslateSimilarLanguageMatch, match);
UMA_HISTOGRAM_BOOLEAN(metrics_internal::kTranslateSimilarLanguageMatch,
match);
}
void ReportLanguageDetectionConflict(const std::string& page_lang,
......@@ -137,17 +120,8 @@ void ReportLanguageDetectionConflict(const std::string& page_lang,
it == std::end(kLanguageDetectionConflictPageLangs) ? "other" : *it;
UMA_HISTOGRAM_SPARSE_SLOWLY(
kTranslateLanguageDetectionConflict,
metrics_internal::kTranslateLanguageDetectionConflict,
base::HashMetricName(page_lang_token + "," + cld_lang));
}
const char* GetMetricsName(MetricsNameIndex index) {
for (size_t i = 0; i < arraysize(kMetricsEntries); ++i) {
if (kMetricsEntries[i].index == index)
return kMetricsEntries[i].name;
}
NOTREACHED();
return NULL;
}
} // namespace translate
......@@ -11,22 +11,24 @@
namespace translate {
// An indexing type to query each UMA entry name via GetMetricsName() function.
// Note: |kMetricsEntries| should be updated when a new entry is added here.
enum MetricsNameIndex {
UMA_LANGUAGE_DETECTION,
UMA_CONTENT_LANGUAGE,
UMA_HTML_LANG,
UMA_LANGUAGE_VERIFICATION,
UMA_TIME_TO_BE_READY,
UMA_TIME_TO_LOAD,
UMA_TIME_TO_TRANSLATE,
UMA_USER_ACTION_DURATION,
UMA_PAGE_SCHEME,
UMA_SIMILAR_LANGUAGE_MATCH,
UMA_LANGUAGE_DETECTION_CONFLICT,
UMA_MAX,
};
// Internals exposed for testing purposes. Should not be relied on by client
// code.
namespace metrics_internal {
// Constant string values to indicate UMA names.
extern const char kRenderer4LanguageDetection[];
extern const char kTranslateContentLanguage[];
extern const char kTranslateHtmlLang[];
extern const char kTranslateLanguageVerification[];
extern const char kTranslateTimeToBeReady[];
extern const char kTranslateTimeToLoad[];
extern const char kTranslateTimeToTranslate[];
extern const char kTranslateUserActionDuration[];
extern const char kTranslatePageScheme[];
extern const char kTranslateSimilarLanguageMatch[];
extern const char kTranslateLanguageDetectionConflict[];
} // namespace metrics_internal
// A page may provide a Content-Language HTTP header or a META tag.
// TranslateHelper checks if a server provides a valid Content-Language.
......@@ -102,9 +104,6 @@ void ReportSimilarLanguageMatch(bool match);
void ReportLanguageDetectionConflict(const std::string& page_lang,
const std::string& cld_lang);
// Gets UMA name for an entry specified by |index|.
const char* GetMetricsName(MetricsNameIndex index);
} // namespace translate
#endif // COMPONENTS_TRANSLATE_CORE_COMMON_TRANSLATE_METRICS_H_
......@@ -37,11 +37,11 @@ class MetricsRecorder {
base_samples_ = histogram->SnapshotSamples();
}
void CheckLanguage(translate::MetricsNameIndex index,
void CheckLanguage(const char* metric_name,
int expected_not_provided,
int expected_valid,
int expected_invalid) {
ASSERT_EQ(translate::GetMetricsName(index), key_);
ASSERT_EQ(metric_name, key_);
Snapshot();
......@@ -60,7 +60,7 @@ class MetricsRecorder {
int expected_cld_disagree,
int expected_trust_cld,
int expected_cld_complement_sub_code) {
ASSERT_EQ(translate::GetMetricsName(translate::UMA_LANGUAGE_VERIFICATION),
ASSERT_EQ(translate::metrics_internal::kTranslateLanguageVerification,
key_);
Snapshot();
......@@ -71,9 +71,8 @@ class MetricsRecorder {
EXPECT_EQ(
expected_cld_only,
GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_CLD_ONLY));
EXPECT_EQ(
expected_unknown,
GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_UNKNOWN));
EXPECT_EQ(expected_unknown, GetCountWithoutSnapshot(
translate::LANGUAGE_VERIFICATION_UNKNOWN));
EXPECT_EQ(
expected_cld_agree,
GetCountWithoutSnapshot(translate::LANGUAGE_VERIFICATION_CLD_AGREE));
......@@ -89,7 +88,7 @@ class MetricsRecorder {
}
void CheckScheme(int expected_http, int expected_https, int expected_others) {
ASSERT_EQ(translate::GetMetricsName(translate::UMA_PAGE_SCHEME), key_);
ASSERT_EQ(translate::metrics_internal::kTranslatePageScheme, key_);
Snapshot();
......@@ -161,32 +160,40 @@ class MetricsRecorder {
TEST(TranslateMetricsTest, ReportContentLanguage) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_CONTENT_LANGUAGE));
translate::metrics_internal::kTranslateContentLanguage);
recorder.CheckLanguage(translate::UMA_CONTENT_LANGUAGE, 0, 0, 0);
recorder.CheckLanguage(translate::metrics_internal::kTranslateContentLanguage,
0, 0, 0);
translate::ReportContentLanguage(std::string(), std::string());
recorder.CheckLanguage(translate::UMA_CONTENT_LANGUAGE, 1, 0, 0);
recorder.CheckLanguage(translate::metrics_internal::kTranslateContentLanguage,
1, 0, 0);
translate::ReportContentLanguage("ja_JP", "ja-JP");
recorder.CheckLanguage(translate::UMA_CONTENT_LANGUAGE, 1, 0, 1);
recorder.CheckLanguage(translate::metrics_internal::kTranslateContentLanguage,
1, 0, 1);
translate::ReportContentLanguage("en", "en");
recorder.CheckLanguage(translate::UMA_CONTENT_LANGUAGE, 1, 1, 1);
recorder.CheckLanguage(translate::metrics_internal::kTranslateContentLanguage,
1, 1, 1);
}
TEST(TranslateMetricsTest, ReportHtmlLang) {
MetricsRecorder recorder(translate::GetMetricsName(translate::UMA_HTML_LANG));
MetricsRecorder recorder(translate::metrics_internal::kTranslateHtmlLang);
recorder.CheckLanguage(translate::UMA_HTML_LANG, 0, 0, 0);
recorder.CheckLanguage(translate::metrics_internal::kTranslateHtmlLang, 0, 0,
0);
translate::ReportHtmlLang(std::string(), std::string());
recorder.CheckLanguage(translate::UMA_HTML_LANG, 1, 0, 0);
recorder.CheckLanguage(translate::metrics_internal::kTranslateHtmlLang, 1, 0,
0);
translate::ReportHtmlLang("ja_JP", "ja-JP");
recorder.CheckLanguage(translate::UMA_HTML_LANG, 1, 0, 1);
recorder.CheckLanguage(translate::metrics_internal::kTranslateHtmlLang, 1, 0,
1);
translate::ReportHtmlLang("en", "en");
recorder.CheckLanguage(translate::UMA_HTML_LANG, 1, 1, 1);
recorder.CheckLanguage(translate::metrics_internal::kTranslateHtmlLang, 1, 1,
1);
}
TEST(TranslateMetricsTest, ReportLanguageVerification) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_LANGUAGE_VERIFICATION));
translate::metrics_internal::kTranslateLanguageVerification);
recorder.CheckLanguageVerification(0, 0, 0, 0, 0, 0, 0);
translate::ReportLanguageVerification(
......@@ -214,7 +221,7 @@ TEST(TranslateMetricsTest, ReportLanguageVerification) {
TEST(TranslateMetricsTest, ReportTimeToBeReady) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_TIME_TO_BE_READY));
translate::metrics_internal::kTranslateTimeToBeReady);
recorder.CheckTotalCount(0);
translate::ReportTimeToBeReady(3.14);
recorder.CheckValueInLogs(3.14);
......@@ -222,8 +229,7 @@ TEST(TranslateMetricsTest, ReportTimeToBeReady) {
}
TEST(TranslateMetricsTest, ReportTimeToLoad) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_TIME_TO_LOAD));
MetricsRecorder recorder(translate::metrics_internal::kTranslateTimeToLoad);
recorder.CheckTotalCount(0);
translate::ReportTimeToLoad(573.0);
recorder.CheckValueInLogs(573.0);
......@@ -232,7 +238,7 @@ TEST(TranslateMetricsTest, ReportTimeToLoad) {
TEST(TranslateMetricsTest, ReportTimeToTranslate) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_TIME_TO_TRANSLATE));
translate::metrics_internal::kTranslateTimeToTranslate);
recorder.CheckTotalCount(0);
translate::ReportTimeToTranslate(4649.0);
recorder.CheckValueInLogs(4649.0);
......@@ -241,7 +247,7 @@ TEST(TranslateMetricsTest, ReportTimeToTranslate) {
TEST(TranslateMetricsTest, ReportUserActionDuration) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_USER_ACTION_DURATION));
translate::metrics_internal::kTranslateUserActionDuration);
recorder.CheckTotalCount(0);
TimeTicks begin = TimeTicks::Now();
TimeTicks end = begin + base::TimeDelta::FromSeconds(3776);
......@@ -251,8 +257,7 @@ TEST(TranslateMetricsTest, ReportUserActionDuration) {
}
TEST(TranslateMetricsTest, ReportPageScheme) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_PAGE_SCHEME));
MetricsRecorder recorder(translate::metrics_internal::kTranslatePageScheme);
recorder.CheckScheme(0, 0, 0);
translate::ReportPageScheme("http");
recorder.CheckScheme(1, 0, 0);
......@@ -264,7 +269,7 @@ TEST(TranslateMetricsTest, ReportPageScheme) {
TEST(TranslateMetricsTest, ReportSimilarLanguageMatch) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_SIMILAR_LANGUAGE_MATCH));
translate::metrics_internal::kTranslateSimilarLanguageMatch);
recorder.CheckTotalCount(0);
EXPECT_EQ(0, recorder.GetCount(kTrue));
EXPECT_EQ(0, recorder.GetCount(kFalse));
......@@ -278,7 +283,7 @@ TEST(TranslateMetricsTest, ReportSimilarLanguageMatch) {
TEST(TranslateMetricsTest, ReportLanguageDetectionTime) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_LANGUAGE_DETECTION));
translate::metrics_internal::kRenderer4LanguageDetection);
recorder.CheckTotalCount(0);
TimeTicks begin = TimeTicks::Now();
TimeTicks end = begin + base::TimeDelta::FromMicroseconds(9009);
......@@ -289,7 +294,7 @@ TEST(TranslateMetricsTest, ReportLanguageDetectionTime) {
TEST(TranslateMetricsTest, ReportLanguageDetectionConflict) {
MetricsRecorder recorder(
translate::GetMetricsName(translate::UMA_LANGUAGE_DETECTION_CONFLICT));
translate::metrics_internal::kTranslateLanguageDetectionConflict);
recorder.CheckTotalCount(0);
translate::ReportLanguageDetectionConflict("en", "es");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment