Project

General

Profile

« Previous | Next » 

Revision 8f3a49d7

Added by Steve Beaver almost 8 years ago

First part of dashboard update system

View differences:

src/usr/local/www/index.php
529 529
		});
530 530
}
531 531

  
532
// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------
533
// These need to live outsie of the events.push() function to enable the widgets to see them
534
var ajaxspecs = new Array();	// Array to hold widget refresh specifications (objects )
535
var ajaxidx = 0;
536
var ajaxmutex = false;
537
var ajaxcntr = 0;
538

  
539
// Add a widget refresh object to the array list
540
function register_ajax(ws) {
541
  ajaxspecs.push(ws);
542
}
543
// ---------------------------------------------------------------------------------------------------
544

  
532 545
events.push(function() {
533 546

  
534 547
	// Make panels destroyable
......
585 598
			$('#btnstore').removeClass("invisible");
586 599
		}
587 600
	});
601

  
602
	// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------
603
	function make_ajax_call(wd) {
604
		ajaxmutex = true;
605

  
606
		$.ajax({
607
			type: 'POST',
608
			url: wd.url,
609
			dataType: 'html',
610
			data: wd.parms,
611

  
612
			success: function(data){
613
				wd.callback(data);
614
				ajaxmutex = false;
615
			},
616

  
617
			error: function(e){
618
//				alert("Error: " + e);
619
				ajaxmutex = false;
620
			}
621
		});
622
	}
623

  
624
	// Loop through each AJAX widget refresh object, make the AJAX call and pass the
625
	// results back to the widget's callback function
626
	function executewidget() {
627
		if (ajaxspecs.length > 0) {
628
			var freq = ajaxspecs[ajaxidx].freq;	// widget can specifify it should be called freq times around hte loop
629

  
630
			if (!ajaxmutex) {
631
				if (((ajaxcntr % freq) === 0) && (typeof ajaxspecs[ajaxidx].callback === "function" )) {
632
				    make_ajax_call(ajaxspecs[ajaxidx]);
633
				}
634

  
635
			    if (++ajaxidx >= ajaxspecs.length) {
636
					ajaxidx = 0;
637

  
638
					if (++ajaxcntr >= 4096) {
639
						ajaxcntr = 0;
640
					}
641
			    }
642
			}
643

  
644
		    setTimeout(function() { executewidget(); }, 1000);
645
	  	}
646
	}
647

  
648
	// Kick it off
649
	executewidget();
588 650
});
589 651
//]]>
590 652
</script>
src/usr/local/www/widgets/widgets/gateways.widget.php
199 199
	</div>
200 200
</form>
201 201

  
202
<script>
203
//<![CDATA[
204

  
205
	function get_gw_stats() {
206
		var ajaxRequest;
207

  
208
		ajaxRequest = $.ajax({
209
				url: "/widgets/widgets/gateways.widget.php",
210
				type: "post",
211
				data: { ajax: "ajax"}
212
			});
213

  
214
		// Deal with the results of the above ajax call
215
		ajaxRequest.done(function (response, textStatus, jqXHR) {
216
			$('#gwtblbody').html(response);
217
			// and do it again
218
			setTimeout(get_gw_stats, "<?=$widgetperiod?>");
219
		});
220
	}
221

  
222
	events.push(function(){
223
		set_widget_checkbox_events("#widget-<?=$widgetname?>_panel-footer [id^=show]", "showallgateways");
224

  
225
		// Start polling for updates some small random number of seconds from now (so that all the widgets don't
226
		// hit the server at exactly the same time)
227
		setTimeout(get_gw_stats, Math.floor((Math.random() * 10000) + 1000));
228
	});
