import { hierarchy } from "d3-hierarchy" import { AttributeTypes, AttributeTypeValue, CCFile, CodeMapNode, EdgeMetricData, ExcludedNode, MetricData, MetricRule, NodeMetricData } from "../model/codeCharta.model" import { isLeaf } from "./codeMapHelper" import { UNARY_METRIC } from "./metric/unaryMetric" import { createMetricRuleMatcher } from "./metricRule/metricRuleMatcher" import { createExcludeMatcher } from "./nodeRules/excludeMatcher" const enum MedianSelectors { MEDIAN = "MEDIAN", DELTA = "DELTA", INCOMING = "INCOMING", OUTGOING = "OUTGOING" } const enum EdgeAttributeType { INCOMING = "incoming", OUTGOING = "outgoing" } export const NodeDecorator = { /** * Only exclusion is decorated onto the tree: it decides which nodes the map holds, so everything * downstream has to see it. Flattening is answered while the map is laid out instead — see * `flattenPredicateSelector` — so that flattening a node does not rebuild this tree. */ decorateMap( map: CodeMapNode, metricData: Pick, excludedNodes: ExcludedNode[], excludeMetricRules: MetricRule[] = [] ) { const matcher = createExcludeMatcher(excludedNodes) const metricsOnMap = new Set(metricData.nodeMetricData.map(({ name }) => name)) const metricRuleMatcher = createMetricRuleMatcher(excludeMetricRules, metricsOnMap) for (const { data } of hierarchy(map)) { const isLeafNode = isLeaf(data) data.isExcluded = isLeafNode && (matcher.isExcludedLeaf(data.path) || metricRuleMatcher.matches(data.attributes)) } map.isExcluded = false this.decorateMapWithMetricData(map, metricData) }, // The view-state-INDEPENDENT structure pass (Slice 14e-3): assign the ordinal id and merge // single-child folder chains. It reads nothing but the tree shape (no rules, no metrics), so // it is deterministic — the structure lens runs it on its own undecorated tree to own `id -> node` // resolution (`structureIdToNode`) without reaching up to the composing layer or any view state, // which is what structurally breaks the CF #1 cycle. `decorateMapWithMetricData` runs it first so // the decorated tree the composing layer builds carries the exact same ids and merged shape. decorateMapWithStructure(map: CodeMapNode) { let id = 0 for (const { data } of hierarchy(map)) { data.id = id id++ mergeFolderChain(data) } }, decorateMapWithMetricData(map: CodeMapNode, metricData: Pick) { const { nodeMetricData, edgeMetricData } = metricData this.decorateMapWithStructure(map) for (const { data } of hierarchy(map)) { decorateNodeWithMetricData(data, nodeMetricData, edgeMetricData) } }, decorateMapWithPathAttribute(file: CCFile) { for (const node of hierarchy(file.map)) { node.data.path = node.parent ? `${node.parent.data.path}/${node.data.name}` : `/${node.data.name}` } return file }, decorateParentNodesWithAggregatedAttributes(map: CodeMapNode, isDeltaState: boolean, attributeTypes: AttributeTypes) { const medians: Map = new Map() const attributeKeys = Object.keys(map.attributes) const edgeKeys = Object.keys(map.edgeAttributes) hierarchy(map).eachAfter(function decorateNode({ data, parent }) { // skip root if (data.isExcluded || !parent) { return } for (const name of attributeKeys) { const selector = `${name}${data.path}` const parentSelector = `${name}${parent.data.path}` if (attributeTypes.nodes[name] === AttributeTypeValue.relative) { setNodeMediansToParent(medians, selector, parentSelector, data, name, isDeltaState) collectNodeMediansOnParent(medians, parentSelector, data, name, isDeltaState) } else { parent.data.attributes[name] += data.attributes[name] if (isDeltaState && parent.data.deltas) { parent.data.deltas[name] = parent.data.deltas[name] ?? 0 parent.data.deltas[name] += data.deltas[name] ?? 0 } } } if (isDeltaState && parent.data.fileCount) { parent.data.fileCount.added += data.fileCount.added parent.data.fileCount.changed += data.fileCount.changed parent.data.fileCount.removed += data.fileCount.removed } for (const name of edgeKeys) { aggregateEdgeAttribute(medians, data, parent.data, name, attributeTypes) } }) // Set Median for root node for (const name of edgeKeys) { if (attributeTypes.edges[name] === AttributeTypeValue.relative) { map.edgeAttributes[name].incoming = getMedian(medians.get(`${MedianSelectors.INCOMING}${name}${map.path}`)) map.edgeAttributes[name].outgoing = getMedian(medians.get(`${MedianSelectors.OUTGOING}${name}${map.path}`)) } } for (const name of attributeKeys) { if (attributeTypes.nodes[name] === AttributeTypeValue.relative) { map.attributes[name] = getMedian(medians.get(`${MedianSelectors.MEDIAN}${name}${map.path}`)) if (isDeltaState && map.deltas) { map.deltas[name] = getMedian(medians.get(`${MedianSelectors.DELTA}${name}${map.path}`)) } } } } } function decorateNodeWithMetricData(data: CodeMapNode, nodeMetricData: NodeMetricData[], edgeMetricData: EdgeMetricData[]) { data.attributes ??= {} if (isLeaf(data)) { data.attributes[UNARY_METRIC] = 1 } for (const metric of nodeMetricData) { data.attributes[metric.name] ??= 0 if (data.deltas !== undefined) { data.deltas[metric.name] ??= 0 } } data.edgeAttributes ??= {} for (const metric of edgeMetricData) { data.edgeAttributes[metric.name] ??= { incoming: 0, outgoing: 0 } } } function aggregateEdgeAttribute( medians: Map, child: CodeMapNode, parent: CodeMapNode, name: string, attributeTypes: AttributeTypes ) { const value = child.edgeAttributes[name] if (!value) { return } const selector = `${name}${child.path}` const parentSelector = `${name}${parent.path}` if (attributeTypes.edges[name] === AttributeTypeValue.relative) { setEdgeMediansToParent( medians, `${MedianSelectors.INCOMING}${selector}`, `${MedianSelectors.INCOMING}${parentSelector}`, child, name, EdgeAttributeType.INCOMING ) setEdgeMediansToParent( medians, `${MedianSelectors.OUTGOING}${selector}`, `${MedianSelectors.OUTGOING}${parentSelector}`, child, name, EdgeAttributeType.OUTGOING ) collectEdgeMediansOnParent(medians, `${MedianSelectors.INCOMING}${parentSelector}`, child, name, EdgeAttributeType.INCOMING) collectEdgeMediansOnParent(medians, `${MedianSelectors.OUTGOING}${parentSelector}`, child, name, EdgeAttributeType.OUTGOING) } else { parent.edgeAttributes[name].incoming += value.incoming parent.edgeAttributes[name].outgoing += value.outgoing } } function mergeFolderChain(data: CodeMapNode) { // Nodes with only one child which also have children are merged into one node // e.g. a /folder which includes anotherFolder that includes other files or folders // will be merged to a node with path /folder/anotherFolder and children are set accordingly if (data.children?.length === 1 && data.children[0]?.fixedPosition) { return } if (data.children?.length === 1 && data.children[0].children?.length > 0) { const [child] = data.children data.children = child.children data.name += `/${child.name}` data.path += `/${child.name}` if (child.link) { data.link = child.link } } } function collectEdgeMediansOnParent( medians: Map, selector: string, child: CodeMapNode, metricName: string, type: EdgeAttributeType ) { if (child.edgeAttributes[metricName][type] !== 0) { collectMedians(medians, selector, child, child.edgeAttributes[metricName][type]) } } function collectNodeMediansOnParent( medians: Map, parentSelector: string, child: CodeMapNode, metricName: string, isDeltaState: boolean ) { if (child.attributes[metricName] !== 0) { collectMedians(medians, `${MedianSelectors.MEDIAN}${parentSelector}`, child, child.attributes[metricName]) } if (isDeltaState && child.deltas && child.deltas[metricName] !== 0) { collectMedians(medians, `${MedianSelectors.DELTA}${parentSelector}`, child, child.deltas[metricName]) } } function setNodeMediansToParent( medians: Map, selector: string, parentSelector: string, child: CodeMapNode, metricName: string, isDeltaState: boolean ) { if (isLeaf(child)) { return } const numbers = medians.get(`${MedianSelectors.MEDIAN}${selector}`) if (numbers !== undefined) { child.attributes[metricName] = getMedian(numbers) setMediansToParents(medians, `${MedianSelectors.MEDIAN}${parentSelector}`, numbers) } if (isDeltaState && child.deltas) { const deltaNumbers = medians.get(`${MedianSelectors.DELTA}${selector}`) if (deltaNumbers !== undefined) { child.deltas[metricName] = getMedian(deltaNumbers) setMediansToParents(medians, `${MedianSelectors.DELTA}${parentSelector}`, deltaNumbers) } } } function setEdgeMediansToParent( medians: Map, selector: string, parentSelector: string, child: CodeMapNode, metricName: string, type: EdgeAttributeType ) { if (isLeaf(child)) { return } const numbers = medians.get(selector) if (numbers !== undefined) { child.edgeAttributes[metricName][type] = getMedian(numbers) setMediansToParents(medians, parentSelector, numbers) } } function setMediansToParents(medians: Map, parentSelector: string, numbers: number[]) { const median = medians.get(parentSelector) if (median === undefined) { medians.set(parentSelector, numbers) } else { pushSortedArray(median, numbers) } } function collectMedians(medians: Map, selector: string, child: CodeMapNode, value: number) { const median = medians.get(selector) if (median === undefined) { medians.set(selector, [value]) } else if (isLeaf(child)) { pushSorted(median, value) } } function getMedian(numbers: number[]) { if (numbers === undefined || numbers.length === 0) { return 0 } const middle = (numbers.length - 1) / 2 return (numbers[Math.floor(middle)] + numbers[Math.ceil(middle)]) / 2 } function pushSorted(numbers: number[], number: number) { let min = 0 let max = numbers.length - 1 let guess = 0 if (max < 0 || numbers[max] <= number) { numbers.push(number) return } if (numbers[0] >= number) { numbers.unshift(number) return } // Use a binary search to find the correct entry. while (min <= max) { guess = Math.floor((min + max) / 2) if (numbers[guess] < number) { min = guess + 1 } else { max = guess - 1 if (numbers[max] <= number) { numbers.splice(guess, 0, number) return } } } } function pushSortedArray(numbers: number[], toPush: number[]) { let totalPushes = 0 for (let index = 0; index < numbers.length; index++) { let pushCount = 0 while (numbers[index] > toPush[totalPushes]) { pushCount++ totalPushes++ if (totalPushes === toPush.length) { break } } if (pushCount > 0) { numbers.splice(index, 0, ...toPush.slice(totalPushes - pushCount, totalPushes)) if (totalPushes === toPush.length) { return } } } numbers.push(...toPush.slice(totalPushes)) }