5 return uuid.uuid4().hex
12 def __init__ (self, action=None, subject=None, timestamp='now',
13 filters=[], params=[], fields=[],
14 sort=None, limit=None, offset=None,
16 self.query_uuid=uniqid()
20 self.timestamp=timestamp
24 # xxx these are unused for now....
28 # internal data - likewise, this is unused as of yet, although we have
29 # (untested) code below in analyze_subqueries to compute these..
30 self.analyzed_query=None
34 result="[[Q: id=%(query_uuid)s - %(action)s on %(subject)s "%self.__dict__
35 result += " with %d filters, %d fields"%(len(self.filters),len(self.params))
40 query_uuid=self.query_uuid
44 f=json.dumps (self.filters)
45 p=json.dumps (self.params)
46 c=json.dumps (self.fields)
47 # xxx unique can be removed, but for now we pad the js structure
50 aq = self.analyzed_query.to_json() if self.analyzed_query else 'null'
51 # subqueries is a dictionary subject:query
52 if not self.subqueries:
55 sq=", ".join ( [ "'%s':%s" % (subject, subquery.to_json())
56 for (subject, subquery) in self.subqueries.iteritems()])
59 result= """ new ManifoldQuery('%(a)s', '%(s)s', '%(t)s', %(f)s, %(p)s, %(c)s, %(unique)s, '%(query_uuid)s', %(aq)s, %(sq)s)"""%locals()
60 if debug: print 'ManifoldQuery.to_json:',result
63 # this builds a ManifoldQuery object from a dict as received from javascript through its ajax request
64 # we use a json-encoded string - see manifold.js for the sender part
65 # e.g. here's what I captured from the server's output
66 # manifoldproxy.proxy: request.POST <QueryDict: {u'json': [u'{"action":"get","subject":"resource","timestamp":"latest","filters":[["slice_hrn","=","ple.inria.omftest"]],"params":[],"fields":["hrn","hostname"],"unique":0,"query_uuid":"436aae70a48141cc826f88e08fbd74b1","analyzed_query":null,"subqueries":{}}']}>
67 def fill_from_POST (self, POST_dict):
69 json_string=POST_dict['json']
70 dict=json.loads(json_string)
71 for (k,v) in dict.iteritems():
74 print "Could not decode incoming ajax request as a Query, POST=",POST_dict
80 def analyze_subqueries(self):
81 analyzed_query = ManifoldQuery()
82 analyzed_query.query_uuid = self.query_uuid
83 analyzed_query.action = self.action
84 analyzed_query.subject = self.subject
85 analyzed_query.timestamp = self.timestamp
87 # analyse query filters
88 # filter syntax : ['key', 'oparation', 'value']
89 for filter in self.filters:
97 if not analyzed_query.subqueries[subject]:
98 subquery = ManifoldQuery()
99 subquery.action = self.action
100 subquery.subject = subject
101 subquery.timestamp = self.timestamp
102 analyzed_query.subqueries[subject] = subquery
104 analyzed_query.subqueries[subject].filters.append([field, operation, value])
106 analyzed_query.filters.append(filter)
108 # analyse query params
109 # params syntax : params = {'param1': value1, 'param2': value2, ...}
110 for param in self.params.keys():
112 parts=param.split('.')
115 if not analyzed_query.subqueries[subject]:
116 subquery = ManifoldQuery()
117 subquery.action = self.action
118 subquery.subject = subject
119 subquery.timestamp = self.timestamp
120 analyzed_query.subqueries[subject] = subquery
122 analyzed_query.subqueries[subject].params[field] = self.params[param]
124 analyzed_query.params[param] = self.params[param]
126 # analyse query fields
127 # fields syntax: fields = [element1, element2, ....]
128 for element in self.fields:
130 parts=element.split('.')
133 if not analyzed_query.subqueries[subject]:
134 subquery = ManifoldQuery()
135 subquery.action = self.action
136 subquery.subject = subject
137 subquery.timestamp = self.timestamp
138 analyzed_query.subqueries[subject] = subquery
140 analyzed_query.subqueries[subject].fields.append(field)
142 analyzed_query.fields.append(element)
146 if analyzed_query.subject == 'slice':
147 if not analyzed_query.subqueries['resource']:
148 subquery = ManifoldQuery()
149 subquery.action = self.action
150 subquery.subject = subject
151 subquery.timestamp = self.timestamp
152 analyzed_query.subqueries['resource'] = subquery
154 if not analyzed_query.subqueries['lease']:
155 subquery = ManifoldQuery()
156 subquery.action = self.action
157 subquery.subject = subject
158 subquery.timestamp = self.timestamp
159 analyzed_query.subqueries['lease'] = subquery
162 self.analyzed_query = analyzed_query