ae35630eb5e410d639171622482cc51e5e27c047
[unfold.git] / engine / static / js / manifold-async.js
1 // Helper functions for asynchronous requests
2
3 var api_url = '/manifold/api/json/'
4 var api_render_url = '/manifold/render/json'
5
6 function manifold_array_size(obj) {
7     var size = 0, key;
8     for (key in obj) {
9         if (obj.hasOwnProperty(key)) size++;
10     }
11     return size;
12 };
13
14 // Executes all async. queries contained in manifold_async_query_array, which is
15 // an array of hash (action, method, ts, filter, fields)
16 //
17 function manifold_async_exec(arr)
18 {
19     console.log('manifold_async_exec length='+ arr.length);
20     // start spinners
21     // xxx todo - I don't have the spinner jquery plugin yet
22 //    jQuery('.loading').spin();
23
24     // We use js function closure to be able to pass the query (array) to the
25     // callback function used when data is received
26     var manifold_async_success_closure = function(query, id) {
27         return function(data, textStatus) {manifold_async_success(data, query, id);}
28     };
29
30     // Loop through query array and issue XML/RPC queries
31     jQuery.each(arr, function(index, elt) {
32         console.log ('sending POST on ' + api_url + " iterating on " + elt);
33         jQuery.post(api_url, {'query': elt.query.to_hash()}, manifold_async_success_closure(elt.query, elt.id));
34     })
35 }
36
37 function manifold_async_error(str) {
38     var out = '<div class="error"><h2>Error</h2><dl id="system-message"><dt class="error">Notice</dt><dd class="error message"><ul><li>' + jQuery('<div />').text(str).html() + '</li></ul></dd></dl></div>';
39     jQuery('#manifold_message').html(out);
40     //onObjectAvailable('Spinners', function(){ Spinners.get('.loading').remove(); }, this, true);
41     jQuery('.loading').spin();
42 }
43
44 /* what the hell is this doing here ?
45 function apply_format(key, value, type, method) {
46     // type = type, key = 
47     var link = {
48         'platform': {'_all': 'platforms'},
49         'src_hostname': {'traceroute': 'agents', '_other': 'hostname'},
50         'dst_hostname': {'traceroute': 'agents', '_other': 'hostname'},
51         'src_ip': {'traceroute': 'agents', '_other': 'ip'},
52         'dst_ip': {'traceroute': 'agents', '_other': 'ip'},
53         'as_name': {'_all': 'as'},
54         'asn': {'_all': 'as'},
55         'city': {'_all': 'cities'},
56         'continent': {'_all': 'continents'},
57         'continent_code': {'_all': 'continents'},
58         'country': {'_all': 'countries'},
59         'country_code': {'_all': 'countries'},
60         'hostname': {'agents': 'agents', 'nodes': 'node', '_other': 'hostname'},
61         'ip': {'agents': 'agents', '_other': 'ip'},
62         'network_hrn': {'_all': 'network'},
63         'region': {'_all': 'regions'},
64         'region_code': {'_all': 'regions'},
65         'slice_hrn': {'_all': 'slice'},
66     };
67     if (link[type]) {
68         // creates problems sorting ?
69         if (link[type]['_all']) {
70             var urlpart = link[type]['_all'];
71         } else {
72             if (link[type][method]) {
73                 var urlpart = link[type][method];
74             } else {
75                 if (link[type]['_other']) {
76                     var urlpart = link[type]['_other'];
77                 } else {
78                     return key;
79                 }
80             }
81         }
82         return '<a href="/view/' + urlpart + '/' + key +'">' + value + '</a>';
83     } else {
84         return key;
85     }
86 }
87 */
88
89 function manifold_html_a(key, value, type) {
90     if (type == 'network_hrn') {
91         return "<a href='/view/network/" + key + "'>" + value + '</a>';
92     } else if (type == 'slice_hrn') {
93         return "<a href='/view/slice/" + key + "'>" + value + '</a>';
94     } else {
95
96     }
97 }
98
99 function manifold_html_li(type, value, is_cached) {
100     var cached = '';
101     if (is_cached)
102         cached='<div class="cache"><span><b>Cached information from the database</b><br/>Timestamp: XX/XX/XX XX:XX:XX<br/><br/><i>Refresh in progress...</i></span></div>';
103     if (type == 'slice_hrn') {
104         return "<li class='icn icn-play'>" + value + cached + "</li>";
105     } else if (type == 'network_hrn') {
106         return "<li class='icn icn-play'>" + value + cached + "</li>";
107     } else {
108         return "<li>" + value + "</li>";
109     }
110 }
111
112
113 function manifold_html_ul(data, key, value, type, method, is_cached) {
114     var out = "<ul>";
115     for (var i = 0; i < data.length; i++) {
116         out += manifold_html_li(key, apply_format(data[i][key], data[i][value], key, method), is_cached);
117         //out += manifold_html_li(key, manifold_html_a(data[i][key], data[i][value], key), is_cached);
118     }
119     out += "</ul>";
120
121     return out;
122 }
123
124 function manifold_async_render_list(data, method, is_cached) {
125     // we suppose we only have one column, or we need more precisions
126     var col = [];
127     if (manifold_array_size(data[0]) == 1) {
128         for (var k in data[0]) {
129             key = k;
130             value = k;
131         }
132     } else {
133         for (var k in data[0]) {
134             if (k.substr(-4) == '_hrn') {
135                 key = k;
136             } else {
137                 value = k;
138             }
139         }
140     }
141     var out = manifold_html_ul(data, key, value, key, method, is_cached);
142     var element = '#manifold__list__' + key + '__' + value;
143     jQuery(element).html(out);
144     // FIXME spinners
145     //onObjectAvailable('Spinners', function(){ Spinners.get(element).remove(); }, this, true);
146     jQuery('.loading').spin();
147 }
148
149
150
151 function manifold_update_template(data) 
152 {
153     jQuery.each(data, function(key, value) {
154         if ((typeof value == 'string') || (typeof value == 'number') || (typeof value == 'boolean')) {
155             // Simple field
156             jQuery('#manifold__' + key).html(value);
157         } else if (value == null) {
158             jQuery('#manifold__' + key).html("N/A");
159         } else { 
160             manifold_update_table('#manifold__' + key, value);
161         }
162     });
163 }
164
165 function manifold_async_success(data, query, id) {
166     if (data) {
167
168         if (!!id) {
169             /* Directly inform the requestor */
170             jQuery('#' + id).trigger('results', [data]);
171         } else {
172             /* Publish an update announce */
173             jQuery.publish("/results/" + query.uuid + "/changed", [data, query]);
174         }
175
176         // Is there a linked query ?
177         //if ((query.done == 'now') && (query.ts == 'latest')) {
178         //    var new_query = [query_json.replace("latest", "now")];
179         //    manifold_async_exec(new_query);
180         //}
181     }
182 }
183
184 function __old__manifold_async_render_success(data, query) {
185     if (data) {
186
187         // We loop through all the fields to update the corresponding
188         // locations in the page
189         if (typeof(data[0].error) != 'undefined') {
190             manifold_async_error(data[0].error);
191         }
192
193         /* Publish an update announce */
194         jQuery.publish("/rendering/changed", [data, query]);
195
196         // Is there a linked query ?
197         if ((query.done == 'now') && (query.ts == 'latest')) {
198             var new_query = [query_json.replace("latest", "now")];
199             manifold_async_exec(new_query);
200         }
201     }
202 }