telemetry_json.cpp 4.8 KB

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