forked from rszimm/sprinklers_pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logging.cpp
274 lines (244 loc) · 7.18 KB
/
Logging.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
// Logging.cpp
// This manages the Logging to SQL for the Sprinkling System.
// Author: Richard Zimmerman
// Copyright (c) 2013 Richard Zimmerman
//
#define __STDC_FORMAT_MACROS
#include "Logging.h"
#include "port.h"
#include "settings.h"
#include <sqlite3.h>
#include <string.h>
Logging::Logging()
: m_db(0)
{
}
Logging::~Logging()
{
Close();
}
static int GetDBVersion(sqlite3 * db)
{
int ret_val = 0;
sqlite3_stmt * statement;
// Now determine if we're running V1.0 of the schema.
int res = sqlite3_prepare_v2(db, "SELECT MAX(version) FROM versions;", -1, &statement, NULL);
if (res)
{
trace("Prepare Failure (%s)\n", sqlite3_errmsg(db));
return 0;
}
res = sqlite3_step(statement);
if (res == SQLITE_ROW)
{
ret_val = sqlite3_column_int(statement, 0);
}
sqlite3_finalize(statement);
return ret_val;
}
static bool CreateSchema(sqlite3 * db)
{
char * zErrMsg = 0;
if (sqlite3_exec(db,
"DROP TABLE IF EXISTS versions; DROP TABLE IF EXISTS zonelog; CREATE TABLE versions (version INT);"
"INSERT INTO versions VALUES (2);CREATE TABLE zonelog(date INTEGER, zone INTEGER, duration INTEGER, schedule INTEGER,"
"seasonal INTEGER, wunderground INTEGER);",
NULL, NULL, &zErrMsg) != SQLITE_OK)
{
trace("SQL Error (%s)\n", zErrMsg);
sqlite3_free(zErrMsg);
return false;
}
return true;
}
static bool UpdateV1toV2(sqlite3 * db)
{
char * zErrMsg = 0;
if (sqlite3_exec(db,
"begin;DROP TABLE IF EXISTS tmp_zonelog; CREATE TABLE tmp_zonelog(date INTEGER, zone INTEGER, duration INTEGER, schedule INTEGER,"
"seasonal INTEGER, wunderground INTEGER);INSERT INTO tmp_zonelog SELECT date, zone, duration, schedule, -1, -1 from zonelog;"
"DROP TABLE zonelog; ALTER TABLE tmp_zonelog rename to zonelog;INSERT INTO versions VALUES (2);commit;",
NULL, NULL, &zErrMsg) != SQLITE_OK)
{
trace("SQL Error (%s)\n", zErrMsg);
sqlite3_free(zErrMsg);
return false;
}
return true;
}
bool Logging::Init()
{
int rc = sqlite3_open("db.sql", &m_db);
if (rc)
{
trace("Can't open Database (%s)\n", sqlite3_errmsg(m_db));
Close();
return false;
}
int version = GetDBVersion(m_db);
if (version == 0)
CreateSchema(m_db);
else if (version == 1)
UpdateV1toV2(m_db);
else if (version != 2)
CreateSchema(m_db);
return true;
}
void Logging::Close()
{
if (m_db)
{
sqlite3_close(m_db);
m_db = 0;
}
}
bool Logging::LogZoneEvent(time_t start, int zone, int duration, int schedule, int sadj, int wunderground)
{
char * zErrMsg = 0;
char sSQL[100];
snprintf(sSQL, sizeof(sSQL), "INSERT INTO zonelog VALUES(%ld, %d, %d, %d, %d, %d);", start, zone, duration, schedule, sadj, wunderground);
sSQL[sizeof(sSQL) - 1] = 0;
if (sqlite3_exec(m_db, sSQL, NULL, NULL, &zErrMsg) != SQLITE_OK)
{
trace("SQL Error (%s)\n", zErrMsg);
sqlite3_free(zErrMsg);
return false;
}
return true;
}
static void DumpData(FILE * stream_file, uint32_t bin_data[], uint32_t bins, uint32_t bin_scale, uint32_t bin_offset, bool bMil)
{
for (uint32_t i=0; i<bins; i++)
fprintf(stream_file, "%s[%" PRIu32 "%s, %" PRIu32 "]", (i==0)?"":",", bin_offset + i*bin_scale, bMil?"000":"", bin_data[i]);
}
bool Logging::GraphZone(FILE* stream_file, time_t start, time_t end, GROUPING grouping)
{
if (start == 0)
start = nntpTimeServer.LocalNow();
end = spi_max(start,end) + 24*3600; // add 1 day to end time.
grouping = spi_max(NONE, spi_min(grouping, MONTHLY));
char sSQL[200];
uint16_t bins = 0;
uint32_t bin_offset = 0;
uint32_t bin_scale = 1;
switch (grouping)
{
case HOURLY:
snprintf(sSQL, sizeof(sSQL),
"SELECT zone, strftime('%%H', date, 'unixepoch') as hour, SUM(duration)"
" FROM zonelog WHERE date BETWEEN %lu AND %lu"
" GROUP BY zone,hour ORDER BY zone,hour",
start, end);
bins = 24;
break;
case DAILY:
snprintf(sSQL, sizeof(sSQL),
"SELECT zone, strftime('%%w', date, 'unixepoch') as bucket, SUM(duration)"
" FROM zonelog WHERE date BETWEEN %lu AND %lu"
" GROUP BY zone,bucket ORDER BY zone,bucket",
start, end);
bins = 7;
break;
case MONTHLY:
snprintf(sSQL, sizeof(sSQL),
"SELECT zone, strftime('%%m', date, 'unixepoch') as bucket, SUM(duration)"
" FROM zonelog WHERE date BETWEEN %lu AND %lu"
" GROUP BY zone,bucket ORDER BY zone,bucket",
start, end);
bins = 12;
break;
case NONE:
bins = 100;
bin_offset = start;
bin_scale = (end-start)/bins;
snprintf(sSQL, sizeof(sSQL),
"SELECT zone, ((date-%lu)/%" PRIu32 ") as bucket, SUM(duration)"
" FROM zonelog WHERE date BETWEEN %lu AND %lu"
" GROUP BY zone,bucket ORDER BY zone,bucket",
start, bin_scale, start, end);
break;
}
// Make sure we've zero terminated this string.
sSQL[sizeof(sSQL)-1] = 0;
trace("%s\n", sSQL);
sqlite3_stmt * statement;
// Now determine if we're running V1.0 of the schema.
int res = sqlite3_prepare_v2(m_db, sSQL, -1, &statement, NULL);
if (res)
{
trace("Prepare Failure (%s)\n", sqlite3_errmsg(m_db));
return false;
}
int current_zone = -1;
bool bFirstZone = true;
uint32_t bin_data[bins];
memset(bin_data, 0, bins*sizeof(uint32_t));
while (sqlite3_step(statement) == SQLITE_ROW)
{
int zone = sqlite3_column_int(statement, 0);
if (current_zone != zone)
{
current_zone = zone;
if (!bFirstZone)
DumpData(stream_file, bin_data, bins, bin_scale, bin_offset, grouping == NONE);
fprintf(stream_file, "%s\t\"%d\" : [", bFirstZone?"":"],\n", zone);
memset(bin_data, 0, bins*sizeof(uint32_t));
bFirstZone = false;
}
uint32_t bin = sqlite3_column_int(statement, 1);
uint32_t value = sqlite3_column_int(statement, 2);
bin_data[bin] = value;
}
if (!bFirstZone)
{
DumpData(stream_file, bin_data, bins, bin_scale, bin_offset, grouping == NONE);
fprintf(stream_file, "]\n");
}
sqlite3_finalize(statement);
return true;
}
bool Logging::TableZone(FILE* stream_file, time_t start, time_t end)
{
if (start == 0)
start = nntpTimeServer.LocalNow();
end = spi_max(start,end) + 24*3600; // add 1 day to end time.
char sSQL[200];
snprintf(sSQL, sizeof(sSQL),
"SELECT zone, date, duration, schedule, seasonal, wunderground"
" FROM zonelog WHERE date BETWEEN %lu AND %lu"
" ORDER BY zone,date",
start, end);
// Make sure we've zero terminated this string.
sSQL[sizeof(sSQL)-1] = 0;
sqlite3_stmt * statement;
// Now determine if we're running V1.0 of the schema.
int res = sqlite3_prepare_v2(m_db, sSQL, -1, &statement, NULL);
if (res)
{
trace("Prepare Failure (%s)\n", sqlite3_errmsg(m_db));
return false;
}
int current_zone=-1;
bool bFirstRow = false;
while (sqlite3_step(statement) == SQLITE_ROW)
{
int zone = sqlite3_column_int(statement, 0);
if (current_zone != zone)
{
fprintf(stream_file, "%s\t\t{\n\t\t\t\"zone\" : %d,\n\t\t\t\"entries\" : [", current_zone==-1?"":"\n\t\t\t]\n\t\t},\n", zone);
current_zone = zone;
bFirstRow = true;
}
fprintf(stream_file, "%s\n\t\t\t\t{ \"date\":%ld, \"duration\":%d, \"schedule\":%d, \"seasonal\":%d, \"wunderground\":%d}",
bFirstRow ? "":",",
(long)sqlite3_column_int(statement, 1), sqlite3_column_int(statement, 2), sqlite3_column_int(statement, 3),
sqlite3_column_int(statement, 4), sqlite3_column_int(statement, 5));
bFirstRow = false;
}
if (current_zone!=-1)
{
fprintf(stream_file, "\n\t\t\t]\n\t\t}\n");
}
sqlite3_finalize(statement);
return true;
}