# HG changeset patch # User Daniele Nicolodi # Date 1320751337 -3600 # Node ID c52ae3f0196f55b4f4b0b34e0380d7a2a2801a7e # Parent 90b18bfd737591e07fdd9e8487c58945ec5f76a2 Make named queries store ordering and work for timeseries searches. diff -r 90b18bfd7375 -r c52ae3f0196f src/ltpdarepo/query.py --- a/src/ltpdarepo/query.py Tue Nov 08 12:19:12 2011 +0100 +++ b/src/ltpdarepo/query.py Tue Nov 08 12:22:17 2011 +0100 @@ -1,6 +1,6 @@ import re -from flask import Markup, g, json +from flask import g, json from MySQLdb.cursors import DictCursor from wtforms.fields import TextField, HiddenField from wtforms import validators @@ -29,9 +29,32 @@ class Query(dict): - def __init__(self, title=None, db=None, query=None): - super(Query, self).__init__(title=title, db=db, query=query) - self.__dict__ = self + __slots__ = ('id', 'title', 'db', '_query', '_order', '_times') + + def __init__(self, query='', id=None, title=None, db=None): + self.id = id + self.title = title + self.db = db + self.query = query + + def _getquery(self): + if self._times is not None: + return json.dumps({'query': self._query, 'order': self._order, 'times': self._times}) + return json.dumps({'query': self._query, 'order': self._order}) + + def _setquery(self, string): + if not string: + return + obj = json.loads(string) + self._query = obj.get('query') + self._order = obj.get('order') + self._times = obj.get('times') + + query = property(_getquery, _setquery) + + @property + def kind(self): + return self._times is not None and 'timeseries' or 'query' @staticmethod def load(id): @@ -41,22 +64,64 @@ query = curs.fetchone() if query is None: return None - obj = Query() - obj.update(query) + obj = Query(**query) return obj + def parse(self, string): + # parse url params + from urlparse import parse_qs + from werkzeug.datastructures import MultiDict + + # parse string into dictionary + formdata = parse_qs(string, keep_blank_values=True, strict_parsing=True) + formdata = MultiDict(formdata) + + # query parameters + fields = formdata.getlist('field') + operators = formdata.getlist('operator') + values = formdata.getlist('value') + self._query = zip(fields, operators, values) + + # ordering + self._order = formdata.get('o', 'id'), formdata.get('r', 0) + + # timeseries search + self._times = formdata.get('t1'), formdata.get('t2') + if self._times[0] is None and self._times[1] is None: + self._times = None + + @property + def params(self, **kwargs): + # return url params to compose the query + fields = [] + operators = [] + values = [] + for f, o, v in self._query: + fields.append(f) + operators.append(o) + values.append(v) + params = {'field': fields, + 'operator': operators, + 'value': values, + 'o': self._order[0], + 'r': self._order[1]} + if self._times: + params.update({'t1': self._times[0], + 't2': self._times[1]}) + params.update(**kwargs) + return params + def create(self): curs = g.db.cursor() curs.execute("""INSERT INTO queries (title, db, query) - VALUES (%(title)s, %(db)s, %(query)s)""", dict(self)) + VALUES (%s, %s, %s)""", (self.title, self.db, self.query)) g.db.commit() return curs.lastrowid def save(self): curs = g.db.cursor() - curs.execute("""UPDATE queries - SET title=%(title)s, db=%(db)s, query=%(query)s - WHERE id=%(id)s""", dict(self)) + curs.execute("""UPDATE queries SET title=%s, db=%s, query=%s + WHERE id=%s""", (self.title, self.db, self.query, self.id)) g.db.commit() def drop(self): @@ -64,8 +129,14 @@ curs.execute("""DELETE FROM queries WHERE id=%s""", self.id) g.db.commit() - def tostring(self): - criteria = json.loads(self.query) - query = ["%s %s '%s'" % (field, op, value) for field, op, value in criteria] - string = ' AND '.join(s.replace(' ', ' ') for s in query) - return Markup(string) + def __str__(self): + query = [] + if self._times: + query.append("time BETWEEN '%s' AND '%s'" % (self._times[0], self._times[1])) + query += ["%s %s '%s'" % (field, op, value) for field, op, value in self._query] + string = " AND ".join(query) + if self._order[0]: + string += " ORDER BY %s" % self._order[0] + if self._order[1]: + string += " DESC" + return string diff -r 90b18bfd7375 -r c52ae3f0196f src/ltpdarepo/templates/queries/view.html --- a/src/ltpdarepo/templates/queries/view.html Tue Nov 08 12:19:12 2011 +0100 +++ b/src/ltpdarepo/templates/queries/view.html Tue Nov 08 12:22:17 2011 +0100 @@ -3,8 +3,8 @@ {% block body %}

Query «{{ query.title }}»

-
Database
{{ query.db }}
-
Query
{{ query.tostring() }}
+
Databases
{{ query.db }}
+
Query
{{ query }}