var filtUndefined = function(data) {
	var ret = [];
	for (var i = 0; i < data.length; i++) {
		try {
			var obj = JSON.parse(data[i]);
			ret.push(obj);
		} catch (e) {
		}
	}
	return ret;
};

var dtLocalLang = {
		"sProcessing" : "处理中...",
		"lengthMenu" : '显示 <select>' + '<option value="10">10</option>'
				+ '<option value="50">50</option>'
				+ '<option value="100">100</option>'
				+ '<option value="-1">全部</option>' + '</select> 项结果',
		"sZeroRecords" : "没有匹配结果",
		"sInfo" : "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
		"sInfoEmpty" : "显示第 0 至 0 项结果，共 0 项",
		"sInfoFiltered" : "(由 _MAX_ 项结果过滤)",
		"sInfoPostFix" : "",
		"sSearch" : "搜索:",
		"sUrl" : "",
		"sEmptyTable" : "表中数据为空",
		"sLoadingRecords" : "载入中...",
		"sInfoThousands" : ",",
		"oPaginate" : {
			"sFirst" : "首页",
			"sPrevious" : "上页",
			"sNext" : "下页",
			"sLast" : "末页"
		},
		"oAria" : {
			"sSortAscending" : ": 以升序排列此列",
			"sSortDescending" : ": 以降序排列此列"
		}
	};

var nodeLogActionToCategory = function(action) {
	switch (action) {
	case 'getNodeSessionID':
	case 'login':
	case 'getNodeRole':
		return "登录类";
	case 'listAllAuthRole':
	case 'listUnAuthRole':
	case 'countRole':
	case 'applyNodeRole':
	case 'authNodeRole':
	case 'deleteRole':
		return "用户类";
	case 'listLocalNodeLog':
	case 'listLocalContractLog':
	case 'listLocalNodeLogPie':
	case 'listLocalNodeLogLine':
	case 'listLocalContractLogPie':
	case 'listLocalContractLogLine':
	case 'onGetHashCount':
	case 'onQueryDataByHash':
		return "日志类";
	case 'loadNodeConfig':
	case 'updateLicence':
	case 'listNodes':
	case 'changeNodeName':
	case 'changeYJSPath':
	case 'changeNodeCenter':
	case 'getPeerID':
	case 'getNodeID':
	case 'uploadLicence':
	case 'connectTo':
		return "维护类";
	case 'startContract':
	case 'initcodeManage':
	case 'listContractProcess':
	case 'startContractBatched':
	case 'startContractConfig':
	case 'killContractProcess':
	case 'killAllContract':
	case 'switchProject':
	case 'stopContract':
	case 'staticVerify':
	case 'staticVerifyContractByPath':
	case 'executeContract':
	case 'dumpContract':
	case 'loadMemory':
	case 'changeDumpPeriod':
	case 'getDumpPeriod':
	case 'startSync':
	case 'stopSync':
	case 'changeSyncType':
	case 'getSyncType':
	case 'recoverBySync':
		return "合约类";
	case 'deleteFile':
	case 'uploadFile':
	case 'downloadContract':
	case 'listProjects':
	case 'listProject':
	case 'listFile':
	case 'createFile':
	case 'uploadRequest':
	case 'changePublic':
	case 'listMemoryFiles':
	case 'saveFile':
		return "文件类";
	default:{
		console.log(action);
		return "其他类";
	}
	}
};