229
//]]>
230
</script>
231

  
232 202
<?php
233 203
function compose_table_body_contents() {
234 204
	global $user_settings;
......
359 329
	return($rtnstr);
360 330
}
361 331
?>
332

  
333
<script>
334
//<![CDATA[
335

  
336
events.push(function(){
337
	// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------
338

  
339
	// Callback function called by refresh system when data is retrieved
340
	function gateways_callback(s) {
341
		$('#gwtblbody').html(s);
342
	}
343

  
344
	// POST data to send via AJAX
345
	var postdata = {
346
		ajax: "ajax",
347
	 	widgetkey : "<?=$widgetkey?>"
348
	 };
349

  
350
	// Create an object defining the widget refresh AJAX call
351
	var gatewaysObject = new Object();
352
	gatewaysObject.name = "Gateways";
353
	gatewaysObject.url = "/widgets/widgets/gateways.widget.php";
354
	gatewaysObject.callback = gateways_callback;
355
	gatewaysObject.parms = postdata;
356
	gatewaysObject.freq = 1;
357

  
358
	// Register the AJAX object
359
	register_ajax(gatewaysObject);
360

  
361
	// ---------------------------------------------------------------------------------------------------
362
});
363

  
364
//]]>
365
</script>
src/usr/local/www/widgets/widgets/interface_statistics.widget.php
201 201

  
202 202
<script type="text/javascript">
203 203
//<![CDATA[
204

  
205
	function get_if_stats() {
204
/*
205
	function get_if_stats_<?=$widgetkey_nodash?>() {
206 206
		var ajaxRequest;
207 207

  
208 208
		ajaxRequest = $.ajax({
209 209
				url: "/widgets/widgets/interface_statistics.widget.php",
210 210
				type: "post",
211
				data: { ajax: "ajax"}
211
				data: { ajax: "ajax", widgetkey: "<?=$widgetkey?>"}
212 212
			});
213 213

  
214 214
		// Deal with the results of the above ajax call
215 215
		ajaxRequest.done(function (response, textStatus, jqXHR) {
216
			$('#iftbl').html(response);
216
			$('#<?=$widgetkey?>-iftbl').html(response);
217 217

  
218 218
			// and do it again
219
			setTimeout(get_if_stats, "<?=$widgetperiod?>");
219
			setTimeout(get_if_stats_<?=$widgetkey_nodash?>, "<?=$widgetperiod?>");
220 220
		});
221 221
	}
222
*/
223
	events.push(function() {
224
		// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------
222 225

  
223
	events.push(function(){
226
		// Callback function called by refresh system when data is retrieved
227
		function interface_statistics_callback(s) {
228
			$('#iftbl').html(s);
229
		}
230

  
231
		// POST data to send via AJAX
232
		var postdata = {
233
			ajax : "ajax",
234
		 	widgetkey :"<?=$widgetkey?>"
235
		 };
236

  
237
		// Create an object defining the widget refresh AJAX call
238
		var ifstatObject = new Object();
239
		ifstatObject.name = "IFstats";
240
		ifstatObject.url = "/widgets/widgets/interface_statistics.widget.php";
241
		ifstatObject.callback = interface_statistics_callback;
242
		ifstatObject.parms = postdata;
243
		ifstatObject.freq = 1;
244

  
245
		// Register the AJAX object
246
		register_ajax(ifstatObject);
247

  
248
		// ---------------------------------------------------------------------------------------------------
249
		// Note: This manages all settings checkboxes with id starting with "show"
250
		// (i.e. both the interface and stats item selection groups)
251
		// using a single All/None button
224 252
		set_widget_checkbox_events("#widget-<?=$widgetname?>_panel-footer [id^=show]", "showallinterfacesforstats");
225 253

  
226
		// Start polling for updates some small random number of seconds from now (so that all the widgets don't
227
		// hit the server at exactly the same time)
228
		setTimeout(get_if_stats, Math.floor((Math.random() * 10000) + 1000));
229 254
	});
230 255
//]]>
231 256
</script>
257

  
src/usr/local/www/widgets/widgets/interfaces.widget.php
61 61

  
62 62
$ifdescrs = get_configured_interface_with_descr();
63 63

  
64
if ($_POST) {
64
if ($_POST && !$_REQUEST['ajax']) {
65 65

  
66 66
	$validNames = array();
67 67

  
......
79 79
	header("Location: /index.php");
80 80
}
81 81

  
82
if (!$_REQUEST['ajax']) {
82 83
?>
83 84

  
84
<div class="table-responsive">
85
<div id="ifaces_status" class="table-responsive">
86
	<?php } ?>
85 87
	<table class="table table-striped table-hover table-condensed">
86 88
		<tbody>
87 89
<?php
......
174 176
?>
175 177
		</tbody>
176 178
	</table>
179

  
180
<?php
181
/* for AJAX response, we only need the panels */
182
if ($_REQUEST['ajax']) {
183
	exit;
184
}
185
?>
177 186
</div>
187

  
178 188
<!-- close the body we're wrapped in and add a configuration-panel -->
179 189
</div><div id="widget-<?=$widgetname?>_panel-footer" class="panel-footer collapse">
180 190

  
......
220 230
<script>
221 231
//<![CDATA[
222 232
	events.push(function(){
233

  
234
		// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------
235

  
236
		// Callback function called by refresh system when data is retrieved
237
		function interfaces_callback(s) {
238
			$('#ifaces_status').html(s);
239
		}
240

  
241
		// POST data to send via AJAX
242
		var postdata = {
243
			widgetkey :"<?=$widgetkey?>",
244
			ajax: "ajax"
245
		};
246

  
247
		// Create an object defining the widget refresh AJAX call
248
		var interfacesObject = new Object();
249
		interfacesObject.name = "Interfaces";
250
		interfacesObject.url = "/widgets/widgets/interfaces.widget.php";
251
		interfacesObject.callback = interfaces_callback;
252
		interfacesObject.parms = postdata;
253
		interfacesObject.freq = 1;
254

  
255
		// Register the AJAX object
256
		register_ajax(interfacesObject);
257

  
258
		// ---------------------------------------------------------------------------------------------------
223 259
		set_widget_checkbox_events("#widget-<?=$widgetname?>_panel-footer [id^=show]", "showallinterfaces");
224 260
	});
225 261
//]]>
src/usr/local/www/widgets/widgets/system_information.widget.php
553 553

  
554 554
<script type="text/javascript">
555 555
//<![CDATA[
556
<?php if (!isset($config['system']['firmware']['disablecheck'])): ?>
557
function systemStatusGetUpdateStatus() {
558
	$.ajax({
559
		type: 'get',
560
		url: '/widgets/widgets/system_information.widget.php',
561
		data: 'getupdatestatus=1',
562
		dataFilter: function(raw){
563
			// We reload the entire widget, strip this block of javascript from it
564
			return raw.replace(/<script>([\s\S]*)<\/script>/gi, '');
565
		},
566
		dataType: 'html',
567
		success: function(data){
568
			$('#widget-system_information #updatestatus').html(data);
569
		}
570
	});
571
}
572

  
573
setTimeout('systemStatusGetUpdateStatus()', 4000);
574
<?php endif; ?>
575

  
576
function updateMeters() {
577
	url = '/getstats.php';
578

  
579
	$.ajax(url, {
580
		type: 'get',
581
		success: function(data) {
582
			response = data || "";
583
			if (response != "")
584
				stats(data);
585
		}
586
	});
587

  
588
	setTimer();
589

  
590
}
591 556

  
592 557
events.push(function(){
593 558
	set_widget_checkbox_events("#widget-<?=$widgetname?>_panel-footer [id^=show]", "showallsysinfoitems");
......
599 564
	$('#' + barName).css('width', percent + '%').attr('aria-valuenow', percent);
600 565
}
601 566

  
602
function setTimer() {
603
	timeout = window.setTimeout('updateMeters()', update_interval);
604
}
605

  
606 567
function stats(x) {
607 568
	var values = x.split("|");
608 569
	if ($.each(values,function(key,value) {
......
773 734
	}
774 735
}
775 736

  
776
/* start updater */
737

  
777 738
events.push(function(){
778
	setTimer();
739
	// --------------------- EXPERIMENTAL centralized widget refresh system ------------------------------
740

  
741
	// Callback function called by refresh system when data is retrieved
742
	function meters_callback(s) {
743
		stats(s);
744
	}
745

  
746
	// POST data to send via AJAX
747
	var postdata = {
748
		ajax: "ajax"
749
	 };
750

  
751
	// Create an object defining the widget refresh AJAX call
752
	var metersObject = new Object();
753
	metersObject.name = "Meters";
754
	metersObject.url = "/getstats.php";
755
	metersObject.callback = meters_callback;
756
	metersObject.parms = postdata;
757
	metersObject.freq = 1;
758

  
759
	// Register the AJAX object
760
	register_ajax(metersObject);
761

  
762
	//set_widget_checkbox_events("#<?=$widget_panel_footer_id?> [id^=show]", "<?=$widget_showallnone_id?>");
763

  
779 764
});
780 765
//]]>
781 766
</script>

Also available in: Unified diff