history_clusters_debug_jsons.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2022 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. #include "history_clusters_debug_jsons.h"
  5. #include <utility>
  6. #include "base/json/json_writer.h"
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "base/values.h"
  9. namespace history_clusters {
  10. // Gets a loggable JSON representation of `visits`.
  11. std::string GetDebugJSONForVisits(
  12. const std::vector<history::AnnotatedVisit>& visits) {
  13. base::Value::List debug_visits_list;
  14. for (auto& visit : visits) {
  15. base::Value::Dict debug_visit;
  16. debug_visit.Set("visitId", static_cast<int>(visit.visit_row.visit_id));
  17. debug_visit.Set("url", visit.url_row.url().spec());
  18. debug_visit.Set("title", visit.url_row.title());
  19. debug_visit.Set(
  20. "foreground_time_secs",
  21. static_cast<int>(visit.visit_row.visit_duration.InSeconds()));
  22. debug_visit.Set(
  23. "navigationTimeMs",
  24. static_cast<int>(visit.visit_row.visit_time.ToDeltaSinceWindowsEpoch()
  25. .InMilliseconds()));
  26. debug_visit.Set("pageEndReason", visit.context_annotations.page_end_reason);
  27. debug_visit.Set("pageTransition",
  28. static_cast<int>(visit.visit_row.transition));
  29. debug_visit.Set(
  30. "referringVisitId",
  31. static_cast<int>(visit.referring_visit_of_redirect_chain_start));
  32. debug_visit.Set(
  33. "openerVisitId",
  34. static_cast<int>(visit.opener_visit_of_redirect_chain_start));
  35. debug_visits_list.Append(std::move(debug_visit));
  36. }
  37. base::Value::Dict debug_value;
  38. debug_value.Set("visits", std::move(debug_visits_list));
  39. std::string debug_string;
  40. if (!base::JSONWriter::WriteWithOptions(
  41. debug_value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &debug_string)) {
  42. debug_string = "Error: Could not write visits to JSON.";
  43. }
  44. return debug_string;
  45. }
  46. // Gets a loggable JSON representation of `clusters`.
  47. std::string GetDebugJSONForClusters(
  48. const std::vector<history::Cluster>& clusters) {
  49. base::Value::List debug_clusters_list;
  50. for (const auto& cluster : clusters) {
  51. base::Value::Dict debug_cluster;
  52. debug_cluster.Set("label", cluster.label.value_or(u""));
  53. base::Value::Dict debug_keyword_to_data_map;
  54. for (const auto& keyword_data_p : cluster.keyword_to_data_map) {
  55. base::Value::List debug_collection;
  56. for (const auto& collection : keyword_data_p.second.entity_collections) {
  57. debug_collection.Append(collection);
  58. }
  59. base::Value::Dict debug_keyword_data;
  60. debug_keyword_data.Set("collections", std::move(debug_collection));
  61. debug_keyword_to_data_map.Set(base::UTF16ToUTF8(keyword_data_p.first),
  62. std::move(debug_keyword_data));
  63. }
  64. debug_cluster.Set("keyword_to_data_map",
  65. std::move(debug_keyword_to_data_map));
  66. debug_cluster.Set("should_show_on_prominent_ui_surfaces",
  67. cluster.should_show_on_prominent_ui_surfaces);
  68. base::Value::List debug_visits;
  69. for (const auto& visit : cluster.visits) {
  70. base::Value::Dict debug_visit;
  71. debug_visit.Set(
  72. "visit_id",
  73. static_cast<int>(visit.annotated_visit.visit_row.visit_id));
  74. debug_visit.Set("score", visit.score);
  75. base::Value::List debug_categories;
  76. for (const auto& category : visit.annotated_visit.content_annotations
  77. .model_annotations.categories) {
  78. base::Value::Dict debug_category;
  79. debug_category.Set("name", category.id);
  80. debug_category.Set("value", category.weight);
  81. debug_categories.Append(std::move(debug_category));
  82. }
  83. debug_visit.Set("categories", std::move(debug_categories));
  84. base::Value::List debug_entities;
  85. for (const auto& entity : visit.annotated_visit.content_annotations
  86. .model_annotations.entities) {
  87. base::Value::Dict debug_entity;
  88. debug_entity.Set("name", entity.id);
  89. debug_entity.Set("value", entity.weight);
  90. debug_entities.Append(std::move(debug_entity));
  91. }
  92. debug_visit.Set("entities", std::move(debug_entities));
  93. if (!visit.annotated_visit.content_annotations.search_terms.empty()) {
  94. debug_visit.Set("search_terms",
  95. visit.annotated_visit.content_annotations.search_terms);
  96. }
  97. debug_visit.Set("site_engagement_score", visit.engagement_score);
  98. base::Value::List debug_duplicate_visits;
  99. for (const auto& duplicate_visit : visit.duplicate_visits) {
  100. debug_duplicate_visits.Append(static_cast<int>(
  101. duplicate_visit.annotated_visit.visit_row.visit_id));
  102. }
  103. debug_visit.Set("duplicate_visits", std::move(debug_duplicate_visits));
  104. debug_visits.Append(std::move(debug_visit));
  105. }
  106. debug_cluster.Set("visits", std::move(debug_visits));
  107. debug_clusters_list.Append(std::move(debug_cluster));
  108. }
  109. std::string debug_string;
  110. if (!base::JSONWriter::WriteWithOptions(
  111. debug_clusters_list, base::JSONWriter::OPTIONS_PRETTY_PRINT,
  112. &debug_string)) {
  113. debug_string = "Error: Could not write clusters to JSON.";
  114. }
  115. return debug_string;
  116. }
  117. template <typename T>
  118. std::string GetDebugJSONForUrlKeywordSet(
  119. const std::unordered_set<T>& keyword_set) {
  120. base::Value::List keyword_list;
  121. for (const auto& keyword : keyword_set) {
  122. keyword_list.Append(keyword);
  123. }
  124. std::string debug_string;
  125. if (!base::JSONWriter::WriteWithOptions(
  126. keyword_list, base::JSONWriter::OPTIONS_PRETTY_PRINT,
  127. &debug_string)) {
  128. debug_string = "Error: Could not write keywords list to JSON.";
  129. }
  130. return debug_string;
  131. }
  132. template std::string GetDebugJSONForUrlKeywordSet<std::u16string>(
  133. const std::unordered_set<std::u16string>&);
  134. template std::string GetDebugJSONForUrlKeywordSet<std::string>(
  135. const std::unordered_set<std::string>&);
  136. std::string GetDebugJSONForKeywordMap(
  137. const std::unordered_map<std::u16string, history::ClusterKeywordData>&
  138. keyword_to_data_map) {
  139. base::Value::Dict debug_keyword_to_data_map;
  140. for (const auto& keyword_data_p : keyword_to_data_map) {
  141. base::Value::List debug_collection;
  142. for (const auto& collection : keyword_data_p.second.entity_collections) {
  143. debug_collection.Append(collection);
  144. }
  145. base::Value::Dict debug_keyword_data;
  146. debug_keyword_data.Set("collections", std::move(debug_collection));
  147. debug_keyword_to_data_map.Set(base::UTF16ToUTF8(keyword_data_p.first),
  148. std::move(debug_keyword_data));
  149. }
  150. std::string debug_string;
  151. if (!base::JSONWriter::WriteWithOptions(
  152. debug_keyword_to_data_map, base::JSONWriter::OPTIONS_PRETTY_PRINT,
  153. &debug_string)) {
  154. debug_string = "Error: Could not write keywords list to JSON.";
  155. }
  156. return debug_string;
  157. }
  158. } // namespace history_clusters