<commit_before>import * as angular from "angular";

export class AngularStats {
	private rootScope: ng.IRootScopeService;
	private document: ng.IDocumentService;
	private window: ng.IWindowService;
	private timeout: ng.ITimeoutService;

	static $inject = ["$rootScope", "$document", "$window", "$timeout"];

	constructor($rootScope: ng.IRootScopeService,
				$document: ng.IDocumentService,
				$window: ng.IWindowService,
				$timeout: ng.ITimeoutService) {
		this.rootScope = $rootScope;
		this.document = $document;
		this.window = $window;
		this.timeout = $timeout;
	}

	/**
	 * Count the number of scopes / watchers
	 * for every component. Analyze the DOM
	 */
	public analyzeWebApp(): string {

		const scopesList = [];
		const watchersList = [];
		const componentsInfo = {};

		let domElementsCount = 0;
		const nodeNameList = {};

		const analizeScope = (currentScope: ng.IScope) => {

			if (scopesList.indexOf(currentScope) == -1) {
				scopesList.push(currentScope);

				const name = currentScope["$ctrl"] ? currentScope["$ctrl"]["name"] : "Unknown";
				if (componentsInfo[name] == undefined) {
					componentsInfo[name] = {
						name: name, scopesCount: 1, watchers: []
					};
				} else {
					componentsInfo[name].scopesCount++;
				}

				angular.forEach(currentScope["$$watchers"], (watcher) => {
					if (watchersList.indexOf(watcher) == -1) watchersList.push(watcher);

					if (componentsInfo[name].watchers.indexOf(watcher) == -1) componentsInfo[name].watchers.push(watcher);
				});

				if (currentScope["$$childHead"]) {
					analizeScope(currentScope["$$childHead"]);
				}

				if (currentScope["$$childTail"]) {
					analizeScope(currentScope["$$childTail"]);
				}

				if (currentScope["$$prevSibling"]) {
					analizeScope(currentScope["$$prevSibling"]);
				}

				if (currentScope["$$nextSibling"]) {
					analizeScope(currentScope["$$nextSibling"]);
				}
			}
		};

		const detectFromElement = (element) => {

			domElementsCount++;

			if (element.data().hasOwnProperty("$scope")) analizeScope(element.data()["$scope"]);

			angular.forEach(element.children(), (childElement: HTMLElement) => {
				if (nodeNameList[childElement.nodeName] == undefined) {
					nodeNameList[childElement.nodeName] = 1;
				} else {
					nodeNameList[childElement.nodeName]++;
				}

				detectFromElement(this.document.find(childElement));
			});
		};

		analizeScope(this.rootScope);
		detectFromElement(this.document.find("[ui-view]"));

		let mex = "GENERAL\n";
		mex += "------------------------\n";
		mex += "Tot scopes: " + scopesList.length + "\n";
		mex += "Tot watchers: " + watchersList.length + "\n";
		mex += "Tot DOM Elements: " + domElementsCount + "\n\n";

		mex += "\nCOMPONENTS\n";
		mex += "------------------------\n";<commit_msg>Addition of digest time measurement<commit_after>import * as angular from "angular";

export class AngularStats {
	private rootScope: ng.IRootScopeService;
	private document: ng.IDocumentService;
	private window: ng.IWindowService;
	private timeout: ng.ITimeoutService;

	private startingElement: string;
	private digestInfo = {duration: "0"};

	static $inject = ["$rootScope", "$document", "$window", "$timeout"];

	constructor($rootScope: ng.IRootScopeService,
				$document: ng.IDocumentService,
				$window: ng.IWindowService,
				$timeout: ng.ITimeoutService) {
		this.rootScope = $rootScope;
		this.document = $document;
		this.window = $window;
		this.timeout = $timeout;
	}

	public analyzeWebApp(): string {
		const scopesList = [];
		const watchersList = [];
		const componentsInfo = {};

		let domElementsCount = 0;
		const nodeNameList = {};

		const analizeScope = (currentScope: ng.IScope) => {

			if (scopesList.indexOf(currentScope) == -1) {
				scopesList.push(currentScope);

				const name = currentScope["$ctrl"] ? currentScope["$ctrl"]["name"] : "Unknown";
				if (componentsInfo[name] == undefined) {
					componentsInfo[name] = {
						name: name, scopesCount: 1, watchers: []
					};
				} else {
					componentsInfo[name].scopesCount++;
				}

				angular.forEach(currentScope["$$watchers"], (watcher) => {
					if (watchersList.indexOf(watcher) == -1) watchersList.push(watcher);

					if (componentsInfo[name].watchers.indexOf(watcher) == -1) componentsInfo[name].watchers.push(watcher);
				});

				if (currentScope["$$childHead"]) {
					analizeScope(currentScope["$$childHead"]);
				}

				if (currentScope["$$childTail"]) {
					analizeScope(currentScope["$$childTail"]);
				}

				if (currentScope["$$prevSibling"]) {
					analizeScope(currentScope["$$prevSibling"]);
				}

				if (currentScope["$$nextSibling"]) {
					analizeScope(currentScope["$$nextSibling"]);
				}
			}
		};

		const detectFromElement = (element) => {
			domElementsCount++;
			if (element.data().hasOwnProperty("$scope")) analizeScope(element.data()["$scope"]);

			angular.forEach(element.children(), (childElement: HTMLElement) => {
				if (nodeNameList[childElement.nodeName] == undefined) {
					nodeNameList[childElement.nodeName] = 1;
				} else {
					nodeNameList[childElement.nodeName]++;
				}
				detectFromElement(this.document.find(childElement));
			});
		};

		const calculateDigestDuration = () => {
			let duration = 0;
			let scopePrototype = Object.getPrototypeOf(this.rootScope);
			let angularDigest = scopePrototype.$digest;

			scopePrototype.$digest = (...args) => {
				let start = performance.now();
				angularDigest.apply(this.rootScope, args);
				duration = performance.now() - start;
				this.digestInfo.duration = duration.toFixed(2);
			};
		};

		analizeScope(this.rootScope);
		detectFromElement(this.document.find("app"));
		calculateDigestDuration();

		let mex = "GENERAL\n";
		mex += "------------------------\n";
		mex += "Tot scopes: " + scopesList.length + "\n";
		mex += "Tot watchers: " + watchersList.length + "\n";
		mex += "Tot DOM Elements: " + domElementsCount + "\n";
		mex += "Digest duration: " + this.digestInfo.duration + " ms \n\n";

		mex += "\nCOMPONENTS\n";
		mex += "------------------------\n";
{'fraction_non_alphanumeric': 0.17829328914664458, 'fraction_numerical': 0.0024855012427506215, 'mean_word_length': 3.7045985970381916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3369403', 'n_tokens_mistral': 2281, 'n_tokens_neox': 1907, 'n_words': 346}