-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathload_dev_env_data.php
290 lines (239 loc) · 9.79 KB
/
load_dev_env_data.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
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
<?php
// -------------------------------------------------------------------------------------
// This script pulls in public data from heatpumpmonitor.org and loads it into the database
// private data is not loaded and is replaced with dummy data
// -------------------------------------------------------------------------------------
// Set the host to pull data from
$heatpumpmonitor_host = "https://heatpumpmonitor.org";
// Change to www directory
$dir = dirname(__FILE__);
chdir("$dir/www");
chdir("/var/www/heatpumpmonitororg");
// Load the database
require "Lib/load_database.php";
require "core.php";
require "Lib/dbschemasetup.php";
// Load the schema
$schema = array();
require "Modules/user/user_schema.php";
require "Modules/system/system_schema.php";
require ("Modules/system/system_model.php");
$system_class = new System($mysqli);
require ("Modules/system/system_stats_model.php");
$system_stats_class = new SystemStats($mysqli,$system_class);
// Before starting load system list, if this fails we can exit before clearing the database
$data = file_get_contents("$heatpumpmonitor_host/system/list/public.json");
$systems = json_decode($data);
if ($systems==null) die("Error: could not load data from heatpumpmonitor.org");
// -------------------------------------------------------------------------------------
// 1. Confirm what data to load
// -------------------------------------------------------------------------------------
$reload_all = 0;
$load_users = 0;
$load_system_meta = 0;
$load_running_stats = 0;
$load_monthly_stats = 0;
$load_daily_stats = 0;
// Check if we should reload all data
if (isset($_ENV["RELOAD_ALL"])) {
$reload_all = (int) $_ENV["RELOAD_ALL"];
} else {
if (confirm("Would you like to reload all data?")) $reload_all = 1;
}
// Check if we should load users
if (isset($_ENV["LOAD_USERS"])) {
$load_users = (int) $_ENV["LOAD_USERS"];
} else {
if (confirm("Would you like to load users?")) $load_users = 1;
}
if ($reload_all) $load_users = 1;
// Check if we should load system meta
if (isset($_ENV["LOAD_SYSTEM_META"])) {
$load_system_meta = (int) $_ENV["LOAD_SYSTEM_META"];
} else {
if (confirm("Would you like to load system meta?")) $load_system_meta = 1;
}
if ($reload_all) $load_system_meta = 1;
// Check if we should load running stats
if (isset($_ENV["LOAD_RUNNING_STATS"])) {
$load_running_stats = (int) $_ENV["LOAD_RUNNING_STATS"];
} else {
if (confirm("Would you like to load running stats?")) $load_running_stats = 1;
}
if ($reload_all) $load_running_stats = 1;
// Check if we should load monthly stats
if (isset($_ENV["LOAD_MONTHLY_STATS"])) {
$load_monthly_stats = (int) $_ENV["LOAD_MONTHLY_STATS"];
} else {
if (confirm("Would you like to load monthly stats? This takes a little while, please only do this if required so as not to overload the server.")) $load_monthly_stats = 1;
}
if ($reload_all) $load_monthly_stats = 1;
// Check if we should load daily stats
if (isset($_ENV["LOAD_DAILY_STATS"])) {
$load_daily_stats = (int) $_ENV["LOAD_DAILY_STATS"];
} else {
if (confirm("Would you like to load daily stats? This takes ages, please only do this if required so as not to overload the server.")) $load_daily_stats = 1;
}
if ($reload_all) $load_daily_stats = 1;
// -------------------------------------------------------------------------------------
// 1. Clear the database
// -------------------------------------------------------------------------------------
if ($reload_all) {
$result = $mysqli->query("SHOW TABLES");
while ($row = $result->fetch_row()) {
$mysqli->query("DROP TABLE IF EXISTS `$row[0]`");
}
}
db_schema_setup($mysqli, $schema, true);
// -------------------------------------------------------------------------------------
// 2. Create users
// -------------------------------------------------------------------------------------
if ($load_users) {
// Get list of userid's
$users = array();
foreach ($systems as $system) {
$users[$system->userid] = $system->location;
}
// Create users using userid and dummy data
$index = 0;
$created_users = 0;
foreach ($users as $userid => $location) {
$username = "user".$userid;
$email = "[email protected]";
$password = "password";
// Make first user admin
if ($index==0) {
$admin = 1;
$username = "admin";
$password = "admin";
} else {
$admin = 0;
}
$hash = hash('sha256', $password);
$salt = generate_secure_key(16);
$hash = hash('sha256', $salt . $hash);
// Check if username already exists without prepared statement
$result = $mysqli->query("SELECT * FROM users WHERE username='$username'");
if ($result->num_rows==0) {
$created = time();
$stmt = $mysqli->prepare("INSERT INTO users ( id, username, hash, email, salt, created, email_verified, admin) VALUES (?,?,?,?,?,?,1,?)");
$stmt->bind_param("issssii", $userid, $username, $hash, $email, $salt, $created, $admin);
$stmt->execute();
$stmt->close();
$created_users++;
}
$index++;
}
print "- Created ".$created_users." users\n";
}
// -------------------------------------------------------------------------------------
// 3. Create systems
// -------------------------------------------------------------------------------------
if ($load_system_meta) {
$created_systems = 0;
foreach ($systems as $system) {
// Check if system already exists
$result = $mysqli->query("SELECT * FROM system_meta WHERE id='$system->id'");
if ($result->num_rows>0) {
print "- System already exists: $system->id\n";
continue;
}
// Create system
$mysqli->query("INSERT INTO system_meta (id,userid) VALUES ('$system->id', '$system->userid')");
$result = $system_class->save($system->userid, $system->id, $system, false);
if ($result['success']==false) {
echo "Error: could not save system: ".$system->id."\n";
print_r($result);
die;
} else {
$created_systems++;
}
}
$mysqli->query("UPDATE system_meta SET published=1");
print "- Created ".$created_systems." systems\n";
}
// -------------------------------------------------------------------------------------
// 4. Load stats summaries
// -------------------------------------------------------------------------------------
if ($load_running_stats) {
$tables = ["last7","last30","last90","last365","all"];
foreach ($tables as $table) {
print "- Loading $table stats summary ";
$data = file_get_contents("$heatpumpmonitor_host/system/stats/$table");
$stats = json_decode($data,true);
if ($stats==null) die("Error: could not load $table data from heatpumpmonitor.org");
foreach ($stats as $system) {
$system_stats_class->save_stats_table("system_stats_".$table."_v2",$system);
}
print count($stats)." systems\n";
}
}
// -------------------------------------------------------------------------------------
// 4. Load monthly summaries
// -------------------------------------------------------------------------------------
if ($load_monthly_stats) {
foreach ($systems as $system) {
print "- Loading monthly stats for system: $system->id ";
// https://heatpumpmonitor.org/system/stats/monthly?id=2
$data = file_get_contents("$heatpumpmonitor_host/system/stats/monthly?id=$system->id");
$stats = json_decode($data,true);
if ($stats==null) {
print "Error: could not load monthly data from heatpumpmonitor.org\n";
print $data;
continue;
}
foreach ($stats as $month) {
$system_stats_class->save_stats_table('system_stats_monthly_v2',$month);
}
print count($stats)." months\n";
}
}
// -------------------------------------------------------------------------------------
// 5. Load daily data
// -------------------------------------------------------------------------------------
if ($load_daily_stats) {
foreach ($systems as $system) {
$id = $system->id;
print "- Loading daily stats for system: $id ";
$days = 0;
// Initialize cURL session to download the CSV
$apiUrl = 'https://heatpumpmonitor.org/system/stats/export/daily?id='.$id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$csvData = curl_exec($ch);
curl_close($ch);
// Temporarily save CSV data to a file
$tempFilePath = tempnam(sys_get_temp_dir(), 'csv');
file_put_contents($tempFilePath, $csvData);
// Open the temporary file for reading
if (($handle = fopen($tempFilePath, 'r')) !== FALSE) {
// Skip the header row
$header = fgetcsv($handle, 2000, ",");
// Read the rest of the file
while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
// Build an associative array from the CSV data
$row = array();
foreach ($header as $i => $field) {
$row[$field] = $data[$i];
}
// Save the data to the database
$system_stats_class->save_day($row['id'], $row);
$days++;
// print a . every 1000 rows
if ($days % 100 == 0) {
print ".";
}
}
}
print " $days days\n";
}
}
print "Done\n";
function confirm($message) {
echo "$message (y/n): ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
return trim($line) == 'y';
}