LEFT | RIGHT |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import itertools | 18 import itertools |
19 import sitescripts.filterhits.db as db | |
20 from functools import partial | |
21 | 19 |
22 def filter_hits(data): | 20 from sitescripts.filterhits import db |
| 21 |
| 22 def update(interval, data): |
23 """ | 23 """ |
24 Generator that provides all filter hits for the given data, | 24 Generator that provides all the SQL and parameters needed to update the |
25 in tuples like (filter, domain, hits, latest). | 25 aggregations for the given data + interval in the database. |
26 """ | 26 """ |
27 for filter, filter_data in data['filters'].iteritems(): | 27 for filter, filter_data in data["filters"].iteritems(): |
| 28 yield ("""INSERT IGNORE INTO `filters` |
| 29 (filter, sha1) VALUES (%s, UNHEX(SHA1(filter)))""", filter) |
| 30 |
28 domains = itertools.chain(filter_data.get("thirdParty", {}).iteritems(), | 31 domains = itertools.chain(filter_data.get("thirdParty", {}).iteritems(), |
29 filter_data.get("firstParty", {}).iteritems()) | 32 filter_data.get("firstParty", {}).iteritems()) |
30 for domain, domain_data in domains: | 33 for domain, domain_data in domains: |
31 yield (filter, domain, domain_data["hits"], domain_data["latest"] / 1000) | 34 yield ("""INSERT INTO `frequencies` |
32 | 35 (filter_sha1, domain, frequency, timestamp) |
33 def update_query(interval, filter, domain, hits, latest): | 36 VALUES (UNHEX(SHA1(%s)), %s, %s, FROM_UNIXTIME(%s)) |
34 """ | 37 ON DUPLICATE KEY UPDATE |
35 Function that takes the fields for a filter hit and returns them arranged | 38 frequency = ( |
36 as the update SQL requires along with the SQL itself. | 39 POW(frequency, 1 - (UNIX_TIMESTAMP(VALUES(timestamp)) - |
37 """ | 40 UNIX_TIMESTAMP(timestamp)) / %s) * |
38 return (("""INSERT IGNORE INTO `filters` | 41 POW(VALUES(frequency), (UNIX_TIMESTAMP(VALUES(timestamp)) - |
39 (filter, sha1) VALUES (%s, UNHEX(SHA1(filter)))""", | 42 UNIX_TIMESTAMP(timestamp)) / %s)), |
40 filter), | 43 timestamp = VALUES(timestamp)""", |
41 ("""INSERT INTO `geometrical_mean` | 44 filter, domain, domain_data["hits"], |
42 (filter_sha1, domain, hits, timestamp) | 45 int(domain_data["latest"] / 1000), interval, interval) |
43 VALUES (UNHEX(SHA1(%s)), %s, %s, FROM_UNIXTIME(%s)) | |
44 ON DUPLICATE KEY UPDATE | |
45 hits = ( | |
46 POW(hits, 1 - (UNIX_TIMESTAMP(VALUES(timestamp)) - | |
47 UNIX_TIMESTAMP(timestamp)) / %s) * | |
48 POW(VALUES(hits), (UNIX_TIMESTAMP(VALUES(timestamp)) - | |
49 UNIX_TIMESTAMP(timestamp)) / %s)), | |
50 timestamp = VALUES(timestamp)""", | |
51 filter, domain, hits, int(latest), interval, interval)) | |
52 | |
53 def update(interval, data): | |
54 """ | |
55 Returns an iterator of all the SQL and parameters needed to | |
56 update the aggregations for the given data + interval in the database. | |
57 """ | |
58 for fields in filter_hits(data): | |
59 for query in update_query(interval, *fields): | |
60 yield query | |
LEFT | RIGHT |