<commit_before>import { StoreService, StoreSubscriber } from "../../../store.service"
import { IRootScopeService } from "angular"
import { calculateNewEdgeMetricData, EdgeMetricDataActions } from "./edgeMetricData.actions"
import { isActionOfType } from "../../../../util/reduxHelper"
import { AttributeTypes, AttributeTypeValue, BlacklistItem, CodeMapNode, EdgeMetricData } from "../../../../codeCharta.model"
import { FileState } from "../../../../model/files/files"
import { HierarchyNode } from "d3"
import { BlacklistService, BlacklistSubscriber } from "../../fileSettings/blacklist/blacklist.service"
import { FilesSelectionSubscriber, FilesService } from "../../files/files.service"
import { EdgeMetricCountMap, nodeEdgeMetricsMap } from "./edgeMetricData.reducer"
import { AttributeTypesService, AttributeTypesSubscriber } from "../../fileSettings/attributeTypes/attributeTypes.service"

export interface EdgeMetricDataSubscriber {
	onEdgeMetricDataChanged(edgeMetricData: EdgeMetricData[])
}

export class EdgeMetricDataService implements StoreSubscriber, BlacklistSubscriber, FilesSelectionSubscriber, AttributeTypesSubscriber {
	private static EDGE_METRIC_DATA_CHANGED_EVENT = "edge-metric-data-changed"
	public static NONE_METRIC = "None"

	constructor(private $rootScope: IRootScopeService, private storeService: StoreService) {
		StoreService.subscribe(this.$rootScope, this)
		BlacklistService.subscribe(this.$rootScope, this)
		FilesService.subscribe(this.$rootScope, this)
		AttributeTypesService.subscribe(this.$rootScope, this)
	}

	public onStoreChanged(actionType: string) {
		if (isActionOfType(actionType, EdgeMetricDataActions)) {
			this.notify(this.select())
		}
	}

	public onBlacklistChanged(blacklist: BlacklistItem[]) {
		this.storeService.dispatch(calculateNewEdgeMetricData(this.storeService.getState().files, blacklist))
	}

	public onFilesSelectionChanged(files: FileState[]) {
		this.storeService.dispatch(calculateNewEdgeMetricData(files, this.storeService.getState().fileSettings.blacklist))
	}

	public onAttributeTypesChanged(attributeTypes: AttributeTypes) {
		this.storeService.dispatch(
			calculateNewEdgeMetricData(this.storeService.getState().files, this.storeService.getState().fileSettings.blacklist)
		)
	}

	public getMetricNames(): string[] {
		if (!this.storeService.getState().metricData.edgeMetricData) {
			return []
		}

		return this.storeService.getState().metricData.edgeMetricData.map(x => x.name)
	}

	public getAmountOfAffectedBuildings(metricName: string): number {
		if (!nodeEdgeMetricsMap || !nodeEdgeMetricsMap.has(metricName)) {
			return 0
		}
		return nodeEdgeMetricsMap.get(metricName).size
	}

	public getNodesWithHighestValue(metricName: string, numberOfNodes: number): string[] {
		if (!nodeEdgeMetricsMap || !nodeEdgeMetricsMap.has(metricName)) {
			return []
		}

		const highestEdgeCountBuildings: string[] = []
		const edgeMetricMapKeyIterator = nodeEdgeMetricsMap.get(metricName).keys()
		for (let i = 0; i < numberOfNodes; i++) {
			highestEdgeCountBuildings.push(edgeMetricMapKeyIterator.next().value)
		}

		return highestEdgeCountBuildings
	}

	public getMetricValuesForNode(node: HierarchyNode<CodeMapNode>): EdgeMetricCountMap {
		const metricNames = this.getMetricNames().filter(it => !!nodeEdgeMetricsMap.get(it))
		const nodeEdgeMetrics = new Map()

		metricNames.forEach(metric => {
			nodeEdgeMetrics.set(metric, nodeEdgeMetricsMap.get(metric).get(node.data.path))
		})
		return nodeEdgeMetrics
	}

	public getAttributeTypeByMetric(metricName: string): AttributeTypeValue {
		return this.storeService.getState().fileSettings.attributeTypes.edges[metricName]
	}

	private select() {
		return this.storeService.getState().metricData.edgeMetricData
	}

	private notify(newState: EdgeMetricData[]) {
		this.$rootScope.$broadcast(EdgeMetricDataService.EDGE_METRIC_DATA_CHANGED_EVENT, { edgeMetricData: newState })
	}

