telemetry_json.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <nlohmann/json.hpp>
  5. #include "common/detached_tasks.h"
  6. #include "common/web_result.h"
  7. #include "web_service/telemetry_json.h"
  8. #include "web_service/web_backend.h"
  9. namespace WebService {
  10. struct TelemetryJson::Impl {
  11. Impl(std::string host, std::string username, std::string token)
  12. : host{std::move(host)}, username{std::move(username)}, token{std::move(token)} {}
  13. nlohmann::json& TopSection() {
  14. return sections[static_cast<u8>(Telemetry::FieldType::None)];
  15. }
  16. const nlohmann::json& TopSection() const {
  17. return sections[static_cast<u8>(Telemetry::FieldType::None)];
  18. }
  19. template <class T>
  20. void Serialize(Telemetry::FieldType type, const std::string& name, T value) {
  21. sections[static_cast<u8>(type)][name] = value;
  22. }
  23. void SerializeSection(Telemetry::FieldType type, const std::string& name) {
  24. TopSection()[name] = sections[static_cast<unsigned>(type)];
  25. }
  26. nlohmann::json output;
  27. std::array<nlohmann::json, 7> sections;
  28. std::string host;
  29. std::string username;
  30. std::string token;
  31. };
  32. TelemetryJson::TelemetryJson(std::string host, std::string username, std::string token)
  33. : impl{std::make_unique<Impl>(std::move(host), std::move(username), std::move(token))} {}
  34. TelemetryJson::~TelemetryJson() = default;
  35. void TelemetryJson::Visit(const Telemetry::Field<bool>& field) {
  36. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  37. }
  38. void TelemetryJson::Visit(const Telemetry::Field<double>& field) {
  39. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  40. }
  41. void TelemetryJson::Visit(const Telemetry::Field<float>& field) {
  42. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  43. }
  44. void TelemetryJson::Visit(const Telemetry::Field<u8>& field) {
  45. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  46. }
  47. void TelemetryJson::Visit(const Telemetry::Field<u16>& field) {
  48. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  49. }
  50. void TelemetryJson::Visit(const Telemetry::Field<u32>& field) {
  51. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  52. }
  53. void TelemetryJson::Visit(const Telemetry::Field<u64>& field) {
  54. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  55. }
  56. void TelemetryJson::Visit(const Telemetry::Field<s8>& field) {
  57. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  58. }
  59. void TelemetryJson::Visit(const Telemetry::Field<s16>& field) {
  60. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  61. }
  62. void TelemetryJson::Visit(const Telemetry::Field<s32>& field) {
  63. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  64. }
  65. void TelemetryJson::Visit(const Telemetry::Field<s64>& field) {
  66. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  67. }
  68. void TelemetryJson::Visit(const Telemetry::Field<std::string>& field) {
  69. impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
  70. }
  71. void TelemetryJson::Visit(const Telemetry::Field<const char*>& field) {
  72. impl->Serialize(field.GetType(), field.GetName(), std::string(field.GetValue()));
  73. }
  74. void TelemetryJson::Visit(const Telemetry::Field<std::chrono::microseconds>& field) {
  75. impl->Serialize(field.GetType(), field.GetName(), field.GetValue().count());
  76. }
  77. void TelemetryJson::Complete() {
  78. impl->SerializeSection(Telemetry::FieldType::App, "App");
  79. impl->SerializeSection(Telemetry::FieldType::Session, "Session");
  80. impl->SerializeSection(Telemetry::FieldType::Performance, "Performance");
  81. impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
  82. impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
  83. auto content = impl->TopSection().dump();
  84. // Send the telemetry async but don't handle the errors since they were written to the log
  85. Common::DetachedTasks::AddTask([host{impl->host}, content]() {
  86. Client{host, "", ""}.PostJson("/telemetry", content, true);
  87. });
  88. }
  89. bool TelemetryJson::SubmitTestcase() {
  90. impl->SerializeSection(Telemetry::FieldType::App, "App");
  91. impl->SerializeSection(Telemetry::FieldType::Session, "Session");
  92. impl->SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback");
  93. impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
  94. impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
  95. auto content = impl->TopSection().dump();
  96. Client client(impl->host, impl->username, impl->token);
  97. auto value = client.PostJson("/gamedb/testcase", content, false);
  98. return value.result_code == Common::WebResult::Code::Success;
  99. }
  100. } // namespace WebService