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

types.hpp « ugc - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5beb7703171ad88e3bff7aad0fac0bd5091c701 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#pragma once

#include "indexer/feature_decl.hpp"

#include "coding/hex.hpp"

#include "base/visitor.hpp"

#include <chrono>
#include <cstdint>
#include <memory>
#include <sstream>
#include <string>
#include <string>
#include <vector>

namespace ugc
{
using Clock = std::chrono::system_clock;
using Time = std::chrono::time_point<Clock>;

struct TranslationKey
{
  TranslationKey() = default;
  TranslationKey(std::string const & key): m_key(key) {}
  TranslationKey(char const * key): m_key(key) {}

  DECLARE_VISITOR(visitor(m_key, "key"))

  bool operator==(TranslationKey const & rhs) const { return m_key == rhs.m_key; }
  bool operator<(TranslationKey const & rhs) const { return m_key < rhs.m_key; }

  friend std::string DebugPrint(TranslationKey const & key)
  {
    return "TranslationKey [ " + key.m_key + " ]";
  }

  std::string m_key;
};

enum class Sentiment
{
  Positive,
  Negative
};

inline std::string DebugPrint(Sentiment const & sentiment)
{
  switch (sentiment)
  {
  case Sentiment::Positive: return "Positive";
  case Sentiment::Negative: return "Negative";
  }
}

inline uint32_t ToDaysSinceEpoch(Time const & time)
{
  auto const hours = std::chrono::duration_cast<std::chrono::hours>(time.time_since_epoch());
  return static_cast<uint32_t>(hours.count()) / 24;
}

inline Time FromDaysSinceEpoch(uint32_t days)
{
  auto const hours = std::chrono::hours(days * 24);
  return Time(hours);
}

inline uint32_t DaysAgo(Time const & time)
{
  auto const now = std::chrono::system_clock::now();
  if (now < time)
    return 0;
  auto const h = std::chrono::duration_cast<std::chrono::hours>(now - time).count() / 24;
  return static_cast<uint32_t>(h);
}

struct RatingRecord
{
  RatingRecord() = default;
  RatingRecord(TranslationKey const & key, float const value) : m_key(key), m_value(value) {}

  DECLARE_VISITOR(visitor(m_key, "key"), visitor.VisitRating(m_value, "value"))

  bool operator==(RatingRecord const & rhs) const
  {
    return m_key == rhs.m_key && m_value == rhs.m_value;
  }

  friend std::string DebugPrint(RatingRecord const & ratingRecord)
  {
    std::ostringstream os;
    os << "RatingRecord [ " << DebugPrint(ratingRecord.m_key) << " " << ratingRecord.m_value
       << " ]";
    return os.str();
  }

  TranslationKey m_key{};
  float m_value{};
};

using Ratings = std::vector<RatingRecord>;

struct Rating
{
  Rating() = default;
  Rating(std::vector<RatingRecord> const & ratings, float const aggValue)
    : m_ratings(ratings), m_aggValue(aggValue)
  {
  }

  DECLARE_VISITOR(visitor(m_ratings, "ratings"), visitor.VisitRating(m_aggValue, "aggValue"))

  bool operator==(Rating const & rhs) const
  {
    return m_ratings == rhs.m_ratings && m_aggValue == rhs.m_aggValue;
  }

  friend std::string DebugPrint(Rating const & rating)
  {
    std::ostringstream os;
    os << "Rating [ ratings:" << ::DebugPrint(rating.m_ratings)
       << ", aggValue:" << rating.m_aggValue << " ]";
    return os.str();
  }

  std::vector<RatingRecord> m_ratings;
  float m_aggValue{};
};

struct UID
{
  UID() = default;
  UID(uint64_t const hi, uint64_t const lo) : m_hi(hi), m_lo(lo) {}

  std::string ToString() const { return NumToHex(m_hi) + NumToHex(m_lo); }

  DECLARE_VISITOR(visitor(m_hi, "hi"), visitor(m_lo, "lo"));

  bool operator==(UID const & rhs) const { return m_hi == rhs.m_hi && m_lo == rhs.m_lo; }
  friend std::string DebugPrint(UID const & uid)
  {
    std::ostringstream os;
    os << "UID [ " << uid.ToString() << " ]";
    return os.str();
  }

  uint64_t m_hi{};
  uint64_t m_lo{};
};

struct Author
{
  Author() = default;
  Author(UID const & uid, std::string const & name) : m_uid(uid), m_name(name) {}

  DECLARE_VISITOR(visitor(m_uid, "uid"), visitor(m_name, "name"));

  bool operator==(Author const & rhs) const { return m_uid == rhs.m_uid && m_name == rhs.m_name; }
  friend std::string DebugPrint(Author const & author)
  {
    std::ostringstream os;
    os << "Author [ " << DebugPrint(author.m_uid) << " " << author.m_name << " ]";
    return os.str();
  }

  UID m_uid{};
  std::string m_name;
};

struct Text
{
  Text() = default;
  Text(std::string const & text, uint8_t const lang) : m_text(text), m_lang(lang) {}

  DECLARE_VISITOR(visitor(m_lang, "lang"), visitor(m_text, "text"));

