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
|
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2015 Alexander Zolotarev <me@alex.bio> from Minsk, Belarus
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#ifndef EVENT_BASE_H
#define EVENT_BASE_H
// Define ALOHALYTICS_SERVER when using this header on a server-side.
#include <chrono>
#include <sstream>
#include "cereal/include/cereal.hpp"
#include "cereal/include/types/base_class.hpp"
#include "cereal/include/types/polymorphic.hpp"
#include "cereal/include/archives/binary.hpp"
#include "cereal/include/types/string.hpp"
#include "cereal/include/types/map.hpp"
#include "location.h"
// Small helper, used for cereal smart-pointers polymorphic serialization.
// TODO(AlexZ): Remove it later together with Cereal.
namespace alohalytics {
struct NoOpDeleter {
template <typename T>
void operator()(T *) {}
};
} // namespace alohalytics
// For easier processing on a server side, every statistics event should derive from this base class.
struct AlohalyticsBaseEvent {
uint64_t timestamp;
static std::string TimestampToString(uint64_t ts) {
const time_t timet = static_cast<const time_t>(ts / 1000);
char buf[100];
if (::strftime(buf, sizeof(buf), "%e-%b-%Y %H:%M:%S", ::gmtime(&timet))) {
return buf;
} else {
return "INVALID_TIME";
}
}
virtual std::string ToString() const { return TimestampToString(timestamp); }
static uint64_t CurrentTimestamp() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
.count();
}
#ifndef ALOHALYTICS_SERVER
// We need automatic timestamping on a client-side only.
AlohalyticsBaseEvent() : timestamp(CurrentTimestamp()) {}
#endif
template <class Archive>
void serialize(Archive & ar) {
ar(CEREAL_NVP(timestamp));
}
// To use polymorphism on a server side.
virtual ~AlohalyticsBaseEvent() = default;
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsBaseEvent, "b")
// Special event in the beginning of each block (file) sent to stats server.
// Designed to mark all events in this data block as belonging to one user with specified id.
struct AlohalyticsIdEvent : public AlohalyticsBaseEvent {
std::string id;
virtual std::string ToString() const { return AlohalyticsBaseEvent::ToString() + " ID: " + id; }
template <class Archive>
void serialize(Archive & ar) {
AlohalyticsBaseEvent::serialize(ar);
ar(CEREAL_NVP(id));
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsIdEvent, "i")
#ifdef ALOHALYTICS_SERVER
// Special event to store additional server-only information which can be useful for analysis.
struct AlohalyticsIdServerEvent : public AlohalyticsIdEvent {
uint64_t server_timestamp;
std::string ip;
std::string user_agent;
std::string uri;
virtual std::string ToString() const {
return AlohalyticsIdEvent::ToString() + " SRV:" + TimestampToString(server_timestamp) + " IP:" + ip + " UA:" +
user_agent + " URI:" + uri;
}
template <class Archive>
void serialize(Archive & ar) {
AlohalyticsIdEvent::serialize(ar);
ar(CEREAL_NVP(server_timestamp), CEREAL_NVP(ip), CEREAL_NVP(user_agent), CEREAL_NVP(uri));
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsIdServerEvent, "is")
#endif
// Simple event with a string name (key) only.
struct AlohalyticsKeyEvent : public AlohalyticsBaseEvent {
std::string key;
virtual std::string ToString() const { return AlohalyticsBaseEvent::ToString() + " " + key; }
template <class Archive>
void serialize(Archive & ar) {
AlohalyticsBaseEvent::serialize(ar);
ar(CEREAL_NVP(key));
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsKeyEvent, "k")
// Simple event with a string key/value pair.
struct AlohalyticsKeyValueEvent : public AlohalyticsKeyEvent {
std::string value;
virtual std::string ToString() const { return AlohalyticsKeyEvent::ToString() + " = " + value; }
template <class Archive>
void serialize(Archive & ar) {
AlohalyticsKeyEvent::serialize(ar);
ar(CEREAL_NVP(value));
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsKeyValueEvent, "v")
// Simple event with a string key and map<string, string> value.
struct AlohalyticsKeyPairsEvent : public AlohalyticsKeyEvent {
std::map<std::string, std::string> pairs;
virtual std::string ToString() const {
std::ostringstream stream;
stream << AlohalyticsKeyEvent::ToString() << " [ ";
for (const auto & pair : pairs) {
stream << pair.first << '=' << pair.second << ' ';
}
stream << ']';
return stream.str();
}
template <class Archive>
void serialize(Archive & ar) {
AlohalyticsKeyEvent::serialize(ar);
ar(CEREAL_NVP(pairs));
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsKeyPairsEvent, "p")
// Key + location.
struct AlohalyticsKeyLocationEvent : public AlohalyticsKeyEvent {
alohalytics::Location location;
virtual std::string ToString() const { return AlohalyticsKeyEvent::ToString() + ' ' + location.ToDebugString(); }
template <class Archive>
void serialize(Archive & ar) {
AlohalyticsKeyEvent::serialize(ar);
ar(CEREAL_NVP(location));
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsKeyLocationEvent, "kl")
// Key + value + location.
struct AlohalyticsKeyValueLocationEvent : public AlohalyticsKeyValueEvent {
alohalytics::Location location;
virtual std::string ToString() const { return AlohalyticsKeyValueEvent::ToString() + ' ' + location.ToDebugString(); }
template <class Archive>
void serialize(Archive & ar) {
AlohalyticsKeyValueEvent::serialize(ar);
ar(CEREAL_NVP(location));
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsKeyValueLocationEvent, "vl")
// Key + pairs of key/value + location.
struct AlohalyticsKeyPairsLocationEvent : public AlohalyticsKeyPairsEvent {
alohalytics::Location location;
virtual std::string ToString() const { return AlohalyticsKeyPairsEvent::ToString() + ' ' + location.ToDebugString(); }
template <class Archive>
void serialize(Archive & ar) {
AlohalyticsKeyPairsEvent::serialize(ar);
ar(CEREAL_NVP(location));
}
};
CEREAL_REGISTER_TYPE_WITH_NAME(AlohalyticsKeyPairsLocationEvent, "pl")
#endif // EVENT_BASE_H
|