-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadevents.cpp
311 lines (255 loc) · 8.19 KB
/
loadevents.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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <random>
#include <cassert>
#include <unistd.h>
#include <boost/program_options.hpp>
#include "hiredis.h"
using namespace std;
namespace po = boost::program_options;
struct Args {
string cmd, server, key, file;
int port, n;
};
static random_device rd;
static mt19937 gen(rd());
static uniform_real_distribution<> distr1(-180.0, 180.0);
static uniform_real_distribution<> distr2(-90.0, 90.0);
static uniform_int_distribution<> month_distr(0, 11);
static uniform_int_distribution<> day_distr(1, 31);
static uniform_int_distribution<> year_distr(116, 124);
static uniform_int_distribution<> hour_distr(0, 23);
static uniform_int_distribution<> minute_distr(0, 59);
static uniform_int_distribution<unsigned int> dur_distr(60*15, 3600*5);
static uniform_int_distribution<> id_distr(1000000, 20000000);
Args ParseOptions(int argc, char **argv){
Args args;
po::options_description descr("Reventis Client Options");
try {
descr.add_options()
("help,h", "produce help message")
("key,k", po::value<string>(&args.key)->required(), "redis key string")
("cmd,c", po::value<string>(&args.cmd)->required(), "command: events, objects, gen")
("server,s", po::value<string>(&args.server)->default_value("localhost"),
"reids server hostname or unix socket path - e.g. localhost or 127.0.0.1")
("port,p", po::value<int>(&args.port)->default_value(6379), "redis server port")
("file,f", po::value<string>(&args.file)->default_value(""), "events file or objects file")
("num,n", po::value<int>(&args.n)->default_value(1000), "number of random events to add");
po::positional_options_description pd;
pd.add("cmd", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(descr).positional(pd).run(), vm);
if (vm.count("help") || args.cmd == "help"){
cout << descr << endl;
exit(0);
}
po::notify(vm);
} catch (const po::error &ex){
cout << descr << endl;
exit(0);
}
return args;
}
int genlonglat(double &x, double &y){
x = distr1(gen);
y = distr2(gen);
return 0;
}
int gentimes(string &startdatetime, string &enddatetime){
const char *datetime_fmt = "%Y-%m-%dT%H:%M:00";
char buffer[64];
time_t t1, t2;
tm start;
start.tm_sec = 0;
start.tm_min = minute_distr(gen);
start.tm_hour = hour_distr(gen);
start.tm_mday = day_distr(gen);
start.tm_mon = month_distr(gen);
start.tm_year = year_distr(gen);
start.tm_isdst = 0;
t1 = mktime(&start);
unsigned int dur = dur_distr(gen);
t2 = t1 + (time_t)dur;
strftime(buffer, 64, datetime_fmt, gmtime(&t1));
startdatetime = string(buffer);
strftime(buffer, 64, datetime_fmt, gmtime(&t2));
enddatetime = string(buffer);
if (t2 < t1) return -1;
return 0;
}
int GenerateEvents(redisContext *c, const string &key, const int n){
int count = 0;
redisReply *reply = NULL;
const char *cmd_fmt = "reventis.insert %s %f %f %s %s %s";
while (count < n){
double x, y;
genlonglat(x, y);
string dt1, dt2;
if (gentimes(dt1, dt2) < 0)
continue;
long long tag_num = id_distr(gen);
string descr = "\"Event ID # " + to_string(tag_num) + "\"";
reply = (redisReply*)redisCommand(c, cmd_fmt,
key.c_str(),
x, y,
dt1.c_str(), dt2.c_str(),
descr.c_str());
if (reply && reply->type == REDIS_REPLY_INTEGER){
count++;
} else if (reply && reply->type == REDIS_REPLY_STRING){
continue;
} else if (reply && reply->type == REDIS_REPLY_ERROR){
continue;
} else if (reply == NULL){
cout << "Error no reply";
freeReplyObject(reply);
break;
}
freeReplyObject(reply);
}
return count;
}
typedef struct entry_t {
long long object_id;
double longitude;
double latitude;
string startdatetime;
string enddatetime;
string descr;
} Entry;
int parse_event(string line, Entry &entry){
string word;
stringstream ss(line);
getline(ss, word, ',');
entry.latitude = atof(word.c_str());
getline(ss, word, ',');
entry.longitude = atof(word.c_str());
string datestr, startstr, endstr;
getline(ss, datestr, ',');
getline(ss, startstr, ',');
getline(ss, endstr, ',');
entry.startdatetime = datestr + "T" + startstr;
entry.enddatetime = datestr + "T" + endstr;
entry.object_id = 0;
return 0;
}
int parse_object(string line, Entry &entry){
string word;
stringstream ss(line);
getline(ss, word, ',');
entry.object_id = atoi(word.c_str());
getline(ss, word, ',');
entry.longitude = atof(word.c_str());
getline(ss, word, ',');
entry.latitude = atof(word.c_str());
getline(ss, word, ',');
entry.startdatetime = word;
entry.enddatetime = word;
getline(ss, word, ',');
entry.descr = word;
return 0;
}
int LoadEvents(redisContext *c, const string &key, const string &file){
ifstream input(file);
redisReply *reply;
const char *cmd_fmt = "reventis.insert %s %f %f %s %s %s";
char cmd[128];
int count = 0;
for (string line; getline(input, line);){
Entry e;
parse_event(line, e);
snprintf(cmd, 128, cmd_fmt, key.c_str(), e.longitude, e.latitude,
e.startdatetime.c_str(), e.enddatetime.c_str(), e.descr.c_str());
cout << "send => " << cmd << endl;
reply = (redisReply*)redisCommand(c, cmd_fmt, key.c_str(),
e.longitude, e.latitude,
e.startdatetime.c_str(), e.enddatetime.c_str(),
e.descr.c_str());
if (reply && reply->type == REDIS_REPLY_INTEGER){
count++;
cout << "reply => event id = " << reply->integer << endl;
} else if (reply && reply->type == REDIS_REPLY_ERROR){
cout << "ERR - error submitting entry: " << reply->str << endl;
freeReplyObject(reply);
return count;
} else if (reply == NULL){
cout << "ERR - no reply" << endl;
freeReplyObject(reply);
return count;
}
freeReplyObject(reply);
}
return count;
}
int LoadObjects(redisContext *c, const string &key, const string &file){
ifstream input(file);
redisReply *reply;
const char *cmd_fmt = "reventis.update %s %f %f %s %ld %s";
char cmd[128];
int count = 0;
for (string line; getline(input, line);){
Entry e;
parse_object(line, e);
snprintf(cmd, 128, cmd_fmt, key.c_str(), e.longitude, e.latitude,
e.startdatetime.c_str(), e.object_id, e.descr.c_str());
cout << "send => " << cmd << endl;
reply = (redisReply*)redisCommand(c, cmd_fmt, key.c_str(), e.longitude, e.latitude,
e.startdatetime.c_str(), e.object_id, e.descr.c_str());
if (reply && reply->type == REDIS_REPLY_INTEGER){
count++;
cout << "reply => event id = " << reply->integer << endl;
} else if (reply && reply->type == REDIS_REPLY_ERROR){
cout << "error: " << reply->str << endl;
return count;
} else if (reply == NULL){
cout << "ERR - no reply" << endl;
freeReplyObject(reply);
return count;
}
freeReplyObject(reply);
}
cout << "Loaded " << count << " objects from " << file << endl;
return count;
}
void print_header(){
cout << endl << "------------Reventis Client------------" << endl << endl;
}
int main(int argc, char **argv){
print_header();
Args args = ParseOptions(argc, argv);
cout << "Open connection to " << args.server << ":" << args.port << endl;
redisContext *c = redisConnect(args.server.c_str(), args.port);
if (c == NULL || c->err){
if (c){
cout << "Error: " << c->errstr << endl;
} else {
cout << "Unable to allocate redis context" << endl;
}
exit(0);
}
cout << "Submit using key = " << args.key << endl << endl;
if (args.cmd.compare("events") == 0){
cout << "Submit events from " << args.file << endl;
int n_events = LoadEvents(c, args.key, args.file);
cout << endl << n_events << " events successfully submitted" << endl;
} else if (args.cmd.compare("objects") == 0){
cout << "Submit objects from " << args.file << endl;
int n_objects = LoadObjects(c, args.key, args.file);
cout << endl << n_objects << " objects successfully submitted" << endl;
} else if (args.cmd.compare("gen") == 0){
cout << "Submit " << args.n << " randomly generated events" << endl;
int n_random = GenerateEvents(c, args.key, args.n);
cout << endl << n_random << " events successfully submitted" << endl;
} else {
cout << endl << "Unrecognized option: " << args.cmd << endl;
}
cout << "Done." << endl;
redisFree(c);
return 0;
}