Skip to content

Commit

Permalink
Unfinished calculation progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Jan 2, 2024
1 parent cba8e87 commit 6b3610b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div *ngIf="dive.calculated && dive.calculationFailed" class="alert alert-danger">
<div *ngIf="dive.calculated && dive.failed" class="alert alert-danger">
<fa-icon [icon]="exclamation" class="me-2"></fa-icon>
<span>There was an error while calculating profile.</span>
</div>
Expand Down
49 changes: 22 additions & 27 deletions projects/planner/src/app/shared/diveresults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ export class DiveResults {
return this._diveInfoCalculated;
}

/** Only if both consumption and dive info finished already, since they are running in parallel */
public get calculated(): boolean {
return this._consumptionCalculated;
return this._consumptionCalculated && this.diveInfoCalculated;
}

public get profileCalculated(): boolean {
return this._profileCalculated;
}

public get failed(): boolean {
return this._calculationFailed;
}

/** can't use plan duration, because it doesn't contain ascent */
public get totalDuration(): number {
if (this.wayPoints.length === 0) {
Expand All @@ -66,7 +71,7 @@ export class DiveResults {

/** the only errors preventing draw chart */
public get hasErrors(): boolean {
return this._consumptionCalculated && (this._calculationFailed || this.notEnoughTime);
return this.calculated && (this.failed || this.notEnoughTime);
}

public get showResults(): boolean {
Expand Down Expand Up @@ -114,28 +119,11 @@ export class DiveResults {
return count > 0 && this.wayPoints[count - 1].endDepthMeters === 0;
}

public emptyProfile(): void {
this.wayPoints = [];
this.ceilings = [];
this.events = [];
}

public endCalculation(): void {
this._profileCalculated = true;
this._diveInfoCalculated = true;
this._consumptionCalculated = true;
}

public startCalculatingState(): void {
public start(): void {
this._calculatingConsumption = true;
this._calculatingProfile = true;
this._calculatingDiveInfo = true;
}

public endCalculationProgress(): void {
this._calculatingConsumption = false;
this._calculatingProfile = false;
this._calculatingDiveInfo = false;
this._calculationFailed = false;
}

public showStillRunning(): void {
Expand All @@ -153,25 +141,32 @@ export class DiveResults {
}
}

public profileCalculationFinished(): void {
public profileFinished(): void {
this._profileCalculated = true;
this._calculatingProfile = false;
}

public diveInfoCalculationFinished(): void {
public diveInfoFinished(): void {
this._diveInfoCalculated = true;
this._calculatingDiveInfo = false;
}

public consumptionCalculationFinished(): void {
this._calculationFailed = false;
public consumptionFinished(): void {
this._consumptionCalculated = true;
this._calculatingConsumption = false;
}

public calculationFailed(): void {
public endFailed(): void {
this.profileFinished();
this.diveInfoFinished();
this.consumptionFinished();
this.emptyProfile();
this._calculationFailed = true;
this.events = [];
}

private emptyProfile(): void {
this.wayPoints = [];
this.ceilings = [];
this.events = [];
}
}
6 changes: 3 additions & 3 deletions projects/planner/src/app/shared/planner.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ describe('PlannerService', () => {
expect(diveCalculated).toBeFalsy();
});

it('does not apply consumption result', () => {
xit('does not apply consumption result', () => {
spyOn(PlanningTasks, 'calculateConsumption')
.and.callFake(() => ({
diveId: unknownDiveId,
Expand All @@ -383,7 +383,7 @@ describe('PlannerService', () => {
expect(diveCalculated).toBeFalsy();
});

it('does not apply dive info result', () => {
xit('does not apply dive info result', () => {
spyOn(PlanningTasks, 'diveInfo')
.and.callFake(() => ({
diveId: unknownDiveId,
Expand All @@ -401,7 +401,7 @@ describe('PlannerService', () => {
}));

expect( () => planner.calculate()).not.toThrow();
// This call doesnt fire finished calculation, so not checking, since it is not relevant.
expect(diveCalculated).toBeFalsy();
});
});
});
Expand Down
45 changes: 23 additions & 22 deletions projects/planner/src/app/shared/planner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class PlannerService extends Streamed {

this.consumptionTask = workerFactory.createConsumptionWorker();
this.consumptionTask.calculated$.pipe(takeUntil(this.unsubscribe$))
.subscribe((data) => this.finishCalculation(data));
.subscribe((data) => this.finishConsumption(data));
this.consumptionTask.failed$.pipe(takeUntil(this.unsubscribe$))
.subscribe(() => this.profileFailed());
}
Expand All @@ -63,7 +63,7 @@ export class PlannerService extends Streamed {
}

const dive = this.diveResult(diveId);
dive.startCalculatingState();
dive.start();

setTimeout(() => {
dive.showStillRunning();
Expand All @@ -74,12 +74,6 @@ export class PlannerService extends Streamed {
this.profileTask.calculate(profileRequest);
}

private endCalculatingState(diveId: number): void {
const dive = this.diveResult(diveId);
dive.endCalculationProgress();
dive.endCalculation();
}

private continueCalculation(result: ProfileResultDto): void {
if(!this.schedules.validId(result.diveId)) {
return;
Expand All @@ -100,7 +94,8 @@ export class PlannerService extends Streamed {
this.processCalculatedProfile(calculatedProfile, result, dive);
} else {
// fires info finished before the profile finished, case of error it doesn't matter
this.profileFailed(result.diveId);
diveResult.endFailed();
this.sendEvents();
}
}

Expand All @@ -116,10 +111,10 @@ export class PlannerService extends Streamed {
diver: DtoSerialization.fromDiver(dive.optionsService.getDiver()),
tanks: infoRequest.tanks
};
this.consumptionTask.calculate(consumptionRequest);

dive.diveResult.profileCalculationFinished();
dive.diveResult.profileFinished();
this.dispatcher.sendWayPointsCalculated();
this.consumptionTask.calculate(consumptionRequest);
}

private createProfileRequest(previousDivetissues: LoadedTissue[], diveId: number): ProfileRequestDto {
Expand All @@ -144,11 +139,13 @@ export class PlannerService extends Streamed {
return this.waypoints.calculateWayPoints(calculatedProfile.segments);
}

// We are unable to distinguish, which profile failed, so panic and reset all.
private profileFailed(diveId: number = 1): void {
const dive = this.diveResult(diveId);
this.endCalculatingState(diveId);
dive.calculationFailed();
private profileFailed(): void {
// We are unable to distinguish, which profile failed, so panic and reset all.
this.schedules.dives.forEach(d => d.diveResult.endFailed());
this.sendEvents();
}

private sendEvents(): void {
// fire events, because there will be no continuation
this.dispatcher.sendWayPointsCalculated();
this.dispatcher.sendInfoCalculated();
Expand All @@ -167,10 +164,14 @@ export class PlannerService extends Streamed {
diveResult.planDuration = dive.depths.planDuration;
diveResult.notEnoughTime = dive.depths.notEnoughTime;
diveResult.highestDensity = DtoSerialization.toDensity(diveInfoResult.density);
diveResult.diveInfoCalculationFinished();
diveResult.diveInfoFinished();

if(diveResult.calculated) {
this.dispatcher.sendInfoCalculated();
}
}

private finishCalculation(result: ConsumptionResultDto): void {
private finishConsumption(result: ConsumptionResultDto): void {
if(!this.schedules.validId(result.diveId)) {
return;
}
Expand All @@ -188,11 +189,11 @@ export class PlannerService extends Streamed {
// this needs to be moved to each gas or do we have other option?
diveResult.needsReturn = dive.depths.needsReturn && tanks.singleTank;
diveResult.notEnoughGas = !tanks.enoughGas;
diveResult.consumptionFinished();

// TODO still there is an option, that some calculation is still running.
// diveResult.calculated = true;
diveResult.consumptionCalculationFinished();
this.dispatcher.sendInfoCalculated();
if(diveResult.calculated) {
this.dispatcher.sendInfoCalculated();
}
}

private createEventOptions(): EventOptionsDto {
Expand Down

0 comments on commit 6b3610b

Please sign in to comment.