<commit_before>import RestClient from 'another-rest-client';
import R from 'ramda';

let instance;

export class RestApi {

	static getInstance() {
		if (!instance) {
			instance = new RestApi();
		}
		return instance;
	}

	constructor() {
		const prefix = location.href.includes('localhost:3000') ? 'http://localhost:8686' : '';
		this.api = new RestClient(prefix + '/api/v1/rest');
		this.api.res('common').res('details');
		this.api.res('common').res('documentation');
		this.api.res('common').res('statistics').res('entities');
		this.api.res('common').res('statistics').res('hits');
		this.api.on('response', (xhr) => {
			try {
				const limits = {
					total: xhr.getResponseHeader('X-Throttle-Limit-Total'),
					remaining: xhr.getResponseHeader('X-Throttle-Limit-Remaining')
				};
				if (this.onLimitUpdateCallback) {
					this.onLimitUpdateCallback(limits);
				}
			} catch(e) {}
		});
		this.api.common.details.get().then(response => {
			response.details.sort((left, right) => {
				return left.symbol > right.symbol ? 1 : -1;
			});
			this.details = response.details;
			this.details.forEach((url) => {
				var res = {};
				res[url.apiEndpointSuffix] = ['search'];
				this.api.res(res);
			});
			if (this.callback) {
				this.callback();
			}
			this.loadStatistics();
		}).catch(error => {
			if (this.errorCallback) {
				this.errorCallback(error);
			}
		});
	}

	search(symbol, phrase, single) {
		const serviceName = this.findBySymbol(symbol).apiEndpointSuffix;
		const searchApi = this.api[serviceName].search;
		var promise = phrase ? searchApi.post({
			title: phrase, name: phrase
		}, 'application/x-www-form-urlencoded') : searchApi.get();
		return promise.then(response => {
			return {
				page: response.page,
				content: response[this.getContentKey(response)]
			};
		});
	}

	get(symbol, uid) {
		const serviceName = this.findBySymbol(symbol).apiEndpointSuffix;
		const api = this.api[serviceName];
		return api.get({uid: uid}).then(response => {
			return {
				page: null,
				content: response[this.getContentKey(response)]
			};
		});
	}

	loadStatistics() {
		const entitiesStatisticsPromise = this.api.common.statistics.entities.get().then(response => {
			response.statistics.sort((left, right) => {
				return left > right ? 1 : left === right ? 0 : -1;
			});
			this.statistics = this.statistics || {};
			this.statistics.entitiesStatistics = response;
		});
		const hitsStatisticsPromise = this.api.common.statistics.hits.get().then(response => {
			response.statistics.sort((left, right) => {
				return left > right ? 1 : left === right ? 0 : -1;
			});
			this.statistics = this.statistics || {};
			this.statistics.hitsStatistics = response;
		});
		Promise.all([entitiesStatisticsPromise, hitsStatisticsPromise]).then(() => {
			this.statistics.loaded = true;
			if (this.onStatisticsReadyCallbackList && this.onStatisticsReadyCallbackList.length) {
				this.onStatisticsReadyCallbackList.forEach(onStatisticsReadyCallback => {
					onStatisticsReadyCallback();
				});
			}
		});
	}

	loadDocumentation() {
		if (!this.documentation && !this.documentationPromise) {
			this.documentationPromise = this.api.common.documentation.get().then(response => {
				this.documentation = response;
				return this.documentation;
			});
			return this.documentationPromise;
		} else if (this.documentationPromise) {
			return this.documentationPromise;
		} else {
			return Promise.all([this.documentationPromise]);
		}
	}

	getStatistics() {
		return this.statistics;
	}

	hasStatistics() {
		return this.statistics && this.statistics.loaded;
	}

	getContentKey(response) {
		return R.filter(key => key !== 'page', Object.keys(response))[0];
	}

	getDetails() {
		return this.details;
	}

	hasDetails() {
		return !!this.details;
	}<commit_msg>Fix for API results no showing<commit_after>import RestClient from 'another-rest-client';
import R from 'ramda';

let instance;

export class RestApi {

