2 function debug_dict_keys (msg, o) {
4 for (var k in o) keys.push(k);
5 messages.debug ("debug_dict_keys: " + msg + " keys= " + keys);
7 function debug_dict (msg, o) {
8 for (var k in o) messages.debug ("debug_dict: " + msg + " [" + k + "]=" + o[k]);
10 function debug_value (msg, value) {
11 messages.debug ("debug_value: " + msg + " " + value);
13 function debug_query (msg, query) {
14 if (query === undefined) messages.debug ("debug_query: " + msg + " -> undefined");
15 else if (query == null) messages.debug ("debug_query: " + msg + " -> null");
16 else if ('query_uuid' in query) messages.debug ("debug_query: " + msg + query.__repr());
17 else messages.debug ("debug_query: " + msg + " query= " + query);
20 // http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
21 Object.toType = (function toType(global) {
22 return function(obj) {
26 return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
30 /* ------------------------------------------------------------ */
32 // Constants that should be somehow moved to a plugin.js file
34 var FILTER_REMOVED = 2;
35 var CLEAR_FILTERS = 3;
37 var FIELD_REMOVED = 5;
40 var CLEAR_RECORDS = 8;
43 * event: FIELD_STATE_CHANGED
47 * .state : ???? used to be FIELD_REQUEST_ADD / FIELD_REQUEST_REMOVE
48 * .key : ??? the key fields of the record
49 * .op : the key of the record who has received an update
50 * .value : the new state of the record
52 var FIELD_STATE_CHANGED = 9;
54 var IN_PROGRESS = 101;
55 var DONE = 102; //XXX Should be harmonized with query state
57 /* Update requests related to subqueries */
61 var SET_REMOVED = 202;
66 var FIELD_REQUEST_CHANGE = 301;
67 var FIELD_REQUEST_ADD = 302;
68 var FIELD_REQUEST_REMOVE = 303;
69 var FIELD_REQUEST_ADD_RESET = 304;
70 var FIELD_REQUEST_REMOVE_RESET = 305;
72 // status (XXX Should be deprecated)
73 var FIELD_REQUEST_PENDING = 401;
74 var FIELD_REQUEST_SUCCESS = 402;
75 var FIELD_REQUEST_FAILURE = 403;
76 var STATUS_OKAY = 404;
77 var STATUS_SET_WARNING = 405;
78 var STATUS_ADD_WARNING = 406;
79 var STATUS_REMOVE_WARNING = 407;
80 var STATUS_RESET = 408;
82 /* Requests for query cycle */
88 var TYPE_LIST_OF_VALUES = 3;
89 var TYPE_LIST_OF_RECORDS = 4;
91 /******************************************************************************
92 * QUERY STATUS (for manifold events)
93 ******************************************************************************/
95 var STATUS_NONE = 500; // Query has not been started yet
96 var STATUS_GET_IN_PROGRESS = 501; // Query has been sent, no result has been received
97 var STATUS_GET_RECEIVED = 502; // Success
98 var STATUS_GET_ERROR = 503; // Error
99 var STATUS_UPDATE_PENDING = 504;
100 var STATUS_UPDATE_IN_PROGRESS = 505;
101 var STATUS_UPDATE_RECEIVED = 506;
102 var STATUS_UPDATE_ERROR = 507;
104 /******************************************************************************
105 * QUERY STATE (for query_store)
106 ******************************************************************************/
108 // XXX Rendundant with query status ?
110 var QUERY_STATE_INIT = 0;
111 var QUERY_STATE_INPROGRESS = 1;
112 var QUERY_STATE_DONE = 2;
114 /******************************************************************************
115 * RECORD STATES (for query_store)
116 ******************************************************************************/
120 var STATE_WARNINGS = 2;
121 var STATE_VISIBLE = 3;
124 var STATE_SET_CHANGE = 0;
125 var STATE_SET_ADD = 1;
126 var STATE_SET_REMOVE = 2;
127 var STATE_SET_CLEAR = 3;
130 var STATE_SET_IN = 0;
131 var STATE_SET_OUT = 1;
132 var STATE_SET_IN_PENDING = 2;
133 var STATE_SET_OUT_PENDING = 3;
134 var STATE_SET_IN_SUCCESS = 4;
135 var STATE_SET_OUT_SUCCESS = 5;
136 var STATE_SET_IN_FAILURE = 6;
137 var STATE_SET_OUT_FAILURE = 7;
138 var STATE_VALUE_CHANGE_PENDING = 8;
139 var STATE_VALUE_CHANGE_SUCCESS = 9;
140 var STATE_VALUE_CHANGE_FAILURE = 10;
142 // STATE_WARNINGS : dict
144 // STATE_VISIBLE : boolean
146 /******************************************************************************
148 ******************************************************************************/
150 var CONSTRAINT_RESERVABLE_LEASE = 0;
152 var CONSTRAINT_RESERVABLE_LEASE_MSG = "Configuration required: this resource needs to be scheduled";
154 // A structure for storing queries
156 function QueryExt(query, parent_query_ext, main_query_ext, update_query_ext, disabled, domain_query_ext) {
159 if (typeof query == "undefined")
160 throw "Must pass a query in QueryExt constructor";
162 this.parent_query_ext = (typeof parent_query_ext == "undefined") ? null : parent_query_ext;
163 this.main_query_ext = (typeof main_query_ext == "undefined") ? null : main_query_ext;
164 this.update_query_ext = (typeof update_query_ext == "undefined") ? null : update_query_ext;
165 this.update_query_orig_ext = (typeof update_query_orig_ext == "undefined") ? null : update_query_orig_ext;
166 this.disabled = (typeof disabled == "undefined") ? false : disabled;
168 // A domain query is a query that is issued to retrieve all possible values for a set
169 // eg. all resources that can be attached to a slice
170 // It is null unless we are a subquery for which a domain query has been issued
171 this.domain_query_ext = (typeof domain_query_ext == "undefined") ? null : domain_query_ext;
173 // Set members to buffer until the domain query is completed
175 this.set_members = [];
177 // The set query is the query for which the domain query has been issued.
178 // It is null unless the query is a domain query
179 this.set_query_ext = (typeof set_query_ext == "undefined") ? null : domain_query_ext;
181 this.query_state = QUERY_STATE_INIT;
183 // Results from a query consists in a dict that maps keys to records
184 this.records = new Hashtable();
186 // Status is a dict that maps keys to record status
187 this.state = new Hashtable();
189 // Filters that impact visibility in the local interface
192 // XXX Until we find a better solution
193 this.num_pending = 0;
194 this.num_unconfigured = 0;
196 // update_query null unless we are a main_query (aka parent_query == null); only main_query_fields can be updated...
199 function QueryStore() {
201 this.main_queries = {};
202 this.analyzed_queries = {};
206 this.insert = function(query) {
207 // We expect only main_queries are inserted
209 /* If the query has not been analyzed, then we analyze it */
210 if (query.analyzed_query == null) {
211 query.analyze_subqueries();
214 /* We prepare the update query corresponding to the main query and store both */
215 /* Note: they have the same UUID */
217 // XXX query.change_action() should become deprecated
218 update_query = query.clone();
219 update_query.action = 'update';
220 update_query.analyzed_query.action = 'update';
221 update_query.params = {};
222 update_query_ext = new QueryExt(update_query);
224 /* We remember the original query to be able to reset it */
225 update_query_orig_ext = new QueryExt(update_query.clone());
228 /* We store the main query */
229 query_ext = new QueryExt(query, null, null, update_query_ext, update_query_orig_ext, false);
230 manifold.query_store.main_queries[query.query_uuid] = query_ext;
231 /* Note: the update query does not have an entry! */
234 // The query is disabled; since it is incomplete until we know the content of the set of subqueries
235 // XXX unless we have no subqueries ???
236 // we will complete with params when records are received... this has to be done by the manager
237 // SET_ADD, SET_REMOVE will change the status of the elements of the set
238 // UPDATE will change also, etc.
239 // XXX We need a proper structure to store this information...
241 // We also need to insert all queries and subqueries from the analyzed_query
242 // XXX We need the root of all subqueries
243 query.iter_subqueries(function(sq, data, parent_query) {
244 var parent_query_ext;
246 parent_query_ext = manifold.query_store.find_analyzed_query_ext(parent_query.query_uuid);
248 parent_query_ext = null;
250 // XXX parent_query_ext == false
251 // XXX main.subqueries = {} # Normal, we need analyzed_query
252 sq_ext = new QueryExt(sq, parent_query_ext, query_ext)
255 /* Let's issue a query for the subquery domain. This query will not need any update etc.
256 eg. for resources in a slice, we also query all resources */
257 var all_fields = manifold.metadata.get_field_names(sq.object);
258 var domain_query = new ManifoldQuery('get', sq.object, 'now', [], {}, all_fields);
259 //var domain_query = new ManifoldQuery('get', sq.object);
261 console.log("Created domain query", domain_query);
262 var domain_query_ext = new QueryExt(domain_query);
264 domain_query_ext.set_query_ext = sq_ext;
265 sq_ext.domain_query_ext = domain_query_ext;
267 // One of these two is useless ?
268 manifold.query_store.main_queries[domain_query.query_uuid] = domain_query_ext;
269 manifold.query_store.analyzed_queries[domain_query.query_uuid] = domain_query_ext;
271 // XXX This query is run before the plugins are initialized and listening
272 manifold.run_query(domain_query);
275 manifold.query_store.analyzed_queries[sq.query_uuid] = sq_ext;
278 // XXX We have spurious update queries...
283 this.find_query_ext = function(query_uuid)
285 return this.main_queries[query_uuid];
288 this.find_query = function(query_uuid)
290 return this.find_query_ext(query_uuid).query;
293 this.find_analyzed_query_ext = function(query_uuid)
295 return this.analyzed_queries[query_uuid];
298 this.find_analyzed_query = function(query_uuid)
300 return this.find_analyzed_query_ext(query_uuid).query;
303 this.state_dict_create = function(default_set)
305 default_set = (default_set === undefined) ? STATE_SET_OUT : default_set;
307 // We cannot use constants in literal definition, so...
308 state_dict[STATE_WARNINGS] = {};
309 state_dict[STATE_SET] = default_set;
310 state_dict[STATE_VISIBLE] = true;
316 this.set_records = function(query_uuid, records, default_set)
318 default_set = (default_set === undefined) ? STATE_SET_OUT : default_set;
321 var query_ext = this.find_analyzed_query_ext(query_uuid);
322 var record_key = manifold.metadata.get_key(query_ext.query.object);
324 $.each(records, function(i, record) {
325 //var key = manifold.metadata.get_key(query_ext.query.object);
326 // ["start_time", "resource", "end_time"]
328 var record_key_value = manifold.record_get_value(record, record_key);
330 query_ext.records.put(record_key_value, record);
332 if (!(query_ext.state.get(record_key_value)))
333 query_ext.state.put(record_key_value, self.state_dict_create(default_set));
337 this.get_records = function(query_uuid)
339 var query_ext = this.find_analyzed_query_ext(query_uuid);
340 return query_ext.records.values();
343 this.get_record = function(query_uuid, record_key)
345 var query_ext = this.find_analyzed_query_ext(query_uuid);
346 return query_ext.records.get(record_key);
349 this.del_record = function(query_uuid, record_key)
351 var query_ext = this.find_analyzed_query_ext(query_uuid);
352 return query_ext.records.remove(record_key);
355 this.del_state = function(query_uuid, record_key)
357 var query_ext = this.find_analyzed_query_ext(query_uuid);
358 return query_ext.state.remove(record_key);
361 this.add_record = function(query_uuid, record, new_state)
363 var query_ext, key, record_key;
364 query_ext = this.find_analyzed_query_ext(query_uuid);
366 if (typeof(record) == 'object') {
367 key = manifold.metadata.get_key(query_ext.query.object);
368 record_key = manifold.record_get_value(record, key);
373 var record_entry = query_ext.records.get(record_key);
375 query_ext.records.put(record_key, record);
377 manifold.query_store.set_record_state(query_uuid, record_key, STATE_SET, new_state);
380 this.remove_record = function(query_uuid, record, new_state)
382 var query_ext, key, record_key;
383 query_ext = this.find_analyzed_query_ext(query_uuid);
385 if (typeof(record) == 'object') {
386 key = manifold.metadata.get_key(query_ext.query.object);
387 record_key = manifold.record_get_value(record, key);
392 if ((query_ext.query.object == 'lease') && (new_state == STATE_SET_OUT)) {
393 // Leases that are marked out are in fact leases from other slices
394 // We need to _remove_ leases that we mark as OUT
395 manifold.query_store.del_record(query_uuid, record_key);
396 manifold.query_store.del_state(query_uuid, record_key);
398 manifold.query_store.set_record_state(query_uuid, record_key, STATE_SET, new_state);
402 this.iter_records = function(query_uuid, callback)
404 var query_ext = this.find_analyzed_query_ext(query_uuid);
405 query_ext.records.each(callback);
406 //callback = function(record_key, record)
409 this.iter_visible_records = function(query_uuid, callback)
411 var query_ext = this.find_analyzed_query_ext(query_uuid);
412 query_ext.records.each(function(record_key, record) {
413 if (query_ext.state.get(record_key)[STATE_VISIBLE]) // .STATE_VISIBLE would be for the string key
414 callback(record_key, record);
416 //callback = function(record_key, record)
422 this.set_record_state = function(query_uuid, result_key, state, value)
424 var query_ext = this.find_analyzed_query_ext(query_uuid);
425 var state_dict = query_ext.state.get(result_key);
427 state_dict = this.state_dict_create();
429 state_dict[state] = value;
431 query_ext.state.put(result_key, state_dict);
434 this.get_record_state = function(query_uuid, result_key, state)
436 var query_ext = this.find_analyzed_query_ext(query_uuid);
437 var state_dict = query_ext.state.get(result_key);
440 return state_dict[state];
445 this.add_filter = function(query_uuid, filter)
447 var query_ext = this.find_analyzed_query_ext(query_uuid);
448 // XXX When we update a filter
449 query_ext.filters.push(filter);
451 this.apply_filters(query_uuid);
455 this.update_filter = function(query_uuid, filter)
459 this.apply_filters(query_uuid);
462 this.remove_filter = function(query_uuid, filter)
464 var query_ext = this.find_analyzed_query_ext(query_uuid);
465 query_ext.filters = $.grep(query_ext.filters, function(x) {
469 this.apply_filters(query_uuid);
472 this.get_filters = function(query_uuid)
474 var query_ext = this.find_analyzed_query_ext(query_uuid);
475 return query_ext.filters;
478 this.recount = function(query_uuid)
481 var is_reserved, is_pending, in_set, is_unconfigured;
483 query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
484 query_ext.num_pending = 0;
485 query_ext.num_unconfigured = 0;
487 this.iter_records(query_uuid, function(record_key, record) {
488 var record_state = manifold.query_store.get_record_state(query_uuid, record_key, STATE_SET);
489 var record_warnings = manifold.query_store.get_record_state(query_uuid, record_key, STATE_WARNINGS);
491 is_reserved = (record_state == STATE_SET_IN)
492 || (record_state == STATE_SET_OUT_PENDING)
493 || (record_state == STATE_SET_IN_SUCCESS)
494 || (record_state == STATE_SET_OUT_FAILURE);
496 is_pending = (record_state == STATE_SET_IN_PENDING)
497 || (record_state == STATE_SET_OUT_PENDING);
499 in_set = (record_state == STATE_SET_IN) // should not have warnings
500 || (record_state == STATE_SET_IN_PENDING)
501 || (record_state == STATE_SET_IN_SUCCESS)
502 || (record_state == STATE_SET_OUT_FAILURE); // should not have warnings
504 is_unconfigured = (in_set && !$.isEmptyObject(record_warnings));
506 /* Let's update num_pending and num_unconfigured at this stage */
508 query_ext.num_pending++;
510 query_ext.num_unconfigured++;
515 this.apply_filters = function(query_uuid)
517 var start = new Date().getTime();
519 // Toggle visibility of records according to the different filters.
522 var filters = this.get_filters(query_uuid);
524 /* Let's update num_pending and num_unconfigured at this stage */
526 // Adapted from querytable._querytable_filter()
528 this.iter_records(query_uuid, function(record_key, record) {
529 var is_reserved, is_pending, in_set, is_unconfigured;
532 var record_state = manifold.query_store.get_record_state(query_uuid, record_key, STATE_SET);
533 var record_warnings = manifold.query_store.get_record_state(query_uuid, record_key, STATE_WARNINGS);
535 is_reserved = (record_state == STATE_SET_IN)
536 || (record_state == STATE_SET_OUT_PENDING)
537 || (record_state == STATE_SET_IN_SUCCESS)
538 || (record_state == STATE_SET_OUT_FAILURE);
540 is_pending = (record_state == STATE_SET_IN_PENDING)
541 || (record_state == STATE_SET_OUT_PENDING);
543 in_set = (record_state == STATE_SET_IN) // should not have warnings
544 || (record_state == STATE_SET_IN_PENDING)
545 || (record_state == STATE_SET_IN_SUCCESS)
546 || (record_state == STATE_SET_OUT_FAILURE); // should not have warnings
548 is_unconfigured = (in_set && !$.isEmptyObject(record_warnings));
550 // We go through each filter and decide whether it affects the visibility of the record
551 $.each(filters, function(index, filter) {
554 var value = filter[2];
557 /* We do some special handling for the manifold:status filter
560 if (key == 'manifold:status') {
561 if (op != '=' && op != '==') {
562 // Unsupported filter, let's ignore it
563 console.log("Unsupported filter on manifold:status. Should be EQUAL only.");
564 return true; // ~ continue
569 // true => ~ continue
571 visible = is_reserved;
574 visible = is_unconfigured;
577 visible = is_pending;
580 return false; // ~ break
583 /* Normal filtering behaviour (according to the record content) follows... */
584 col_value = manifold.record_get_value(record, key);
586 // When the filter does not match, we hide the column by default
587 if (col_value === 'undefined') {
589 return false; // ~ break
592 // XXX This should accept pluggable filtering functions.
595 /* Test whether current filter is compatible with the column */
596 if (op == '=' || op == '==') {
597 if ( col_value != value || col_value==null || col_value=="" || col_value=="n/a")
599 }else if (op == 'included') {
600 $.each(value, function(i,x) {
603 return false; // ~ break
608 }else if (op == '!=') {
609 if ( col_value == value || col_value==null || col_value=="" || col_value=="n/a")
612 if ( parseFloat(col_value) >= value || col_value==null || col_value=="" || col_value=="n/a")
615 if ( parseFloat(col_value) <= value || col_value==null || col_value=="" || col_value=="n/a")
617 } else if(op=='<=' || op=='≤') {
618 if ( parseFloat(col_value) > value || col_value==null || col_value=="" || col_value=="n/a")
620 } else if(op=='>=' || op=='≥') {
621 if ( parseFloat(col_value) < value || col_value==null || col_value=="" || col_value=="n/a")
624 // How to break out of a loop ?
625 alert("filter not supported");
626 return false; // break
631 // Set the visibility status in the query store
632 self.set_record_state(query_uuid, record_key, STATE_VISIBLE, visible);
635 var end = new Date().getTime();
636 console.log("APPLY FILTERS took", end - start, "ms");
643 * This namespace holds functions for globally managing query objects
648 /**************************************************************************
650 **************************************************************************/
654 get_type: function(variable) {
655 switch(Object.toType(variable)) {
662 if ((variable.length > 0) && (Object.toType(variable[0]) === 'object'))
663 return TYPE_LIST_OF_RECORDS;
665 return TYPE_LIST_OF_VALUES;
671 * fields: A String instance (field name), or a set of String instances
672 * (field names) # XXX tuple !!
674 * If fields is a String, return the corresponding value.
675 * If fields is a set, return a tuple of corresponding value.
678 * KeyError if at least one of the fields is not found
680 record_get_value: function(record, fields)
682 if (typeof(fields) === 'string') {
683 if (fields.indexOf('.') != -1) {
684 key_subkey = key.split('.', 2);
686 subkey = key_subkey[1];
688 if (record.indexOf(key) == -1) {
691 // Tests if the following is an array (typeof would give object)
692 if (Object.prototype.toString.call(record[key]) === '[object Array]') {
694 return $.map(record[key], function(subrecord) { return manifold.record_get_value(subrecord, subkey) });
695 } else if (typeof(record) == 'object') {
697 return manifold.record_get_value(record[key], subkey);
699 console.log('Unknown field');
702 return record[fields];
705 // see. get_map_entries
706 if (fields.length == 1)
707 return manifold.record_get_value(record, fields[0])
709 // Build a new record
711 $.each(fields, function(i, field) {
712 ret[field] = manifold.record_get_value(record, field);
714 ret.hashCode = record.hashCode;
715 ret.equals = record.equals;
717 // this was an array, we want a dictionary
718 //return $.map(fields, function(x) { manifold.record_get_value(record, x) });
723 record_hashcode: function(key_fields)
727 for (var i=0; i < key_fields.length; i++)
728 ret += "@@" + this[key_fields[i]];
733 _record_equals: function(self, other, key_fields)
735 for (var i=0; i < key_fields.length; i++) {
736 var this_value = self[key_fields[i]];
737 var other_value = other[key_fields[i]];
739 var this_type = manifold.get_type(this_value);
740 var other_type = manifold.get_type(other_value);
741 if (this_type != other_type)
746 case TYPE_LIST_OF_VALUES:
747 if (this_value != other_value)
751 if (!(_record_equals(this_value, other_value, key_fields)))
754 case TYPE_LIST_OF_RECORDS:
755 if (this_value.length != other_value.length)
757 for (var j = 0; j < this_value.length; j++)
758 if (!(_record_equals(this_value[j], other_value[j], key_fields)))
766 record_equals: function(key_fields)
768 return function(other) {
769 return manifold._record_equals(this, other, key_fields);
773 _in_array: function(element, array, key_fields)
775 if (key_fields.length > 1) {
776 for (var i = 0; i < array.length; i++) {
777 if (manifold._record_equals(element, array[i], key_fields))
782 // XXX TODO If we have a dict, extract the key first
783 return ($.inArray(element, array) != -1);
787 /**************************************************************************
788 * Metadata management
789 **************************************************************************/
793 get_table: function(method) {
794 var table = MANIFOLD_METADATA[method];
795 return (typeof table === 'undefined') ? null : table;
798 get_columns: function(method) {
799 var table = this.get_table(method);
804 return (typeof table.column === 'undefined') ? null : table.column;
807 get_field_names: function(method)
809 var columns = this.get_columns(method);
812 return $.map(columns, function (x) { return x.name });
815 get_key: function(method) {
816 var table = this.get_table(method);
820 return (typeof table.key === 'undefined') ? null : table.key;
824 get_column: function(method, name) {
825 var columns = this.get_columns(method);
829 $.each(columns, function(i, c) {
836 get_type: function(method, name) {
837 var table = this.get_table(method);
841 var match = $.grep(table.column, function(x) { return x.name == name });
842 if (match.length == 0) {
845 return match[0].type;
847 return (typeof table.type === 'undefined') ? null : table.type;
852 /**************************************************************************
854 **************************************************************************/
856 query_store: new QueryStore(),
858 // XXX Remaining functions are deprecated since they are replaced by the query store
861 * Associative array storing the set of queries active on the page
867 * Insert a query in the global hash table associating uuids to queries.
868 * If the query has no been analyzed yet, let's do it.
869 * \fn insert_query(query)
871 * \param ManifoldQuery query Query to be added
873 insert_query : function (query) {
875 manifold.query_store.insert(query);
878 $(document).ready(function() {
879 manifold.run_query(query);
883 if (query.analyzed_query == null) {
884 query.analyze_subqueries();
886 manifold.all_queries[query.query_uuid]=query;
890 * Returns the query associated to a UUID
891 * \fn find_query(query_uuid)
893 * \param string query_uuid The UUID of the query to be returned
895 find_query : function (query_uuid) {
896 return manifold.all_queries[query_uuid];
899 /**************************************************************************
901 **************************************************************************/
903 // trigger a query asynchroneously
904 proxy_url : '/manifold/proxy/json/',
906 // reasonably low-noise, shows manifold requests coming in and out
907 asynchroneous_debug : true,
908 // print our more details on result publication and related callbacks
909 pubsub_debug : false,
912 * \brief We use js function closure to be able to pass the query (array)
913 * to the callback function used when data is received
915 success_closure: function(query, publish_uuid, callback) {
916 return function(data, textStatus) {
917 manifold.asynchroneous_success(data, query, publish_uuid, callback);
921 run_query: function(query, callback)
923 // default value for callback = null
924 if (typeof callback === 'undefined')
927 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
928 query_ext.query_state = QUERY_STATE_INPROGRESS;
930 var query_json = JSON.stringify(query);
932 // Inform plugins about the progress
933 query.iter_subqueries(function (sq) {
934 var sq_query_ext = manifold.query_store.find_analyzed_query_ext(sq.query_uuid);
935 sq_query_ext.query_state = QUERY_STATE_INPROGRESS;
937 manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
941 $.post(manifold.proxy_url, {'json': query_json} , manifold.success_closure(query, null, callback));
945 // Executes all async. queries - intended for the javascript header to initialize queries
946 // input queries are specified as a list of {'query_uuid': <query_uuid> }
947 // each plugin is responsible for managing its spinner through on_query_in_progress
948 asynchroneous_exec : function (query_exec_tuples) {
950 // Loop through input array, and use publish_uuid to publish back results
951 $.each(query_exec_tuples, function(index, tuple) {
952 var query=manifold.find_query(tuple.query_uuid);
953 var query_json=JSON.stringify (query);
954 var publish_uuid=tuple.publish_uuid;
955 // by default we publish using the same uuid of course
956 if (publish_uuid==undefined) publish_uuid=query.query_uuid;
957 if (manifold.pubsub_debug) {
958 messages.debug("sending POST on " + manifold.proxy_url + query.__repr());
961 query.iter_subqueries(function (sq) {
962 manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
965 // not quite sure what happens if we send a string directly, as POST data is named..
966 // this gets reconstructed on the proxy side with ManifoldQuery.fill_from_POST
967 $.post(manifold.proxy_url, {'json':query_json},
968 manifold.success_closure(query, publish_uuid, tuple.callback));
973 * \brief Forward a query to the manifold backend
974 * \param query (dict) the query to be executed asynchronously
975 * \param callback (function) the function to be called when the query terminates
977 forward: function(query, callback) {
978 var query_json = JSON.stringify(query);
979 $.post(manifold.proxy_url, {'json': query_json} ,
980 manifold.success_closure(query, query.query_uuid, callback));
984 * Returns whether a query expects a unique results.
985 * This is the case when the filters contain a key of the object
986 * \fn query_expects_unique_result(query)
988 * \param ManifoldQuery query Query for which we are testing whether it expects a unique result
990 query_expects_unique_result: function(query) {
991 /* XXX we need functions to query metadata */
992 //var keys = MANIFOLD_METADATA[query.object]['keys']; /* array of array of field names */
993 /* TODO requires keys in metadata */
999 * \fn publish_result(query, results)
1000 * \memberof Manifold
1001 * \param ManifoldQuery query Query which has received results
1002 * \param array results results corresponding to query
1004 publish_result: function(query, result) {
1005 if (typeof result === 'undefined')
1009 manifold.raise_record_event(query.query_uuid, CLEAR_RECORDS);
1010 if (manifold.pubsub_debug)
1011 messages.debug(".. publish_result (1) ");
1013 $.each(result, function(i, record) {
1014 manifold.raise_record_event(query.query_uuid, NEW_RECORD, record);
1017 if (manifold.pubsub_debug)
1018 messages.debug(".. publish_result (2) has used NEW API on " + count + " records");
1019 manifold.raise_record_event(query.query_uuid, DONE);
1020 if (manifold.pubsub_debug)
1021 messages.debug(".. publish_result (3) has used NEW API to say DONE");
1023 // OLD PLUGIN API BELOW
1024 /* Publish an update announce */
1025 var channel="/results/" + query.query_uuid + "/changed";
1026 if (manifold.pubsub_debug)
1027 messages.debug(".. publish_result (4) OLD API on channel" + channel);
1029 $.publish(channel, [result, query]);
1031 if (manifold.pubsub_debug)
1032 messages.debug(".. publish_result (5) END q=" + query.__repr());
1035 store_records: function(query, records) {
1037 var query_ext = manifold.query_store.find_analyzed_query_ext(query.query_uuid);
1038 if (query_ext.set_query_ext) {
1039 // We have a domain query
1040 // The results are stored in the corresponding set_query
1041 manifold.query_store.set_records(query_ext.set_query_ext.query.query_uuid, records);
1043 } else if (query_ext.domain_query_ext) {
1044 // We have a set query, it is only used to determine which objects are in the set, we should only retrieve the key
1045 // Has it a domain query, and has it completed ?
1046 $.each(records, function(i, record) {
1047 var key = manifold.metadata.get_key(query.object);
1048 var record_key = manifold.record_get_value(record, key);
1049 manifold.query_store.set_record_state(query.query_uuid, record_key, STATE_SET, STATE_SET_IN);
1053 // We have a normal query
1054 manifold.query_store.set_records(query.query_uuid, records, STATE_SET_IN);
1059 * Recursively publish result
1060 * \fn publish_result_rec(query, result)
1061 * \memberof Manifold
1062 * \param ManifoldQuery query Query which has received result
1063 * \param array result result corresponding to query
1065 * Note: this function works on the analyzed query
1067 publish_result_rec: function(query, records) {
1068 /* If the result is not unique, only publish the top query;
1069 * otherwise, publish the main object as well as subqueries
1070 * XXX how much recursive are we ?
1072 if (manifold.pubsub_debug)
1073 messages.debug (">>>>> publish_result_rec " + query.object);
1074 if (manifold.query_expects_unique_result(query)) {
1075 /* Also publish subqueries */
1076 $.each(query.subqueries, function(object, subquery) {
1077 manifold.publish_result_rec(subquery, records[0][object]);
1078 /* TODO remove object from result */
1081 if (manifold.pubsub_debug)
1082 messages.debug ("===== publish_result_rec " + query.object);
1084 var query_ext = manifold.query_store.find_analyzed_query_ext(query.query_uuid);
1085 query_ext.query_state = QUERY_STATE_DONE;
1087 this.store_records(query, records);
1091 if (query_ext.set_query_ext) {
1092 if (query_ext.set_query_ext.query_state != QUERY_STATE_DONE)
1094 pub_query = query_ext.set_query_ext.query;
1095 } else if (query_ext.domain_query_ext) {
1096 if (query_ext.domain_query_ext.query_state != QUERY_STATE_DONE)
1102 // We can only publish results if the query (and its related domain query) is complete
1103 manifold.publish_result(pub_query, records);
1105 if (manifold.pubsub_debug)
1106 messages.debug ("<<<<< publish_result_rec " + query.object);
1109 setup_update_query: function(query, records)
1111 // We don't prepare an update query if the result has more than 1 entry
1112 if (records.length != 1)
1114 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1116 var record = records[0];
1118 var update_query_ext = query_ext.update_query_ext;
1120 if (!update_query_ext)
1123 var update_query = update_query_ext.query;
1124 var update_query_ext = query_ext.update_query_ext;
1125 var update_query_orig = query_ext.update_query_orig_ext.query;
1127 // Testing whether the result has subqueries (one level deep only)
1128 // iif the query has subqueries
1130 var obj = query.analyzed_query.subqueries;
1131 for (method in obj) {
1132 if (obj.hasOwnProperty(method)) {
1133 var key = manifold.metadata.get_key(method);
1137 var subrecords = record[method];
1140 $.each(subrecords, function (i, subrecord) {
1141 sq_keys.push(manifold.record_get_value(subrecord, key));
1143 update_query.params[method] = sq_keys;
1144 update_query_orig.params[method] = sq_keys.slice();
1150 update_query_ext.disabled = false;
1151 update_query_orig_ext.disabled = false;
1155 process_get_query_records: function(query, records) {
1156 this.setup_update_query(query, records);
1158 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1159 query_ext.query_state = QUERY_STATE_DONE;
1161 /* Publish full results */
1162 var tmp_query = manifold.query_store.find_analyzed_query(query.query_uuid);
1163 manifold.publish_result_rec(tmp_query, records);
1166 make_records: function(object, records)
1168 $.each(records, function(i, record) {
1169 manifold.make_record(object, record);
1173 make_record: function(object, record)
1175 // To make an object a record, we just add the hash function
1176 var key = manifold.metadata.get_key(object);
1177 record.hashCode = manifold.record_hashcode(key.sort());
1178 record.equals = manifold.record_equals(key);
1180 // Looking after subrecords
1181 for (var field in record) {
1182 var result_value = record[field];
1184 switch (this.get_type(result_value)) {
1186 var subobject = manifold.metadata.get_type(object, field);
1187 // if (subobject) XXX Bugs with fields declared string while they are not : network.version is a dict in fact
1188 if (subobject && subobject != 'string')
1189 manifold.make_record(subobject, result_value);
1191 case TYPE_LIST_OF_RECORDS:
1192 var subobject = manifold.metadata.get_type(object, field);
1194 manifold.make_records(subobject, result_value);
1202 * What we need to do when receiving results from an update query:
1203 * - differences between what we had, what we requested, and what we obtained
1204 * . what we had : update_query_orig (simple fields and set fields managed differently)
1205 * . what we requested : update_query
1206 * . what we received : records
1207 * - raise appropriate events
1209 * The normal process is that results similar to Get will be pushed in the
1210 * pubsub mechanism, thus repopulating everything while we only need
1211 * diff's. This means we need to move the publish functionalities in the
1212 * previous 'process_get_query_records' function.
1214 process_update_query_records: function(query, records) {
1215 // First issue: we request everything, and not only what we modify, so will will have to ignore some fields
1216 var query_uuid = query.query_uuid;
1217 var query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
1218 var update_query = query_ext.main_query_ext.update_query_ext.query;
1219 var update_query_orig = query_ext.main_query_ext.update_query_orig_ext.query;
1221 // Since we update objects one at a time, we can get the first record
1222 var record = records[0];
1224 // Let's iterate over the object properties
1225 for (var field in record) {
1226 var result_value = record[field];
1227 switch (this.get_type(result_value)) {
1229 // Did we ask for a change ?
1230 var update_value = update_query[field];
1232 // Not requested, if it has changed: OUT OF SYNC
1233 // How we can know ?
1234 // We assume it won't have changed
1238 throw "Internal error";
1244 value : (update_value == result_value) ? STATE_VALUE_CHANGE_SUCCESS : STATE_VALUE_CHANGE_FAILURE,
1246 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1250 throw "Not implemented";
1254 case TYPE_LIST_OF_VALUES:
1255 // Same as list of records, but we don't have to extract keys
1257 // The rest of exactly the same (XXX factorize)
1258 var update_keys = update_query_orig.params[field];
1259 var query_keys = update_query.params[field];
1260 var added_keys = $.grep(query_keys, function (x) { return $.inArray(x, update_keys) == -1 });
1261 var removed_keys = $.grep(update_keys, function (x) { return $.inArray(x, query_keys) == -1 });
1264 $.each(added_keys, function(i, key) {
1265 if ($.inArray(key, result_value) == -1) {
1267 request: FIELD_REQUEST_ADD,
1270 status: FIELD_REQUEST_FAILURE,
1274 request: FIELD_REQUEST_ADD,
1277 status: FIELD_REQUEST_SUCCESS,
1280 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1282 $.each(removed_keys, function(i, key) {
1283 if ($.inArray(key, result_keys) == -1) {
1285 request: FIELD_REQUEST_REMOVE,
1288 status: FIELD_REQUEST_SUCCESS,
1292 request: FIELD_REQUEST_REMOVE,
1295 status: FIELD_REQUEST_FAILURE,
1298 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1304 case TYPE_LIST_OF_VALUES: // XXX Until fixed
1305 case TYPE_LIST_OF_RECORDS:
1306 var key, new_state, cur_query_uuid;
1308 cur_query_uuid = query.analyzed_query.subqueries[field].query_uuid;
1310 // example: slice.resource
1311 // - update_query_orig.params.resource = resources in slice before update
1312 // - update_query.params.resource = resource requested in slice
1313 // - keys from field = resources obtained
1315 if (field == 'lease') {
1316 // lease_id has been added to be repeated when
1317 // constructing request rspec. We don't want it for
1319 key = ['start_time', 'end_time', 'resource'];
1321 key = manifold.metadata.get_key(field);
1326 if (key.length > 1) {
1327 throw "Not implemented";
1333 /* XXX should be modified for multiple keys */
1334 var result_keys = $.map(record[field], function(x) { return manifold.record_get_value(x, key); });
1336 // XXX All this could be deduced from record state : STATE_IN_PENDING and STATE_OUT_PENDING
1337 // what we had at the begining
1338 var update_keys = update_query_orig.params[field];
1340 var query_keys = update_query.params[field];
1341 // what we added and removed
1342 var added_keys = $.grep(query_keys, function (x) { return (!(manifold._in_array(x, update_keys, key))); });
1343 var removed_keys = $.grep(update_keys, function (x) { return (!(manifold._in_array(x, query_keys, key))); });
1345 // Send events related to parent query
1346 $.each(added_keys, function(i, added_key) {
1347 new_state = (manifold._in_array(added_key, result_keys, key)) ? STATE_SET_IN_SUCCESS : STATE_SET_IN_FAILURE;
1349 // Update record state for children queries
1350 manifold.query_store.set_record_state(cur_query_uuid, added_key, STATE_SET, new_state);
1352 // XXX This could be optimized
1353 manifold.query_store.recount(cur_query_uuid);
1355 data = { state: STATE_SET, key : field, op : new_state, value: added_key }
1356 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1358 // Inform subquery also
1360 manifold.raise_record_event(cur_query_uuid, FIELD_STATE_CHANGED, data);
1361 // XXX Passing no parameters so that they can redraw everything would
1362 // be more efficient but is currently not supported
1363 // XXX We could also need to inform plugins about nodes IN (not pending) that are no more, etc.
1364 // XXX refactor all this when suppressing update_queries, and relying on state instead !
1366 $.each(removed_keys, function(i, removed_key) {
1367 new_state = (manifold._in_array(removed_key, result_keys, key)) ? STATE_SET_OUT_FAILURE : STATE_SET_OUT_SUCCESS;
1369 // Update record state for children queries
1370 manifold.query_store.set_record_state(cur_query_uuid, removed_key, STATE_SET, new_state);
1372 // XXX This could be optimized
1373 manifold.query_store.recount(cur_query_uuid);
1375 data = { state: STATE_SET, key : field, op : new_state, value: removed_key }
1376 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1378 // Inform subquery also
1380 manifold.raise_record_event(cur_query_uuid, FIELD_STATE_CHANGED, data);
1387 // XXX Now we need to adapt 'update' and 'update_orig' queries as if we had done a get
1388 this.setup_update_query(query, records);
1390 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1391 query_ext.query_state = QUERY_STATE_DONE;
1394 // Send DONE message to plugins
1395 query.iter_subqueries(function(sq, data, parent_query) {
1396 manifold.raise_record_event(sq.query_uuid, DONE);
1401 process_query_records: function(query, records) {
1402 if (query.action == 'get') {
1403 this.process_get_query_records(query, records);
1404 } else if (query.action == 'update') {
1405 this.process_update_query_records(query, records);
1409 // if set callback is provided it is called
1410 // most of the time publish_uuid will be query.query_uuid
1411 // however in some cases we wish to publish the result under a different uuid
1412 // e.g. an updater wants to publish its result as if from the original (get) query
1413 asynchroneous_success : function (data, query, publish_uuid, callback) {
1414 // xxx should have a nicer declaration of that enum in sync with the python code somehow
1416 var start = new Date();
1417 if (manifold.asynchroneous_debug)
1418 messages.debug(">>>>>>>>>> asynchroneous_success query.object=" + query.object);
1420 if (data.code == 2) { // ERROR
1421 // We need to make sense of error codes here
1422 alert("Your session has expired, please log in again");
1423 localStorage.removeItem('user');
1424 window.location="/logout/";
1425 if (manifold.asynchroneous_debug) {
1426 duration=new Date()-start;
1427 messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- error returned - logging out " + duration + " ms");
1431 if (data.code == 1) { // WARNING
1432 messages.error("Some errors have been received from the manifold backend at " + MANIFOLD_URL + " [" + data.description + "]");
1433 // publish error code and text message on a separate channel for whoever is interested
1435 $.publish("/results/" + publish_uuid + "/failed", [data.code, data.description] );
1439 // If a callback has been specified, we redirect results to it
1442 if (manifold.asynchroneous_debug) {
1443 duration=new Date()-start;
1444 messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- callback ended " + duration + " ms");
1449 if (manifold.asynchroneous_debug)
1450 messages.debug ("========== asynchroneous_success " + query.object + " -- before process_query_records [" + query.query_uuid +"]");
1452 // once everything is checked we can use the 'value' part of the manifoldresult
1453 var result=data.value;
1455 /* Eventually update the content of related queries (update, etc) */
1456 manifold.make_records(query.object, result);
1457 this.process_query_records(query, result);
1459 /* Publish results: disabled here, done in the previous call */
1460 //tmp_query = manifold.find_query(query.query_uuid);
1461 //manifold.publish_result_rec(tmp_query.analyzed_query, result);
1463 if (manifold.asynchroneous_debug) {
1464 duration=new Date()-start;
1465 messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- done " + duration + " ms");
1470 /**************************************************************************
1471 * Plugin API helpers
1472 **************************************************************************/
1474 raise_event_handler: function(type, query_uuid, event_type, value) {
1475 if (manifold.pubsub_debug)
1476 messages.debug("raise_event_handler, quuid="+query_uuid+" type="+type+" event_type="+event_type);
1477 if ((type != 'query') && (type != 'record'))
1478 throw 'Incorrect type for manifold.raise_event()';
1479 // xxx we observe quite a lot of incoming calls with an undefined query_uuid
1480 // this should be fixed upstream in manifold I expect
1481 if (query_uuid === undefined) {
1482 messages.warning("undefined query in raise_event_handler");
1486 // notify the change to objects that either listen to this channel specifically,
1487 // or to the wildcard channel
1488 var channels = [ manifold.get_channel(type, query_uuid), manifold.get_channel(type, '*') ];
1490 $.each(channels, function(i, channel) {
1491 if (value === undefined) {
1492 if (manifold.pubsub_debug) messages.debug("triggering [no value] on channel="+channel+" and event_type="+event_type);
1493 $('.pubsub').trigger(channel, [event_type]);
1495 if (manifold.pubsub_debug) messages.debug("triggering [value="+value+"] on channel="+channel+" and event_type="+event_type);
1496 $('.pubsub').trigger(channel, [event_type, value]);
1501 raise_query_event: function(query_uuid, event_type, value) {
1502 manifold.raise_event_handler('query', query_uuid, event_type, value);
1505 raise_record_event: function(query_uuid, event_type, value) {
1506 manifold.raise_event_handler('record', query_uuid, event_type, value);
1510 * Event handler helpers
1512 _get_next_state_add: function(prev_state)
1514 switch (prev_state) {
1516 case STATE_SET_OUT_SUCCESS:
1517 case STATE_SET_IN_FAILURE:
1518 new_state = STATE_SET_IN_PENDING;
1521 case STATE_SET_OUT_PENDING:
1522 new_state = STATE_SET_IN;
1526 case STATE_SET_IN_PENDING:
1527 case STATE_SET_IN_SUCCESS:
1528 case STATE_SET_OUT_FAILURE:
1529 console.log("Inconsistent state: already in");
1535 _get_next_state_remove: function(prev_state)
1537 switch (prev_state) {
1539 case STATE_SET_IN_SUCCESS:
1540 case STATE_SET_OUT_FAILURE:
1541 new_state = STATE_SET_OUT_PENDING;
1544 case STATE_SET_IN_PENDING:
1545 new_state = STATE_SET_OUT;
1549 case STATE_SET_OUT_PENDING:
1550 case STATE_SET_OUT_SUCCESS:
1551 case STATE_SET_IN_FAILURE:
1552 console.log("Inconsistent state: already out");
1558 _grep_active_lease_callback: function(lease_query, resource_key) {
1559 return function(lease_key_lease) {
1560 var state, lease_key, lease;
1562 lease_key = lease_key_lease[0];
1563 lease = lease_key_lease[1];
1565 if (lease['resource'] != resource_key)
1568 state = manifold.query_store.get_record_state(lease_query.query_uuid, lease_key, STATE_SET);;
1571 case STATE_SET_IN_PENDING:
1572 case STATE_SET_IN_SUCCESS:
1573 case STATE_SET_OUT_FAILURE:
1576 case STATE_SET_OUT_PENDING:
1577 case STATE_SET_OUT_SUCCESS:
1578 case STATE_SET_IN_FAILURE:
1584 _enforce_constraints: function(query_ext, record, record_key, event_type)
1588 query = query_ext.query;
1590 switch(query.object) {
1593 // CONSTRAINT_RESERVABLE_LEASE
1595 // +) If a reservable node is added to the slice, then it should have a corresponding lease
1596 // XXX Not always a resource
1597 var is_reservable = (record.exclusive == true);
1598 if (is_reservable) {
1599 var warnings = manifold.query_store.get_record_state(query.query_uuid, record_key, STATE_WARNINGS);
1601 if (event_type == STATE_SET_ADD) {
1602 // We should have a lease_query associated
1603 var lease_query = query_ext.parent_query_ext.query.subqueries['lease']; // in options
1604 var lease_query_ext = manifold.query_store.find_analyzed_query_ext(lease_query.query_uuid);
1605 // Do we have lease records (in) with this resource
1606 var lease_records = $.grep(lease_query_ext.records.entries(), this._grep_active_lease_callback(lease_query, record_key));
1607 if (lease_records.length == 0) {
1609 // XXX Need for a better function to manage warnings
1610 var warn = CONSTRAINT_RESERVABLE_LEASE_MSG;
1611 warnings[CONSTRAINT_RESERVABLE_LEASE] = warn;
1613 // Lease are defined, delete the warning in case it was set previously
1614 delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1617 // Remove warnings attached to this resource
1618 delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1621 manifold.query_store.set_record_state(query.query_uuid, record_key, STATE_WARNINGS, warnings);
1624 /* This was redundant */
1625 // manifold.query_store.recount(query.query_uuid);
1627 // Signal the change to plugins (even if the constraint does not apply, so that the plugin can display a checkmark)
1629 state: STATE_WARNINGS,
1634 manifold.raise_record_event(query.query_uuid, FIELD_STATE_CHANGED, data);
1638 var resource_key = record_key.resource;
1639 var resource_query = query_ext.parent_query_ext.query.subqueries['resource'];
1640 var warnings = manifold.query_store.get_record_state(resource_query.query_uuid, resource_key, STATE_WARNINGS);
1642 if (event_type == STATE_SET_ADD) {
1643 // A lease is added, it removes the constraint
1644 delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1646 // A lease is removed, it might trigger the warning
1647 var lease_records = $.grep(query_ext.records.entries(), this._grep_active_lease_callback(query, resource_key));
1648 if (lease_records.length == 0) { // XXX redundant cases
1650 // XXX Need for a better function to manage warnings
1651 var warn = CONSTRAINT_RESERVABLE_LEASE_MSG;
1652 warnings[CONSTRAINT_RESERVABLE_LEASE] = warn;
1654 // Lease are defined, delete the warning in case it was set previously
1655 delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1660 manifold.query_store.recount(resource_query.query_uuid);
1662 // Signal the change to plugins (even if the constraint does not apply, so that the plugin can display a checkmark)
1664 state: STATE_WARNINGS,
1669 manifold.raise_record_event(resource_query.query_uuid, FIELD_STATE_CHANGED, data);
1673 // -) When a lease is added, it might remove the warning associated to a reservable node
1675 // If a NITOS node is reserved, then at least a NITOS channel should be reserved
1676 // - When a NITOS channel is added, it might remove a warning associated to all NITOS nodes
1678 // If a NITOS channel is reserved, then at least a NITOS node should be reserved
1679 // - When a NITOS node is added, it might remove a warning associated to all NITOS channels
1681 // A lease is present while the resource has been removed => Require warnings on nodes not in set !
1685 _get_query_path: function(query_ext) {
1688 while (sq.parent_query_ext) {
1691 path = sq.query.object + path;
1692 sq = sq.parent_query_ext;
1699 * Handling events raised by plugins
1701 raise_event: function(query_uuid, event_type, data)
1703 var query, query_ext;
1705 // Query uuid has been updated with the key of a new element
1706 query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
1707 query = query_ext.query;
1709 switch(event_type) {
1711 // XXX At some point, should be renamed to RECORD_STATE_CHANGED
1712 case FIELD_STATE_CHANGED:
1714 // value is an object (request, key, value, status)
1715 // update is only possible is the query is not pending, etc
1716 // SET_ADD is on a subquery, FIELD_STATE_CHANGED on the query itself
1717 // we should map SET_ADD on this...
1719 // 1. Update internal query store about the change in status
1721 // 2. Update the update query
1722 update_query = query_ext.main_query_ext.update_query_ext.query;
1723 update_query_orig = query_ext.main_query_ext.update_query_orig_ext.query;
1725 switch(data.state) {
1730 /* Set parameter data.key in the update_query to VALUE */
1731 if (update_query.params[data.key] === undefined)
1732 update_query.params[data.key] = Array();
1733 update_query.params[data.key] = value.value;
1740 var prev_state, new_state;
1741 var main_query, record, new_data, path;
1743 // We only track state in the analyzed query
1744 prev_state = manifold.query_store.get_record_state(query_uuid, data.value, STATE_SET);
1745 if (prev_state === null)
1746 prev_state = STATE_SET_OUT;
1750 new_state = this._get_next_state_add(prev_state);
1752 /* data.value containts the resource key */
1753 manifold.query_store.add_record(query_uuid, data.value, new_state);
1754 record = manifold.query_store.get_record(query_uuid, data.value);
1755 this._enforce_constraints(query_ext, record, data.value, STATE_SET_ADD);
1757 /* Process update query in parent */
1758 path = this._get_query_path(query_ext);
1759 if (update_query.params[path] === undefined)
1760 update_query.params[path] = Array();
1761 update_query.params[path].push(data.value);
1765 case STATE_SET_REMOVE:
1766 new_state = this._get_next_state_remove(prev_state);
1768 /* data.value contains the resource key */
1769 manifold.query_store.remove_record(query_uuid, data.value, new_state);
1770 record = manifold.query_store.get_record(query_uuid, data.value);
1771 this._enforce_constraints(query_ext, record, data.value, STATE_SET_REMOVE);
1773 /* Process update query in parent */
1774 path = this._get_query_path(query_ext);
1775 arr = update_query.params[path];
1777 var key = manifold.metadata.get_key(query.object);
1778 arr = $.grep(arr, function(x) { return (!(manifold._record_equals(x, data.value, key))); });
1779 if (update_query.params[path] === undefined)
1780 update_query.params[path] = Array();
1781 update_query.params[path] = arr;
1785 /* Inform the parent query: important for update */
1792 main_query = query_ext.main_query_ext.query;
1793 manifold.raise_record_event(main_query.query_uuid, event_type, new_data);
1794 /* Propagate the event to other plugins subscribed to the query */
1795 manifold.query_store.recount(query_uuid);
1797 manifold.raise_record_event(query_uuid, event_type, new_data);
1802 // 3. Inform others about the change
1803 // a) the main query...
1804 manifold.raise_record_event(query_uuid, event_type, data);
1806 // b) subqueries eventually (dot in the key)
1809 var cur_query = query;
1810 if (cur_query.analyzed_query)
1811 cur_query = cur_query.analyzed_query;
1814 var path_array = data.key.split('.');
1815 var value_key = data.key.split('.');
1816 $.each(path_array, function(i, method) {
1817 cur_query = cur_query.subqueries[method];
1818 value_key.shift(); // XXX check that method is indeed shifted
1820 data.key = value_key;
1822 manifold.raise_record_event(cur_query.query_uuid, event_type, data);
1828 manifold.run_query(query_ext.main_query_ext.update_query_ext.query);
1831 /* QUERY STATE CHANGED */
1836 /* Update internal record state */
1837 manifold.query_store.add_filter(query_uuid, data);
1839 /* Propagate the message to plugins */
1840 manifold.raise_query_event(query_uuid, event_type, data);
1844 case FILTER_REMOVED:
1845 /* Update internal record state */
1846 manifold.query_store.remove_filter(query_uuid, data);
1848 /* Propagate the message to plugins */
1849 manifold.raise_query_event(query_uuid, event_type, data);
1854 main_query = query_ext.main_query_ext.query;
1855 main_update_query = query_ext.main_query_ext.update_query;
1858 // Here we need the full path through all subqueries
1860 // XXX We might need the query name in the QueryExt structure
1861 main_query.select(data);
1863 // XXX When is an update query associated ?
1864 // XXX main_update_query.select(value);
1866 manifold.raise_query_event(query_uuid, event_type, data);
1870 query = query_ext.query;
1871 main_query = query_ext.main_query_ext.query;
1872 main_update_query = query_ext.main_query_ext.update_query;
1873 query.unselect(data);
1874 main_query.unselect(data);
1876 // We need to inform about changes in these queries to the respective plugins
1877 // Note: query & main_query have the same UUID
1878 manifold.raise_query_event(query_uuid, event_type, data);
1881 // We need to inform about changes in these queries to the respective plugins
1882 // Note: query, main_query & update_query have the same UUID
1884 // http://trac.myslice.info/ticket/32
1885 // Avoid multiple calls to the same event
1886 //manifold.raise_query_event(query_uuid, event_type, value);
1888 // We are targeting the same object with get and update
1889 // The notion of query is bad, we should have a notion of destination, and issue queries on the destination
1890 // NOTE: Editing a subquery == editing a local view on the destination
1892 // XXX We might need to run the new query again and manage the plugins in the meantime with spinners...
1893 // For the time being, we will collect all columns during the first query
1896 /* Publish/subscribe channels for internal use */
1897 get_channel: function(type, query_uuid) {
1898 if ((type !== 'query') && (type != 'record'))
1900 return '/' + type + '/' + query_uuid;
1903 }; // manifold object
1904 /* ------------------------------------------------------------ */
1908 // OLD PLUGIN API: extend jQuery/$ with pubsub capabilities
1909 // https://gist.github.com/661855
1911 $.subscribe = function( channel, selector, data, fn) {
1912 /* borrowed from jQuery */
1913 if ( data == null && fn == null ) {
1916 data = selector = undefined;
1917 } else if ( fn == null ) {
1918 if ( typeof selector === "string" ) {
1919 // ( channel, selector, fn )
1923 // ( channel, data, fn )
1926 selector = undefined;
1931 /* We use an indirection function that will clone the object passed in
1932 * parameter to the subscribe callback
1934 * FIXME currently we only clone query objects which are the only ones
1935 * supported and editable, we might have the same issue with results but
1936 * the page load time will be severely affected...
1938 o.on.apply(o, [channel, selector, data, function() {
1939 for(i = 1; i < arguments.length; i++) {
1940 if ( arguments[i].constructor.name == 'ManifoldQuery' )
1941 arguments[i] = arguments[i].clone();
1943 fn.apply(o, arguments);
1947 $.unsubscribe = function() {
1948 o.off.apply(o, arguments);
1951 $.publish = function() {
1952 o.trigger.apply(o, arguments);
1957 /* ------------------------------------------------------------ */
1959 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
1960 //make sure to expose csrf in our outcoming ajax/post requests
1962 beforeSend: function(xhr, settings) {
1963 function getCookie(name) {
1964 var cookieValue = null;
1965 if (document.cookie && document.cookie != '') {
1966 var cookies = document.cookie.split(';');
1967 for (var i = 0; i < cookies.length; i++) {
1968 var cookie = jQuery.trim(cookies[i]);
1969 // Does this cookie string begin with the name we want?
1970 if (cookie.substring(0, name.length + 1) == (name + '=')) {
1971 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
1978 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
1979 // Only send the token to relative URLs i.e. locally.
1980 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));