<commit_before>(function($) {

if (Echo.Utils.isComponentDefined("Echo.StreamServer.Controls.Counter")) return;

var counter = Echo.Control.skeleton("Echo.StreamServer.Controls.Counter");

counter.config = {
	"liveUpdatesTimeout": 10 
};

counter.events["Submit.onPostComplete"] =
counter.events["Submit.onEditComplete"] = {
	"context" : "global",
	"handler" : function() {
		this.APIRequest.send({"force": true});
	}
};

//TODO REMOVE INPUT
counter.templates.main = '<span>{data:count}<input type="submit" class="{class:submit}"></span>';


//TODO REMOVE ME
counter.renderers.submit = function(element) {
	var self = this;
	element.click(function() {
		self.config.set("query", "childrenof:http://example.com/*");
		self.refresh();
	});
};

counter.constructor = function() {
	var self = this;
	//TODO showmessage should be refactored
	this.showMessage({"type": "loading"});
	// data can be defined explicitly
	// in this case we do not make API requests
	if ($.isEmptyObject(this.data)){
		this.request();
	} else {
		this.render();
		this.listen();
	}
};

counter.methods.request = function() {
	var self = this;
	Echo.StreamServer.API.request({
		"endpoint": "count",
		"method": "GET",
		"data": {
			"q": self.config.get("query"),
			"appkey": self.config.get("appkey")
		},
		"onError": function(response) {
			//TODO add error handling
			// including more_than error
			console.log("error");
		},
		"onData": function(response) {
			self.data = response;
			self.render();
			self.listen();
		}
	}).send();
};

counter.methods.listen = function() {
	var self = this;
	this.APIRequest = Echo.StreamServer.API.request({
		"endpoint": "count",
		"liveUpdatesTimeout": self.config.get("liveUpdatesTimeout"),
		"method": "GET",
		"recurring": true,
		"data": {
			"q": self.config.get("query"),
			"appkey": self.config.get("appkey")
		},
		"onError": function(response) {
			//TODO add error handling
			// including more_than error
			console.log("error");
			self.APIRequest.abort();
		},
		"onData": function(response) {
			//TODO check if counter was updated
			self.data = response;
			self.rerender();
		}
	});
	this.APIRequest.send();
};

counter.methods.refresh = function() {
	this.APIRequest.abort();
	this.data = {};
	//TODO showmessage should be refactored
	this.showMessage({"type": "loading"});<commit_msg>further counter updates<commit_after>(function($) {

if (Echo.Utils.isComponentDefined("Echo.StreamServer.Controls.Counter")) return;

var counter = Echo.Control.skeleton("Echo.StreamServer.Controls.Counter");

counter.config = {
	"liveUpdatesTimeout": 10 
};

counter.events["Submit.onPostComplete"] =
counter.events["Submit.onEditComplete"] = {
	"context" : "global",
	"handler" : function() {
		this.APIRequest.send({"force": true});
	}
};

counter.templates.main = '<span>{data:count}</span>';

counter.constructor = function() {
	var self = this;
	//TODO showmessage should be refactored
	this.showMessage({"type": "loading"});
	// data can be defined explicitly
	// in this case we do not make API requests
	if ($.isEmptyObject(this.data)){
		this.request();
	} else {
		this.render();
		this.listen();
	}
};

counter.methods.request = function() {
	var self = this;
	Echo.StreamServer.API.request({
		"endpoint": "count",
		"method": "GET",
		"data": {
			"q": self.config.get("query"),
			"appkey": self.config.get("appkey")
		},
		"onError": function(response) {
			//TODO add error handling
			// including more_than error
			self.error(response);
		},
		"onData": function(response) {
			//self.data = response;
			self.update(response);
			self.listen();
		}
	}).send();
};

counter.methods.listen = function() {
	var self = this;
	this.APIRequest = Echo.StreamServer.API.request({
		"endpoint": "count",
		"liveUpdatesTimeout": self.config.get("liveUpdatesTimeout"),
		"method": "GET",
		"recurring": true,
		"data": {
			"q": self.config.get("query"),
			"appkey": self.config.get("appkey")
		},
		"onError": function(response) {
			//TODO add error handling
			// including more_than error
			console.log("error");
			self.error(response);
			self.APIRequest.abort();
		},
		"onData": function(response) {
			//TODO check if counter was updated
			self.data = response;
			self.update(response);
		}
	});
	this.APIRequest.send();
};

counter.methods.update = function(data) {
	if ($.isEmptyObject(this.data) || this.data.count != data.count) {
		this.data = data;
		this.render();
		console.log("Counter.onUpdate");
		this.events.publish({
			"topic": "Counter.onUpdate",
			"data": {
				"data": data,
				"query": this.config.get("query"),
				"target": this.config.get("target").get(0)
			}
		});
	}
};

counter.methods.error = function(data) {
	if (data.errorCode === "more_than") {
		this.data.count = data.errorMessage + "+";
		this.rerender();
	} else {
		//TODO update showMessage function
		this.showMessage({"type": "error"});
	}
};

/*
 target.html(data.errorCode == "more_than" ? (data.errorMessage + "+") : data.count);
        if ($.isEmptyObject(this.data) || this.data.count != data.count) {
                this.publish("Counter.onUpdate", {
                        "data": data,
                        "query": this.config.get("query", ""),
                        "target": this.config.get("target").get(0)
                });
        }
*/

counter.methods.refresh = function() {
	//TODO we should consider moving refresh logic to the parent method
	this.APIRequest.abort();
	this.data = {};
	//TODO showmessage should be refactored
	this.showMessage({"type": "loading"});
{'fraction_non_alphanumeric': 0.18564714389009399, 'fraction_numerical': 0.0010845986984815619, 'mean_word_length': 3.708936170212766, 'pattern_counts': {'":': 49, '<': 8, '<?xml version=': 0, '>': 8, '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': '2962147', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1832, 'n_words': 356}