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