forked from sergeychernyshev/showslow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteolddata.php
102 lines (81 loc) · 2.14 KB
/
deleteolddata.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
<?php require_once('global.php');
if ($oldDataInterval > 0) {
$tables = array(
'har',
'metric',
'yslow2',
'pagespeed',
'pagetest',
'dommonster',
'dynatrace'
);
foreach ($tables as $table) {
# deleting old data
$query = sprintf("DELETE FROM $table WHERE timestamp < DATE_SUB(now(), INTERVAL '%s' DAY)",
mysql_real_escape_string($oldDataInterval)
);
$result = mysql_query($query);
if (!$result) {
error_log(mysql_error());
exit;
}
}
# deleting URLs with no measurements that are not requested to be tracked by users
$query = 'DELETE urls FROM urls';
foreach ($tables as $table) {
$query .= "\nLEFT JOIN (SELECT DISTINCT url_id FROM $table) AS x_$table
ON urls.id = x_$table.url_id";
}
$query .= "\nLEFT JOIN (SELECT DISTINCT url_id FROM user_urls) AS uu ON urls.id = uu.url_id";
$query .= "\nWHERE\n";
$first = true;
foreach ($tables as $table) {
if ($first) {
$first = false;
} else {
$query .= "AND ";
}
$query .= "x_$table.url_id IS NULL\n";
}
$query .= 'AND uu.url_id IS NULL';
$result = mysql_query($query);
if (!$result) {
error_log(mysql_error());
exit;
}
# resetting last_updated for URLs that have no measurements
$query = "UPDATE urls";
foreach ($tables as $table) {
$query .= "\nLEFT JOIN (SELECT DISTINCT url_id FROM $table) AS x_$table
ON urls.id = x_$table.url_id";
}
$query .= "\nSET last_update = NULL";
foreach ($tables as $table) {
$query .= ",\n$table"."_last_id = NULL";
}
$query .= "\nWHERE\n";
$first = true;
foreach ($tables as $table) {
if ($first) {
$first = false;
} else {
$query .= "AND ";
}
$query .= "x_$table.url_id IS NULL\n";
}
$result = mysql_query($query);
if (!$result) {
error_log(mysql_error());
exit;
}
# deleting old events separately as they match broadly, not per URL
$query = sprintf("DELETE FROM event WHERE (end IS NOT NULL AND end < DATE_SUB(now(), INTERVAL '%s' DAY)) OR (start < DATE_SUB(now(), INTERVAL '%s' DAY))",
mysql_real_escape_string($oldDataInterval),
mysql_real_escape_string($oldDataInterval)
);
$result = mysql_query($query);
if (!$result) {
error_log(mysql_error());
exit;
}
}