diff options
| author | 2017-07-12 21:31:12 -0400 | |
|---|---|---|
| committer | 2017-07-12 21:31:12 -0400 | |
| commit | 9cf261ba8ba4fd9929d275cc793d48d13df624f3 (patch) | |
| tree | 2eb47ab96bde081a84c5dd7c8107a21cb8a3511c /src/web_service | |
| parent | Merge pull request #2815 from mailwl/bossp (diff) | |
| parent | web_backend: Specify api-version on JSON post. (diff) | |
| download | yuzu-9cf261ba8ba4fd9929d275cc793d48d13df624f3.tar.gz yuzu-9cf261ba8ba4fd9929d275cc793d48d13df624f3.tar.xz yuzu-9cf261ba8ba4fd9929d275cc793d48d13df624f3.zip | |
Merge pull request #2819 from bunnei/telemetry-submit
Telemetry: Submit logged data to the Citra service
Diffstat (limited to 'src/web_service')
| -rw-r--r-- | src/web_service/CMakeLists.txt | 14 | ||||
| -rw-r--r-- | src/web_service/telemetry_json.cpp | 87 | ||||
| -rw-r--r-- | src/web_service/telemetry_json.h | 54 | ||||
| -rw-r--r-- | src/web_service/web_backend.cpp | 52 | ||||
| -rw-r--r-- | src/web_service/web_backend.h | 31 |
5 files changed, 238 insertions, 0 deletions
diff --git a/src/web_service/CMakeLists.txt b/src/web_service/CMakeLists.txt new file mode 100644 index 000000000..334d82a8a --- /dev/null +++ b/src/web_service/CMakeLists.txt | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | set(SRCS | ||
| 2 | telemetry_json.cpp | ||
| 3 | web_backend.cpp | ||
| 4 | ) | ||
| 5 | |||
| 6 | set(HEADERS | ||
| 7 | telemetry_json.h | ||
| 8 | web_backend.h | ||
| 9 | ) | ||
| 10 | |||
| 11 | create_directory_groups(${SRCS} ${HEADERS}) | ||
| 12 | |||
| 13 | add_library(web_service STATIC ${SRCS} ${HEADERS}) | ||
| 14 | target_link_libraries(web_service PUBLIC common cpr json-headers) | ||
diff --git a/src/web_service/telemetry_json.cpp b/src/web_service/telemetry_json.cpp new file mode 100644 index 000000000..a2d007e77 --- /dev/null +++ b/src/web_service/telemetry_json.cpp | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/assert.h" | ||
| 6 | #include "core/settings.h" | ||
| 7 | #include "web_service/telemetry_json.h" | ||
| 8 | #include "web_service/web_backend.h" | ||
| 9 | |||
| 10 | namespace WebService { | ||
| 11 | |||
| 12 | template <class T> | ||
| 13 | void TelemetryJson::Serialize(Telemetry::FieldType type, const std::string& name, T value) { | ||
| 14 | sections[static_cast<u8>(type)][name] = value; | ||
| 15 | } | ||
| 16 | |||
| 17 | void TelemetryJson::SerializeSection(Telemetry::FieldType type, const std::string& name) { | ||
| 18 | TopSection()[name] = sections[static_cast<unsigned>(type)]; | ||
| 19 | } | ||
| 20 | |||
| 21 | void TelemetryJson::Visit(const Telemetry::Field<bool>& field) { | ||
| 22 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 23 | } | ||
| 24 | |||
| 25 | void TelemetryJson::Visit(const Telemetry::Field<double>& field) { | ||
| 26 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 27 | } | ||
| 28 | |||
| 29 | void TelemetryJson::Visit(const Telemetry::Field<float>& field) { | ||
| 30 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 31 | } | ||
| 32 | |||
| 33 | void TelemetryJson::Visit(const Telemetry::Field<u8>& field) { | ||
| 34 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 35 | } | ||
| 36 | |||
| 37 | void TelemetryJson::Visit(const Telemetry::Field<u16>& field) { | ||
| 38 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 39 | } | ||
| 40 | |||
| 41 | void TelemetryJson::Visit(const Telemetry::Field<u32>& field) { | ||
| 42 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 43 | } | ||
| 44 | |||
| 45 | void TelemetryJson::Visit(const Telemetry::Field<u64>& field) { | ||
| 46 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 47 | } | ||
| 48 | |||
| 49 | void TelemetryJson::Visit(const Telemetry::Field<s8>& field) { | ||
| 50 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 51 | } | ||
| 52 | |||
| 53 | void TelemetryJson::Visit(const Telemetry::Field<s16>& field) { | ||
| 54 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 55 | } | ||
| 56 | |||
| 57 | void TelemetryJson::Visit(const Telemetry::Field<s32>& field) { | ||
| 58 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 59 | } | ||
| 60 | |||
| 61 | void TelemetryJson::Visit(const Telemetry::Field<s64>& field) { | ||
| 62 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 63 | } | ||
| 64 | |||
| 65 | void TelemetryJson::Visit(const Telemetry::Field<std::string>& field) { | ||
| 66 | Serialize(field.GetType(), field.GetName(), field.GetValue()); | ||
| 67 | } | ||
| 68 | |||
| 69 | void TelemetryJson::Visit(const Telemetry::Field<const char*>& field) { | ||
| 70 | Serialize(field.GetType(), field.GetName(), std::string(field.GetValue())); | ||
| 71 | } | ||
| 72 | |||
| 73 | void TelemetryJson::Visit(const Telemetry::Field<std::chrono::microseconds>& field) { | ||
| 74 | Serialize(field.GetType(), field.GetName(), field.GetValue().count()); | ||
| 75 | } | ||
| 76 | |||
| 77 | void TelemetryJson::Complete() { | ||
| 78 | SerializeSection(Telemetry::FieldType::App, "App"); | ||
| 79 | SerializeSection(Telemetry::FieldType::Session, "Session"); | ||
| 80 | SerializeSection(Telemetry::FieldType::Performance, "Performance"); | ||
| 81 | SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback"); | ||
| 82 | SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig"); | ||
| 83 | SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem"); | ||
| 84 | PostJson(Settings::values.telemetry_endpoint_url, TopSection().dump()); | ||
| 85 | } | ||
| 86 | |||
| 87 | } // namespace WebService | ||
diff --git a/src/web_service/telemetry_json.h b/src/web_service/telemetry_json.h new file mode 100644 index 000000000..39038b4f9 --- /dev/null +++ b/src/web_service/telemetry_json.h | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <string> | ||
| 9 | #include <json.hpp> | ||
| 10 | #include "common/telemetry.h" | ||
| 11 | |||
| 12 | namespace WebService { | ||
| 13 | |||
| 14 | /** | ||
| 15 | * Implementation of VisitorInterface that serialized telemetry into JSON, and submits it to the | ||
| 16 | * Citra web service | ||
| 17 | */ | ||
| 18 | class TelemetryJson : public Telemetry::VisitorInterface { | ||
| 19 | public: | ||
| 20 | TelemetryJson() = default; | ||
| 21 | ~TelemetryJson() = default; | ||
| 22 | |||
| 23 | void Visit(const Telemetry::Field<bool>& field) override; | ||
| 24 | void Visit(const Telemetry::Field<double>& field) override; | ||
| 25 | void Visit(const Telemetry::Field<float>& field) override; | ||
| 26 | void Visit(const Telemetry::Field<u8>& field) override; | ||
| 27 | void Visit(const Telemetry::Field<u16>& field) override; | ||
| 28 | void Visit(const Telemetry::Field<u32>& field) override; | ||
| 29 | void Visit(const Telemetry::Field<u64>& field) override; | ||
| 30 | void Visit(const Telemetry::Field<s8>& field) override; | ||
| 31 | void Visit(const Telemetry::Field<s16>& field) override; | ||
| 32 | void Visit(const Telemetry::Field<s32>& field) override; | ||
| 33 | void Visit(const Telemetry::Field<s64>& field) override; | ||
| 34 | void Visit(const Telemetry::Field<std::string>& field) override; | ||
| 35 | void Visit(const Telemetry::Field<const char*>& field) override; | ||
| 36 | void Visit(const Telemetry::Field<std::chrono::microseconds>& field) override; | ||
| 37 | |||
| 38 | void Complete() override; | ||
| 39 | |||
| 40 | private: | ||
| 41 | nlohmann::json& TopSection() { | ||
| 42 | return sections[static_cast<u8>(Telemetry::FieldType::None)]; | ||
| 43 | } | ||
| 44 | |||
| 45 | template <class T> | ||
| 46 | void Serialize(Telemetry::FieldType type, const std::string& name, T value); | ||
| 47 | |||
| 48 | void SerializeSection(Telemetry::FieldType type, const std::string& name); | ||
| 49 | |||
| 50 | nlohmann::json output; | ||
| 51 | std::array<nlohmann::json, 7> sections; | ||
| 52 | }; | ||
| 53 | |||
| 54 | } // namespace WebService | ||
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp new file mode 100644 index 000000000..13e4555ac --- /dev/null +++ b/src/web_service/web_backend.cpp | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cpr/cpr.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include "common/logging/log.h" | ||
| 8 | #include "web_service/web_backend.h" | ||
| 9 | |||
| 10 | namespace WebService { | ||
| 11 | |||
| 12 | static constexpr char API_VERSION[]{"1"}; | ||
| 13 | static constexpr char ENV_VAR_USERNAME[]{"CITRA_WEB_SERVICES_USERNAME"}; | ||
| 14 | static constexpr char ENV_VAR_TOKEN[]{"CITRA_WEB_SERVICES_TOKEN"}; | ||
| 15 | |||
| 16 | static std::string GetEnvironmentVariable(const char* name) { | ||
| 17 | const char* value{getenv(name)}; | ||
| 18 | if (value) { | ||
| 19 | return value; | ||
| 20 | } | ||
| 21 | return {}; | ||
| 22 | } | ||
| 23 | |||
| 24 | const std::string& GetUsername() { | ||
| 25 | static const std::string username{GetEnvironmentVariable(ENV_VAR_USERNAME)}; | ||
| 26 | return username; | ||
| 27 | } | ||
| 28 | |||
| 29 | const std::string& GetToken() { | ||
| 30 | static const std::string token{GetEnvironmentVariable(ENV_VAR_TOKEN)}; | ||
| 31 | return token; | ||
| 32 | } | ||
| 33 | |||
| 34 | void PostJson(const std::string& url, const std::string& data) { | ||
| 35 | if (url.empty()) { | ||
| 36 | LOG_ERROR(WebService, "URL is invalid"); | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | if (GetUsername().empty() || GetToken().empty()) { | ||
| 41 | LOG_ERROR(WebService, "Environment variables %s and %s must be set to POST JSON", | ||
| 42 | ENV_VAR_USERNAME, ENV_VAR_TOKEN); | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | |||
| 46 | cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"}, | ||
| 47 | {"x-username", GetUsername()}, | ||
| 48 | {"x-token", GetToken()}, | ||
| 49 | {"api-version", API_VERSION}}); | ||
| 50 | } | ||
| 51 | |||
| 52 | } // namespace WebService | ||
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h new file mode 100644 index 000000000..2753d3b68 --- /dev/null +++ b/src/web_service/web_backend.h | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <string> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | |||
| 10 | namespace WebService { | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Gets the current username for accessing services.citra-emu.org. | ||
| 14 | * @returns Username as a string, empty if not set. | ||
| 15 | */ | ||
| 16 | const std::string& GetUsername(); | ||
| 17 | |||
| 18 | /** | ||
| 19 | * Gets the current token for accessing services.citra-emu.org. | ||
| 20 | * @returns Token as a string, empty if not set. | ||
| 21 | */ | ||
| 22 | const std::string& GetToken(); | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Posts JSON to services.citra-emu.org. | ||
| 26 | * @param url URL of the services.citra-emu.org endpoint to post data to. | ||
| 27 | * @param data String of JSON data to use for the body of the POST request. | ||
| 28 | */ | ||
| 29 | void PostJson(const std::string& url, const std::string& data); | ||
| 30 | |||
| 31 | } // namespace WebService | ||