  bool operator==(Text const & rhs) const { return m_lang == rhs.m_lang && m_text == rhs.m_text; }

  friend std::string DebugPrint(Text const & text)
  {
    std::ostringstream os;
    os << "Text [ " << StringUtf8Multilang::GetLangByCode(text.m_lang) << ": " << text.m_text
       << " ]";
    return os.str();
  }

  std::string m_text;
  uint8_t m_lang = StringUtf8Multilang::kDefaultCode;
};

struct Review
{
  using ReviewId = uint32_t;

  Review() = default;
  Review(ReviewId id, Text const & text, Author const & author, float const rating,
         Time const & time)
    : m_text(text), m_author(author), m_rating(rating), m_time(time)
  {
  }

  DECLARE_VISITOR(visitor(m_id, "id"), visitor(m_text, "text"), visitor(m_author, "author"),
                  visitor.VisitRating(m_rating, "rating"), visitor(m_time, "time"))

  bool operator==(Review const & rhs) const
  {
    return m_id == rhs.m_id && m_text == rhs.m_text && m_author == rhs.m_author &&
           m_rating == rhs.m_rating && m_time == rhs.m_time;
  }

  friend std::string DebugPrint(Review const & review)
  {
    std::ostringstream os;
    os << "Review [ ";
    os << "id:" << review.m_id << ", ";
    os << "text:" << DebugPrint(review.m_text) << ", ";
    os << "author:" << DebugPrint(review.m_author) << ", ";
    os << "rating:" << review.m_rating << ", ";
    os << "days since epoch:" << ToDaysSinceEpoch(review.m_time) << " ]";
    return os.str();
  }

  ReviewId m_id{};
  Text m_text{};
  Author m_author{};
  float m_rating{};
  Time m_time{};
};

using Reviews = std::vector<Review>;

struct Attribute
{
  Attribute() = default;
  Attribute(TranslationKey const & key, TranslationKey const & value) : m_key(key), m_value(value)
  {
  }

  DECLARE_VISITOR(visitor(m_key, "key"), visitor(m_value, "value"))

  bool operator==(Attribute const & rhs) const
  {
    return m_key == rhs.m_key && m_value == rhs.m_value;
  }

  friend std::string DebugPrint(Attribute const & attribute)
  {
    std::ostringstream os;
    os << "Attribute [ key:" << DebugPrint(attribute.m_key)
       << ", value:" << DebugPrint(attribute.m_value) << " ]";
    return os.str();
  }

  TranslationKey m_key{};
  TranslationKey m_value{};
};

struct UGC
{
  UGC() = default;
  UGC(Ratings const & records, Reviews const & reviews, float const rating)
    : m_ratings(records), m_reviews(reviews), m_aggRating(rating)
  {
  }

  DECLARE_VISITOR(visitor(m_ratings, "ratings"), visitor(m_reviews, "reviews"),
                  visitor.VisitRating(m_aggRating, "aggRating"))

  bool operator==(UGC const & rhs) const
  {
    return m_ratings == rhs.m_ratings && m_reviews == rhs.m_reviews;
  }

  friend std::string DebugPrint(UGC const & ugc)
  {
    std::ostringstream os;
    os << "UGC [ ";
    os << "records:" << ::DebugPrint(ugc.m_ratings) << ", ";
    os << "reviews:" << ::DebugPrint(ugc.m_reviews) << " ]";
    return os.str();
  }

  Ratings m_ratings;
  Reviews m_reviews;
  float m_aggRating{};
};

struct UGCUpdate
{
  UGCUpdate() = default;
  UGCUpdate(Ratings const & records, Text const & text, Time const & time)
    : m_ratings(records), m_text(text), m_time(time)
  {
  }

  DECLARE_VISITOR(visitor(m_ratings, "ratings"), visitor(m_text, "text"), visitor(m_time, "time"))

  bool operator==(UGCUpdate const & rhs) const
  {
    return m_ratings == rhs.m_ratings && m_text == rhs.m_text && m_time == rhs.m_time;
  }

  friend std::string DebugPrint(UGCUpdate const & ugcUpdate)
  {
    std::ostringstream os;
    os << "UGCUpdate [ ";
    os << "records:" << ::DebugPrint(ugcUpdate.m_ratings) << ", ";
    os << "text:" << DebugPrint(ugcUpdate.m_text) << ", ";
    os << "days since epoch:" << ToDaysSinceEpoch(ugcUpdate.m_time) << " ]";
    return os.str();
  }

  Ratings m_ratings;
  Text m_text;
  Time m_time{};
};

struct ReviewFeedback
{
  ReviewFeedback() = default;
  ReviewFeedback(Sentiment const sentiment, Time const & time)
    : m_sentiment(sentiment), m_time(time)
  {
  }

  Sentiment m_sentiment{};
  Time m_time{};
};

struct ReviewAbuse
{
  ReviewAbuse() = default;
  ReviewAbuse(std::string const & reason, Time const & time) : m_reason(reason), m_time(time) {}

  std::string m_reason{};
  Time m_time{};
};
}  // namespace ugc

#undef DECLARE_VISITOR