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://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript
21 // attach the .equals method to Array's prototype to call it on any array
22 Array.prototype.equals = function (array) {
23 // if the other array is a falsy value, return
27 // compare lengths - can save a lot of time
28 if (this.length != array.length)
31 for (var i = 0, l=this.length; i < l; i++) {
32 // Check if we have nested arrays
33 if (this[i] instanceof Array && array[i] instanceof Array) {
34 // recurse into the nested arrays
35 if (!this[i].equals(array[i]))
38 else if (this[i] != array[i]) {
39 // Warning - two different object instances will never be equal: {x:20} != {x:20}
46 // http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
47 Object.toType = (function toType(global) {
48 return function(obj) {
52 return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
56 /* ------------------------------------------------------------ */
58 // Constants that should be somehow moved to a plugin.js file
60 var FILTER_REMOVED = 2;
61 var CLEAR_FILTERS = 3;
63 var FIELD_REMOVED = 5;
66 var CLEAR_RECORDS = 8;
69 * event: FIELD_STATE_CHANGED
73 * .state : ???? used to be FIELD_REQUEST_ADD / FIELD_REQUEST_REMOVE
74 * .key : ??? the key fields of the record
75 * .op : the key of the record who has received an update
76 * .value : the new state of the record
78 var FIELD_STATE_CHANGED = 9;
80 var IN_PROGRESS = 101;
81 var DONE = 102; //XXX Should be harmonized with query state
83 /* Update requests related to subqueries */
87 var SET_REMOVED = 202;
92 var FIELD_REQUEST_CHANGE = 301;
93 var FIELD_REQUEST_ADD = 302;
94 var FIELD_REQUEST_REMOVE = 303;
95 var FIELD_REQUEST_ADD_RESET = 304;
96 var FIELD_REQUEST_REMOVE_RESET = 305;
98 // status (XXX Should be deprecated)
99 var FIELD_REQUEST_PENDING = 401;
100 var FIELD_REQUEST_SUCCESS = 402;
101 var FIELD_REQUEST_FAILURE = 403;
102 var STATUS_OKAY = 404;
103 var STATUS_SET_WARNING = 405;
104 var STATUS_ADD_WARNING = 406;
105 var STATUS_REMOVE_WARNING = 407;
106 var STATUS_RESET = 408;
108 /* Requests for query cycle */
109 var RUN_UPDATE = 601;
114 var TYPE_LIST_OF_VALUES = 3;
115 var TYPE_LIST_OF_RECORDS = 4;
117 /******************************************************************************
118 * QUERY STATUS (for manifold events)
119 ******************************************************************************/
121 var STATUS_NONE = 500; // Query has not been started yet
122 var STATUS_GET_IN_PROGRESS = 501; // Query has been sent, no result has been received
123 var STATUS_GET_RECEIVED = 502; // Success
124 var STATUS_GET_ERROR = 503; // Error
125 var STATUS_UPDATE_PENDING = 504;
126 var STATUS_UPDATE_IN_PROGRESS = 505;
127 var STATUS_UPDATE_RECEIVED = 506;
128 var STATUS_UPDATE_ERROR = 507;
130 /******************************************************************************
131 * QUERY STATE (for query_store)
132 ******************************************************************************/
134 // XXX Rendundant with query status ?
136 var QUERY_STATE_INIT = 0;
137 var QUERY_STATE_INPROGRESS = 1;
138 var QUERY_STATE_DONE = 2;
140 /******************************************************************************
141 * RECORD STATES (for query_store)
142 ******************************************************************************/
146 var STATE_WARNINGS = 2;
147 var STATE_VISIBLE = 3;
150 var STATE_SET_CHANGE = 0;
151 var STATE_SET_ADD = 1;
152 var STATE_SET_REMOVE = 2;
153 var STATE_SET_CLEAR = 3;
156 var STATE_SET_IN = 0;
157 var STATE_SET_OUT = 1;
158 var STATE_SET_IN_PENDING = 2;
159 var STATE_SET_OUT_PENDING = 3;
160 var STATE_SET_IN_SUCCESS = 4;
161 var STATE_SET_OUT_SUCCESS = 5;
162 var STATE_SET_IN_FAILURE = 6;
163 var STATE_SET_OUT_FAILURE = 7;
164 var STATE_VALUE_CHANGE_PENDING = 8;
165 var STATE_VALUE_CHANGE_SUCCESS = 9;
166 var STATE_VALUE_CHANGE_FAILURE = 10;
168 // STATE_WARNINGS : dict
170 // STATE_VISIBLE : boolean
172 /******************************************************************************
174 ******************************************************************************/
176 var CONSTRAINT_RESERVABLE_LEASE = 0;
178 var CONSTRAINT_RESERVABLE_LEASE_MSG = "Configuration required: this resource needs to be scheduled";
180 // A structure for storing queries
182 function QueryExt(query, parent_query_ext, main_query_ext, update_query_ext, disabled, domain_query_ext) {
185 if (typeof query == "undefined")
186 throw "Must pass a query in QueryExt constructor";
188 this.parent_query_ext = (typeof parent_query_ext == "undefined") ? null : parent_query_ext;
189 this.main_query_ext = (typeof main_query_ext == "undefined") ? null : main_query_ext;
190 this.update_query_ext = (typeof update_query_ext == "undefined") ? null : update_query_ext;
191 this.update_query_orig_ext = (typeof update_query_orig_ext == "undefined") ? null : update_query_orig_ext;
192 this.disabled = (typeof disabled == "undefined") ? false : disabled;
194 // A domain query is a query that is issued to retrieve all possible values for a set
195 // eg. all resources that can be attached to a slice
196 // It is null unless we are a subquery for which a domain query has been issued
197 this.domain_query_ext = (typeof domain_query_ext == "undefined") ? null : domain_query_ext;
199 // Set members to buffer until the domain query is completed
201 this.set_members = [];
203 // The set query is the query for which the domain query has been issued.
204 // It is null unless the query is a domain query
205 this.set_query_ext = (typeof set_query_ext == "undefined") ? null : domain_query_ext;
207 this.query_state = QUERY_STATE_INIT;
209 // Results from a query consists in a dict that maps keys to records
210 this.records = new Hashtable();
212 // Status is a dict that maps keys to record status
213 this.state = new Hashtable();
215 // Filters that impact visibility in the local interface
218 // XXX Until we find a better solution
219 this.num_pending = 0;
220 this.num_unconfigured = 0;
222 // update_query null unless we are a main_query (aka parent_query == null); only main_query_fields can be updated...
225 function QueryStore() {
227 this.main_queries = {};
228 this.analyzed_queries = {};
232 this.insert = function(query) {
233 // We expect only main_queries are inserted
235 /* If the query has not been analyzed, then we analyze it */
236 if (query.analyzed_query == null) {
237 query.analyze_subqueries();
240 /* We prepare the update query corresponding to the main query and store both */
241 /* Note: they have the same UUID */
243 // XXX query.change_action() should become deprecated
244 update_query = query.clone();
245 update_query.action = 'update';
246 update_query.analyzed_query.action = 'update';
247 update_query.params = {};
248 update_query_ext = new QueryExt(update_query);
250 /* We remember the original query to be able to reset it */
251 update_query_orig_ext = new QueryExt(update_query.clone());
254 /* We store the main query */
255 query_ext = new QueryExt(query, null, null, update_query_ext, update_query_orig_ext, false);
256 manifold.query_store.main_queries[query.query_uuid] = query_ext;
257 /* Note: the update query does not have an entry! */
260 // The query is disabled; since it is incomplete until we know the content of the set of subqueries
261 // XXX unless we have no subqueries ???
262 // we will complete with params when records are received... this has to be done by the manager
263 // SET_ADD, SET_REMOVE will change the status of the elements of the set
264 // UPDATE will change also, etc.
265 // XXX We need a proper structure to store this information...
267 // We also need to insert all queries and subqueries from the analyzed_query
268 // XXX We need the root of all subqueries
269 query.iter_subqueries(function(sq, data, parent_query) {
270 var parent_query_ext;
272 parent_query_ext = manifold.query_store.find_analyzed_query_ext(parent_query.query_uuid);
274 parent_query_ext = null;
276 // XXX parent_query_ext == false
277 // XXX main.subqueries = {} # Normal, we need analyzed_query
278 sq_ext = new QueryExt(sq, parent_query_ext, query_ext)
281 /* Let's issue a query for the subquery domain. This query will not need any update etc.
282 eg. for resources in a slice, we also query all resources */
283 var all_fields = manifold.metadata.get_field_names(sq.object);
284 var domain_query = new ManifoldQuery('get', sq.object, 'now', [], {}, all_fields);
285 //var domain_query = new ManifoldQuery('get', sq.object);
287 console.log("Created domain query", domain_query);
288 var domain_query_ext = new QueryExt(domain_query);
290 domain_query_ext.set_query_ext = sq_ext;
291 sq_ext.domain_query_ext = domain_query_ext;
293 // One of these two is useless ?
294 manifold.query_store.main_queries[domain_query.query_uuid] = domain_query_ext;
295 manifold.query_store.analyzed_queries[domain_query.query_uuid] = domain_query_ext;
297 // XXX This query is run before the plugins are initialized and listening
298 manifold.run_query(domain_query);
301 manifold.query_store.analyzed_queries[sq.query_uuid] = sq_ext;
304 // XXX We have spurious update queries...
309 this.find_query_ext = function(query_uuid)
311 return this.main_queries[query_uuid];
314 this.find_query = function(query_uuid)
316 return this.find_query_ext(query_uuid).query;
319 this.find_analyzed_query_ext = function(query_uuid)
321 return this.analyzed_queries[query_uuid];
324 this.find_analyzed_query = function(query_uuid)
326 return this.find_analyzed_query_ext(query_uuid).query;
329 this.state_dict_create = function(default_set)
331 default_set = (default_set === undefined) ? STATE_SET_OUT : default_set;
333 // We cannot use constants in literal definition, so...
334 state_dict[STATE_WARNINGS] = {};
335 state_dict[STATE_SET] = default_set;
336 state_dict[STATE_VISIBLE] = true;
342 this.set_records = function(query_uuid, records, default_set)
344 default_set = (default_set === undefined) ? STATE_SET_OUT : default_set;
347 var key, object, query_ext, record_key;
349 query_ext = this.find_analyzed_query_ext(query_uuid);
350 object = query_ext.query.object;
351 if (object.indexOf(':') != -1) {
352 object = object.split(':')[1];
354 record_key = manifold.metadata.get_key(object);
356 // ["start_time", "resource", "end_time"]
359 $.each(records, function(i, record) {
360 //var key = manifold.metadata.get_key(query_ext.query.object);
362 var record_key_value = manifold.record_get_value(record, record_key);
363 query_ext.records.put(record_key_value, record);
365 if (!(query_ext.state.get(record_key_value)))
366 query_ext.state.put(record_key_value, self.state_dict_create(default_set));
370 this.get_records = function(query_uuid)
372 var query_ext = this.find_analyzed_query_ext(query_uuid);
373 return query_ext.records.values();
376 this.get_record = function(query_uuid, record_key)
378 var query_ext = this.find_analyzed_query_ext(query_uuid);
379 return query_ext.records.get(record_key);
382 this.del_record = function(query_uuid, record_key)
384 var query_ext = this.find_analyzed_query_ext(query_uuid);
385 return query_ext.records.remove(record_key);
388 this.del_state = function(query_uuid, record_key)
390 var query_ext = this.find_analyzed_query_ext(query_uuid);
391 return query_ext.state.remove(record_key);
394 this.add_record = function(query_uuid, record, new_state)
396 var query_ext, key, record_key;
397 query_ext = this.find_analyzed_query_ext(query_uuid);
399 if (typeof(record) == 'object') {
400 key = manifold.metadata.get_key(query_ext.query.object);
401 record_key = manifold.record_get_value(record, key);
406 var record_entry = query_ext.records.get(record_key);
408 query_ext.records.put(record_key, record);
410 manifold.query_store.set_record_state(query_uuid, record_key, STATE_SET, new_state);
413 this.remove_record = function(query_uuid, record, new_state)
415 var query_ext, key, record_key;
416 query_ext = this.find_analyzed_query_ext(query_uuid);
418 if (typeof(record) == 'object') {
419 key = manifold.metadata.get_key(query_ext.query.object);
420 record_key = manifold.record_get_value(record, key);
425 if ((query_ext.query.object == 'lease') && (new_state == STATE_SET_OUT)) {
426 // Leases that are marked out are in fact leases from other slices
427 // We need to _remove_ leases that we mark as OUT
428 manifold.query_store.del_record(query_uuid, record_key);
429 manifold.query_store.del_state(query_uuid, record_key);
431 manifold.query_store.set_record_state(query_uuid, record_key, STATE_SET, new_state);
435 this.iter_records = function(query_uuid, callback)
437 var query_ext = this.find_analyzed_query_ext(query_uuid);
438 query_ext.records.each(callback);
439 //callback = function(record_key, record)
442 this.iter_visible_records = function(query_uuid, callback)
444 var query_ext = this.find_analyzed_query_ext(query_uuid);
445 query_ext.records.each(function(record_key, record) {
446 if (query_ext.state.get(record_key)[STATE_VISIBLE]) // .STATE_VISIBLE would be for the string key
447 callback(record_key, record);
449 //callback = function(record_key, record)
455 this.set_record_state = function(query_uuid, result_key, state, value)
457 var query_ext = this.find_analyzed_query_ext(query_uuid);
458 var state_dict = query_ext.state.get(result_key);
460 state_dict = this.state_dict_create();
462 state_dict[state] = value;
464 query_ext.state.put(result_key, state_dict);
467 this.get_record_state = function(query_uuid, result_key, state)
469 var query_ext = this.find_analyzed_query_ext(query_uuid);
470 var state_dict = query_ext.state.get(result_key);
473 return state_dict[state];
478 this.add_filter = function(query_uuid, filter)
480 var query_ext = this.find_analyzed_query_ext(query_uuid);
481 // XXX When we update a filter
482 query_ext.filters.push(filter);
484 this.apply_filters(query_uuid);
488 this.update_filter = function(query_uuid, filter)
492 this.apply_filters(query_uuid);
495 this.remove_filter = function(query_uuid, filter)
497 var query_ext = this.find_analyzed_query_ext(query_uuid);
498 query_ext.filters = $.grep(query_ext.filters, function(x) {
499 return !(x.equals(filter));
502 this.apply_filters(query_uuid);
505 this.get_filters = function(query_uuid)
507 var query_ext = this.find_analyzed_query_ext(query_uuid);
508 return query_ext.filters;
511 this.recount = function(query_uuid)
514 var is_reserved, is_pending, in_set, is_unconfigured;
516 query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
517 query_ext.num_pending = 0;
518 query_ext.num_unconfigured = 0;
520 this.iter_records(query_uuid, function(record_key, record) {
521 var record_state = manifold.query_store.get_record_state(query_uuid, record_key, STATE_SET);
522 var record_warnings = manifold.query_store.get_record_state(query_uuid, record_key, STATE_WARNINGS);
524 is_reserved = (record_state == STATE_SET_IN)
525 || (record_state == STATE_SET_OUT_PENDING)
526 || (record_state == STATE_SET_IN_SUCCESS)
527 || (record_state == STATE_SET_OUT_FAILURE);
529 is_pending = (record_state == STATE_SET_IN_PENDING)
530 || (record_state == STATE_SET_OUT_PENDING);
532 in_set = (record_state == STATE_SET_IN) // should not have warnings
533 || (record_state == STATE_SET_IN_PENDING)
534 || (record_state == STATE_SET_IN_SUCCESS)
535 || (record_state == STATE_SET_OUT_FAILURE); // should not have warnings
537 is_unconfigured = (in_set && !$.isEmptyObject(record_warnings));
539 /* Let's update num_pending and num_unconfigured at this stage */
541 query_ext.num_pending++;
543 query_ext.num_unconfigured++;
548 this.apply_filters = function(query_uuid)
550 var start = new Date().getTime();
552 // Toggle visibility of records according to the different filters.
555 var filters = this.get_filters(query_uuid);
557 /* Let's update num_pending and num_unconfigured at this stage */
559 // Adapted from querytable._querytable_filter()
561 this.iter_records(query_uuid, function(record_key, record) {
562 var is_reserved, is_pending, in_set, is_unconfigured;
564 /* By default, a record is visible unless a filter says the opposite */
567 var record_state = manifold.query_store.get_record_state(query_uuid, record_key, STATE_SET);
568 var record_warnings = manifold.query_store.get_record_state(query_uuid, record_key, STATE_WARNINGS);
570 is_reserved = (record_state == STATE_SET_IN)
571 || (record_state == STATE_SET_OUT_PENDING)
572 || (record_state == STATE_SET_IN_SUCCESS)
573 || (record_state == STATE_SET_OUT_FAILURE);
575 is_pending = (record_state == STATE_SET_IN_PENDING)
576 || (record_state == STATE_SET_OUT_PENDING);
578 in_set = (record_state == STATE_SET_IN) // should not have warnings
579 || (record_state == STATE_SET_IN_PENDING)
580 || (record_state == STATE_SET_IN_SUCCESS)
581 || (record_state == STATE_SET_OUT_FAILURE); // should not have warnings
583 is_unconfigured = (in_set && !$.isEmptyObject(record_warnings));
585 // We go through each filter and decide whether it affects the visibility of the record
586 $.each(filters, function(index, filter) {
589 var value = filter[2];
592 /* We do some special handling for the manifold:status filter
595 if (key == 'manifold:status') {
596 if (op != '=' && op != '==') {
597 // Unsupported filter, let's ignore it
598 console.log("Unsupported filter on manifold:status. Should be EQUAL only.");
599 return true; // ~ continue
604 // true => ~ continue
606 visible = is_reserved;
609 visible = is_unconfigured;
612 visible = is_pending;
615 return false; // ~ break
618 /* Normal filtering behaviour (according to the record content) follows... */
619 col_value = manifold.record_get_value(record, key);
621 // When the filter does not match, we hide the column by default
622 if (col_value === 'undefined') {
624 return false; // ~ break
627 // XXX This should accept pluggable filtering functions.
630 /* Test whether current filter is compatible with the column */
631 if (op == '=' || op == '==') {
632 if ( col_value != value || col_value==null || col_value=="" || col_value=="n/a")
635 }else if (op == 'included') {
636 /* By default, the filter returns false unless the record
637 * field match at least one value of the included statement
640 $.each(value, function(i,x) {
643 return false; // ~ break
646 }else if (op == '!=') {
647 if ( col_value == value || col_value==null || col_value=="" || col_value=="n/a")
650 if ( parseFloat(col_value) >= value || col_value==null || col_value=="" || col_value=="n/a")
653 if ( parseFloat(col_value) <= value || col_value==null || col_value=="" || col_value=="n/a")
655 } else if(op=='<=' || op=='≤') {
656 if ( parseFloat(col_value) > value || col_value==null || col_value=="" || col_value=="n/a")
658 } else if(op=='>=' || op=='≥') {
659 if ( parseFloat(col_value) < value || col_value==null || col_value=="" || col_value=="n/a")
662 // How to break out of a loop ?
663 alert("filter not supported");
664 return false; // break
669 // Set the visibility status in the query store
670 self.set_record_state(query_uuid, record_key, STATE_VISIBLE, visible);
673 var end = new Date().getTime();
674 console.log("APPLY FILTERS [", filters, "] took", end - start, "ms");
681 * This namespace holds functions for globally managing query objects
686 /**************************************************************************
688 **************************************************************************/
692 get_type: function(variable) {
693 switch(Object.toType(variable)) {
700 if ((variable.length > 0) && (Object.toType(variable[0]) === 'object'))
701 return TYPE_LIST_OF_RECORDS;
703 return TYPE_LIST_OF_VALUES;
709 * fields: A String instance (field name), or a set of String instances
710 * (field names) # XXX tuple !!
712 * If fields is a String, return the corresponding value.
713 * If fields is a set, return a tuple of corresponding value.
716 * KeyError if at least one of the fields is not found
718 record_get_value: function(record, fields)
720 if (typeof(fields) === 'string') {
721 if (fields.indexOf('.') != -1) {
722 key_subkey = key.split('.', 2);
724 subkey = key_subkey[1];
726 if (record.indexOf(key) == -1) {
729 // Tests if the following is an array (typeof would give object)
730 if (Object.prototype.toString.call(record[key]) === '[object Array]') {
732 return $.map(record[key], function(subrecord) { return manifold.record_get_value(subrecord, subkey) });
733 } else if (typeof(record) == 'object') {
735 return manifold.record_get_value(record[key], subkey);
737 console.log('Unknown field');
740 return record[fields];
743 // see. get_map_entries
744 if (fields.length == 1)
745 return manifold.record_get_value(record, fields[0])
747 // Build a new record
749 $.each(fields, function(i, field) {
750 ret[field] = manifold.record_get_value(record, field);
752 ret.hashCode = record.hashCode;
753 ret.equals = record.equals;
755 // this was an array, we want a dictionary
756 //return $.map(fields, function(x) { manifold.record_get_value(record, x) });
761 record_hashcode: function(key_fields)
765 for (var i=0; i < key_fields.length; i++)
766 ret += "@@" + this[key_fields[i]];
771 _record_equals: function(self, other, key_fields)
773 if ((typeof self === "string") && (typeof other === "string")) {
774 return self == other;
776 for (var i=0; i < key_fields.length; i++) {
777 var this_value = self[key_fields[i]];
778 var other_value = other[key_fields[i]];
780 var this_type = manifold.get_type(this_value);
781 var other_type = manifold.get_type(other_value);
782 if (this_type != other_type)
787 case TYPE_LIST_OF_VALUES:
788 if (this_value != other_value)
792 if (!(_record_equals(this_value, other_value, key_fields)))
795 case TYPE_LIST_OF_RECORDS:
796 if (this_value.length != other_value.length)
798 for (var j = 0; j < this_value.length; j++)
799 if (!(_record_equals(this_value[j], other_value[j], key_fields)))
807 record_equals: function(key_fields)
809 return function(other) {
810 return manifold._record_equals(this, other, key_fields);
814 _in_array: function(element, array, key_fields)
816 if (key_fields.length > 1) {
817 for (var i = 0; i < array.length; i++) {
818 if (manifold._record_equals(element, array[i], key_fields))
823 // XXX TODO If we have a dict, extract the key first
824 return ($.inArray(element, array) != -1);
828 /**************************************************************************
829 * Metadata management
830 **************************************************************************/
834 get_table: function(method) {
835 var table = MANIFOLD_METADATA[method];
836 return (typeof table === 'undefined') ? null : table;
839 get_columns: function(method) {
840 var table = this.get_table(method);
845 return (typeof table.column === 'undefined') ? null : table.column;
848 get_field_names: function(method)
850 var columns = this.get_columns(method);
853 return $.map(columns, function (x) { return x.name });
856 get_key: function(method) {
857 var table = this.get_table(method);
861 return (typeof table.key === 'undefined') ? null : table.key;
865 get_column: function(method, name) {
866 var columns = this.get_columns(method);
870 $.each(columns, function(i, c) {
877 get_type: function(method, name) {
878 var table = this.get_table(method);
882 var match = $.grep(table.column, function(x) { return x.name == name });
883 if (match.length == 0) {
886 return match[0].type;
888 return (typeof table.type === 'undefined') ? null : table.type;
893 /**************************************************************************
895 **************************************************************************/
897 query_store: new QueryStore(),
899 // XXX Remaining functions are deprecated since they are replaced by the query store
902 * Associative array storing the set of queries active on the page
908 * Insert a query in the global hash table associating uuids to queries.
909 * If the query has no been analyzed yet, let's do it.
910 * \fn insert_query(query)
912 * \param ManifoldQuery query Query to be added
914 insert_query : function (query) {
916 manifold.query_store.insert(query);
919 $(document).ready(function() {
920 manifold.run_query(query);
924 if (query.analyzed_query == null) {
925 query.analyze_subqueries();
927 manifold.all_queries[query.query_uuid]=query;
931 * Returns the query associated to a UUID
932 * \fn find_query(query_uuid)
934 * \param string query_uuid The UUID of the query to be returned
936 find_query : function (query_uuid) {
937 return manifold.all_queries[query_uuid];
940 /**************************************************************************
942 **************************************************************************/
944 // trigger a query asynchroneously
945 proxy_url : '/manifold/proxy/json/',
947 // reasonably low-noise, shows manifold requests coming in and out
948 asynchroneous_debug : true,
949 // print our more details on result publication and related callbacks
950 pubsub_debug : false,
953 * \brief We use js function closure to be able to pass the query (array)
954 * to the callback function used when data is received
956 success_closure: function(query, publish_uuid, callback) {
957 return function(data, textStatus) {
958 manifold.asynchroneous_success(data, query, publish_uuid, callback);
962 run_query: function(query, callback)
964 // default value for callback = null
965 if (typeof callback === 'undefined')
968 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
969 query_ext.query_state = QUERY_STATE_INPROGRESS;
971 var query_json = JSON.stringify(query);
973 // Inform plugins about the progress
974 query.iter_subqueries(function (sq) {
975 var sq_query_ext = manifold.query_store.find_analyzed_query_ext(sq.query_uuid);
976 sq_query_ext.query_state = QUERY_STATE_INPROGRESS;
978 manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
982 $.post(manifold.proxy_url, {'json': query_json} , manifold.success_closure(query, null, callback));
986 // Executes all async. queries - intended for the javascript header to initialize queries
987 // input queries are specified as a list of {'query_uuid': <query_uuid> }
988 // each plugin is responsible for managing its spinner through on_query_in_progress
989 asynchroneous_exec : function (query_exec_tuples) {
991 // Loop through input array, and use publish_uuid to publish back results
992 $.each(query_exec_tuples, function(index, tuple) {
993 var query=manifold.find_query(tuple.query_uuid);
994 var query_json=JSON.stringify (query);
995 var publish_uuid=tuple.publish_uuid;
996 // by default we publish using the same uuid of course
997 if (publish_uuid==undefined) publish_uuid=query.query_uuid;
998 if (manifold.pubsub_debug) {
999 messages.debug("sending POST on " + manifold.proxy_url + query.__repr());
1002 query.iter_subqueries(function (sq) {
1003 manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
1006 // not quite sure what happens if we send a string directly, as POST data is named..
1007 // this gets reconstructed on the proxy side with ManifoldQuery.fill_from_POST
1008 $.post(manifold.proxy_url, {'json':query_json},
1009 manifold.success_closure(query, publish_uuid, tuple.callback));
1014 * \brief Forward a query to the manifold backend
1015 * \param query (dict) the query to be executed asynchronously
1016 * \param callback (function) the function to be called when the query terminates
1018 forward: function(query, callback) {
1019 var query_json = JSON.stringify(query);
1020 $.post(manifold.proxy_url, {'json': query_json} ,
1021 manifold.success_closure(query, query.query_uuid, callback));
1025 * Returns whether a query expects a unique results.
1026 * This is the case when the filters contain a key of the object
1027 * \fn query_expects_unique_result(query)
1028 * \memberof Manifold
1029 * \param ManifoldQuery query Query for which we are testing whether it expects a unique result
1031 query_expects_unique_result: function(query) {
1032 /* XXX we need functions to query metadata */
1033 //var keys = MANIFOLD_METADATA[query.object]['keys']; /* array of array of field names */
1034 /* TODO requires keys in metadata */
1040 * \fn publish_result(query, results)
1041 * \memberof Manifold
1042 * \param ManifoldQuery query Query which has received results
1043 * \param array results results corresponding to query
1045 publish_result: function(query, result) {
1046 if (typeof result === 'undefined')
1050 manifold.raise_record_event(query.query_uuid, CLEAR_RECORDS);
1051 if (manifold.pubsub_debug)
1052 messages.debug(".. publish_result (1) ");
1054 $.each(result, function(i, record) {
1055 manifold.raise_record_event(query.query_uuid, NEW_RECORD, record);
1058 if (manifold.pubsub_debug)
1059 messages.debug(".. publish_result (2) has used NEW API on " + count + " records");
1060 manifold.raise_record_event(query.query_uuid, DONE);
1061 if (manifold.pubsub_debug)
1062 messages.debug(".. publish_result (3) has used NEW API to say DONE");
1064 // OLD PLUGIN API BELOW
1065 /* Publish an update announce */
1066 var channel="/results/" + query.query_uuid + "/changed";
1067 if (manifold.pubsub_debug)
1068 messages.debug(".. publish_result (4) OLD API on channel" + channel);
1070 $.publish(channel, [result, query]);
1072 if (manifold.pubsub_debug)
1073 messages.debug(".. publish_result (5) END q=" + query.__repr());
1076 store_records: function(query, records) {
1078 var query_ext = manifold.query_store.find_analyzed_query_ext(query.query_uuid);
1079 if (query_ext.set_query_ext) {
1080 // We have a domain query
1081 // The results are stored in the corresponding set_query
1082 manifold.query_store.set_records(query_ext.set_query_ext.query.query_uuid, records);
1084 } else if (query_ext.domain_query_ext) {
1085 // We have a set query, it is only used to determine which objects are in the set, we should only retrieve the key
1086 // Has it a domain query, and has it completed ?
1087 $.each(records, function(i, record) {
1088 var key = manifold.metadata.get_key(query.object);
1089 var record_key = manifold.record_get_value(record, key);
1090 manifold.query_store.set_record_state(query.query_uuid, record_key, STATE_SET, STATE_SET_IN);
1094 // We have a normal query
1095 manifold.query_store.set_records(query.query_uuid, records, STATE_SET_IN);
1100 * Recursively publish result
1101 * \fn publish_result_rec(query, result)
1102 * \memberof Manifold
1103 * \param ManifoldQuery query Query which has received result
1104 * \param array result result corresponding to query
1106 * Note: this function works on the analyzed query
1108 publish_result_rec: function(query, records) {
1109 /* If the result is not unique, only publish the top query;
1110 * otherwise, publish the main object as well as subqueries
1111 * XXX how much recursive are we ?
1113 if (manifold.pubsub_debug)
1114 messages.debug (">>>>> publish_result_rec " + query.object);
1115 if (manifold.query_expects_unique_result(query)) {
1116 /* Also publish subqueries */
1117 $.each(query.subqueries, function(object, subquery) {
1118 manifold.publish_result_rec(subquery, records[0][object]);
1119 /* TODO remove object from result */
1122 if (manifold.pubsub_debug)
1123 messages.debug ("===== publish_result_rec " + query.object);
1125 var query_ext = manifold.query_store.find_analyzed_query_ext(query.query_uuid);
1126 query_ext.query_state = QUERY_STATE_DONE;
1128 this.store_records(query, records);
1132 if (query_ext.set_query_ext) {
1133 if (query_ext.set_query_ext.query_state != QUERY_STATE_DONE)
1135 pub_query = query_ext.set_query_ext.query;
1136 } else if (query_ext.domain_query_ext) {
1137 if (query_ext.domain_query_ext.query_state != QUERY_STATE_DONE)
1143 // We can only publish results if the query (and its related domain query) is complete
1144 manifold.publish_result(pub_query, records);
1146 if (manifold.pubsub_debug)
1147 messages.debug ("<<<<< publish_result_rec " + query.object);
1150 setup_update_query: function(query, records)
1152 // We don't prepare an update query if the result has more than 1 entry
1153 if (records.length != 1)
1155 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1157 var record = records[0];
1159 var update_query_ext = query_ext.update_query_ext;
1161 if (!update_query_ext)
1164 var update_query = update_query_ext.query;
1165 var update_query_ext = query_ext.update_query_ext;
1166 var update_query_orig = query_ext.update_query_orig_ext.query;
1168 // Testing whether the result has subqueries (one level deep only)
1169 // iif the query has subqueries
1171 var obj = query.analyzed_query.subqueries;
1172 for (method in obj) {
1173 if (obj.hasOwnProperty(method)) {
1174 var key = manifold.metadata.get_key(method);
1178 var subrecords = record[method];
1181 $.each(subrecords, function (i, subrecord) {
1182 sq_keys.push(manifold.record_get_value(subrecord, key));
1184 update_query.params[method] = sq_keys;
1185 update_query_orig.params[method] = sq_keys.slice();
1191 update_query_ext.disabled = false;
1192 update_query_orig_ext.disabled = false;
1196 process_get_query_records: function(query, records) {
1197 this.setup_update_query(query, records);
1199 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1200 query_ext.query_state = QUERY_STATE_DONE;
1202 /* Publish full results */
1203 var tmp_query = manifold.query_store.find_analyzed_query(query.query_uuid);
1204 manifold.publish_result_rec(tmp_query, records);
1207 make_records: function(object, records)
1209 $.each(records, function(i, record) {
1210 manifold.make_record(object, record);
1214 make_record: function(object, record)
1216 // To make an object a record, we just add the hash function
1217 var key, new_object;
1219 if (object.indexOf(':') != -1) {
1220 new_object = object.split(':')[1];
1222 new_object = object;
1225 key = manifold.metadata.get_key(new_object);
1226 record.hashCode = manifold.record_hashcode(key.sort());
1227 record.equals = manifold.record_equals(key);
1229 // Looking after subrecords
1230 for (var field in record) {
1231 var result_value = record[field];
1233 switch (this.get_type(result_value)) {
1235 var subobject = manifold.metadata.get_type(object, field);
1236 // if (subobject) XXX Bugs with fields declared string while they are not : network.version is a dict in fact
1237 if (subobject && subobject != 'string')
1238 manifold.make_record(subobject, result_value);
1240 case TYPE_LIST_OF_RECORDS:
1241 var subobject = manifold.metadata.get_type(object, field);
1243 manifold.make_records(subobject, result_value);
1251 * What we need to do when receiving results from an update query:
1252 * - differences between what we had, what we requested, and what we obtained
1253 * . what we had : update_query_orig (simple fields and set fields managed differently)
1254 * . what we requested : update_query
1255 * . what we received : records
1256 * - raise appropriate events
1258 * The normal process is that results similar to Get will be pushed in the
1259 * pubsub mechanism, thus repopulating everything while we only need
1260 * diff's. This means we need to move the publish functionalities in the
1261 * previous 'process_get_query_records' function.
1263 process_update_query_records: function(query, records) {
1264 // First issue: we request everything, and not only what we modify, so will will have to ignore some fields
1265 var query_uuid = query.query_uuid;
1266 var query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
1267 var update_query = query_ext.main_query_ext.update_query_ext.query;
1268 var update_query_orig = query_ext.main_query_ext.update_query_orig_ext.query;
1270 // Since we update objects one at a time, we can get the first record
1271 var record = records[0];
1273 // Let's iterate over the object properties
1274 for (var field in record) {
1275 var result_value = record[field];
1276 switch (this.get_type(result_value)) {
1278 // Did we ask for a change ?
1279 var update_value = update_query[field];
1281 // Not requested, if it has changed: OUT OF SYNC
1282 // How we can know ?
1283 // We assume it won't have changed
1287 throw "Internal error";
1293 value : (update_value == result_value) ? STATE_VALUE_CHANGE_SUCCESS : STATE_VALUE_CHANGE_FAILURE,
1295 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1299 throw "Not implemented";
1303 case TYPE_LIST_OF_VALUES:
1304 // Same as list of records, but we don't have to extract keys
1306 // The rest of exactly the same (XXX factorize)
1307 var update_keys = update_query_orig.params[field];
1308 var query_keys = update_query.params[field];
1309 var added_keys = $.grep(query_keys, function (x) { return $.inArray(x, update_keys) == -1 });
1310 var removed_keys = $.grep(update_keys, function (x) { return $.inArray(x, query_keys) == -1 });
1313 $.each(added_keys, function(i, key) {
1314 if ($.inArray(key, result_value) == -1) {
1316 request: FIELD_REQUEST_ADD,
1319 status: FIELD_REQUEST_FAILURE,
1323 request: FIELD_REQUEST_ADD,
1326 status: FIELD_REQUEST_SUCCESS,
1329 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1331 $.each(removed_keys, function(i, key) {
1332 if ($.inArray(key, result_keys) == -1) {
1334 request: FIELD_REQUEST_REMOVE,
1337 status: FIELD_REQUEST_SUCCESS,
1341 request: FIELD_REQUEST_REMOVE,
1344 status: FIELD_REQUEST_FAILURE,
1347 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1353 case TYPE_LIST_OF_VALUES: // XXX Until fixed
1354 case TYPE_LIST_OF_RECORDS:
1355 var key, new_state, cur_query_uuid;
1357 cur_query_uuid = query.analyzed_query.subqueries[field].query_uuid;
1359 // example: slice.resource
1360 // - update_query_orig.params.resource = resources in slice before update
1361 // - update_query.params.resource = resource requested in slice
1362 // - keys from field = resources obtained
1364 if (field == 'lease') {
1365 // lease_id has been added to be repeated when
1366 // constructing request rspec. We don't want it for
1368 key = ['start_time', 'end_time', 'resource'];
1370 key = manifold.metadata.get_key(field);
1375 if (key.length > 1) {
1376 throw "Not implemented";
1382 /* XXX should be modified for multiple keys */
1383 var result_keys = $.map(record[field], function(x) { return manifold.record_get_value(x, key); });
1385 // XXX All this could be deduced from record state : STATE_IN_PENDING and STATE_OUT_PENDING
1386 // what we had at the begining
1387 var update_keys = update_query_orig.params[field];
1389 var query_keys = update_query.params[field];
1390 // what we added and removed
1391 var added_keys = $.grep(query_keys, function (x) { return (!(manifold._in_array(x, update_keys, key))); });
1392 var removed_keys = $.grep(update_keys, function (x) { return (!(manifold._in_array(x, query_keys, key))); });
1394 // Send events related to parent query
1395 $.each(added_keys, function(i, added_key) {
1396 new_state = (manifold._in_array(added_key, result_keys, key)) ? STATE_SET_IN_SUCCESS : STATE_SET_IN_FAILURE;
1398 // Update record state for children queries
1399 manifold.query_store.set_record_state(cur_query_uuid, added_key, STATE_SET, new_state);
1401 // XXX This could be optimized
1402 manifold.query_store.recount(cur_query_uuid);
1404 data = { state: STATE_SET, key : field, op : new_state, value: added_key }
1405 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1407 // Inform subquery also
1409 manifold.raise_record_event(cur_query_uuid, FIELD_STATE_CHANGED, data);
1410 // XXX Passing no parameters so that they can redraw everything would
1411 // be more efficient but is currently not supported
1412 // XXX We could also need to inform plugins about nodes IN (not pending) that are no more, etc.
1413 // XXX refactor all this when suppressing update_queries, and relying on state instead !
1415 $.each(removed_keys, function(i, removed_key) {
1416 new_state = (manifold._in_array(removed_key, result_keys, key)) ? STATE_SET_OUT_FAILURE : STATE_SET_OUT_SUCCESS;
1418 // Update record state for children queries
1419 manifold.query_store.set_record_state(cur_query_uuid, removed_key, STATE_SET, new_state);
1421 // XXX This could be optimized
1422 manifold.query_store.recount(cur_query_uuid);
1424 data = { state: STATE_SET, key : field, op : new_state, value: removed_key }
1425 manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1427 // Inform subquery also
1429 manifold.raise_record_event(cur_query_uuid, FIELD_STATE_CHANGED, data);
1436 // XXX Now we need to adapt 'update' and 'update_orig' queries as if we had done a get
1437 this.setup_update_query(query, records);
1439 var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1440 query_ext.query_state = QUERY_STATE_DONE;
1443 // Send DONE message to plugins
1444 query.iter_subqueries(function(sq, data, parent_query) {
1445 manifold.raise_record_event(sq.query_uuid, DONE);
1450 process_query_records: function(query, records) {
1451 if (query.action == 'get') {
1452 this.process_get_query_records(query, records);
1453 } else if (query.action == 'update') {
1454 this.process_update_query_records(query, records);
1458 // if set callback is provided it is called
1459 // most of the time publish_uuid will be query.query_uuid
1460 // however in some cases we wish to publish the result under a different uuid
1461 // e.g. an updater wants to publish its result as if from the original (get) query
1462 asynchroneous_success : function (data, query, publish_uuid, callback) {
1463 // xxx should have a nicer declaration of that enum in sync with the python code somehow
1465 var start = new Date();
1466 if (manifold.asynchroneous_debug)
1467 messages.debug(">>>>>>>>>> asynchroneous_success query.object=" + query.object);
1469 if (data.code == 2) { // ERROR
1470 // We need to make sense of error codes here
1471 alert("Your session has expired, please log in again");
1472 localStorage.removeItem('user');
1473 window.location="/logout/";
1474 if (manifold.asynchroneous_debug) {
1475 duration=new Date()-start;
1476 messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- error returned - logging out " + duration + " ms");
1480 if (data.code == 1) { // WARNING
1481 messages.error("Some errors have been received from the manifold backend at " + MANIFOLD_URL + " [" + data.description + "]");
1482 // publish error code and text message on a separate channel for whoever is interested
1484 $.publish("/results/" + publish_uuid + "/failed", [data.code, data.description] );
1488 // If a callback has been specified, we redirect results to it
1491 if (manifold.asynchroneous_debug) {
1492 duration=new Date()-start;
1493 messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- callback ended " + duration + " ms");
1498 if (manifold.asynchroneous_debug)
1499 messages.debug ("========== asynchroneous_success " + query.object + " -- before process_query_records [" + query.query_uuid +"]");
1501 // once everything is checked we can use the 'value' part of the manifoldresult
1502 var result=data.value;
1504 /* Eventually update the content of related queries (update, etc) */
1505 manifold.make_records(query.object, result);
1506 this.process_query_records(query, result);
1508 /* Publish results: disabled here, done in the previous call */
1509 //tmp_query = manifold.find_query(query.query_uuid);
1510 //manifold.publish_result_rec(tmp_query.analyzed_query, result);
1512 if (manifold.asynchroneous_debug) {
1513 duration=new Date()-start;
1514 messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- done " + duration + " ms");
1519 /**************************************************************************
1520 * Plugin API helpers
1521 **************************************************************************/
1523 raise_event_handler: function(type, query_uuid, event_type, value) {
1524 if (manifold.pubsub_debug)
1525 messages.debug("raise_event_handler, quuid="+query_uuid+" type="+type+" event_type="+event_type);
1526 if ((type != 'query') && (type != 'record'))
1527 throw 'Incorrect type for manifold.raise_event()';
1528 // xxx we observe quite a lot of incoming calls with an undefined query_uuid
1529 // this should be fixed upstream in manifold I expect
1530 if (query_uuid === undefined) {
1531 messages.warning("undefined query in raise_event_handler");
1535 // notify the change to objects that either listen to this channel specifically,
1536 // or to the wildcard channel
1537 var channels = [ manifold.get_channel(type, query_uuid), manifold.get_channel(type, '*') ];
1539 $.each(channels, function(i, channel) {
1540 if (value === undefined) {
1541 if (manifold.pubsub_debug) messages.debug("triggering [no value] on channel="+channel+" and event_type="+event_type);
1542 $('.pubsub').trigger(channel, [event_type]);
1544 if (manifold.pubsub_debug) messages.debug("triggering [value="+value+"] on channel="+channel+" and event_type="+event_type);
1545 $('.pubsub').trigger(channel, [event_type, value]);
1550 raise_query_event: function(query_uuid, event_type, value) {
1551 manifold.raise_event_handler('query', query_uuid, event_type, value);
1554 raise_record_event: function(query_uuid, event_type, value) {
1555 manifold.raise_event_handler('record', query_uuid, event_type, value);
1559 * Event handler helpers
1561 _get_next_state_add: function(prev_state)
1563 switch (prev_state) {
1565 case STATE_SET_OUT_SUCCESS:
1566 case STATE_SET_IN_FAILURE:
1567 new_state = STATE_SET_IN_PENDING;
1570 case STATE_SET_OUT_PENDING:
1571 new_state = STATE_SET_IN;
1575 case STATE_SET_IN_PENDING:
1576 case STATE_SET_IN_SUCCESS:
1577 case STATE_SET_OUT_FAILURE:
1578 console.log("Inconsistent state: already in");
1584 _get_next_state_remove: function(prev_state)
1586 switch (prev_state) {
1588 case STATE_SET_IN_SUCCESS:
1589 case STATE_SET_OUT_FAILURE:
1590 new_state = STATE_SET_OUT_PENDING;
1593 case STATE_SET_IN_PENDING:
1594 new_state = STATE_SET_OUT;
1598 case STATE_SET_OUT_PENDING:
1599 case STATE_SET_OUT_SUCCESS:
1600 case STATE_SET_IN_FAILURE:
1601 console.log("Inconsistent state: already out");
1607 _grep_active_lease_callback: function(lease_query, resource_key) {
1608 return function(lease_key_lease) {
1609 var state, lease_key, lease;
1611 lease_key = lease_key_lease[0];
1612 lease = lease_key_lease[1];
1614 if (lease['resource'] != resource_key)
1617 state = manifold.query_store.get_record_state(lease_query.query_uuid, lease_key, STATE_SET);;
1620 case STATE_SET_IN_PENDING:
1621 case STATE_SET_IN_SUCCESS:
1622 case STATE_SET_OUT_FAILURE:
1625 case STATE_SET_OUT_PENDING:
1626 case STATE_SET_OUT_SUCCESS:
1627 case STATE_SET_IN_FAILURE:
1633 _enforce_constraints: function(query_ext, record, record_key, event_type)
1637 query = query_ext.query;
1639 switch(query.object) {
1642 // CONSTRAINT_RESERVABLE_LEASE
1644 // +) If a reservable node is added to the slice, then it should have a corresponding lease
1645 // XXX Not always a resource
1646 var is_reservable = (record.exclusive == true);
1647 if (is_reservable) {
1648 var warnings = manifold.query_store.get_record_state(query.query_uuid, record_key, STATE_WARNINGS);
1650 if (event_type == STATE_SET_ADD) {
1651 // We should have a lease_query associated
1652 var lease_query = query_ext.parent_query_ext.query.subqueries['lease']; // in options
1653 var lease_query_ext = manifold.query_store.find_analyzed_query_ext(lease_query.query_uuid);
1654 // Do we have lease records (in) with this resource
1655 var lease_records = $.grep(lease_query_ext.records.entries(), this._grep_active_lease_callback(lease_query, record_key));
1656 if (lease_records.length == 0) {
1658 // XXX Need for a better function to manage warnings
1659 var warn = CONSTRAINT_RESERVABLE_LEASE_MSG;
1660 warnings[CONSTRAINT_RESERVABLE_LEASE] = warn;
1662 // Lease are defined, delete the warning in case it was set previously
1663 delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1666 // Remove warnings attached to this resource
1667 delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1670 manifold.query_store.set_record_state(query.query_uuid, record_key, STATE_WARNINGS, warnings);
1673 /* This was redundant */
1674 // manifold.query_store.recount(query.query_uuid);
1676 // Signal the change to plugins (even if the constraint does not apply, so that the plugin can display a checkmark)
1678 state: STATE_WARNINGS,
1683 manifold.raise_record_event(query.query_uuid, FIELD_STATE_CHANGED, data);
1687 var resource_key = record_key.resource;
1688 var resource_query = query_ext.parent_query_ext.query.subqueries['resource'];
1689 var warnings = manifold.query_store.get_record_state(resource_query.query_uuid, resource_key, STATE_WARNINGS);
1691 if (event_type == STATE_SET_ADD) {
1692 // A lease is added, it removes the constraint
1693 delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1695 // A lease is removed, it might trigger the warning
1696 var lease_records = $.grep(query_ext.records.entries(), this._grep_active_lease_callback(query, resource_key));
1697 if (lease_records.length == 0) { // XXX redundant cases
1699 // XXX Need for a better function to manage warnings
1700 var warn = CONSTRAINT_RESERVABLE_LEASE_MSG;
1701 warnings[CONSTRAINT_RESERVABLE_LEASE] = warn;
1703 // Lease are defined, delete the warning in case it was set previously
1704 delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1709 manifold.query_store.recount(resource_query.query_uuid);
1711 // Signal the change to plugins (even if the constraint does not apply, so that the plugin can display a checkmark)
1713 state: STATE_WARNINGS,
1718 manifold.raise_record_event(resource_query.query_uuid, FIELD_STATE_CHANGED, data);
1722 // -) When a lease is added, it might remove the warning associated to a reservable node
1724 // If a NITOS node is reserved, then at least a NITOS channel should be reserved
1725 // - When a NITOS channel is added, it might remove a warning associated to all NITOS nodes
1727 // If a NITOS channel is reserved, then at least a NITOS node should be reserved
1728 // - When a NITOS node is added, it might remove a warning associated to all NITOS channels
1730 // A lease is present while the resource has been removed => Require warnings on nodes not in set !
1734 _get_query_path: function(query_ext) {
1737 while (sq.parent_query_ext) {
1740 path = sq.query.object + path;
1741 sq = sq.parent_query_ext;
1748 * Handling events raised by plugins
1750 raise_event: function(query_uuid, event_type, data)
1752 var query, query_ext;
1754 // Query uuid has been updated with the key of a new element
1755 query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
1756 query = query_ext.query;
1758 switch(event_type) {
1760 // XXX At some point, should be renamed to RECORD_STATE_CHANGED
1761 case FIELD_STATE_CHANGED:
1763 // value is an object (request, key, value, status)
1764 // update is only possible is the query is not pending, etc
1765 // SET_ADD is on a subquery, FIELD_STATE_CHANGED on the query itself
1766 // we should map SET_ADD on this...
1768 // 1. Update internal query store about the change in status
1770 // 2. Update the update query
1771 update_query = query_ext.main_query_ext.update_query_ext.query;
1772 update_query_orig = query_ext.main_query_ext.update_query_orig_ext.query;
1774 switch(data.state) {
1779 /* Set parameter data.key in the update_query to VALUE */
1780 if (update_query.params[data.key] === undefined)
1781 update_query.params[data.key] = Array();
1782 update_query.params[data.key] = value.value;
1789 var prev_state, new_state;
1790 var main_query, record, new_data, path;
1792 // We only track state in the analyzed query
1793 prev_state = manifold.query_store.get_record_state(query_uuid, data.value, STATE_SET);
1794 if (prev_state === null)
1795 prev_state = STATE_SET_OUT;
1799 new_state = this._get_next_state_add(prev_state);
1801 /* data.value containts the resource key */
1802 manifold.query_store.add_record(query_uuid, data.value, new_state);
1803 record = manifold.query_store.get_record(query_uuid, data.value);
1804 this._enforce_constraints(query_ext, record, data.value, STATE_SET_ADD);
1806 /* Process update query in parent */
1807 path = this._get_query_path(query_ext);
1808 if (update_query.params[path] === undefined)
1809 update_query.params[path] = Array();
1810 update_query.params[path].push(data.value);
1814 case STATE_SET_REMOVE:
1815 new_state = this._get_next_state_remove(prev_state);
1817 /* data.value contains the resource key */
1818 manifold.query_store.remove_record(query_uuid, data.value, new_state);
1819 record = manifold.query_store.get_record(query_uuid, data.value);
1820 this._enforce_constraints(query_ext, record, data.value, STATE_SET_REMOVE);
1822 /* Process update query in parent */
1823 path = this._get_query_path(query_ext);
1824 arr = update_query.params[path];
1826 var key = manifold.metadata.get_key(query.object);
1827 arr = $.grep(arr, function(x) { return (!(manifold._record_equals(x, data.value, key))); });
1828 if (update_query.params[path] === undefined)
1829 update_query.params[path] = Array();
1830 update_query.params[path] = arr;
1834 /* Inform the parent query: important for update */
1841 main_query = query_ext.main_query_ext.query;
1842 manifold.raise_record_event(main_query.query_uuid, event_type, new_data);
1843 /* Propagate the event to other plugins subscribed to the query */
1844 manifold.query_store.recount(query_uuid);
1846 manifold.raise_record_event(query_uuid, event_type, new_data);
1851 // 3. Inform others about the change
1852 // a) the main query...
1853 manifold.raise_record_event(query_uuid, event_type, data);
1855 // b) subqueries eventually (dot in the key)
1858 var cur_query = query;
1859 if (cur_query.analyzed_query)
1860 cur_query = cur_query.analyzed_query;
1863 var path_array = data.key.split('.');
1864 var value_key = data.key.split('.');
1865 $.each(path_array, function(i, method) {
1866 cur_query = cur_query.subqueries[method];
1867 value_key.shift(); // XXX check that method is indeed shifted
1869 data.key = value_key;
1871 manifold.raise_record_event(cur_query.query_uuid, event_type, data);
1877 manifold.run_query(query_ext.main_query_ext.update_query_ext.query);
1880 /* QUERY STATE CHANGED */
1885 console.log("FILTER ADDED", data);
1886 /* Update internal record state */
1887 manifold.query_store.add_filter(query_uuid, data);
1889 /* Propagate the message to plugins */
1890 manifold.raise_query_event(query_uuid, event_type, data);
1894 case FILTER_REMOVED:
1895 console.log("FILTER REMOVED", data);
1896 /* Update internal record state */
1897 manifold.query_store.remove_filter(query_uuid, data);
1899 /* Propagate the message to plugins */
1900 manifold.raise_query_event(query_uuid, event_type, data);
1905 main_query = query_ext.main_query_ext.query;
1906 main_update_query = query_ext.main_query_ext.update_query;
1909 // Here we need the full path through all subqueries
1911 // XXX We might need the query name in the QueryExt structure
1912 main_query.select(data);
1914 // XXX When is an update query associated ?
1915 // XXX main_update_query.select(value);
1917 manifold.raise_query_event(query_uuid, event_type, data);
1921 query = query_ext.query;
1922 main_query = query_ext.main_query_ext.query;
1923 main_update_query = query_ext.main_query_ext.update_query;
1924 query.unselect(data);
1925 main_query.unselect(data);
1927 // We need to inform about changes in these queries to the respective plugins
1928 // Note: query & main_query have the same UUID
1929 manifold.raise_query_event(query_uuid, event_type, data);
1932 // We need to inform about changes in these queries to the respective plugins
1933 // Note: query, main_query & update_query have the same UUID
1935 // http://trac.myslice.info/ticket/32
1936 // Avoid multiple calls to the same event
1937 //manifold.raise_query_event(query_uuid, event_type, value);
1939 // We are targeting the same object with get and update
1940 // The notion of query is bad, we should have a notion of destination, and issue queries on the destination
1941 // NOTE: Editing a subquery == editing a local view on the destination
1943 // XXX We might need to run the new query again and manage the plugins in the meantime with spinners...
1944 // For the time being, we will collect all columns during the first query
1947 /* Publish/subscribe channels for internal use */
1948 get_channel: function(type, query_uuid) {
1949 if ((type !== 'query') && (type != 'record'))
1951 return '/' + type + '/' + query_uuid;
1954 }; // manifold object
1955 /* ------------------------------------------------------------ */
1959 // OLD PLUGIN API: extend jQuery/$ with pubsub capabilities
1960 // https://gist.github.com/661855
1962 $.subscribe = function( channel, selector, data, fn) {
1963 /* borrowed from jQuery */
1964 if ( data == null && fn == null ) {
1967 data = selector = undefined;
1968 } else if ( fn == null ) {
1969 if ( typeof selector === "string" ) {
1970 // ( channel, selector, fn )
1974 // ( channel, data, fn )
1977 selector = undefined;
1982 /* We use an indirection function that will clone the object passed in
1983 * parameter to the subscribe callback
1985 * FIXME currently we only clone query objects which are the only ones
1986 * supported and editable, we might have the same issue with results but
1987 * the page load time will be severely affected...
1989 o.on.apply(o, [channel, selector, data, function() {
1990 for(i = 1; i < arguments.length; i++) {
1991 if ( arguments[i].constructor.name == 'ManifoldQuery' )
1992 arguments[i] = arguments[i].clone();
1994 fn.apply(o, arguments);
1998 $.unsubscribe = function() {
1999 o.off.apply(o, arguments);
2002 $.publish = function() {
2003 o.trigger.apply(o, arguments);
2008 /* ------------------------------------------------------------ */
2010 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
2011 //make sure to expose csrf in our outcoming ajax/post requests
2013 beforeSend: function(xhr, settings) {
2014 function getCookie(name) {
2015 var cookieValue = null;
2016 if (document.cookie && document.cookie != '') {
2017 var cookies = document.cookie.split(';');
2018 for (var i = 0; i < cookies.length; i++) {
2019 var cookie = jQuery.trim(cookies[i]);
2020 // Does this cookie string begin with the name we want?
2021 if (cookie.substring(0, name.length + 1) == (name + '=')) {
2022 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
2029 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
2030 // Only send the token to relative URLs i.e. locally.
2031 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));