-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_handlers.cpp
390 lines (346 loc) · 12.1 KB
/
get_handlers.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "get_handlers.h"
#include <algorithm>
#include <string>
#include "db_instance.h"
#include "helpers.h"
#include "http_parser.h"
namespace get_handlers {
namespace {
const std::string kFromData("fromDate");
const std::string kToDate("toDate");
const std::string kCountry("country");
const std::string kToDistance("toDistance");
const std::string kFromAge("fromAge");
const std::string kToAge("toAge");
const std::string kGender("gender");
const std::string kEmptyVisits("HTTP/1.1 200 OK\r\n"
"S: b\r\n"
"C: k\r\n"
"B: a\r\n"
"Content-Length: 13\r\n"
"\r\n"
"{\"visits\":[]}");
const std::string kAvgEmpty("HTTP/1.1 200 OK\r\n"
"S: b\r\n"
"C: k\r\n"
"B: a\r\n"
"Content-Length: 11\r\n"
"\r\n"
"{\"avg\":0.0}");
} // namespace
void GetUsers(const http_request& req, http_response& res, uint32_t id) {
auto* users = g_users_storage;
User* user = users->Get(id);
if (!user) {
NotFound(HTTP_GET, res);
return;
}
user->FillIO(res);
}
void GetLocations(const http_request& req, http_response& res, uint32_t id) {
auto* locations = g_locations_storage;
Location* location = locations->Get(id);
if (!location) {
NotFound(HTTP_GET, res);
return;
}
location->FillIO(res);
}
void GetVisits(const http_request& req, http_response& res, uint32_t id) {
auto* visits = g_visits_storage;
Visit* visit = visits->Get(id);
if (!visit) {
NotFound(HTTP_GET, res);
return;
}
visit->FillIO(res);
}
void GetUsersVisits(const http_request& req, http_response& res, uint32_t id) {
// Parse query params
int search_flags = 0;
int from_date = 0;
int to_date = 0;
std::string country;
uint32_t to_distance = 0;
for (int i = 0; i < req.params_size; ++i) {
auto& param = req.url_params[i];
if (!memcmp(kFromData.c_str(), param.key, kFromData.length() + 1)) {
if (!GetInt(param.val, &from_date)) {
BadRequest(HTTP_GET, res);
return;
}
search_flags |= QueryFlags::FROM_DATE;
} else if (!memcmp(kToDate.c_str(), param.key, kToDate.length() + 1)) {
if (!GetInt(param.val, &to_date)) {
BadRequest(HTTP_GET, res);
return;
}
search_flags |= QueryFlags::TO_DATE;
} else if (!memcmp(kCountry.c_str(), param.key, kCountry.length() + 1)) {
country = param.val;
search_flags |= QueryFlags::COUNTRY;
} else if (!memcmp(kToDistance.c_str(), param.key,
kToDistance.length() + 1)) {
if (!GetUint32(param.val, &to_distance)) {
BadRequest(HTTP_GET, res);
return;
}
search_flags |= QueryFlags::TO_DISTANCE;
}
}
auto* index = g_db_instance->GetUsersVisitsIndex();
auto* visits = index->GetValues(id);
if (!visits || visits->empty()) {
if (g_users_storage->Get(id)) {
SendResponse(res, kEmptyVisits);
return;
} else {
NotFound(HTTP_GET, res);
return;
}
} else {
size_t filtered_visits_index = 0;
Visit* filtered_visits[1000];
for (auto it = visits->begin(); it != visits->end(); ++it) {
Visit* visit = *it;
if (search_flags & QueryFlags::FROM_DATE) {
if (visit->visited_at <= from_date) {
continue;
}
}
if (search_flags & QueryFlags::TO_DATE) {
if (visit->visited_at >= to_date) {
continue;
}
}
if (search_flags & QueryFlags::COUNTRY ||
search_flags & QueryFlags::TO_DISTANCE) {
const Location* location =
g_locations_storage->Get(
visit->location_id);
if (search_flags & QueryFlags::COUNTRY) {
if (location->country != country) {
continue;
}
}
if (search_flags & QueryFlags::TO_DISTANCE) {
if (location->distance >= to_distance) {
continue;
}
}
}
filtered_visits[filtered_visits_index++] = visit;
}
if (!filtered_visits_index) {
SendResponse(res, kEmptyVisits);
return;
}
std::sort(&filtered_visits[0], &filtered_visits[filtered_visits_index],
[](const Visit* l, const Visit* r) {
return l->visited_at < r->visited_at;
});
if (filtered_visits_index * 7 + 3 >= MAX_IOVEC_SIZE) {
std::cout << "GetUsersVisits: filtered size = "
<< filtered_visits_index << std::endl;
BadRequest(HTTP_GET, res);
return;
}
iovec* iov = res.iov;
iov[0].iov_base = (void*)header;
iov[0].iov_len = header_len;
// iov[1] - body size
iov[2].iov_base = (void*)visits_str;
iov[2].iov_len = visits_str_len;
size_t record_size = 7;
for (size_t i = 0; i < filtered_visits_index - 1; ++i) {
iov[3 + i * record_size].iov_base = (void*)visits_mark_str;
iov[3 + i * record_size].iov_len = visits_mark_str_len;
iov[3 + i * record_size + 1].iov_base = filtered_visits[i]->mark_buf;
iov[3 + i * record_size + 1].iov_len = filtered_visits[i]->mark_buf_len;
iov[3 + i * record_size + 2].iov_base = (void*)visited_str;
iov[3 + i * record_size + 2].iov_len = visited_str_len;
iov[3 + i * record_size + 3].iov_base =
filtered_visits[i]->visited_at_buf;
iov[3 + i * record_size + 3].iov_len =
filtered_visits[i]->visited_at_buf_len;
iov[3 + i * record_size + 4].iov_base = (void*)place_str;
iov[3 + i * record_size + 4].iov_len = place_str_len;
iov[3 + i * record_size + 5].iov_base =
(void*)g_locations_storage->Get(
filtered_visits[i]->location_id)->place.c_str();
iov[3 + i * record_size + 5].iov_len =
g_locations_storage->Get(
filtered_visits[i]->location_id)->place.length();
iov[3 + i * record_size + 6].iov_base = (void*)close_with_comma_str;
iov[3 + i * record_size + 6].iov_len = close_with_comma_str_len;
}
int i = filtered_visits_index - 1;
iov[3 + i * record_size].iov_base = (void*)visits_mark_str;
iov[3 + i * record_size].iov_len = visits_mark_str_len;
iov[3 + i * record_size + 1].iov_base = filtered_visits[i]->mark_buf;
iov[3 + i * record_size + 1].iov_len = filtered_visits[i]->mark_buf_len;
iov[3 + i * record_size + 2].iov_base = (void*)visited_str;
iov[3 + i * record_size + 2].iov_len = visited_str_len;
iov[3 + i * record_size + 3].iov_base = filtered_visits[i]->visited_at_buf;
iov[3 + i * record_size + 3].iov_len =
filtered_visits[i]->visited_at_buf_len;
iov[3 + i * record_size + 4].iov_base = (void*)place_str;
iov[3 + i * record_size + 4].iov_len = place_str_len;
iov[3 + i * record_size + 5].iov_base =
(void*)g_locations_storage->Get(
filtered_visits[i]->location_id)->place.c_str();
iov[3 + i * record_size + 5].iov_len =
g_locations_storage->Get(
filtered_visits[i]->location_id)->place.length();
iov[3 + i * record_size + 6].iov_base = (void*)close_close_close_str;
iov[3 + i * record_size + 6].iov_len = close_close_close_str_len;
size_t body_size = 0;
for (size_t i = 2; i <= filtered_visits_index * record_size + 2; ++i) {
body_size += iov[i].iov_len;
}
body_size -= 4; // \r\n\r\n in the visits_str.
size_t body_size_len = sprintf(res.body_size_buf, "%ld", body_size);
iov[1].iov_base = res.body_size_buf;
iov[1].iov_len = body_size_len;
res.iov_size = 3 + filtered_visits_index * record_size;
}
}
void GetLocationsAvg(const http_request& req, http_response& res, uint32_t id) {
// Parse query params
int search_flags = 0;
int from_date = 0;
int to_date = 0;
int from_age = 0;
int to_age = 0;
Gender gender = Gender::UNKNOWN;
for (int i = 0; i < req.params_size; ++i) {
auto& param = req.url_params[i];
if (!memcmp(kFromData.c_str(), param.key, kFromData.length() + 1)) {
if (!GetInt(param.val, &from_date)) {
BadRequest(HTTP_GET, res);
return;
}
search_flags |= QueryFlags::FROM_DATE;
} else if (!memcmp(kToDate.c_str(), param.key, kToDate.length() + 1)) {
if (!GetInt(param.val, &to_date)) {
BadRequest(HTTP_GET, res);
return;
}
search_flags |= QueryFlags::TO_DATE;
} else if (!memcmp(kFromAge.c_str(), param.key, kFromAge.length() + 1)) {
if (!GetInt(param.val, &from_age)) {
BadRequest(HTTP_GET, res);
return;
}
search_flags |= QueryFlags::FROM_AGE;
} else if (!memcmp(kToAge.c_str(), param.key, kToAge.length() + 1)) {
if (!GetInt(param.val, &to_age)) {
BadRequest(HTTP_GET, res);
return;
}
search_flags |= QueryFlags::TO_AGE;
} else if (!memcmp(kGender.c_str(), param.key, kGender.length() + 1)) {
size_t gender_len = strlen(param.val);
if (gender_len == 1 && param.val[0] == 'm') {
gender = Gender::M;
} else if (gender_len == 1 && param.val[0] == 'f') {
gender = Gender::F;
} else {
BadRequest(HTTP_GET, res);
return;
}
search_flags |= QueryFlags::GENDER;
}
}
auto* index = g_db_instance->GetLocationsVisitsIndex();
auto* visits = index->GetValues(id);
if (!visits || visits->empty()) {
if (g_locations_storage->Get(id)) {
SendResponse(res, kAvgEmpty);
return;
} else {
NotFound(HTTP_GET, res);
return;
}
} else {
size_t filtered_visits_index = 0;
Visit* filtered_visits[1000];
for (auto it = visits->begin(); it != visits->end(); ++it) {
Visit* visit = *it;
if (search_flags & QueryFlags::FROM_DATE) {
if (visit->visited_at <= from_date) {
continue;
}
}
if (search_flags & QueryFlags::TO_DATE) {
if (visit->visited_at >= to_date) {
continue;
}
}
if (search_flags & QueryFlags::FROM_AGE ||
search_flags & QueryFlags::TO_AGE) {
const User* user =
g_users_storage->Get(visit->user_id);
static time_t t = g_generate_time; // get time now
static struct tm now = (*localtime(&t));
if (search_flags & QueryFlags::FROM_AGE) {
tm from_age_tm = now;
from_age_tm.tm_year -= from_age;
time_t from_age_t = mktime(&from_age_tm);
if (user->birth_date > from_age_t) {
continue;
}
}
if (search_flags & QueryFlags::TO_AGE) {
tm to_age_tm = now;
to_age_tm.tm_year -= to_age;
time_t to_age_t = mktime(&to_age_tm);
if (user->birth_date < to_age_t) {
continue;
}
}
}
if (search_flags & QueryFlags::GENDER) {
const User* user =
g_users_storage->Get(visit->user_id);
if (user->gender != gender) {
continue;
}
}
filtered_visits[filtered_visits_index++] = visit;
}
double avg = 0.0;
if (filtered_visits_index) {
for (size_t i = 0; i < filtered_visits_index; ++i) {
avg += filtered_visits[i]->mark;
}
avg /= filtered_visits_index;
}
iovec* iov = res.iov;
iov[0].iov_base = (void*)header;
iov[0].iov_len = header_len;
// iov[1] - body size
iov[2].iov_base = (void*)avg_str;
iov[2].iov_len = avg_str_len;
avg *= 100000;
avg += 0.5;
avg = (int)avg;
avg /= 100000.0;
size_t avg_buf_len = sprintf(res.avg_buf, "%f", avg);
iov[3].iov_base = res.avg_buf;
iov[3].iov_len = avg_buf_len;
iov[4].iov_base = (void*)close_str;
iov[4].iov_len = close_str_len;
size_t body_size_len = sprintf(res.body_size_buf, "%lu",
iov[2].iov_len + iov[3].iov_len + iov[4].iov_len - 4); // \r\n\r\n
iov[1].iov_base = res.body_size_buf;
iov[1].iov_len = body_size_len;
res.iov_size = 5;
}
}
} // namespace get_handlers