<commit_before>/**
 * TraCINg-Server - Gathering and visualizing cyber incidents on the world
 *
 * Copyright 2013 <NAME>, <NAME>, <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var detailChartType = "typeDate";

/*
* queries data, filters it, updates the chart
* 
* options: object
*  - chart: reference to a highcharts Chart
*  - name: string, name of the data that should be displayed
*  - detail: boolean, whether to fetch detailed data or just data for the master chart (optional, default: false)
*/
function updateChart(options){
	var rawSeries;
	var title, subtitle, xTitle, yTitle;
	
	// get the chart and delete it from options
	var chart = options.chart;
	delete options.chart;
	
	options.filter = filter.getFilter();
	
	// fetch the series
	if(options.detail){
		options.detailChartType = detailChartType;
		
		// patch start and end into the filter
		options.filter["start"] = currentStartDate;
		options.filter["end"] = currentEndDate;
		
		
		if(detailChartType == "typeDate"){
			options.pie = true;
			
			title = {text: "Types of Incidents"};
			subtitle = {text: "per day"};
			yTitle = {text: "Incidents per day"};
		}
		else if(detailChartType == "countryDate"){
			options.pie = true;
			
			title = {text: "Source countries"};
			subtitle = {text: "per day"};
			yTitle = {text: "Incidents per day"};
		}
		else{
			console.log("error: unknown type: " + detailChartType);
			return;
		}
	}
	else{
		// nothing to do..
	}
	
	// some formatting of the chart
	if(title || subtitle)
		chart.setTitle(title, subtitle);
	if(xTitle)
		chart.xAxis[0].setTitle(xTitle);
	if(yTitle)
		chart.yAxis[0].setTitle(yTitle);
	
	chart.showLoading("loading..");
	
	socket.emit("getStatistics", options, function(data){
		console.log("getStatistics returned: ", data);
		
		if(!data || data.length == 0){
			chart.showLoading("No data for that selection!");
			return;
		}

		// filter the series
		var chartSeries = createChartSeries(data, options);
		
		// display the series
		setChartData(chart, chartSeries);
	});
}

/*
* setChartData
* removes all series from a chart and adds the new series
* 
* chart: reference to a highcharts Chart
* data: array of series to be added
*/
function setChartData(chart, data){
	// remove old series
	// todo: das müsste auch besser gehen...
	for(var i = chart.series.length - 1; i >= 0; i--){
		var old = chart.series[i];
		old.remove(false); // do not redraw
	}
	
	for(var k in data){
		chart.addSeries(data[k], false); // false: do not redraw
	}

	chart.hideLoading();
	chart.redraw();

	$(".highcharts-legend-item > span").each(function(i, el){el.onclick = null;});
}

/*
* createChartSeries
* creates series for the chart from the given series, also calculates the pie serie (if options.pie is true)
* 
* arguments:<commit_msg>stats: redraw between adding new series and removing the old<commit_after>/**
 * TraCINg-Server - Gathering and visualizing cyber incidents on the world
 *
 * Copyright 2013 <NAME>, <NAME>, <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var detailChartType = "typeDate";

/*
* queries data, filters it, updates the chart
* 
* options: object
*  - chart: reference to a highcharts Chart
*  - name: string, name of the data that should be displayed
*  - detail: boolean, whether to fetch detailed data or just data for the master chart (optional, default: false)
*/
function updateChart(options){
	var rawSeries;
	var title, subtitle, xTitle, yTitle;
	
	// get the chart and delete it from options
	var chart = options.chart;
	delete options.chart;
	
	options.filter = filter.getFilter();
	
	// fetch the series
	if(options.detail){
		options.detailChartType = detailChartType;
		
		// patch start and end into the filter
		options.filter["start"] = currentStartDate;
		options.filter["end"] = currentEndDate;
		
		
		if(detailChartType == "typeDate"){
			options.pie = true;
			
			title = {text: "Types of Incidents"};
			subtitle = {text: "per day"};
			yTitle = {text: "Incidents per day"};
		}
		else if(detailChartType == "countryDate"){
			options.pie = true;
			
			title = {text: "Source countries"};
			subtitle = {text: "per day"};
			yTitle = {text: "Incidents per day"};
		}
		else{
			console.log("error: unknown type: " + detailChartType);
			return;
		}
	}
	else{
		// nothing to do..
	}
	
	// some formatting of the chart
	if(title || subtitle)
		chart.setTitle(title, subtitle);
	if(xTitle)
		chart.xAxis[0].setTitle(xTitle);
	if(yTitle)
		chart.yAxis[0].setTitle(yTitle);
	
	chart.showLoading("loading..");
	
	socket.emit("getStatistics", options, function(data){
		console.log("getStatistics returned: ", data);
		
		if(!data || data.length == 0){
			chart.showLoading("No data for that selection!");
			return;
		}

		// filter the series
		var chartSeries = createChartSeries(data, options);
		
		// display the series
		setChartData(chart, chartSeries);
	});
}

/*
* setChartData
* removes all series from a chart and adds the new series
* 
* chart: reference to a highcharts Chart
* data: array of series to be added
*/
function setChartData(chart, data){
	var oldLenght = chart.series.length;

	// add new series
	for(var i = 0; i < data.length; i++){
		chart.addSeries(data[i], false); // false: do not redraw
	}
	
	chart.redraw();

	// remove old series
	for(var count = 0; count < oldLenght; count++){
		chart.series[0].remove(false); // do not redraw
	}
	

	chart.hideLoading();
	chart.redraw();

	$(".highcharts-legend-item > span").each(function(i, el){el.onclick = null;});
}

/*
* createChartSeries
* creates series for the chart from the given series, also calculates the pie serie (if options.pie is true)
* 
* arguments:
{'fraction_non_alphanumeric': 0.12277287019014822, 'fraction_numerical': 0.0040425213355292705, 'mean_word_length': 3.6260387811634347, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7353872', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2009, 'n_words': 713}