summaryrefslogtreecommitdiffstats
path: root/modules-available/eventlog/hooks/cron.inc.php
blob: bf7ced179e809b7c29305a6ca69b1b3bc65826ff (plain) (blame)
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
<?php

if (mt_rand(1, 10) === 1) {
	Database::exec("DELETE FROM eventlog WHERE (UNIX_TIMESTAMP() - 86400 * 190) > dateline");
	// Keep at least 30 events or 7 days wirth of samples (whichever is more)
	$types = Database::simpleQuery("SELECT type, Count(*) AS num, Min(dateline) as oldest
			FROM `notification_sample` GROUP BY type");
	$cutoff = time() - 86400 * 7;
	foreach ($types as $type) {
		if ($type['num'] > 30 && $type['oldest'] < $cutoff) {
			// This type has more than 30 and the oldest one is older than 7 days
			// find out which one takes priority
			$thisCutoff = $cutoff;
			$find = Database::queryFirst("SELECT dateline FROM notification_sample
					WHERE type = :type AND dateline
					ORDER BY dateline DESC
					LIMIT 29, 1",
				['type' => $type['type']]);
			// The 30th entry is older than 7 days? Bump the cutoff dateline back to this date,
			// so we keep at least 30 entries
			if ($find !== false && $find['dateline'] < $thisCutoff) {
				$thisCutoff = $find['dateline'];
			}
			Database::exec("DELETE FROM notification_sample
					WHERE type = :type AND dateline < :dateline",
				['type' => $type['type'], 'dateline' => $thisCutoff]);
		}
	}
}