-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtilegen.cpp
134 lines (110 loc) · 3.5 KB
/
tilegen.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
#include <string>
#include <iostream>
#include <stdio.h>
#include "MysqlQuery.h"
#include "mysql_common.h"
#include "Tile.h"
#include "Channel.h"
bool debug;
Tile *retrieve_tile(int user_id, std::string dev_nickname, std::string ch_name, int level, long long offset,
int checkpoint=-1/*, lgset=nil*/)
{
// Lookup tile
// TODO: fix security here!
std::string tile_ch_name = dev_nickname + "." + ch_name;
// Fetch existing tile, if exists
//
// TODO: optimize by making (user_id, level, offset, ch_name) a unique index, and using
// "on duplicate key update" to update tile in-place when regenerating
{
MysqlQuery tile_query("select * from Tiles where user_id = %d and level = %d and offset = %lld "
"and ch_name = '%s' order by validation_recid desc limit 1",
user_id, level, offset, tile_ch_name.c_str());
MysqlRow *row = tile_query.fetch_row();
if (row) {
Tile *t = new Tile(row);
if (debug) std::cerr << "got a tile with validation_recid " << t->validation_recid << std::endl;
// Find intersecting logrecs
{
double start_time = Tile::offset_at_level_to_unixtime(offset, level);
double end_time = Tile::offset_at_level_to_unixtime(offset+1, level);
MysqlQuery q("select id from Logrecs where user_id = %d and dev_nickname = '%s' and "
"begin_d < %lf and end_d >= %lf and "
"dat_fields LIKE '%%- %s%%' OR dat_fields LIKE '%%name: %s%%' "
"order by id desc limit 1",
user_id, dev_nickname.c_str(),
end_time, start_time,
ch_name.c_str(), ch_name.c_str());
MysqlRow *row = q.fetch_row();
if (row) {
long long logrec_id = row->get_long_long("id");
if (debug) std::cerr << "most recent logrec is " << logrec_id << std::endl;
if (logrec_id <= t->validation_recid) {
if (debug) std::cerr << "valid, returning\n";
return t;
} else {
delete t;
}
} else {
if (debug) std::cerr << "NO LOGRECS: todo: delete tile\n";
delete t;
return NULL;
}
}
}
}
return NULL;
}
void test_retrieve_tile()
{
int user_id = 1;
std::string dev_nickname = "Josh_Basestation";
std::string ch_name = "Temperature";
int level = 0;
long long offset = 2517760;
Tile *t=retrieve_tile(user_id, dev_nickname, ch_name, level, offset);
if (t) {
std::cout << t->json;
}
}
void test_channel()
{
int user_id = 1;
std::string dev_nickname = "Josh_Basestation";
std::string ch_name = "Temperature";
double begin_time = 1289093120.98392;
double end_time = 1289093631.98183;
Channel ch(user_id, dev_nickname, ch_name);
ch.seek(begin_time);
double t, val;
fprintf(stderr, "Fetching from %f to %f\n", begin_time, end_time);
while (ch.fetch_next(t, val)) {
if (t > end_time) {
fprintf(stderr, "Fetching time %f, done\n", t);
break;
}
fprintf(stderr, "%lf: %g\n", t, val);
}
}
int main(int argc, char **argv)
{
debug = true;
std::string hostname="localhost";
std::string username="root";
std::string password="";
std::string database="bodytrack-dev";
std::string csv_root = "/Users/admin/projects/bodytrack/website/data";
Logrec::s_csv_root = csv_root;
MysqlQuery::s_debug = debug;
if (debug) std::cerr << "Connecting to mysql...\n";
if (!mysql_init(&mysql)) {
mysql_abort("mysql_init");
}
if (!mysql_real_connect(&mysql, hostname.c_str(), username.c_str(), password.c_str(),
database.c_str(), 0, 0, 0)) {
mysql_abort("connecting to mysql");
}
//test_retrieve_tile();
test_channel();
return 0;
}