Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/filterhits/web/query.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Added API tests, addressed comments and some other improvements. Created Feb. 28, 2015, 7:36 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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-2014 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 os, MySQLdb, json 18 import os, MySQLdb, json
19 from urlparse import parse_qs 19 from urlparse import parse_qsl
20
20 from sitescripts.web import url_handler 21 from sitescripts.web import url_handler
21 from sitescripts.utils import cached, get_config, setupStderr 22 from sitescripts.utils import cached, setupStderr
23 from sitescripts.filterhits import common
24 from sitescripts.filterhits import db
25
26 def query(domain=None, filter=None, skip=0, take=20, order="DESC", order_by="hit s", **_):
27 """
28 Returns the SQL and parameters needed to perform a query of the filterhits dat a.
29 """
30 sql = """SELECT SQL_CALC_FOUND_ROWS domain, filter, hits
31 FROM geometrical_mean as g
32 LEFT JOIN filters as f ON f.sha1=g.filter_sha1
33 %s
34 ORDER BY %s
35 LIMIT %%s, %%s"""
36
37 where_fields = [(s, "%" + p + "%") for s, p in (("domain", domain),
38 ("filter", filter)) if p]
39 where = " AND ".join([f[0] + " LIKE %s" for f in where_fields])
40 where_sql = "WHERE " + where if where else ""
41
42 order = order.upper() if order.upper() in ("ASC", "DESC") else "ASC"
43 order_by_sql = "`%s` %s" % (MySQLdb.escape_string(order_by), order)
44
45 params = [f[1] for f in where_fields] + [int(skip), int(take)]
46 return [sql % (where_sql, order_by_sql)] + params
22 47
23 @url_handler("/query") 48 @url_handler("/query")
24 def submit(environ, start_response): 49 def query_handler(environ, start_response):
25 setupStderr(environ["wsgi.errors"]) 50 setupStderr(environ["wsgi.errors"])
51 params = dict(parse_qsl(environ.get('QUERY_STRING', '')))
26 52
27 response_headers = [("Content-type", "text/plain")] 53 try:
54 db_connection = db.connect()
55 try:
56 results = db.query(db_connection, *query(**params), dict_result=True)
57 total = db.query(db_connection, "SELECT FOUND_ROWS()")[0][0]
58 finally:
59 db_connection.close()
60 except MySQLdb.Error:
61 return common.showError("Failed to query database!", start_response,
62 "500 Database error")
63
64 try:
65 echo = int(params["echo"])
66 except (ValueError, KeyError):
67 echo = 0
68
69 response_headers = [("Content-type", "application/json")]
28 start_response("200 OK", response_headers) 70 start_response("200 OK", response_headers)
29 return ["Hello world"] 71 return [json.dumps({"results": results, "echo": echo,
72 "total": total, "count": len(results)})]
OLDNEW

Powered by Google App Engine
This is Rietveld