var drawLocalNodeLogPie = function(data) {
	$("#auditLocalNodeLogPie")
			.html(
					"<div class='col-12' id='auditLocalNodeLogDivCanv' style='width:100%; min-height: 250px; padding: 0px'></div>");
	var result = {};
	for (var i = 0; i < data.length; i++) {
		var obj = JSON.parse(data[i]);
		var ca = nodeLogActionToCategory(obj.action);
		if (result[ca] == undefined) {
			result[ca] = obj.times;
		} else
			result[ca] += obj.times;
	}
	global.result = result;

	var dataList = [];
	var valDataList = [];
	for ( var key in result) {
		dataList.push(key);
		var l = {};
		l.name = key
		l.value = result[key];
		valDataList.push(l);
	}

	var myChart = echarts.init($("#auditLocalNodeLogDivCanv")[0],'walden');

	var option222 = {
		tooltip : {
			trigger : 'item',
			formatter : '{a} <br/>{b}: {c} ({d}%)'
		},
		legend : {
			orient : 'vertical',
			left : 10,
			data : dataList
		},
		series : [ {
			name : '访问操作',
			type : 'pie',
			radius : [ '50%', '70%' ],
			avoidLabelOverlap : false,
			label : {
				normal : {
					show : false,
					position : 'center'
				},
				emphasis : {
					show : true,
					textStyle : {
						fontSize : '15',
						fontWeight : 'bold'
					}
				}
			},
			labelLine : {
				normal : {
					show : false
				}
			},
			data : valDataList
		} ]
	};
	myChart.setOption(option222);
};

var contractLogActionToCategory = function(action) {
	switch (action) {
	case 'startContract':
		return "启动";
	case 'stopContract':
		return "终止";
	case 'staticVerify':
		return "静态分析";
	case 'executeContract':
		return "执行";
	case 'changeDumpPeriod':
	case 'getDumpPeriod':
	case 'loadMemory':
	case 'dumpContract':
	case 'startSync':
	case 'stopSync':
	case 'changeSyncType':
	case 'getSyncType':
	case 'recoverBySync':
		return "同步机制";
	default:
		console.log(action);
		return "其他类";
	}
};

var drawLocalContractLogPie = function(data) {
	$("#auditLocalContractLogPie")
			.html(
					"<div class='col-12' id='auditLocalContractLogDivCanv' style='width:100%; min-height: 250px; padding: 0px'></div>");
	var result = {};
	for (var i in data) {

		var ca = contractLogActionToCategory(i);
		if (result[ca] == undefined) {
			result[ca] = data[i];
		} else
			result[ca]+= data[i];
	}
	global.result = result;
	var dataList = [];
	var valDataList = [];
	for ( var key in result) {
		dataList.push(key);
		var l = {};
		l.name = key
		l.value = result[key];
		valDataList.push(l);
	}

	var myChart = echarts.init($("#auditLocalContractLogDivCanv")[0],'walden');

	var option222 = {
		tooltip : {
			trigger : 'item',
			formatter : '{a} <br/>{b}: {c} ({d}%)'
		},
		legend : {
			orient : 'vertical',
			left : 10,
			data : dataList
		},
		series : [ {
			name : '访问操作',
			type : 'pie',
			radius : [ '50%', '70%' ],
			avoidLabelOverlap : false,
			label : {
				normal : {
					show : false,
					position : 'center'
				},
				emphasis : {
					show : true,
					textStyle : {
						fontSize : '15',
						fontWeight : 'bold'
					}
				}
			},
			labelLine : {
				normal : {
					show : false
				}
			},
			data : valDataList
		} ]
	};
	myChart.setOption(option222);
};

var auditConf = {
		firstRowHeight : '200px'
};

var drawLocalNodeLogLine = function(id,data) {
	if ($("#a" + id).css("height") == "0px") {
		console.log("drawLogBar: ignore");
		return;
	}
	$("#" + id).html(
			"<div class='col-12' id='" + id
					+ "Canv' style='width:100%; min-height: "
					+ auditConf.firstRowHeight + "; padding: 0px'></div>");
	var myChart = echarts.init($("#" + id + "Canv")[0], 'walden');
	var xAxisData = data.xAxis;
	var data1 = data.yAxis;

	var option = {
		title : {
			subtext : '次数'
		},
		tooltip : {
			trigger : 'axis'
		},
		legend : {
			data : [ '操作次数' ],
			y : 'bottom',
		},
		toolbox : {
			show : true,
			feature : {
				dataZoom : {
					yAxisIndex : 'none'
				},
				dataView : {
					readOnly : false
				},
				magicType : {
					type : [ 'line', 'bar' ]
				},
				restore : {},
				saveAsImage : {}
			}
		},
		xAxis : {
			type : 'category',
			boundaryGap : false,
			data : xAxisData
		},
		yAxis : {
			type : 'value',
			axisLabel : {
				formatter : '{value}次'
			}
		},
		series : [ {
			name : '操作次数',
			type : 'line',
			data : data1,
			markPoint : {
				data : [ {
					type : 'max',
					name : '最大值'
				}, {
					type : 'min',
					name : '最小值'
				} ]
			},

		} ]
	};

	myChart.setOption(option);

};

