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)
21 //onObjectAvailable('Spinners', function(){ Spinners.create('.loading').play(); }, this, true);
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_wrapper = function(query, id) {
27 return function(data, textStatus) {
28 manifold_async_success(data, query, id);
32 // Loop through query array and issue XML/RPC queries
33 jQuery.each(arr, function(index, elt) {
34 // we do rendering by default
35 jQuery.post(api_url, {'query': elt.query.to_hash()}, manifold_async_success_wrapper(elt.query, elt.id));
39 function manifold_async_exec_render(arr)
43 //onObjectAvailable('Spinners', function(){ Spinners.create('.loading').play(); }, this, true);
44 jQuery('.loading').spin();
46 // We use js function closure to be able to pass the query (array) to the
47 // callback function used when data is received
48 var manifold_async_success_wrapper = function(query, id) {
49 return function(data, textStatus) {
50 manifold_async_success(data, query, id);
54 // Loop through query array and issue XML/RPC queries
55 jQuery.each(arr, function(index, elt) {
56 // we do rendering by default
57 jQuery.post(api_render_url, {'query': elt.query.to_hash()}, manifold_async_success_wrapper(elt.query, elt.id));
61 function manifold_async_render(data, query)
63 // We use js function closure to be able to pass the query (array) to the
64 // callback function used when data is received
65 var manifold_async_render_success_wrapper = function(query) {
66 return function(data, textStatus) {
67 manifold_async_render_success(data, query);
71 jQuery.post(api_render_url, {'data': data, 'query': query.to_hash()}, manifold_async_render_success_wrapper(data, query));
74 function manifold_async_error(str) {
75 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>';
76 jQuery('#manifold_message').html(out);
77 //onObjectAvailable('Spinners', function(){ Spinners.get('.loading').remove(); }, this, true);
78 jQuery('.loading').spin();
81 function apply_format(key, value, type, method) {
84 'platform': {'_all': 'platforms'},
85 'src_hostname': {'traceroute': 'agents', '_other': 'hostname'},
86 'dst_hostname': {'traceroute': 'agents', '_other': 'hostname'},
87 'src_ip': {'traceroute': 'agents', '_other': 'ip'},
88 'dst_ip': {'traceroute': 'agents', '_other': 'ip'},
89 'as_name': {'_all': 'as'},
90 'asn': {'_all': 'as'},
91 'city': {'_all': 'cities'},
92 'continent': {'_all': 'continents'},
93 'continent_code': {'_all': 'continents'},
94 'country': {'_all': 'countries'},
95 'country_code': {'_all': 'countries'},
96 'hostname': {'agents': 'agents', 'nodes': 'node', '_other': 'hostname'},
97 'ip': {'agents': 'agents', '_other': 'ip'},
98 'network_hrn': {'_all': 'network'},
99 'region': {'_all': 'regions'},
100 'region_code': {'_all': 'regions'},
101 'slice_hrn': {'_all': 'slice'},
104 // creates problems sorting ?
105 if (link[type]['_all']) {
106 var urlpart = link[type]['_all'];
108 if (link[type][method]) {
109 var urlpart = link[type][method];
111 if (link[type]['_other']) {
112 var urlpart = link[type]['_other'];
118 return '<a href="/view/' + urlpart + '/' + key +'">' + value + '</a>';
124 function manifold_html_a(key, value, type) {
125 if (type == 'network_hrn') {
126 return "<a href='/view/network/" + key + "'>" + value + '</a>';
127 } else if (type == 'slice_hrn') {
128 return "<a href='/view/slice/" + key + "'>" + value + '</a>';
134 function manifold_html_li(type, value, is_cached) {
137 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>';
138 if (type == 'slice_hrn') {
139 return "<li class='icn icn-play'>" + value + cached + "</li>";
140 } else if (type == 'network_hrn') {
141 return "<li class='icn icn-play'>" + value + cached + "</li>";
143 return "<li>" + value + "</li>";
148 function manifold_html_ul(data, key, value, type, method, is_cached) {
150 for (var i = 0; i < data.length; i++) {
151 out += manifold_html_li(key, apply_format(data[i][key], data[i][value], key, method), is_cached);
152 //out += manifold_html_li(key, manifold_html_a(data[i][key], data[i][value], key), is_cached);
159 function manifold_async_render_list(data, method, is_cached) {
160 // we suppose we only have one column, or we need more precisions
162 if (manifold_array_size(data[0]) == 1) {
163 for (var k in data[0]) {
168 for (var k in data[0]) {
169 if (k.substr(-4) == '_hrn') {
176 var out = manifold_html_ul(data, key, value, key, method, is_cached);
177 var element = '#manifold__list__' + key + '__' + value;
178 jQuery(element).html(out);
180 //onObjectAvailable('Spinners', function(){ Spinners.get(element).remove(); }, this, true);
181 jQuery('.loading').spin();
186 function manifold_update_template(data)
188 jQuery.each(data, function(key, value) {
189 if ((typeof value == 'string') || (typeof value == 'number') || (typeof value == 'boolean')) {
191 jQuery('#manifold__' + key).html(value);
192 } else if (value == null) {
193 jQuery('#manifold__' + key).html("N/A");
195 manifold_update_table('#manifold__' + key, value);
200 function manifold_async_success(data, query, id) {
204 /* Directly inform the requestor */
205 jQuery('#' + id).trigger('results', [data]);
207 /* Publish an update announce */
208 jQuery.publish("/results/" + query.uuid + "/changed", [data, query]);
211 // Is there a linked query ?
212 //if ((query.done == 'now') && (query.ts == 'latest')) {
213 // var new_query = [query_json.replace("latest", "now")];
214 // manifold_async_exec(new_query);
219 function __old__manifold_async_render_success(data, query) {
222 // We loop through all the fields to update the corresponding
223 // locations in the page
224 if (typeof(data[0].error) != 'undefined') {
225 manifold_async_error(data[0].error);
228 /* Publish an update announce */
229 jQuery.publish("/rendering/changed", [data, query]);
231 // Is there a linked query ?
232 if ((query.done == 'now') && (query.ts == 'latest')) {
233 var new_query = [query_json.replace("latest", "now")];
234 manifold_async_exec(new_query);