Skip to content

Commit

Permalink
refactor: create class for transforming report to hierarchy structure…
Browse files Browse the repository at this point in the history
… for reusability (#499)
  • Loading branch information
MatthijsSmets authored Jul 12, 2024
1 parent a79cb97 commit 6a22f31
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 72 deletions.
74 changes: 2 additions & 72 deletions src/app/debug/debug-tree/debug-tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import {
NgbDropdownToggle,
} from '@ng-bootstrap/ng-bootstrap';
import { ButtonComponent } from '../../shared/components/button/button.component';
import { Checkpoint } from '../../shared/interfaces/checkpoint';
import { CheckpointType } from '../../shared/enums/checkpoint-type';
import { ReportHierarchyTransformer } from '../../shared/classes/report-hierarchy-transformer';

@Component({
selector: 'app-debug-tree',
Expand Down Expand Up @@ -143,7 +142,7 @@ export class DebugTreeComponent implements OnDestroy {
if (!this.showMultipleAtATime) {
this.tree.clearItems();
}
const newReport: CreateTreeItem = this.transformReportToHierarchyStructure(report);
const newReport: CreateTreeItem = new ReportHierarchyTransformer().transform(report);
const optional: OptionalParameters = { childrenKey: 'checkpoints', pathAttribute: 'uid' };
const path: string = this.tree.addItem(newReport, optional);
this.tree.selectItem(path);
Expand All @@ -152,75 +151,6 @@ export class DebugTreeComponent implements OnDestroy {
}
}

transformReportToHierarchyStructure(report: Report): Report {
const checkpoints = report.checkpoints;
let checkpointsTemplate: Checkpoint[] = [];
let startPointStack: Checkpoint[] = [];

for (const checkpoint of checkpoints) {
checkpoint.icon = this.helperService.getImage(checkpoint.type, checkpoint.encoding, checkpoint.level);

if (checkpoint.type === CheckpointType.Startpoint) {
this.handleStartpoint(checkpoint, checkpointsTemplate, startPointStack);
} else if (checkpoint.type === CheckpointType.Endpoint) {
this.handleEndpoint(checkpoint, checkpointsTemplate, startPointStack);
} else {
this.handleIntermediatePoint(checkpoint, checkpointsTemplate, startPointStack);
}
}

report.checkpoints = checkpointsTemplate;
return report;
}

private handleStartpoint(
checkpoint: Checkpoint,
checkpointsTemplate: Checkpoint[],
startPointStack: Checkpoint[],
): void {
if (startPointStack.length > 0) {
this.addCheckpointToParent(checkpoint, startPointStack);
} else {
checkpointsTemplate.push(checkpoint);
}
startPointStack.push(checkpoint);
}

private handleEndpoint(
checkpoint: Checkpoint,
checkpointsTemplate: Checkpoint[],
startPointStack: Checkpoint[],
): void {
if (startPointStack.length > 0) {
const currentStartpoint = startPointStack.pop();
if (currentStartpoint) {
this.addCheckpointToParent(checkpoint, [currentStartpoint]);
}
} else {
checkpointsTemplate.push(checkpoint);
}
}

private handleIntermediatePoint(
checkpoint: Checkpoint,
checkpointsTemplate: Checkpoint[],
startPointStack: Checkpoint[],
): void {
if (startPointStack.length > 0) {
this.addCheckpointToParent(checkpoint, startPointStack);
} else {
checkpointsTemplate.push(checkpoint);
}
}

private addCheckpointToParent(checkpoint: Checkpoint, startPointStack: Checkpoint[]): void {
const parentStartpoint = startPointStack.at(-1)!;
if (!parentStartpoint.checkpoints) {
parentStartpoint.checkpoints = [];
}
parentStartpoint.checkpoints.push(checkpoint);
}

selectReport(value: FileTreeItem): void {
this.selectReportEvent.emit(value.originalValue);
}
Expand Down
81 changes: 81 additions & 0 deletions src/app/shared/classes/report-hierarchy-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Report } from '../interfaces/report';
import { Checkpoint } from '../interfaces/checkpoint';
import { CHECKPOINT_TYPE_STRINGS, CheckpointType } from '../enums/checkpoint-type';

export class ReportHierarchyTransformer {
private readonly THROWABLE_ENCODER: string = 'printStackTrace()';
private checkpointsTemplate: Checkpoint[] = [];
private startPointStack: Checkpoint[] = [];

transform(report: Report): Report {
const checkpoints: Checkpoint[] = report.checkpoints;

for (const checkpoint of checkpoints) {
checkpoint.icon = this.getImage(checkpoint.type, checkpoint.encoding, checkpoint.level);

if (checkpoint.type === CheckpointType.Startpoint) {
this.handleStartpoint(checkpoint);
} else if (checkpoint.type === CheckpointType.Endpoint) {
this.handleEndpoint(checkpoint);
} else {
this.handleIntermediatePoint(checkpoint);
}
}

report.checkpoints = this.checkpointsTemplate;
return report;
}

private handleStartpoint(checkpoint: Checkpoint): void {
if (this.startPointStack.length > 0) {
this.addCheckpointToParent(checkpoint, this.startPointStack);
} else {
this.checkpointsTemplate.push(checkpoint);
}
this.startPointStack.push(checkpoint);
}

private handleEndpoint(checkpoint: Checkpoint): void {
if (this.startPointStack.length > 0) {
const currentStartpoint = this.startPointStack.pop();
if (currentStartpoint) {
this.addCheckpointToParent(checkpoint, [currentStartpoint]);
}
} else {
this.checkpointsTemplate.push(checkpoint);
}
}

private handleIntermediatePoint(checkpoint: Checkpoint): void {
if (this.startPointStack.length > 0) {
this.addCheckpointToParent(checkpoint, this.startPointStack);
} else {
this.checkpointsTemplate.push(checkpoint);
}
}

private addCheckpointToParent(checkpoint: Checkpoint, startPointStack: Checkpoint[]): void {
const parentStartpoint = startPointStack.at(-1)!;
if (!parentStartpoint.checkpoints) {
parentStartpoint.checkpoints = [];
}
parentStartpoint.checkpoints.push(checkpoint);
}

getImage(type: CheckpointType, encoding: string, level: number): string {
const even: boolean = this.determineEvenCheckpoint(level);
let img = `assets/tree-icons/${CHECKPOINT_TYPE_STRINGS[type]}`;
if (encoding === this.THROWABLE_ENCODER) {
img += '-error';
}

if (even) {
return `${img}-even.gif`;
}
return `${img}-odd.gif`;
}

private determineEvenCheckpoint(level: number): boolean {
return level % 2 == 0;
}
}

0 comments on commit 6a22f31

Please sign in to comment.