	static getInstance() {
		if (!instance) {
			instance = new RestApi();
		}
		return instance;
	}

	constructor() {
		const prefix = location.href.includes('localhost:3000') ? 'http://localhost:8686' : '';
		this.api = new RestClient(prefix + '/api/v1/rest');
		this.api.res('common').res('details');
		this.api.res('common').res('documentation');
		this.api.res('common').res('statistics').res('entities');
		this.api.res('common').res('statistics').res('hits');
		this.api.on('response', (xhr) => {
			try {
				const limits = {
					total: xhr.getResponseHeader('X-Throttle-Limit-Total'),
					remaining: xhr.getResponseHeader('X-Throttle-Limit-Remaining')
				};
				if (this.onLimitUpdateCallback) {
					this.onLimitUpdateCallback(limits);
				}
			} catch(e) {}
		});
		this.api.common.details.get().then(response => {
			response.details.sort((left, right) => {
				return left.symbol > right.symbol ? 1 : -1;
			});
			this.details = response.details;
			this.details.forEach((url) => {
				var res = {};
				res[url.apiEndpointSuffix] = ['search'];
				this.api.res(res);
			});
			if (this.callback) {
				this.callback();
			}
			this.loadStatistics();
		}).catch(error => {
			if (this.errorCallback) {
				this.errorCallback(error);
			}
		});
	}

	search(symbol, phrase, single) {
		const serviceName = this.findBySymbol(symbol).apiEndpointSuffix;
		const searchApi = this.api[serviceName].search;
		var promise = phrase ? searchApi.post({
			title: phrase, name: phrase
		}, 'application/x-www-form-urlencoded') : searchApi.get();
		return promise.then(response => {
			return {
				page: response.page,
				content: response[this.getContentKey(response)]
			};
		});
	}

	get(symbol, uid) {
		const serviceName = this.findBySymbol(symbol).apiEndpointSuffix;
		const api = this.api[serviceName];
		return api.get({uid: uid}).then(response => {
			return {
				page: null,
				content: response[this.getContentKey(response)]
			};
		});
	}

	loadStatistics() {
		const entitiesStatisticsPromise = this.api.common.statistics.entities.get().then(response => {
			response.statistics.sort((left, right) => {
				return left > right ? 1 : left === right ? 0 : -1;
			});
			this.statistics = this.statistics || {};
			this.statistics.entitiesStatistics = response;
		});
		const hitsStatisticsPromise = this.api.common.statistics.hits.get().then(response => {
			response.statistics.sort((left, right) => {
				return left > right ? 1 : left === right ? 0 : -1;
			});
			this.statistics = this.statistics || {};
			this.statistics.hitsStatistics = response;
		});
		Promise.all([entitiesStatisticsPromise, hitsStatisticsPromise]).then(() => {
			this.statistics.loaded = true;
			if (this.onStatisticsReadyCallbackList && this.onStatisticsReadyCallbackList.length) {
				this.onStatisticsReadyCallbackList.forEach(onStatisticsReadyCallback => {
					onStatisticsReadyCallback();
				});
			}
		});
	}

	loadDocumentation() {
		if (!this.documentation && !this.documentationPromise) {
			this.documentationPromise = this.api.common.documentation.get().then(response => {
				this.documentation = response;
				return this.documentation;
			});
			return this.documentationPromise;
		} else if (this.documentationPromise) {
			return this.documentationPromise;
		} else {
			return Promise.all([this.documentationPromise]);
		}
	}

	getStatistics() {
		return this.statistics;
	}

	hasStatistics() {
		return this.statistics && this.statistics.loaded;
	}

	getContentKey(response) {
		return R.filter(key => key !== 'page' && key !== 'sort', Object.keys(response))[0];
	}

	getDetails() {
		return this.details;
	}

	hasDetails() {
		return !!this.details;
	}
{'fraction_non_alphanumeric': 0.16283348666053357, 'fraction_numerical': 0.004731239321855697, 'mean_word_length': 3.792191435768262, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '6808035', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2392, 'n_words': 401}