1 // Helper functions for asynchronous requests
3 var api_url = '/manifold/api/json/'
4 var api_render_url = '/manifold/render/json'
6 function manifold_array_size(obj) {
9 if (obj.hasOwnProperty(key)) size++;
14 // Executes all async. queries contained in manifold_async_query_array, which is
15 // an array of hash (action, method, ts, filter, fields)
17 function manifold_async_exec(arr)
19 console.log('manifold_async_exec length='+ arr.length);
21 // xxx todo - I don't have the spinner jquery plugin yet
22 // jQuery('.loading').spin();
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);}
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));
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();
44 /* what the hell is this doing here ?
45 function apply_format(key, value, type, method) {
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'},
68 // creates problems sorting ?
69 if (link[type]['_all']) {
70 var urlpart = link[type]['_all'];
72 if (link[type][method]) {
73 var urlpart = link[type][method];
75 if (link[type]['_other']) {
76 var urlpart = link[type]['_other'];
82 return '<a href="/view/' + urlpart + '/' + key +'">' + value + '</a>';
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>';
99 function manifold_html_li(type, value, 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>";
108 return "<li>" + value + "</li>";
113 function manifold_html_ul(data, key, value, type, method, is_cached) {
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);
124 function manifold_async_render_list(data, method, is_cached) {
125 // we suppose we only have one column, or we need more precisions
127 if (manifold_array_size(data[0]) == 1) {
128 for (var k in data[0]) {
133 for (var k in data[0]) {
134 if (k.substr(-4) == '_hrn') {
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);
145 //onObjectAvailable('Spinners', function(){ Spinners.get(element).remove(); }, this, true);
146 jQuery('.loading').spin();
151 function manifold_update_template(data)
153 jQuery.each(data, function(key, value) {
154 if ((typeof value == 'string') || (typeof value == 'number') || (typeof value == 'boolean')) {
156 jQuery('#manifold__' + key).html(value);
157 } else if (value == null) {
158 jQuery('#manifold__' + key).html("N/A");
160 manifold_update_table('#manifold__' + key, value);
165 function manifold_async_success(data, query, id) {
169 /* Directly inform the requestor */
170 jQuery('#' + id).trigger('results', [data]);
172 /* Publish an update announce */
173 jQuery.publish("/results/" + query.uuid + "/changed", [data, query]);
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);
184 function __old__manifold_async_render_success(data, query) {
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);
193 /* Publish an update announce */
194 jQuery.publish("/rendering/changed", [data, query]);
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);