| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <chrono> |
| 7 |
#include <memory> |
| 8 |
#include <shared_mutex> |
| 9 |
#include <string> |
| 10 |
#include <string_view> |
| 11 |
#include <thread> |
| 12 |
#include <utility> |
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#include "Common/CommonTypes.h" |
| 16 |
#include "Common/Event.h" |
| 17 |
#include "Common/Flag.h" |
| 18 |
#include "Common/HttpRequest.h" |
| 19 |
#include "Common/SPSCQueue.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
™. |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
namespace Common |
| 43 |
{ |
| 44 |
|
| 45 |
|
| 46 |
class AnalyticsReportingBackend |
| 47 |
{ |
| 48 |
public: |
| 49 |
virtual ~AnalyticsReportingBackend() = default; |
| 50 |
|
| 51 |
virtual void Send(std::string report) = 0; |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
class AnalyticsReportBuilder |
| 56 |
{ |
| 57 |
public: |
| 58 |
AnalyticsReportBuilder(); |
| 59 |
~AnalyticsReportBuilder() = default; |
| 60 |
|
| 61 |
AnalyticsReportBuilder(const AnalyticsReportBuilder& other) : m_report{other.Get()} {} |
| 62 |
AnalyticsReportBuilder(AnalyticsReportBuilder&& other) : m_report{other.Consume()} {} |
| 63 |
|
| 64 |
const AnalyticsReportBuilder& operator=(const AnalyticsReportBuilder& other) |
| 65 |
{ |
| 66 |
if (this != &other) |
| 67 |
{ |
| 68 |
std::string other_report = other.Get(); |
| 69 |
std::lock_guard lk{m_lock}; |
| 70 |
m_report = std::move(other_report); |
| 71 |
} |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
AnalyticsReportBuilder& AddBuilder(const AnalyticsReportBuilder& other) |
| 77 |
{ |
| 78 |
|
| 79 |
std::string other_report = other.Get(); |
| 80 |
std::lock_guard lk{m_lock}; |
| 81 |
m_report += other_report; |
| 82 |
return *this; |
| 83 |
} |
| 84 |
|
| 85 |
template <typename T> |
| 86 |
AnalyticsReportBuilder& AddData(std::string_view key, const T& value) |
| 87 |
{ |
| 88 |
std::lock_guard lk{m_lock}; |
| 89 |
AppendSerializedValue(&m_report, key); |
| 90 |
AppendSerializedValue(&m_report, value); |
| 91 |
return *this; |
| 92 |
} |
| 93 |
|
| 94 |
template <typename T> |
| 95 |
AnalyticsReportBuilder& AddData(std::string_view key, const std::vector<T>& value) |
| 96 |
{ |
| 97 |
std::lock_guard lk{m_lock}; |
| 98 |
AppendSerializedValue(&m_report, key); |
| 99 |
AppendSerializedValueVector(&m_report, value); |
| 100 |
return *this; |
| 101 |
} |
| 102 |
|
| 103 |
std::string Get() const |
| 104 |
{ |
| 105 |
std::shared_lock lk{m_lock}; |
| 106 |
return m_report; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
std::string Consume() |
| 111 |
{ |
| 112 |
std::lock_guard lk{m_lock}; |
| 113 |
return std::move(m_report); |
| 114 |
} |
| 115 |
|
| 116 |
protected: |
| 117 |
static void AppendSerializedValue(std::string* report, std::string_view v); |
| 118 |
static void AppendSerializedValue(std::string* report, const char* v); |
| 119 |
static void AppendSerializedValue(std::string* report, bool v); |
| 120 |
static void AppendSerializedValue(std::string* report, u64 v); |
| 121 |
static void AppendSerializedValue(std::string* report, s64 v); |
| 122 |
static void AppendSerializedValue(std::string* report, u32 v); |
| 123 |
static void AppendSerializedValue(std::string* report, s32 v); |
| 124 |
static void AppendSerializedValue(std::string* report, float v); |
| 125 |
|
| 126 |
static void AppendSerializedValueVector(std::string* report, const std::vector<u32>& v); |
| 127 |
|
| 128 |
mutable std::shared_mutex m_lock; |
| 129 |
std::string m_report; |
| 130 |
}; |
| 131 |
|
| 132 |
class AnalyticsReporter |
| 133 |
{ |
| 134 |
public: |
| 135 |
AnalyticsReporter(); |
| 136 |
~AnalyticsReporter(); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
void SetBackend(std::unique_ptr<AnalyticsReportingBackend> backend) |
| 141 |
{ |
| 142 |
m_backend = std::move(backend); |
| 143 |
m_reporter_event.Set(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
AnalyticsReportBuilder& BaseBuilder() { return m_base_builder; } |
| 150 |
|
| 151 |
AnalyticsReportBuilder Builder() const { return m_base_builder; } |
| 152 |
|
| 153 |
void Send(AnalyticsReportBuilder&& report); |
| 154 |
|
| 155 |
|
| 156 |
void Send(AnalyticsReportBuilder& report) { Send(std::move(report)); } |
| 157 |
|
| 158 |
protected: |
| 159 |
void ThreadProc(); |
| 160 |
|
| 161 |
std::shared_ptr<AnalyticsReportingBackend> m_backend; |
| 162 |
AnalyticsReportBuilder m_base_builder; |
| 163 |
|
| 164 |
std::thread m_reporter_thread; |
| 165 |
Common::Event m_reporter_event; |
| 166 |
Common::Flag m_reporter_stop_request; |
| 167 |
SPSCQueue<std::string> m_reports_queue; |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
class StdoutAnalyticsBackend : public AnalyticsReportingBackend |
| 173 |
{ |
| 174 |
public: |
| 175 |
void Send(std::string report) override; |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
class HttpAnalyticsBackend : public AnalyticsReportingBackend |
| 181 |
{ |
| 182 |
public: |
| 183 |
explicit HttpAnalyticsBackend(std::string endpoint); |
| 184 |
~HttpAnalyticsBackend() override; |
| 185 |
|
| 186 |
void Send(std::string report) override; |
| 187 |
|
| 188 |
protected: |
| 189 |
std::string m_endpoint; |
| 190 |
HttpRequest m_http{std::chrono::seconds{5}}; |
| 191 |
}; |
| 192 |
} |