PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/src/baruwa/static/js/reports.relays.js

https://bitbucket.org/datopdog/baruwa
JavaScript | 135 lines | 110 code | 2 blank | 23 comment | 8 complexity | 67aae598b79041fbb00204d8450e040f MD5 | raw file
Possible License(s): GPL-2.0
  1// 
  2// Baruwa - Web 2.0 MailScanner front-end.
  3// Copyright (C) 2010  Andrew Colin Kissa <andrew@topdog.za.net>
  4// 
  5// This program is free software; you can redistribute it and/or modify
  6// it under the terms of the GNU General Public License as published by
  7// the Free Software Foundation; either version 2 of the License, or
  8// (at your option) any later version.
  9// 
 10// This program is distributed in the hope that it will be useful,
 11// but WITHOUT ANY WARRANTY; without even the implied warranty of
 12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13// GNU General Public License for more details.
 14// 
 15// You should have received a copy of the GNU General Public License along
 16// with this program; if not, write to the Free Software Foundation, Inc.,
 17// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 18//
 19// vim: ai ts=4 sts=4 et sw=4
 20//
 21dojo.require("dojox.charting.Chart2D");
 22dojo.require("dojox.charting.plot2d.Pie");
 23dojo.require("dojox.charting.action2d.Highlight");
 24dojo.require("dojox.charting.action2d.MoveSlice");
 25dojo.require("dojox.charting.action2d.Tooltip");
 26dojo.require("dojox.charting.themes.Tufte");
 27//functions
 28function build_rows(build_array){
 29	var rows = [];
 30	var count = 0;
 31	var c = 1;
 32	dojo.forEach(build_array, function(item, i){
 33		if (item.from_address) {
 34			address = item.from_address;
 35		};
 36		if (item.to_address) {
 37			address = item.to_address;
 38		};
 39		if (item.from_domain) {
 40			address = item.from_domain;
 41		};
 42		if (item.to_domain) {
 43			address = item.to_domain;
 44		};
 45		rows[count++] = '<div class="graph_row">';
 46		rows[count++] = '<div class="row_hash">'+c+'.</div>';
 47		rows[count++] = '<div class="row_ip">';
 48        rows[count++] = '<div class="pie_{{forloop.counter}} pie"></div>';
 49        rows[count++] = ' '+item.clientip+'</div>';
 50        rows[count++] = '<div class="row_hostname">'+item.hostname+'</div>';
 51        rows[count++] = '<div class="row_country">'+item.country+'</div>';
 52        rows[count++] = '<div class="row_count">'+item.num_count+'/'+item.spam_total+'/'+item.virus_total+'</div>';
 53        rows[count++] = '<div class="row_volume">'+filesizeformat(item.total_size)+'</div>';
 54		rows[count++] = '</div>';
 55		c++;
 56	});
 57	return rows.join('');
 58}
 59
 60function process_response(data){
 61    var spinner = dojo.byId("my-spinner");
 62	spinner.innerHTML = gettext('Processing...........');
 63	if (data.success == true) {
 64		url = window.location.pathname;
 65		var links = build_filters(data.active_filters);
 66		dojo.empty("fhl");
 67		if (links != "") {
 68		    dojo.place(links,"fhl","last");
 69		    dojo.removeClass("filterrow","hide");
 70		}else{
 71		    dojo.addClass("filterrow","hide");
 72		};
 73		dojo.query("#fhl a").onclick(function(e){
 74		    remove_filter(e,this);
 75		});
 76		dojo.xhrGet({
 77			url:url,
 78			handleAs:"json",
 79			load:function(response){
 80				dojo.empty("graphrows");
 81				var rows = build_rows(response.items);
 82				dojo.place(rows,"graphrows","last");
 83				chart.updateSeries("Series A", response.pie_data);
 84				chart.render();
 85				spinner.innerHTML = '';
 86            	dojo.style('my-spinner','display','none');
 87            	dojo.attr("filter_form_submit", {'value':gettext('Add')});
 88            	dojo.removeAttr('filter_form_submit','disabled');
 89			}
 90		});
 91	}else{
 92		dojo.destroy('filter-error');
 93		dojo.create('div',{'id':"filter-error",'innerHTML':data.errors+'<div id="dismiss"><a href="#">'+gettext('Dismiss')+'</a></div>'},'afform','before');
 94		var timeout = setTimeout(function(){dojo.destroy('filter-error');},15050);
 95		dojo.query("#dismiss a").onclick(function(){clearTimeout(timeout); dojo.destroy('filter-error');});
 96		spinner.innerHTML = '';
 97    	dojo.style('my-spinner','display','none');
 98    	dojo.attr("filter_form_submit", {'value':gettext('Add')});
 99    	dojo.removeAttr('filter_form_submit','disabled');
100	};
101}
102
103dojo.addOnLoad(function() {
104    init_form();
105    //bind to form submit
106    dojo.query("#filter-form").onsubmit(function(e){
107    	e.preventDefault();
108    	dojo.attr("filter_form_submit", {'disabled':'disabled','value':gettext('Loading')});
109    	dojo.style('my-spinner','display','block');
110    	dojo.destroy('filter-error');
111    	dojo.xhrPost({
112    		form:"filter-form",
113    		handleAs:"json",
114    		load:function(data){process_response(data);}
115    	});
116    });
117    dojo.query("#fhl a").onclick(function(e){
118        remove_filter(e,this);
119    });
120    //chart init and render
121    var dc = dojox.charting;
122    chart = new dc.Chart2D("chart");
123    chart.setTheme(dc.themes.Tufte).addPlot("default", {
124    type: "Pie",
125    font: "normal normal 8pt Tahoma",
126    fontColor: "black",
127    labelOffset: -30,
128    radius: 80
129    });
130    chart.addSeries("Series A", json_data);
131    var anim_a = new dc.action2d.MoveSlice(chart, "default");
132    var anim_b = new dc.action2d.Highlight(chart, "default");
133    var anim_c = new dc.action2d.Tooltip(chart, "default");
134    chart.render();
135});