Commit | Line | Data |
---|---|---|
385ef3cf MM |
1 | #!/usr/bin/perl -w |
2 | ||
3 | # This script is used to delete tiles that are extremely cheap to rerender. | |
4 | # If a tile can be regenerated in one second, we really don't need to keep it | |
5 | # on disk and regularily update it. This can happen on the fly when it is | |
6 | # requested instead. | |
7 | ||
8 | # Where are the tile directories? | |
9 | $tiledirprefix = '/mnt/tiles/tiles/'; | |
10 | # Map for map-ID in DB to tile subdir. | |
11 | %tiledirmap = ( 1 => 'osm', | |
12 | 2 => 'osmde', | |
13 | 4 => 'osmhd' | |
14 | ); | |
15 | # What is the maximum rendertime to consider a tile useless? | |
16 | $maxuselesstime = 0.5; | |
17 | ||
18 | # How old do files need to be before we delete them? in seconds | |
19 | $minuselessage = 48 * 60 * 60; | |
20 | ||
21 | # Where is the osm stats database? | |
22 | $statsdb = 'osmstats'; | |
23 | ||
24 | # ----------------------------------------------------------------------------- | |
25 | ||
26 | use DBI; | |
27 | use POSIX qw(strftime mktime); | |
28 | use Fcntl ':mode'; | |
29 | ||
30 | # Par. 0: x | |
31 | # Par. 1: y | |
32 | sub calcpathfromcomponents($$) { | |
33 | my @res = (); | |
34 | my $i; my $x = $_[0]; my $y = $_[1]; | |
35 | for ($i = 4; $i >= 0; $i--) { | |
36 | $res[$i] = sprintf("%d", (($x & 0x0f) << 4) + ($y & 0x0f)); | |
37 | $x = $x >> 4; | |
38 | $y = $y >> 4; | |
39 | } | |
40 | return $res[0] . "/" . $res[1] . "/" . $res[2] . "/" . $res[3] . "/" . $res[4]; | |
41 | } | |
42 | ||
43 | unless ($dbh = DBI->connect("dbi:Pg:dbname=$statsdb","","")) { | |
44 | print(STDERR "Failed to open database. Please try again later.\n"); exit(1); | |
45 | } | |
46 | # select x, y, z, avg(rendertime) as avgrt, max(rendertime) as maxrt from renderrequests where mapid=1 group by x, y, z | |
47 | # select distinct on (x, y, z) x, y, z, rendertime from renderrequests where mapid=1 order by x,y,z,ts desc; | |
48 | foreach $mapid (sort(keys(%tiledirmap))) { | |
49 | my $ndeleted = 0; | |
50 | my $sth = $dbh->prepare("select distinct on (x, y, z) x, y, z, rendertime" | |
51 | . " from renderrequests where mapid=? order by x, y, z, ts desc"); | |
52 | unless ($sth->execute($mapid)) { | |
53 | print(STDERR "Sorry, database query blew up.!\n"); | |
54 | exit(1); | |
55 | } | |
56 | while (($x, $y, $z, $rendertime) = $sth->fetchrow_array()) { | |
57 | if ($z < 5) { next; } | |
58 | if ($rendertime > $maxuselesstime) { | |
59 | #print("$tiledirmap{$mapid} $z/$x/$y rendered in $rendertime seconds, leaving it alone\n"); | |
60 | next; | |
61 | } | |
62 | my $tilepath = $tiledirprefix . $tiledirmap{$mapid} . '/'; | |
63 | $tilepath .= $z . '/' . calcpathfromcomponents($x, $y) . '.meta'; | |
64 | unless (-e $tilepath) { | |
65 | #print("...file does not exist, nothing to delete.\n"); | |
66 | next; | |
67 | } | |
68 | @statres = lstat($tilepath); | |
69 | unless (@statres > 12) { | |
70 | # This usually happens if we have a symlink to a file for which we have | |
71 | # no permission to read. | |
72 | #print("...stat failed\n"); | |
73 | next; | |
74 | } | |
75 | if (S_ISLNK($statres[2])) { next; } # A symlink? We no like. | |
76 | if (($statres[9] + $minuselessage) >= time()) { | |
77 | #print("...not old enough, skipped\n"); | |
78 | next; | |
79 | } | |
80 | print("$tiledirmap{$mapid} $z/$x/$y rendered in $rendertime seconds, deleting $tilepath\n"); | |
81 | unlink($tilepath); | |
82 | $ndeleted++; | |
83 | } | |
84 | $sth->finish(); | |
85 | print("Deleted $ndeleted tiles in $tiledirmap{$mapid}\n"); | |
86 | } |