| OLD | NEW |
| (Empty) |
| 1 # coding: utf-8 | |
| 2 | |
| 3 # This file is part of the Adblock Plus web scripts, | |
| 4 # Copyright (C) 2006-2016 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 | |
| 20 from sitescripts.filterhits import db | |
| 21 | |
| 22 | |
| 23 def update(interval, data): | |
| 24 """ | |
| 25 Generator that provides all the SQL and parameters needed to update the | |
| 26 aggregations for the given data + interval in the database. | |
| 27 """ | |
| 28 for filter, filter_data in data["filters"].iteritems(): | |
| 29 yield ("""INSERT IGNORE INTO `filters` | |
| 30 (filter, sha1) VALUES (%s, UNHEX(SHA1(filter)))""", filter) | |
| 31 | |
| 32 domains = itertools.chain(filter_data.get("thirdParty", {}).iteritems(), | |
| 33 filter_data.get("firstParty", {}).iteritems()) | |
| 34 for domain, domain_data in domains: | |
| 35 yield ("""INSERT INTO `frequencies` | |
| 36 (filter_sha1, domain, frequency, timestamp) | |
| 37 VALUES (UNHEX(SHA1(%s)), %s, %s, FROM_UNIXTIME(%s)) | |
| 38 ON DUPLICATE KEY UPDATE | |
| 39 frequency = ( | |
| 40 POW(frequency, 1 - (UNIX_TIMESTAMP(VALUES(timestamp)) - | |
| 41 UNIX_TIMESTAMP(timestamp)) / %s) * | |
| 42 POW(VALUES(frequency), (UNIX_TIMESTAMP(VALUES(timestamp)) - | |
| 43 UNIX_TIMESTAMP(timestamp)) / %s)), | |
| 44 timestamp = VALUES(timestamp)""", | |
| 45 filter, domain, domain_data["hits"], | |
| 46 int(domain_data["latest"] / 1000), interval, interval) | |
| OLD | NEW |