Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | 106x 106x 106x 106x 106x 103x 600x 600x 600x 10x 5x 5x 103x 103x 103x 103x 103x 600x 600x 600x 2x 600x 392x 600x 895x 441x 895x 12x 600x 254x 600x 196x 192x 600x 81x 433x 81x 35x 35x 35x 35x 205x 35x 170x 638x 638x 638x 50x 50x 588x 588x 28x 28x 170x 14x 14x 14x 170x 100x 100x 100x 100x 100x 50x 50x 50x 50x 50x 50x 35x 22x 11x 11x 35x 131x 11x 11x 2x 600x 600x 6x 6x 6x 6x 6x 1x 100x 6x 50x 45x 50x 10x 50x 39x 11x 11x 11x 11x 11x 2x 2x 2x 2x 100x 78x 22x 22x 2x 2x 15x 15x 1x 14x 61x 61x 29x 32x 17x 106x 50x 20x 30x 30x 106x 17x 17x 17x 17x 15x 15x 2x 2x 2x 14x 14x 20x 20x 4x 4x 4x 2x 20x 4x 4x 2x 12x | import { hierarchy } from "d3-hierarchy" import { AttributeTypes, AttributeTypeValue, BlacklistItem, CCFile, CodeMapNode, MetricData } from "../codeCharta.model" import { isLeaf, isNodeExcludedOrFlattened } from "./codeMapHelper" import { UNARY_METRIC } from "../state/selectors/accumulatedData/metricData/nodeMetricData.calculator" const enum MedianSelectors { MEDIAN = "MEDIAN", DELTA = "DELTA", INCOMING = "INCOMING", OUTGOING = "OUTGOING" } const enum EdgeAttributeType { INCOMING = "incoming", OUTGOING = "outgoing" } export const NodeDecorator = { decorateMap(map: CodeMapNode, metricData: Pick<MetricData, "nodeMetricData" | "edgeMetricData">, blacklist: BlacklistItem[]) { for (const { data } of hierarchy(map)) { data.isFlattened = false data.isExcluded = false for (const item of blacklist) { if (item.type === "flatten") { data.isFlattened = data.isFlattened || isNodeExcludedOrFlattened(data, item.path) } else { data.isExcluded = data.isExcluded || (isNodeExcludedOrFlattened(data, item.path) && isLeaf(data)) } } } map.isExcluded = false this.decorateMapWithMetricData(map, metricData) }, decorateMapWithMetricData(map: CodeMapNode, metricData: Pick<MetricData, "nodeMetricData" | "edgeMetricData">) { const { nodeMetricData, edgeMetricData } = metricData let id = 0 for (const { data } of hierarchy(map)) { data.id = id id++ if (data.attributes === undefined) { data.attributes = {} } if (isLeaf(data)) { data.attributes[UNARY_METRIC] = 1 } for (const metric of nodeMetricData) { if (data.attributes[metric.name] === undefined) { data.attributes[metric.name] = 0 } if (data.deltas !== undefined && data.deltas[metric.name] === undefined) { data.deltas[metric.name] = 0 } } if (data.edgeAttributes === undefined) { data.edgeAttributes = {} } for (const metric of edgeMetricData) { if (data.edgeAttributes[metric.name] === undefined) { data.edgeAttributes[metric.name] = { incoming: 0, outgoing: 0 } } } mergeFolderChain(data) } }, 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<string, number[]> = new Map() // TODO: Combine decorateMap, decorateMapWithPathAttribute and this one and // remove the Object.keys calls from then on. They are identical to the // `nodeMetricData` and `edgeMetricData` names. 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) { const value = data.edgeAttributes[name] Iif (!value) { continue } const selector = `${name}${data.path}` const parentSelector = `${name}${parent.data.path}` if (attributeTypes.edges[name] === AttributeTypeValue.relative) { setEdgeMediansToParent( medians, `${MedianSelectors.INCOMING}${selector}`, `${MedianSelectors.INCOMING}${parentSelector}`, data, name, EdgeAttributeType.INCOMING ) setEdgeMediansToParent( medians, `${MedianSelectors.OUTGOING}${selector}`, `${MedianSelectors.OUTGOING}${parentSelector}`, data, name, EdgeAttributeType.OUTGOING ) collectEdgeMediansOnParent( medians, `${MedianSelectors.INCOMING}${parentSelector}`, data, name, EdgeAttributeType.INCOMING ) collectEdgeMediansOnParent( medians, `${MedianSelectors.OUTGOING}${parentSelector}`, data, name, EdgeAttributeType.OUTGOING ) } else { parent.data.edgeAttributes[name].incoming += value.incoming parent.data.edgeAttributes[name].outgoing += value.outgoing } } }) // 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 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 Iif (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<string, number[]>, 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<string, number[]>, 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<string, number[]>, 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<string, number[]>, 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<string, number[]>, parentSelector: string, numbers: number[]) { const median = medians.get(parentSelector) if (median === undefined) { medians.set(parentSelector, numbers) } else { pushSortedArray(median, numbers) } } function collectMedians(medians: Map<string, number[]>, selector: string, child: CodeMapNode, value: number) { // TODO: Check if this should be set if it's not a leaf. const median = medians.get(selector) if (median === undefined) { medians.set(selector, [value]) } else if (isLeaf(child)) { pushSorted(median, value) } } // TODO: Evaluate if sorting in `getMedian` is not better than using a // pre-sorted array. It's a lot less code and should roughly have the same // performance. export 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 } export 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 Iif (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)) } |