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: Created Dec. 19, 2014, 1:16 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
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2014 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 os, MySQLdb, json
19 from urlparse import parse_qsl
20 from sitescripts.web import url_handler
21 from sitescripts.utils import cached, get_config, setupStderr
22
Sebastian Noack 2015/02/11 16:00:12 Nit: The newline should be between the corelib and
kzar 2015/02/17 10:52:24 Done.
23 import sitescripts.filterhits.common as common
Sebastian Noack 2015/02/11 16:00:12 Nit: You are misusing the "import .. as" syntax he
kzar 2015/02/17 10:52:24 Done.
24 import sitescripts.filterhits.db as db
25
26 def query_sql(domain=None, filter=None, skip=0, take=20, order_by="hits DESC", * *_):
27 sql = """SELECT SQL_CALC_FOUND_ROWS domain, filter, hits
28 FROM geometrical_mean as g
29 LEFT JOIN filters as f ON f.md5=g.filter_md5
30 %s
31 ORDER BY %s
32 LIMIT %d, %d;"""
33 where = ["domain LIKE '%%%s%%'" % db.escape(domain) if domain else None,
34 "filter LIKE '%%%s%%'" % db.escape(filter) if filter else None]
35 where = " AND ".join([f for f in where if f])
36 where = "WHERE " + where if where else ""
37 return sql % (where, db.escape(order_by), int(skip), int(take))
38
39 @url_handler("/query")
40 def query(environ, start_response):
41 setupStderr(environ["wsgi.errors"])
42 config = get_config()
43 params = dict(parse_qsl(environ.get('QUERY_STRING', '')))
44
45 try:
46 db.connect(config.get("filterhitstats", "dbuser"),
Sebastian Noack 2015/02/11 16:00:12 It seems this pattern is repeated. How about retri
Sebastian Noack 2015/02/17 14:59:17 What's about this comment?
kzar 2015/02/24 18:05:11 I prefer to do it this way, during development I e
Sebastian Noack 2015/02/26 16:39:25 That doesn't make any sense. The database and cred
kzar 2015/02/28 19:39:56 OK, I changed my mind here and I've now changed it
47 config.get("filterhitstats", "dbpassword"),
48 config.get("filterhitstats", "database"))
49 results = db.query(query_sql(**params), dict_result=True)
50 total = db.query("SELECT FOUND_ROWS();")[0][0]
51 except MySQLdb.Error:
52 return common.showError("Failed to query database!", start_response,
53 "500 Database error")
54 finally:
55 db.disconnect()
56
57 try:
58 echo = int(params["echo"])
59 except (ValueError, KeyError):
60 echo = 0
61
62 response_headers = [("Content-type", "application/json")]
63 start_response("200 OK", response_headers)
64 return [json.dumps({"results": results, "echo": echo,
65 "total": total, "count": len(results)})]
OLDNEW

Powered by Google App Engine
This is Rietveld