var drawLocalContractLogLine = function(id,data) {
	if ($("#a" + id).css("height") == "0px") {
		console.log("drawLogBar: ignore");
		return;
	}
	$("#" + id).html(
			"<div class='col-12' id='" + id
					+ "Canv' style='width:100%; min-height: "
					+ auditConf.firstRowHeight + "; padding: 0px'></div>");
	var myChart = echarts.init($("#" + id + "Canv")[0], 'walden');
	var xAxisData = data.xAxis;
	var data1 = data.yAxis;
	
	var option = {
		title : {
			subtext : '次数'
		},
		tooltip : {
			trigger : 'axis'
		},
		legend : {
			data : [ '操作次数' ],
			y : 'bottom',
		},
		toolbox : {
			show : true,
			feature : {
				dataZoom : {
					yAxisIndex : 'none'
				},
				dataView : {
					readOnly : false
				},
				magicType : {
					type : [ 'line', 'bar' ]
				},
				restore : {},
				saveAsImage : {}
			}
		},
		xAxis : {
			type : 'category',
			boundaryGap : false,
			data : xAxisData
		},
		yAxis : {
			type : 'value',
			axisLabel : {
				formatter : '{value}次'
			}
		},
		series : [ {
			name : '操作次数',
			type : 'line',
			data : data1,
			markPoint : {
				data : [ {
					type : 'max',
					name : '最大值'
				}, {
					type : 'min',
					name : '最小值'
				} ]
			},

		} ]
	};

	myChart.setOption(option);
};

var contractInstanceToCategory = function(action) {
	switch (action) {
	case 'Sole':
		return "单点类型合约";
	case 'RequestOnce':
	case 'ResponseOnce':
	case 'RequestAllResponseFirst':
	case 'RequestAllResponseHalf':
	case 'RequestAllResponseAll':
		return "集群类型合约";
	default:
		return "其他类";
	}
};

var drawContractInstanceTypeTimesPie = function(table) {
	if ($("#ciTimesPieDiv").css("height") == "0px") {
		console.log("drawContractInstanceTimesPie: ignore");
		return;
	}
	$("#ciTimesPieDiv")
			.html(
					"<div class='col-12' id='ciTimesPieDivCanv' style='width:100%; min-height: 150px; padding: 0px'></div>");
	var result = {};
	for (var i = 0; i < table.length; i++) {
		var obj = table[i];
		var ca = contractInstanceToCategory(obj.type);
		if (result[ca] == undefined) {
			result[ca] = obj.times;
		} else
			result[ca] += obj.times;
	}
	global.result = result;
	var dataList = [];
	var valDataList = [];
	for ( var key in result) {
		dataList.push(key);
		var l = {};
		l.name = key
		l.value = result[key];
		valDataList.push(l);
	}

	var myChart = echarts.init($("#ciTimesPieDivCanv")[0], 'walden');

	var option222 = {
		tooltip : {
			trigger : 'item',
			formatter : '{a} <br/>{b}: {c} ({d}%)'
		},
		legend : {
			orient : 'vertical',
			left : 10,
			data : dataList
		},
		series : [ {
			name : '各类合约实例调用次数',
			type : 'pie',
			radius : [ '50%', '70%' ],
			avoidLabelOverlap : false,
			label : {
				normal : {
					show : false,
					position : 'center'
				},
				emphasis : {
					show : true,
					textStyle : {
						fontSize : '15',
						fontWeight : 'bold'
					}
				}
			},
			labelLine : {
				normal : {
					show : false
				}
			},
			data : valDataList
		} ]
	};
	myChart.setOption(option222);
};