-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pipeline DAG: bottom-up design instead of top-down (#86)
* bottom-up design instead of top-down * use maxInArray
- Loading branch information
1 parent
93851b7
commit c210ef5
Showing
1 changed file
with
41 additions
and
26 deletions.
There are no files selected for viewing
67 changes: 41 additions & 26 deletions
67
src/app/webapp-common/shared/services/dag-manager-unsorted.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,56 @@ | ||
import { Injectable } from '@angular/core'; | ||
import {DagManagerService, DagModelItem} from '@ngneat/dag'; | ||
import { DagManagerService, DagModelItem } from '@ngneat/dag'; | ||
import { maxInArray } from '@common/shared/utils/helpers.util'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class DagManagerUnsortedService<T extends DagModelItem> extends DagManagerService<T> { | ||
|
||
override convertArrayToDagModel(itemsArray: Array<T>): Array<Array<T>> { | ||
const result = []; | ||
const levels = {}; | ||
const result: Array<Array<T>> = []; | ||
const nodeLevels = new Map<number, number>(); | ||
const nodeChildren = new Map<number, number[]>(); | ||
|
||
const modify = (data, pid = 0, level = 0) => | ||
data | ||
.filter(({ parentIds, stepId }) => { | ||
if (levels[level] && levels[level].includes(stepId)) { | ||
return false; | ||
} | ||
return parentIds.includes(pid); | ||
}) | ||
.forEach((e) => { | ||
if (!levels[level]) { | ||
levels[level] = []; | ||
} | ||
levels[level].push(e.stepId); | ||
// Initialize nodeChildren | ||
itemsArray.forEach(item => { | ||
item.parentIds.forEach(pid => { | ||
if (!nodeChildren.has(pid)) { | ||
nodeChildren.set(pid, []); | ||
} | ||
nodeChildren.get(pid).push(item.stepId); | ||
}); | ||
}); | ||
|
||
if (this.findInDoubleArray(e.stepId, result) === -1) { | ||
if (!result[level]) { | ||
result[level] = [e]; | ||
} else { | ||
result[level].push(e); | ||
} | ||
} | ||
|
||
modify(data, e.stepId, level + 1); | ||
// Function to assign levels | ||
const assignLevels = (item, currentLevel) => { | ||
const existingLevel = nodeLevels.get(item.stepId); | ||
if (existingLevel === undefined || existingLevel < currentLevel) { | ||
nodeLevels.set(item.stepId, currentLevel); | ||
const parents = itemsArray.filter(i => item.parentIds.includes(i.stepId)); | ||
parents.forEach(parent => { | ||
assignLevels(parent, currentLevel + 1); | ||
}); | ||
} | ||
}; | ||
|
||
// Start from leaf nodes and work upwards | ||
itemsArray.forEach(item => { | ||
if (!nodeChildren.has(item.stepId)) { | ||
assignLevels(item, 0); | ||
} | ||
}); | ||
|
||
// Sort nodes by level and position them | ||
const maxLevel = maxInArray(Array.from(nodeLevels.values())); | ||
itemsArray.forEach(item => { | ||
const level = maxLevel - nodeLevels.get(item.stepId); | ||
if (!result[level]) { | ||
result[level] = []; | ||
} | ||
result[level].push(item); | ||
}); | ||
|
||
modify(itemsArray); | ||
return result; | ||
} | ||
} |