Welcome to mirror list, hosted at ThFree Co, Russian Federation.

statistics.hpp « local_ads - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e870362e5767d4958b206c677c80fafb268c015 (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
#pragma once

#include "local_ads/event.hpp"

#include "base/thread.hpp"

#include <chrono>
#include <condition_variable>
#include <functional>
#include <list>
#include <map>
#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;

  void Startup();

  void SetUserId(std::string const & userId);

  void SetCustomServerSerializer(ServerSerializer const & 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 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::string m_userId;
  ServerSerializer m_serverSerializer;
};
}  // namespace local_ads