From: Jordan Augé Date: Mon, 13 May 2013 09:52:41 +0000 (+0200) Subject: various fixes X-Git-Tag: myslice-0.2-1~127 X-Git-Url: http://git.onelab.eu/?p=myslice.git;a=commitdiff_plain;h=9e6b289bad3042093d0414ed90a336aaa5bfa09a various fixes --- diff --git a/manifold/core/query.py b/manifold/core/query.py index 877b1a84..5a331148 100644 --- a/manifold/core/query.py +++ b/manifold/core/query.py @@ -187,7 +187,7 @@ class Query(object): def to_json (self, analyzed_query=None): query_uuid=self.query_uuid a=self.action - s=self.object + o=self.object t=self.timestamp f=json.dumps (self.filters.to_list()) p=json.dumps (self.params) @@ -201,7 +201,7 @@ class Query(object): aq = analyzed_query.to_json() sq="{}" - 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() + result= """ new ManifoldQuery('%(a)s', '%(o)s', '%(t)s', %(f)s, %(p)s, %(c)s, %(unique)s, '%(query_uuid)s', %(aq)s, %(sq)s)"""%locals() if debug: print 'ManifoldQuery.to_json:',result return result @@ -266,7 +266,7 @@ class Query(object): @classmethod def action(self, action, object): query = Query() - query.action = 'get' + query.action = action query.object = object return query @@ -398,7 +398,7 @@ class AnalyzedQuery(Query): def to_json (self): query_uuid=self.query_uuid a=self.action - s=self.object + o=self.object t=self.timestamp f=json.dumps (self.filters.to_list()) p=json.dumps (self.params) @@ -407,10 +407,10 @@ class AnalyzedQuery(Query): unique=0 aq = 'null' - sq=", ".join ( [ "'%s':%s" % (subject, subquery.to_json()) - for (subject, subquery) in self._subqueries.iteritems()]) + sq=", ".join ( [ "'%s':%s" % (object, subquery.to_json()) + for (object, subquery) in self._subqueries.iteritems()]) sq="{%s}"%sq - 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() + result= """ new ManifoldQuery('%(a)s', '%(o)s', '%(t)s', %(f)s, %(p)s, %(c)s, %(unique)s, '%(query_uuid)s', %(aq)s, %(sq)s)"""%locals() if debug: print 'ManifoldQuery.to_json:',result return result diff --git a/manifold/js/manifold-query.js b/manifold/js/manifold-query.js index 2a1a162f..7c5de925 100644 --- a/manifold/js/manifold-query.js +++ b/manifold/js/manifold-query.js @@ -1,8 +1,8 @@ -function ManifoldQuery(action, subject, timestamp, filters, params, fields, unique, query_uuid, aq, sq) { +function ManifoldQuery(action, object, timestamp, filters, params, fields, unique, query_uuid, aq, sq) { // get, update, delete, create var action; // slice, user, network... - var subject; + var object; // timestamp, now, latest(cache) : date of the results queried var timestamp; // key(field),op(=<>),value @@ -23,17 +23,17 @@ function ManifoldQuery(action, subject, timestamp, filters, params, fields, uniq /*------------------------------------------------------------- Query properties are SQL like : --------------------------------------------------------------- -SELECT fields FROM subject WHERE filter; -UPDATE subject SET field=value WHERE filter; / returns SELECT -DELETE FROM subject WHERE filter -INSERT INTO subject VALUES(field=value) +SELECT fields FROM object WHERE filter; +UPDATE object SET field=value WHERE filter; / returns SELECT +DELETE FROM object WHERE filter +INSERT INTO object VALUES(field=value) -------------------------------------------------------------*/ this.__repr = function () { - res = "ManyfoldQuery "; + res = "ManifoldQuery "; res += " id=" + this.query_uuid; res += " a=" + this.action; - res += " s=" + this.subject; + res += " o=" + this.object; res += " ts=" + this.timestamp; res += " flts=" + this.filters; res += " flds=" + this.fields; @@ -100,7 +100,7 @@ INSERT INTO subject VALUES(field=value) } // we send queries as a json string now // this.as_POST = function() { -// return {'action': this.action, 'subject': this.subject, 'timestamp': this.timestamp, +// return {'action': this.action, 'object': this.object, 'timestamp': this.timestamp, // 'filters': this.filters, 'params': this.params, 'fields': this.fields}; // } this.analyze_subqueries = function() { @@ -108,7 +108,7 @@ INSERT INTO subject VALUES(field=value) var q = new ManifoldQuery(); q.query_uuid = this.query_uuid; q.action = this.action; - q.subject = this.subject; + q.object = this.object; q.timestamp = this.timestamp; /* Filters */ @@ -118,15 +118,15 @@ INSERT INTO subject VALUES(field=value) var v = filter[2]; var pos = k.indexOf('.'); if (pos != -1) { - var subject = k.substr(0, pos); + var object = k.substr(0, pos); var field = k.substr(pos+1); - if (!q.subqueries[subject]) { - q.subqueries[subject] = new ManifoldQuery(); - q.subqueries[subject].action = q.action; - q.subqueries[subject].subject = subject; - q.subqueries[subject].timestamp = q.timestamp; + if (!q.subqueries[object]) { + q.subqueries[object] = new ManifoldQuery(); + q.subqueries[object].action = q.action; + q.subqueries[object].object = object; + q.subqueries[object].timestamp = q.timestamp; } - q.subqueries[subject].filters.push(Array(field, op, v)); + q.subqueries[object].filters.push(Array(field, op, v)); } else { q.filters.push(filter); } @@ -136,15 +136,15 @@ INSERT INTO subject VALUES(field=value) jQuery.each(this.params, function(param, value) { var pos = param.indexOf('.'); if (pos != -1) { - var subject = param.substr(0, pos); + var object = param.substr(0, pos); var field = param.substr(pos+1); - if (!q.subqueries[subject]) { - q.subqueries[subject] = new ManifoldQuery(); - q.subqueries[subject].action = q.action; - q.subqueries[subject].subject = subject; - q.subqueries[subject].timestamp = q.timestamp; + if (!q.subqueries[object]) { + q.subqueries[object] = new ManifoldQuery(); + q.subqueries[object].action = q.action; + q.subqueries[object].object = object; + q.subqueries[object].timestamp = q.timestamp; } - q.subqueries[subject].params[field] = value; + q.subqueries[object].params[field] = value; } else { q.params[field] = value; } @@ -154,15 +154,15 @@ INSERT INTO subject VALUES(field=value) jQuery.each(this.fields, function(i, v) { var pos = v.indexOf('.'); if (pos != -1) { - var subject = v.substr(0, pos); + var object = v.substr(0, pos); var field = v.substr(pos+1); - if (!q.subqueries[subject]) { - q.subqueries[subject] = new ManifoldQuery(); - q.subqueries[subject].action = q.action; - q.subqueries[subject].subject = subject; - q.subqueries[subject].timestamp = q.timestamp; + if (!q.subqueries[object]) { + q.subqueries[object] = new ManifoldQuery(); + q.subqueries[object].action = q.action; + q.subqueries[object].object = object; + q.subqueries[object].timestamp = q.timestamp; } - q.subqueries[subject].fields.push(field); + q.subqueries[object].fields.push(field); } else { q.fields.push(v); } @@ -176,10 +176,10 @@ INSERT INTO subject VALUES(field=value) else this.action = action; - if (typeof subject == "undefined") - this.subject = null; + if (typeof object == "undefined") + this.object = null; else - this.subject = subject; + this.object = object; if (typeof timestamp == "undefined") this.timestamp = "now"; diff --git a/manifold/js/metadata.js b/manifold/js/metadata.js index eefdcbfa..28e6c12d 100644 --- a/manifold/js/metadata.js +++ b/manifold/js/metadata.js @@ -3,11 +3,11 @@ var metadata = { get : function () { return MANIFOLD_METADATA; }, - // returns all fields of a given subject - fields : function (subject) { + // returns all fields of a given object + fields : function (object) { var result=new Array(); jQuery.each(MANIFOLD_METADATA, function(s,obj){ - if(s==subject){ + if(s==object){ jQuery.each(obj['column'], function(i,f){ result.push(f); }); @@ -19,10 +19,10 @@ var metadata = { return result; }, // returns all properties of a given field - field : function (subject, field) { + field : function (object, field) { var result=new Array(); jQuery.each(MANIFOLD_METADATA, function(s,obj){ - if(s==subject){ + if(s==object){ jQuery.each(obj['column'], function(i,f){ if(f['column']==field){ result.push(f); @@ -34,11 +34,11 @@ var metadata = { }); return result[0]; }, - // returns the value of a property from a field within a subject (type of object : resource,node,lease,slice...) - property : function (subject, field, property) { + // returns the value of a property from a field within a object (type of object : resource,node,lease,slice...) + property : function (object, field, property) { var result=null; jQuery.each(MANIFOLD_METADATA, function(s,obj){ - if(s==subject){ + if(s==object){ jQuery.each(obj['column'], function(i,f){ if(f['column']==field){ result=f[property]; diff --git a/manifold/manifoldapi.py b/manifold/manifoldapi.py index 12da1033..2548287f 100644 --- a/manifold/manifoldapi.py +++ b/manifold/manifoldapi.py @@ -82,19 +82,3 @@ class ManifoldAPI: print ret print "="*80 return ret - -#old# (action,subject)= (query.action,query.subject) -#old# # use e.g. self.Get rather than self.server.Get so we use the __getattr__ code -#old# if action=='get': -#old## this makes the backend to squeak and one can't login anymore... -#old## return self.Get(subject, query.filters, query.timestamp, query.fields) -#old# return self.Get(subject, query.filters, {}, query.fields) -#old# elif action=='update': -#old# answer=self.Update(subject, query.filters, query.params, query.fields) -#old# if not isinstance (answer, ManifoldResult): print "UNEXECPECTED answer", answer -#old# return answer -#old# else: -#old# warning="WARNING: ManifoldAPI.send_manifold_query: %s not implemented for now"%action -#old# print warning -#old# print 3 -#old# return ManifoldResult(code=ManifoldCode.NOT_IMPLEMENTED, output=warning) diff --git a/manifold/manifoldproxy.py b/manifold/manifoldproxy.py index eaa128b5..11829557 100644 --- a/manifold/manifoldproxy.py +++ b/manifold/manifoldproxy.py @@ -3,7 +3,8 @@ import json #from django.core import serializers from django.http import HttpResponse, HttpResponseForbidden -from manifold.manifoldquery import ManifoldQuery +#from manifold.manifoldquery import ManifoldQuery +from manifold.core.query import Query from manifold.manifoldapi import ManifoldAPI debug=False @@ -46,9 +47,10 @@ with the query passed using POST""" try: # translate incoming POST request into a query object if debug: print 'manifoldproxy.proxy: request.POST',request.POST - manifold_query = ManifoldQuery() + manifold_query = Query() + #manifold_query = ManifoldQuery() manifold_query.fill_from_POST(request.POST) - offline_filename="offline-%s-%s.json"%(manifold_query.action,manifold_query.subject) + offline_filename="offline-%s-%s.json"%(manifold_query.action,manifold_query.object) # retrieve session for request manifold_api_session_auth = request.session['manifold']['auth'] if debug_empty and manifold_query.action.lower()=='get': diff --git a/manifold/manifoldquery.py b/manifold/manifoldquery.py deleted file mode 100644 index bbcc6b5d..00000000 --- a/manifold/manifoldquery.py +++ /dev/null @@ -1,172 +0,0 @@ -import json -import uuid - -def uniqid (): - return uuid.uuid4().hex - -debug=False -debug=True - -class ManifoldQuery: - - def __init__ (self, action=None, subject=None, timestamp='now', - filters=[], params=[], fields=[], - sort=None, limit=None, offset=None, - ): - self.query_uuid=uniqid() - # settable - self.action=action - self.subject=subject - self.timestamp=timestamp - self.filters=filters - self.params=params - self.fields=fields - # xxx these are unused for now.... - self.sort=sort - self.limit=limit - self.offset=offset - # internal data - likewise, this is unused as of yet, although we have - # (untested) code below in analyze_subqueries to compute these.. - self.analyzed_query=None - self.subqueries = {} - - def __repr__ (self): - result="[[Q: id=%(query_uuid)s - %(action)s on %(subject)s "%self.__dict__ - result += " with %d filters, %d fields"%(len(self.filters),len(self.params)) - result += "]]" - return result - - def to_json (self): - query_uuid=self.query_uuid - a=self.action - s=self.subject - t=self.timestamp - f=json.dumps (self.filters) - p=json.dumps (self.params) - c=json.dumps (self.fields) - # xxx unique can be removed, but for now we pad the js structure - unique=0 - - aq = self.analyzed_query.to_json() if self.analyzed_query else 'null' - # subqueries is a dictionary subject:query - if not self.subqueries: - sq="{}" - else: - sq=", ".join ( [ "'%s':%s" % (subject, subquery.to_json()) - for (subject, subquery) in self.subqueries.iteritems()]) - sq="{%s}"%sq - - 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() - if debug: print 'ManifoldQuery.to_json:',result - return result - - # this builds a ManifoldQuery object from a dict as received from javascript through its ajax request - # we use a json-encoded string - see manifold.js for the sender part - # e.g. here's what I captured from the server's output - # manifoldproxy.proxy: request.POST - def fill_from_POST (self, POST_dict): - try: - json_string=POST_dict['json'] - dict=json.loads(json_string) - for (k,v) in dict.iteritems(): - setattr(self,k,v) - except: - print "Could not decode incoming ajax request as a Query, POST=",POST_dict - if (debug): - import traceback - traceback.print_exc() - - # not used yet .. - def analyze_subqueries(self): - analyzed_query = ManifoldQuery() - analyzed_query.query_uuid = self.query_uuid - analyzed_query.action = self.action - analyzed_query.subject = self.subject - analyzed_query.timestamp = self.timestamp - - # analyse query filters - # filter syntax : ['key', 'oparation', 'value'] - for filter in self.filters: - key = filter[0] - operation = filter[1] - value = filter[2] - if '.' in key: - parts=key.split('.') - subject = parts[0] - field = parts[1] - if not analyzed_query.subqueries[subject]: - subquery = ManifoldQuery() - subquery.action = self.action - subquery.subject = subject - subquery.timestamp = self.timestamp - analyzed_query.subqueries[subject] = subquery - - analyzed_query.subqueries[subject].filters.append([field, operation, value]) - else: - analyzed_query.filters.append(filter) - - # analyse query params - # params syntax : params = {'param1': value1, 'param2': value2, ...} - for param in self.params.keys(): - if '.' in param: - parts=param.split('.') - subject = parts[0] - field = parts[1] - if not analyzed_query.subqueries[subject]: - subquery = ManifoldQuery() - subquery.action = self.action - subquery.subject = subject - subquery.timestamp = self.timestamp - analyzed_query.subqueries[subject] = subquery - - analyzed_query.subqueries[subject].params[field] = self.params[param] - else: - analyzed_query.params[param] = self.params[param] - - # analyse query fields - # fields syntax: fields = [element1, element2, ....] - for element in self.fields: - if '.' in element: - parts=element.split('.') - subject = parts[0] - field = parts[1] - if not analyzed_query.subqueries[subject]: - subquery = ManifoldQuery() - subquery.action = self.action - subquery.subject = subject - subquery.timestamp = self.timestamp - analyzed_query.subqueries[subject] = subquery - - analyzed_query.subqueries[subject].fields.append(field) - else: - analyzed_query.fields.append(element) - - - # default subqueries - if analyzed_query.subject == 'slice': - if not analyzed_query.subqueries['resource']: - subquery = ManifoldQuery() - subquery.action = self.action - subquery.subject = subject - subquery.timestamp = self.timestamp - analyzed_query.subqueries['resource'] = subquery - - if not analyzed_query.subqueries['lease']: - subquery = ManifoldQuery() - subquery.action = self.action - subquery.subject = subject - subquery.timestamp = self.timestamp - analyzed_query.subqueries['lease'] = subquery - - - self.analyzed_query = analyzed_query - - def to_dict(self): - return { - 'action': self.action, - 'fact_table': self.subject, - 'ts': self.timestamp, - 'filters': self.filters, - 'params': self.params, - 'fields': self.fields - } diff --git a/manifold/metadata.py b/manifold/metadata.py index eb0e48a4..1d8d5e8f 100644 --- a/manifold/metadata.py +++ b/manifold/metadata.py @@ -14,7 +14,7 @@ class MetaData: def __init__ (self, auth): self.auth=auth - self.hash_by_subject={} + self.hash_by_object={} # XXX Retrieve all metadata the first time we instanciate the class self.fetch() @@ -24,7 +24,7 @@ class MetaData: if work_offline: try: with file(offline_metadata) as f: - self.hash_by_subject=json.loads(f.read()) + self.hash_by_object=json.loads(f.read()) return except: print "metadata.work_offline: failed to decode %s"%offline_filename @@ -36,7 +36,7 @@ class MetaData: 'column.allowed_values', 'column.platforms.platform', 'column.platforms.platform_url'] rows_result = manifold_api.Get({ - 'fact_table': 'local:objects', # proposed to replace metadata:table + 'object': 'local:objects', # proposed to replace metadata:table 'fields': fields }) #old# rows_result = manifold_api.Get('metadata:table', [], [], fields) @@ -44,17 +44,17 @@ class MetaData: if not rows: print "Failed to retrieve metadata",rows_result.error() rows=[] - self.hash_by_subject = dict ( [ (row['table'], row) for row in rows ] ) + self.hash_by_object = dict ( [ (row['table'], row) for row in rows ] ) # save for next time we use offline mode if debug: with file(offline_filename,'w') as f: - f.write(json.dumps(self.hash_by_subject)) + f.write(json.dumps(self.hash_by_object)) def to_json(self): - return json.dumps(self.hash_by_subject) + return json.dumps(self.hash_by_object) - def details_by_subject (self, subject): - return self.hash_by_subject[subject] + def details_by_object (self, object): + return self.hash_by_object[object] - def sorted_fields_by_subject (self, subject): - return self.hash_by_subject[subject]['columns'].sort() + def sorted_fields_by_object (self, object): + return self.hash_by_object[object]['columns'].sort() diff --git a/plugins/querycode/querycode.js b/plugins/querycode/querycode.js index 6dc00c4c..c655529a 100644 --- a/plugins/querycode/querycode.js +++ b/plugins/querycode/querycode.js @@ -119,7 +119,7 @@ }); ofs = '[' + ofs + ']'; - output += 'pp srv.call("' + mixed_case(query.action) +'", auth, "' + query.subject + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')'; + output += 'pp srv.call("' + mixed_case(query.action) +'", auth, "' + query.object + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')'; var output = '
' + output + "
"; return output; @@ -152,7 +152,7 @@ }); ofs = '[' + ofs + ']'; - output += 'srv.' + mixed_case(query.action) + '(auth, "' + query.subject + '", ' + ifs + ', {}, ' + ofs + ')'; + output += 'srv.' + mixed_case(query.action) + '(auth, "' + query.object + '", ' + ifs + ', {}, ' + ofs + ')'; var output = '
' + output + "
"; return output; } diff --git a/plugins/updater/updater.js b/plugins/updater/updater.js index 3c35b4da..7ac0fc7b 100644 --- a/plugins/updater/updater.js +++ b/plugins/updater/updater.js @@ -59,7 +59,7 @@ var query=manifold.find_query (options.query_uuid); // very rough way of filling this for now this.update_query = - new ManifoldQuery ("update", query.subject, null, query.filters, + new ManifoldQuery ("update", query.object, null, query.filters, {}, // params query.fields, undefined, /* unique */ diff --git a/trash/dashboard.py b/trash/dashboard.py index 6d6de77b..07e60238 100644 --- a/trash/dashboard.py +++ b/trash/dashboard.py @@ -8,7 +8,8 @@ from django.shortcuts import render_to_response from django.contrib.auth.decorators import login_required from unfold.page import Page -from manifold.manifoldquery import ManifoldQuery +from manifold.core.query import Query +#from manifold.manifoldquery import ManifoldQuery from plugins.stack.stack import Stack from plugins.lists.slicelist import SliceList @@ -25,16 +26,12 @@ def dashboard_view (request): page = Page(request) - slices_query = ManifoldQuery (action='get', - subject='slice', - timestamp='latest', - fields=['slice_hrn'], - filters=[], - # xxx filter : should filter on the slices the logged user can see - # we don't have the user's hrn yet - # in addition this currently returns all slices anyways - # filter = ... - sort='slice_hrn',) + slices_query = Query.get('slice').select('slice_hrn') +#old# # xxx filter : should filter on the slices the logged user can see +#old# # we don't have the user's hrn yet +#old# # in addition this currently returns all slices anyways +#old# # filter = ... +#old# sort='slice_hrn',) page.enqueue_query (slices_query) main_plugin = Stack ( diff --git a/trash/pluginview.py b/trash/pluginview.py index efc353b9..6d602204 100644 --- a/trash/pluginview.py +++ b/trash/pluginview.py @@ -8,7 +8,8 @@ from django.shortcuts import render_to_response from django.contrib.auth.decorators import login_required from unfold.page import Page -from manifold.manifoldquery import ManifoldQuery +#from manifold.manifoldquery import ManifoldQuery +from manifold.core.query import Query from plugins.stack.stack import Stack from plugins.tabs.tabs import Tabs @@ -32,12 +33,7 @@ def test_plugin_view (request): template_env = {} slicename='ple.inria.heartbeat' - main_query = ManifoldQuery (action='get', - subject='resource', - timestamp='latest', - fields=['network','type','hrn','hostname','sliver'], - filters= [ [ 'slice_hrn', '=', slicename, ] ], - ) + main_query = Query.get('resource').filter_by('slice_hrn', '=', slicename).select(['network','type','hrn','hostname','sliver']) # without an hazelnut, this would use use : run_it=False as nothing would listen to the results page.enqueue_query (main_query, # run_it=False ) diff --git a/trash/sliceview.py b/trash/sliceview.py index b50eb26f..2bec9553 100644 --- a/trash/sliceview.py +++ b/trash/sliceview.py @@ -38,24 +38,17 @@ def slice_view (request, slicename=tmp_default_slice): # TODO The query to run is embedded in the URL - main_query = Query({'action': 'get', 'object': 'slice'}).filter_by('slice_hrn', '=', slicename) + main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename) # Get default fields from metadata unless specified if not main_query.fields: md_fields = page.get_metadata() - md_fields = md_fields.details_by_subject('slice') + md_fields = md_fields.details_by_object('slice') if debug: print "METADATA", md_fields # TODO Get default fields main_query.fields = ['slice_hrn', 'resource.hrn', 'resource.hostname', 'resource.type', 'resource.authority'] -#old# main_query = ManifoldQuery (action='get', -#old# subject='resource', -#old# timestamp='latest', -#old# fields=['network','type','hrn','hostname'], -#old# filters= [ [ 'slice_hrn', '=', slicename, ] ], -#old# ) - aq = AnalyzedQuery(main_query) page.enqueue_query(main_query, analyzed_query=aq)