<commit_before>// Interface to stats
define(["settings"], function(preferences) {
	var stats = {};
	var source = 'Sugarizer';
	var logging = true;

	// Send log to the API
	function sendLog(object, action, label, value) {
		// Not connected
		if (!preferences.isConnected()) {
			return;
		}

		// Compute URL
		var info = preferences.getServer();
		var url = null;
		if (info != null && info.url) {
			url = info.url;
		}
		if (url == null) {
			url = (util.getClientType() == constant.webAppType) ? util.getCurrentServerUrl() : "localhost";
		}
		url += constant.statsURL;
		url = constant.http + url;

		// Compute request
		var req = new XMLHttpRequest();
		req.open('POST', url, true);
		var token = preferences.getToken();
		req.setRequestHeader('Content-Type', 'application/json');
		req.setRequestHeader('x-key', token.x_key);
		req.setRequestHeader('x-access-token', token.access_token);
		var stat = {};
		stat.user_id = preferences.getNetworkId();
		stat.user_agent = navigator.userAgent;
		stat.timestamp = new Date().getTime();
		stat.client_type = util.getClientName();
		stat.event_source = source;
		stat.event_object = object;
		stat.event_action = action;
		stat.event_label = label;
		stat.event_value = value;
		var body = {};
		var content = [];
		content.push(stat);
		body.stats = JSON.stringify(content);

		// Send request
console.log(source+": "+action+"("+object+", "+label+", "+value+")");
		req.onreadystatechange = function() {
			if (this.readyState === 4) {
				if (this.status === 200) {
				} else {
					if (!this.response) {
						console.log("Error sending log");
					} else {
						try {
							var response = JSON.parse(this.response);
							if (response.code != 20) {
								console.log("Error sending log");
							}
						} catch(e) {
							console.log("Error sending log");
						}
					}
				}
			}
		};
		req.send(JSON.stringify(body));
	}

	// Trace an action
	stats.trace = function(object, action, label, value) {
		if (logging) {
			sendLog(object, action, label, value);
		}
	}

	return stats;
});
<commit_msg>Test if stats are active on the server before sending it<commit_after>// Interface to stats
define(["settings"], function(preferences) {
	var stats = {};
	var source = 'Sugarizer';
	var logging = true;

	// Send log to the API
	function sendLog(object, action, label, value) {
		// Not connected
		if (!preferences.isConnected()) {
			return;
		}

		// Statistics not active on server
		var info = preferences.getServer();
		if (info && info.options && !info.options.statistics) {
			return;
		}

		// Compute URL
		var url = null;
		if (info && info.url) {
			url = info.url;
		}
		if (url == null) {
			url = (util.getClientType() == constant.webAppType) ? util.getCurrentServerUrl() : "localhost";
		}
		url += constant.statsURL;
		url = constant.http + url;

		// Compute request
		var req = new XMLHttpRequest();
		req.open('POST', url, true);
		var token = preferences.getToken();
		req.setRequestHeader('Content-Type', 'application/json');
		req.setRequestHeader('x-key', token.x_key);
		req.setRequestHeader('x-access-token', token.access_token);
		var stat = {};
		stat.user_id = preferences.getNetworkId();
		stat.user_agent = navigator.userAgent;
		stat.timestamp = new Date().getTime();
		stat.client_type = util.getClientName();
		stat.event_source = source;
		stat.event_object = object;
		stat.event_action = action;
		stat.event_label = label;
		stat.event_value = value;
		var body = {};
		var content = [];
		content.push(stat);
		body.stats = JSON.stringify(content);

		// Send request
console.log(source+": "+action+"("+object+", "+label+", "+value+")");
		req.onreadystatechange = function() {
			if (this.readyState === 4) {
				if (this.status === 200) {
				} else {
					if (!this.response) {
						console.log("Error sending log");
					} else {
						try {
							var response = JSON.parse(this.response);
							if (response.code != 20) {
								console.log("Error sending log");
							}
						} catch(e) {
							console.log("Error sending log");
						}
					}
				}
			}
		};
		req.send(JSON.stringify(body));
	}

	// Trace an action
	stats.trace = function(object, action, label, value) {
		if (logging) {
			sendLog(object, action, label, value);
		}
	}

	return stats;
});

{'fraction_non_alphanumeric': 0.16120729995320543, 'fraction_numerical': 0.0028076743097800653, 'mean_word_length': 3.1384317521781218, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6289501', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1392, 'n_words': 327}