-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameAnalytics_redis.cpp
367 lines (311 loc) · 10.6 KB
/
GameAnalytics_redis.cpp
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "GameAnalytics_redis.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/io/coded_stream.h"
#include "json/json.h"
#include <cpp_redis/cpp_redis>
#include <cpp_redis/core/client.hpp>
#undef GetMessage
//////////////////////////////////////////////////////////////////////////
Json::Value ConstructJsonFromMessage(const google::protobuf::Message & msg);
Json::Value ConstructJsonFromMessage(const google::protobuf::Message & msg, const google::protobuf::FieldDescriptor* fdesc)
{
if (fdesc->is_repeated())
return false;
switch (fdesc->type())
{
case google::protobuf::FieldDescriptor::TYPE_DOUBLE:
{
const double val = msg.GetReflection()->GetBool(msg, fdesc);
return Json::Value(val);
}
case google::protobuf::FieldDescriptor::TYPE_FLOAT:
{
const float val = msg.GetReflection()->GetFloat(msg, fdesc);
return Json::Value(val);
}
case google::protobuf::FieldDescriptor::TYPE_INT64:
case google::protobuf::FieldDescriptor::TYPE_FIXED64:
case google::protobuf::FieldDescriptor::TYPE_SINT64:
case google::protobuf::FieldDescriptor::TYPE_SFIXED64:
{
const google::protobuf::int64 val = msg.GetReflection()->GetInt64(msg, fdesc);
return Json::Value((int)val);
}
case google::protobuf::FieldDescriptor::TYPE_UINT64:
{
const google::protobuf::uint64 val = msg.GetReflection()->GetUInt64(msg, fdesc);
return Json::Value((unsigned int)val);
}
case google::protobuf::FieldDescriptor::TYPE_SINT32:
case google::protobuf::FieldDescriptor::TYPE_FIXED32:
case google::protobuf::FieldDescriptor::TYPE_SFIXED32:
case google::protobuf::FieldDescriptor::TYPE_INT32:
{
const google::protobuf::int32 val = msg.GetReflection()->GetInt32(msg, fdesc);
return Json::Value(val);
}
case google::protobuf::FieldDescriptor::TYPE_UINT32:
{
const google::protobuf::uint32 val = msg.GetReflection()->GetUInt32(msg, fdesc);
return Json::Value(val);
}
case google::protobuf::FieldDescriptor::TYPE_BOOL:
{
const bool val = msg.GetReflection()->GetBool(msg, fdesc);
return Json::Value(val);
}
case google::protobuf::FieldDescriptor::TYPE_STRING:
{
auto val = msg.GetReflection()->GetString(msg, fdesc);
return Json::Value(val);
}
case google::protobuf::FieldDescriptor::TYPE_ENUM:
{
const google::protobuf::EnumValueDescriptor* eval = msg.GetReflection()->GetEnum(msg, fdesc);
if (eval == NULL)
return false;
auto val = eval->name();
return Json::Value(val);
}
case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
{
const google::protobuf::Message& childmsg = msg.GetReflection()->GetMessage(msg, fdesc);
return ConstructJsonFromMessage(childmsg);
}
case google::protobuf::FieldDescriptor::TYPE_BYTES:
{
auto val = msg.GetReflection()->GetString(msg, fdesc);
return Json::Value(val);
}
default:
{
return false;
}
}
return true;
}
Json::Value ConstructJsonFromMessage(const google::protobuf::Message & msg)
{
Json::Value json;
std::vector<const google::protobuf::FieldDescriptor*> descriptors;
msg.GetReflection()->ListFields(msg, &descriptors);
for (size_t f = 0; f < descriptors.size(); ++f)
{
Json::Value val = ConstructJsonFromMessage(msg, descriptors[f]);
if (!val.isNull())
{
json[descriptors[f]->name()] = val;
}
}
return json;
}
//////////////////////////////////////////////////////////////////////////
bool StringFromField(std::string & strOut, const google::protobuf::Message & msg, const google::protobuf::FieldDescriptor* fdesc);
//////////////////////////////////////////////////////////////////////////
bool GameAnalytics::OpenRedisConnection(const char *ipAddress, int port)
{
mClient = new cpp_redis::client();
try
{
mClient->connect(ipAddress, port, [](const std::string& host, std::size_t port, cpp_redis::client::connect_state status)
{
if (status == cpp_redis::client::connect_state::dropped)
{
// todo: expose logging to api
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
}, 1000, 30, 2000);
time_t rawtime;
time(&rawtime);
tm * utc = gmtime(&rawtime);
mKeySpacePrefix = vaAnalytics("eventStream_%d.%d.%d-%d.%d.%d",
utc->tm_mday, utc->tm_mon, utc->tm_year, utc->tm_hour, utc->tm_min, utc->tm_sec);
// start the new stream
std::vector<std::string> values;
values.push_back(mKeySpacePrefix);
mClient->rpush("event_streams", values);
const char* cachedSET = "local l = redis.call('SET', KEYS[1], ARGV[1]) \
local pubresult = redis.call('PUBLISH', KEYS[1], ARGV[1]) \
return l";
const char* cachedRPUSH = "local l = redis.call('RPUSH', KEYS[1], ARGV[1]) \
local pubresult = redis.call('PUBLISH', KEYS[1], ARGV[1]) \
return l";
const char* cachedHMSET = "local l = redis.call('HMSET', KEYS[1], ARGV[1], ARGV[2]) \
local pubresult = redis.call('PUBLISH', KEYS[1] .. '~' .. ARGV[1], ARGV[2]) \
return l";
mClient->script_load(cachedSET, [&](cpp_redis::reply& reply) {
std::cout << reply << std::endl;
mScriptSHA_SET = reply.as_string();
});
mClient->script_load(cachedRPUSH, [&](cpp_redis::reply& reply) {
std::cout << reply << std::endl;
mScriptSHA_RPUSH = reply.as_string();
});
mClient->script_load(cachedHMSET, [&](cpp_redis::reply& reply) {
std::cout << reply << std::endl;
mScriptSHA_HMSET = reply.as_string();
});
// we want to wait until we have hashes for all the script snippets
// because that's what we are going to use for sending data to redis
// so that it will both cache that data and also publish it out
mClient->sync_commit();
mSubscriber = new cpp_redis::subscriber();
mSubscriber->connect(ipAddress, port);
return true;
}
catch (const std::exception& e)
{
delete mClient;
mClient = nullptr;
delete mSubscriber;
mSubscriber = nullptr;
return false;
}
}
RedisDatabase::RedisDatabase(GameAnalytics* analytics, const char *ipAddress, int port = 6379)
: Analytics(analytics)
{
mClient = new cpp_redis::client();
mClient->connect(ipAddress, port, [](const std::string& host, std::size_t port, cpp_redis::client::connect_state status)
{
if (status == cpp_redis::client::connect_state::dropped)
{
// todo: expose logging to api
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
});
time_t rawtime;
time(&rawtime);
tm * utc = gmtime(&rawtime);
mKeySpacePrefix = vaAnalytics("eventStream_%d.%d.%d-%d.%d.%d",
utc->tm_mday, utc->tm_mon, utc->tm_year, utc->tm_hour, utc->tm_min, utc->tm_sec);
// start the new stream
std::vector<std::string> values;
values.push_back(mKeySpacePrefix);
mClient->rpush("event_streams", values);
const char* cachedSET = "local l = redis.call('SET', KEYS[1], ARGV[1]) \
local pubresult = redis.call('PUBLISH', KEYS[1], ARGV[1]) \
return l";
const char* cachedRPUSH = "local l = redis.call('RPUSH', KEYS[1], ARGV[1]) \
local pubresult = redis.call('PUBLISH', KEYS[1], ARGV[1]) \
return l";
const char* cachedHMSET = "local l = redis.call('HMSET', KEYS[1], ARGV[1], ARGV[2]) \
local pubresult = redis.call('PUBLISH', KEYS[1] .. '~' .. ARGV[1], ARGV[2]) \
return l";
mClient->script_load(cachedSET, [&](cpp_redis::reply& reply) {
std::cout << reply << std::endl;
mScriptSHA_SET = reply.as_string();
});
mClient->script_load(cachedRPUSH, [&](cpp_redis::reply& reply) {
std::cout << reply << std::endl;
mScriptSHA_RPUSH = reply.as_string();
});
mClient->script_load(cachedHMSET, [&](cpp_redis::reply& reply) {
std::cout << reply << std::endl;
mScriptSHA_HMSET = reply.as_string();
});
// we want to wait until we have hashes for all the script snippets
// because that's what we are going to use for sending data to redis
// so that it will both cache that data and also publish it out
mClient->sync_commit();
mSubscriber = new cpp_redis::subscriber();
mSubscriber->connect(ipAddress, port);
}
RedisDatabase::~RedisDatabase()
{
mClient->disconnect(true);
delete mClient;
mClient = nullptr;
tacopie::set_default_io_service(nullptr);
}
void RedisDatabase::AddEvent(const google::protobuf::Message& msg, const std::string & payload)
{
Analytics::RedisKeyType redisKeyType = Analytics::UNKNOWN;
if (msg.GetDescriptor()->options().HasExtension(Analytics::rediskeytype))
redisKeyType = msg.GetDescriptor()->options().GetExtension(Analytics::rediskeytype);
std::string keyName = msg.GetDescriptor()->name();
if (msg.GetDescriptor()->options().HasExtension(Analytics::rediskeysuffix))
{
std::string suffix = msg.GetDescriptor()->options().GetExtension(Analytics::rediskeysuffix);
const google::protobuf::FieldDescriptor* fdesc = msg.GetDescriptor()->FindFieldByCamelcaseName(suffix);
if (fdesc != NULL && !fdesc->is_repeated())
{
std::string fieldString;
if (StringFromField(fieldString, msg, fdesc))
{
suffix = fieldString;
}
}
keyName += ":" + suffix;
}
const std::string eventKey = vaAnalytics("%s:%s", mKeySpacePrefix.c_str(), keyName.c_str()).c_str();
switch (redisKeyType)
{
case Analytics::SET:
case Analytics::UNKNOWN:
{
std::vector<std::string> keys, args;
keys.push_back(eventKey);
args.push_back(payload);
mClient->evalsha(mScriptSHA_SET, 1, keys, args);
break;
}
case Analytics::RPUSH:
{
std::vector<std::string> keys, args;
keys.push_back(eventKey);
args.push_back(payload);
mClient->evalsha(mScriptSHA_RPUSH, 1, keys, args);
break;
}
case Analytics::HMSET:
{
std::string setKey = msg.GetTypeName();
if (msg.GetDescriptor()->options().HasExtension(Analytics::redishmsetkey))
{
setKey = msg.GetDescriptor()->options().GetExtension(Analytics::redishmsetkey);
const google::protobuf::FieldDescriptor* fdesc = msg.GetDescriptor()->FindFieldByCamelcaseName(setKey);
if (fdesc != NULL && !fdesc->is_repeated())
{
std::string fieldString;
if (StringFromField(fieldString, msg, fdesc))
{
setKey = fieldString;
}
}
}
std::vector<std::string> keys, args;
keys.push_back(eventKey);
args.push_back(setKey);
args.push_back(payload);
mClient->evalsha(mScriptSHA_HMSET, 1, keys, args);
break;
}
}
}
void RedisDatabase::EndOfFrame()
{
mClient->commit();
}
void RedisDatabase::Subscribe(const std::string& channelName)
{
mSubscriber->subscribe(vaAnalytics("%s:%s", mKeySpacePrefix.c_str(), channelName.c_str()).c_str(), [&](const std::string& chan, const std::string& msg)
{
std::lock_guard<std::mutex> lock(mChannelDataMutex);
ChannelData data;
data.mChannel = chan;
data.mData = msg;
mChannelData.push(data);
});
mSubscriber->commit();
}
void RedisDatabase::ProcessPublishedMessages(channel_message_recieved_t exec)
{
std::lock_guard<std::mutex> lock(mChannelDataMutex);
while (!mChannelData.empty())
{
const ChannelData& data = mChannelData.front();
exec(data.mChannel, data.mData);
mChannelData.pop();
}
}