blob: 875bf62c3fbdde5fa57f76c5bcefc31ec2298ff4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#pragma once
#include "local_ads/event.hpp"
#include "base/thread.hpp"
#include <chrono>
#include <condition_variable>
#include <functional>
#include <list>
#include <map>
#include <mutex>
#include <string>
#include <vector>
namespace local_ads
{
using ServerSerializer =
std::function<std::vector<uint8_t>(std::list<Event> const & events, std::string const & userId,
std::string & contentType, std::string & contentEncoding)>;
class Statistics final
{
public:
struct PackedData
{
int64_t m_mercator = 0;
uint32_t m_featureIndex = 0;
uint32_t m_seconds = 0;
uint16_t m_accuracy = 0;
uint8_t m_eventType = 0;
uint8_t m_zoomLevel = 0;
};
Statistics() = default;
~Statistics();
void Startup();
void Teardown();
void SetUserId(std::string const & userId);
void SetCustomServerSerializer(ServerSerializer && serializer);
void RegisterEvent(Event && event);
void RegisterEvents(std::list<Event> && events);
std::list<Event> WriteEventsForTesting(std::list<Event> const & events,
std::string & fileNameToRebuild);
std::list<Event> ReadEventsForTesting(std::string const & fileName);
void ProcessEventsForTesting(std::list<Event> const & events);
void CleanupAfterTesting();
private:
void ThreadRoutine();
bool RequestEvents(std::list<Event> & events, bool & needToSend);
void IndexMetadata();
void ExtractMetadata(std::string const & fileName);
void BalanceMemory();
std::list<Event> WriteEvents(std::list<Event> & events, std::string & fileNameToRebuild);
std::list<Event> ReadEvents(std::string const & fileName) const;
void ProcessEvents(std::list<Event> & events);
void SendToServer();
std::vector<uint8_t> SerializeForServer(std::list<Event> const & events) const;
using MetadataKey = std::pair<std::string, int64_t>;
struct Metadata
{
std::string m_fileName;
Timestamp m_timestamp;
Metadata() = default;
Metadata(std::string const & fileName, Timestamp const & timestamp)
: m_fileName(fileName), m_timestamp(timestamp)
{
}
};
std::map<MetadataKey, Metadata> m_metadataCache;
std::chrono::steady_clock::time_point m_lastSending;
bool m_isFirstSending = true;
std::string m_userId;
ServerSerializer m_serverSerializer;
bool m_isRunning = false;
std::list<Event> m_events;
std::condition_variable m_condition;
std::mutex m_mutex;
threads::SimpleThread m_thread;
};
} // namespace local_ads
|