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