	public static subscribe($rootScope: IRootScopeService, subscriber: EdgeMetricDataSubscriber) {
		$rootScope.$on(EdgeMetricDataService.EDGE_METRIC_DATA_CHANGED_EVENT, (event, data) => {<commit_msg>Refactor shorten code by combining has and get calls on map #1095<commit_after>import { StoreService, StoreSubscriber } from "../../../store.service"
import { IRootScopeService } from "angular"
import { calculateNewEdgeMetricData, EdgeMetricDataActions } from "./edgeMetricData.actions"
import { isActionOfType } from "../../../../util/reduxHelper"
import { AttributeTypes, AttributeTypeValue, BlacklistItem, CodeMapNode, EdgeMetricData } from "../../../../codeCharta.model"
import { FileState } from "../../../../model/files/files"
import { HierarchyNode } from "d3"
import { BlacklistService, BlacklistSubscriber } from "../../fileSettings/blacklist/blacklist.service"
import { FilesSelectionSubscriber, FilesService } from "../../files/files.service"
import { EdgeMetricCountMap, nodeEdgeMetricsMap } from "./edgeMetricData.reducer"
import { AttributeTypesService, AttributeTypesSubscriber } from "../../fileSettings/attributeTypes/attributeTypes.service"

export interface EdgeMetricDataSubscriber {
	onEdgeMetricDataChanged(edgeMetricData: EdgeMetricData[])
}

export class EdgeMetricDataService implements StoreSubscriber, BlacklistSubscriber, FilesSelectionSubscriber, AttributeTypesSubscriber {
	private static EDGE_METRIC_DATA_CHANGED_EVENT = "edge-metric-data-changed"
	public static NONE_METRIC = "None"

	constructor(private $rootScope: IRootScopeService, private storeService: StoreService) {
		StoreService.subscribe(this.$rootScope, this)
		BlacklistService.subscribe(this.$rootScope, this)
		FilesService.subscribe(this.$rootScope, this)
		AttributeTypesService.subscribe(this.$rootScope, this)
	}

	public onStoreChanged(actionType: string) {
		if (isActionOfType(actionType, EdgeMetricDataActions)) {
			this.notify(this.select())
		}
	}

	public onBlacklistChanged(blacklist: BlacklistItem[]) {
		this.storeService.dispatch(calculateNewEdgeMetricData(this.storeService.getState().files, blacklist))
	}

	public onFilesSelectionChanged(files: FileState[]) {
		this.storeService.dispatch(calculateNewEdgeMetricData(files, this.storeService.getState().fileSettings.blacklist))
	}

	public onAttributeTypesChanged(attributeTypes: AttributeTypes) {
		this.storeService.dispatch(
			calculateNewEdgeMetricData(this.storeService.getState().files, this.storeService.getState().fileSettings.blacklist)
		)
	}

	public getMetricNames(): string[] {
		return this.storeService.getState().metricData.edgeMetricData.map(x => x.name)
	}

	public getAmountOfAffectedBuildings(metricName: string): number {
		const nodeEdgeMetrics = nodeEdgeMetricsMap.get(metricName)
		return nodeEdgeMetrics === undefined ? 0 : nodeEdgeMetrics.size
	}

	public getNodesWithHighestValue(metricName: string, numberOfNodes: number): string[] {
		const nodeEdgeMetrics = nodeEdgeMetricsMap.get(metricName)

		if (!nodeEdgeMetrics) {
			return []
		}

		const highestEdgeCountBuildings: string[] = []
		const edgeMetricMapKeyIterator = nodeEdgeMetrics.keys()
		for (let i = 0; i < numberOfNodes; i++) {
			highestEdgeCountBuildings.push(edgeMetricMapKeyIterator.next().value)
		}

		return highestEdgeCountBuildings
	}

	public getMetricValuesForNode(node: HierarchyNode<CodeMapNode>): EdgeMetricCountMap {
		const metricNames = this.getMetricNames().filter(it => !!nodeEdgeMetricsMap.get(it))
		const nodeEdgeMetrics = new Map()

		metricNames.forEach(metric => {
			nodeEdgeMetrics.set(metric, nodeEdgeMetricsMap.get(metric).get(node.data.path))
		})
		return nodeEdgeMetrics
	}

	public getAttributeTypeByMetric(metricName: string): AttributeTypeValue {
		return this.storeService.getState().fileSettings.attributeTypes.edges[metricName]
	}

	private select() {
		return this.storeService.getState().metricData.edgeMetricData
	}

	private notify(newState: EdgeMetricData[]) {
		this.$rootScope.$broadcast(EdgeMetricDataService.EDGE_METRIC_DATA_CHANGED_EVENT, { edgeMetricData: newState })
	}

	public static subscribe($rootScope: IRootScopeService, subscriber: EdgeMetricDataSubscriber) {
		$rootScope.$on(EdgeMetricDataService.EDGE_METRIC_DATA_CHANGED_EVENT, (event, data) => {
{'fraction_non_alphanumeric': 0.1156579269786817, 'fraction_numerical': 0.0012251899044351876, 'mean_word_length': 6.185739436619718, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5664767', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2331, 'n_words': 411}