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

eye.cpp « metrics - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b32aecb82a183c0dc2a5d2c0cbee9051d6959e89 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "metrics/eye.hpp"
#include "metrics/eye_serdes.hpp"
#include "metrics/eye_storage.hpp"

#include "platform/platform.hpp"

#include "coding/file_writer.hpp"

#include "base/logging.hpp"

#include <algorithm>
#include <memory>
#include <utility>
#include <vector>

using namespace eye;

namespace
{
void Load(Info & info)
{
  std::vector<int8_t> fileData;
  if (!Storage::Load(Storage::GetEyeFilePath(), fileData))
  {
    info = {};
    return;
  }

  try
  {
    Serdes::Deserialize(fileData, info);
  }
  catch (Serdes::UnknownVersion const & ex)
  {
    LOG(LERROR, ("Unknown eye file version, eye will be disabled. Exception:", ex.Msg()));
  }
}

bool Save(Info const & info)
{
  std::vector<int8_t> fileData;
  Serdes::Serialize(info, fileData);
  return Storage::Save(Storage::GetEyeFilePath(), fileData);
}
}  // namespace

namespace eye
{
Eye::Eye()
{
  Info info;
  Load(info);
  m_info.Set(std::make_shared<Info>(info));
}

// static
Eye & Eye::Instance()
{
  static Eye instance;
  return instance;
}

Eye::InfoType Eye::GetInfo() const
{
  return m_info.Get();
}

void Eye::Save(InfoType const & info)
{
  if (::Save(*info))
    m_info.Set(info);
}

void Eye::AppendTip(Tip::Type type, Tip::Event event)
{
  auto const info = m_info.Get();
  auto editableInfo = std::make_shared<Info>(*info);
  auto & editableTips = editableInfo->m_tips;

  auto it = std::find_if(editableTips.begin(), editableTips.end(), [type](Tip const & tip)
  {
    return tip.m_type == type;
  });

  auto const now = Clock::now();
  if (it != editableTips.cend())
  {
    it->m_eventCounters.Increment(event);
    it->m_lastShownTime = now;
  }
  else
  {
    Tip tip;
    tip.m_type = type;
    tip.m_eventCounters.Increment(event);
    tip.m_lastShownTime = now;
    editableTips.emplace_back(std::move(tip));
  }

  Save(editableInfo);
}

void Eye::UpdateBookingFilterUsedTime()
{
  auto const info = m_info.Get();
  auto editableInfo = std::make_shared<Info>(*info);

  editableInfo->m_booking.m_lastFilterUsedTime = Clock::now();

  Save(editableInfo);
}

void Eye::UpdateBoomarksCatalogShownTime()
{
  auto const info = m_info.Get();
  auto editableInfo = std::make_shared<Info>(*info);

  editableInfo->m_bookmarks.m_lastOpenedTime = Clock::now();

  Save(editableInfo);
}

void Eye::UpdateDiscoveryShownTime()
{
  auto const info = m_info.Get();
  auto editableInfo = std::make_shared<Info>(*info);

  editableInfo->m_discovery.m_lastOpenedTime = Clock::now();

  Save(editableInfo);
}

void Eye::IncrementDiscoveryItem(Discovery::Event event)
{
  auto const info = m_info.Get();
  auto editableInfo = std::make_shared<Info>(*info);

  editableInfo->m_discovery.m_lastClickedTime = Clock::now();
  editableInfo->m_discovery.m_eventCounters.Increment(event);

  Save(editableInfo);
}

void Eye::AppendLayer(Layer::Type type)
{
  auto const info = m_info.Get();
  auto editableInfo = std::make_shared<Info>(*info);
  auto & editableLayers = editableInfo->m_layers;

  auto it = std::find_if(editableLayers.begin(), editableLayers.end(), [type](Layer const & layer)
  {
    return layer.m_type == type;
  });

  if (it != editableLayers.end())
  {
    ++it->m_useCount;
    it->m_lastTimeUsed = Clock::now();
  }
  else
  {
    Layer layer;
    layer.m_type = type;

    ++layer.m_useCount;
    layer.m_lastTimeUsed = Clock::now();
    editableLayers.emplace_back(std::move(layer));
  }

  Save(editableInfo);
}

// Eye::Event methods ------------------------------------------------------------------------------
// static
void Eye::Event::TipShown(Tip::Type type, Tip::Event event)
{
  GetPlatform().RunTask(Platform::Thread::File, [type, event]
  {
    Instance().AppendTip(type, event);
  });
}

// static
void Eye::Event::BookingFilterUsed()
{
  GetPlatform().RunTask(Platform::Thread::File, []
  {
    Instance().UpdateBookingFilterUsedTime();
  });
}

// static
void Eye::Event::BoomarksCatalogShown()
{
  GetPlatform().RunTask(Platform::Thread::File, []
  {
    Instance().UpdateBoomarksCatalogShownTime();
  });
}

// static
void Eye::Event::DiscoveryShown()
{
  GetPlatform().RunTask(Platform::Thread::File, []
  {
    Instance().UpdateDiscoveryShownTime();
  });
}

// static
void Eye::Event::DiscoveryItemClicked(Discovery::Event event)
{
  GetPlatform().RunTask(Platform::Thread::File, [event]
  {
    Instance().IncrementDiscoveryItem(event);
  });
}

// static
void Eye::Event::LayerUsed(Layer::Type type)
{
  GetPlatform().RunTask(Platform::Thread::File, [type]
  {
    Instance().AppendLayer(type);
  });
}
}  // namespace eye