-
Notifications
You must be signed in to change notification settings - Fork 2
/
tstat_rebuild_db.php
182 lines (155 loc) · 4.14 KB
/
tstat_rebuild_db.php
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
<?php
// Thermostat Poll for MRTG - v1.0
//
// This script is to rebuild the database from the cvs files.
// This was mostly needed during development, but
//https://fw.jwsmythe.com/thermostat/tstat_api_gw.php/tstat
//Notes:
//https://stackoverflow.com/questions/1711631/improve-insert-per-second-performance-of-sqlite
require_once("tstat_globals.php");
// If you want to test in a safe sandbox.
//$datadb = "data/testing.sqlite";
// Back up old DB file.
$datadb_back = $datadb . '.' . date('Ymd.His') . '.bak';
print "Backing up $datadb to $datadb_back \n\n";
rename($datadb, $datadb_back);
if (!file_exists($datadb)){
print "DB file $datadb missing. Creating new DB\n";
$db = new SQLite3($datadb);
$query = "
CREATE TABLE data
(
ID INTEGER PRIMARY KEY,
ts INT NOT NULL,
t_actual REAL NOT NULL,
t_target FLOAT NOT NULL,
i_mode INT NOT NULL,
s_mode VARCHAR(8)
);
CREATE INDEX ts_idx ON data(ts);
";
$return = $db->exec($query);
if(!$return){
print $db->lastErrorMsg();
}else{
print "Table created successfully\n";
}
$db->close();
};
// Open database
$db = new SQLite3($datadb);
if(!$db) {
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
// Drop the index for speed.
/*
$query = "DROP INDEX ts_idx";
$return = $db->exec($query);
if(!$return){
print $db->lastErrorMsg();
}else{
print "index dropped successfully.\n";
}
*/
// This speeds up inserts. Without it, there is a pause telling the filesystem to sync on each write.
// Disabling it lets the OS handle writes at it's leisure. It'll be active when the normal code runs.
$query = " CREATE TABLE data
(
ID INTEGER PRIMARY KEY,
ts INT NOT NULL,
t_actual REAL NOT NULL,
t_target FLOAT NOT NULL,
i_mode INT NOT NULL,
s_mode VARCHAR(8)
);
CREATE INDEX ts_idx ON data(ts);
";
/*
$query = "PRAGMA synchronous = OFF;";
$return = $db->exec($query);
if(!$return){
print $db->lastErrorMsg();
}else{
print "PRAGMA synchronous = OFF;\n";
}
*/
/*
$query = "PRAGMA journal_mode = MEMORY";
$return = $db->exec($query);
if(!$return){
print $db->lastErrorMsg();
}else{
print "PRAGMA journal_mode = MEMORY\n";
}
*/
$files = `ls -1 --sort=time data/*csv`;
// */ // Notepad++ is having problems parsing the previous line. This fixes it.
$files = explode("\n", ltrim(rtrim($files)));
// Iterate through the files.
foreach ($files as $thisfile){
print "curfile: $thisfile\n";
$raw = explode("\n", ltrim(rtrim(file_get_contents($thisfile))));
$num_rows = count($raw);
print "I detected $num_rows rows in insert file.\n";
$count=0;
$query = "
INSERT INTO data
(ts, t_actual, t_target, i_mode, s_mode)
VALUES ";
// Iterate through the rows.
$goodrow = 0;
foreach ($raw as $thisline){
$thisrow = explode(", ", $thisline);
print "THIS ROW: |" . print_r($thisrow, TRUE) . "|\n";
if ($thisrow[0] > 0){
$goodrow++;
$query .= "(" . $thisrow[0] . ", " . $thisrow[1] . ", " . $thisrow[2] . ", " . $thisrow[3] . ", \"" . $thisrow[4] . "\")";
if ($count < $num_rows-1){
$query .= ",";
};
$count++;
}else{
print "Skipping blank row\n";
};
};
if ($goodrow > 0){
print "QUERY\n$query\n";
$return = $db->exec($query);
if(!$return){
print $db->lastErrorMsg();
}else{
print "Bulk Record inserted successfully.\n";
}
};
};
/*
$query = "PRAGMA synchronous = ON;";
$return = $db->exec($query);
if(!$return){
print $db->lastErrorMsg();
}else{
print "PRAGMA synchronous = ON;\n";
}
*/
/*
// Recreate index.
$query = "CREATE INDEX ts_idx ON data(ts);";
$return = $db->exec($query);
if(!$return){
print $db->lastErrorMsg();
}else{
print "index recreated successfully.\n";
}
*/
$db->close();
$check_cmd = "sqlite3 $datadb '.schema'; sqlite3 $datadb 'select count(*) from data;'";
print "\n";
print "==============================================================\n";
print "=== Checking results with: =================================\n";
print "$check_cmd \n\n";
print `$check_cmd`;
print "==============================================================\n";
exit;
?>