-
Notifications
You must be signed in to change notification settings - Fork 7
/
ITpings__database_maintenance.php
53 lines (45 loc) · 1.64 KB
/
ITpings__database_maintenance.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
<?php
function Execute_Multiple_Queries($sql)
{
echo "<PRE>" . str_replace(';', '<BR/>', $sql) . "</PRE>";
SQL_MULTI_QUERY($sql);
}
/**
* KEEP all sensorvalues that are in: minutes ending on: 0,5,10,15,20,25,30,35,40,45,50,55
* UP TO the values reveived over 1 day ago (so last 24 hour entries are All received sensor readings)
* @param $orgTable - TABLE-nnnnn constant
* @return string
*/
function Clean_DataTable_SQL($orgTable)
{
$newTable = $orgTable . "_new";
$oldTable = $orgTable . "_old";
$PKey = PRIMARYKEY_Ping;
$created = ITPINGS_CREATED_TIMESTAMP;
$sql = "DROP TABLE IF EXISTS $newTable;";
$sql .= "CREATE TABLE $newTable LIKE $orgTable;";
$sql .= "INSERT INTO $newTable";
$sql .= " (SELECT T.* FROM " . TABLE_PINGS . " P";
$sql .= " JOIN $orgTable T ON P.$PKey = T.$PKey";
$sql .= " WHERE MINUTE(P.$created) IN (0,5,10,15,20,25,30,35,40,45,50,55)";
$sql .= " AND P.$created < DATE_SUB(NOW(), INTERVAL 1 DAY)";
$sql .= " LIMIT 999999);";
$sql .= "DROP TABLE IF EXISTS $oldTable;";
$sql .= "RENAME TABLE $orgTable TO $oldTable;";
$sql .= "RENAME TABLE $newTable TO $orgTable;";
$sql .= "DROP TABLE IF EXISTS $oldTable;";
return $sql;
}
function Clean_DataTables()
{
$sql = Clean_DataTable_SQL(TABLE_DATA_TEMPERATURE);
$sql .= Clean_DataTable_SQL(TABLE_DATA_LUMINOSITY);
$sql .= Clean_DataTable_SQL(TABLE_DATA_BATTERY);
Execute_Multiple_Queries($sql);
}
function Clean_SensorValues()
{
// delete Cayenne Digital-3 sensorvalues
$sql = "DELETE FROM " . TABLE_SENSORVALUES . " WHERE " . PRIMARYKEY_Sensor . " IN (5,12);";
Execute_Multiple_Queries($sql);
}