1 manifold_async_debug=true;
3 // Helper functions for asynchronous requests
5 var api_url = '/manifold/api/json/'
7 // Executes all async. queries
8 // input queries are specified as a list of {'query_uuid': <query_uuid>, 'id': <possibly null>}
9 function manifold_async_exec(queries) {
10 if (manifold_async_debug) console.log('manifold_async_exec length='+ queries.length);
12 jQuery('.need-spin').spin();
14 // We use js function closure to be able to pass the query (array) to the
15 // callback function used when data is received
16 var manifold_async_success_closure = function(query, id) {
17 return function(data, textStatus) {manifold_async_success(data, query, id);}};
19 // Loop through query array and use ajax to send back queries (to frontend) with json
20 jQuery.each(queries, function(index, tuple) {
21 var query=manifold.find_query(tuple.query_uuid);
22 var hash=query.to_hash();
23 if (manifold_async_debug) console.log ("sending POST on " + api_url + " iterating on " + tuple.query_uuid + " -> " + hash);
24 jQuery.post(api_url, {'query': hash}, manifold_async_success_closure(query, tuple.id));
28 function manifold_async_success(data, query, id) {
32 /* Directly inform the requestor */
33 jQuery('#' + id).trigger('results', [data]);
35 /* Publish an update announce */
36 jQuery.publish("/results/" + query.query_uuid + "/changed", [data, query]);
39 // Is there a linked query ?
40 //if ((query.done == 'now') && (query.ts == 'latest')) {
41 // var new_query = [query_json.replace("latest", "now")];
42 // manifold_async_exec(new_query);
48 function manifold_async_error(str) {
49 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>';
50 jQuery('#manifold_message').html(out);
51 //onObjectAvailable('Spinners', function(){ Spinners.get('.loading').remove(); }, this, true);
52 jQuery('.need-spin').spin(false);
56 /* what the hell is this doing here ?
57 function apply_format(key, value, type, method) {
60 'platform': {'_all': 'platforms'},
61 'src_hostname': {'traceroute': 'agents', '_other': 'hostname'},
62 'dst_hostname': {'traceroute': 'agents', '_other': 'hostname'},
63 'src_ip': {'traceroute': 'agents', '_other': 'ip'},
64 'dst_ip': {'traceroute': 'agents', '_other': 'ip'},
65 'as_name': {'_all': 'as'},
66 'asn': {'_all': 'as'},
67 'city': {'_all': 'cities'},
68 'continent': {'_all': 'continents'},
69 'continent_code': {'_all': 'continents'},
70 'country': {'_all': 'countries'},
71 'country_code': {'_all': 'countries'},
72 'hostname': {'agents': 'agents', 'nodes': 'node', '_other': 'hostname'},
73 'ip': {'agents': 'agents', '_other': 'ip'},
74 'network_hrn': {'_all': 'network'},
75 'region': {'_all': 'regions'},
76 'region_code': {'_all': 'regions'},
77 'slice_hrn': {'_all': 'slice'},
80 // creates problems sorting ?
81 if (link[type]['_all']) {
82 var urlpart = link[type]['_all'];
84 if (link[type][method]) {
85 var urlpart = link[type][method];
87 if (link[type]['_other']) {
88 var urlpart = link[type]['_other'];
94 return '<a href="/view/' + urlpart + '/' + key +'">' + value + '</a>';
101 function manifold_html_a(key, value, type) {
102 if (type == 'network_hrn') {
103 return "<a href='/view/network/" + key + "'>" + value + '</a>';
104 } else if (type == 'slice_hrn') {
105 return "<a href='/view/slice/" + key + "'>" + value + '</a>';
111 function manifold_html_li(type, value, is_cached) {
114 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>';
115 if (type == 'slice_hrn') {
116 return "<li class='icn icn-play'>" + value + cached + "</li>";
117 } else if (type == 'network_hrn') {
118 return "<li class='icn icn-play'>" + value + cached + "</li>";
120 return "<li>" + value + "</li>";
125 function manifold_html_ul(data, key, value, type, method, is_cached) {
127 for (var i = 0; i < data.length; i++) {
128 out += manifold_html_li(key, apply_format(data[i][key], data[i][value], key, method), is_cached);
129 //out += manifold_html_li(key, manifold_html_a(data[i][key], data[i][value], key), is_cached);
136 function manifold_update_template(data)
138 jQuery.each(data, function(key, value) {
139 if ((typeof value == 'string') || (typeof value == 'number') || (typeof value == 'boolean')) {
141 jQuery('#manifold__' + key).html(value);
142 } else if (value == null) {
143 jQuery('#manifold__' + key).html("N/A");
145 manifold_update_table('#manifold__' + key, value);
150 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
151 //make sure to expose csrf in our outcoming ajax/post requests
153 beforeSend: function(xhr, settings) {
154 function getCookie(name) {
155 var cookieValue = null;
156 if (document.cookie && document.cookie != '') {
157 var cookies = document.cookie.split(';');
158 for (var i = 0; i < cookies.length; i++) {
159 var cookie = jQuery.trim(cookies[i]);
160 // Does this cookie string begin with the name we want?
161 if (cookie.substring(0, name.length + 1) == (name + '=')) {
162 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
169 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
170 // Only send the token to relative URLs i.e. locally.
171 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));