Skip to content

Commit

Permalink
fix issues with BlmValuesCalculator
Browse files Browse the repository at this point in the history
  • Loading branch information
aciddaute authored and hotzevzl committed Jan 5, 2022
1 parent 5881e21 commit 9ecda96
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import {
invalidRange,
updateFailure,
} from './change-blm-range.command';
import { SetProjectBlm } from './set-project-blm';
import { PlanningUnitAreaFetcher } from './planning-unit-area-fetcher';
import { BlmValuesPolicyFactory } from './blm-values-policy-factory';

@CommandHandler(ChangeBlmRange)
export class ChangeBlmRangeHandler
implements IInferredCommandHandler<ChangeBlmRange> {
private readonly logger: Logger = new Logger(SetProjectBlm.name);
private readonly logger: Logger = new Logger(ChangeBlmRange.name);

constructor(
private readonly blmRepository: ProjectBlmRepo,
Expand Down Expand Up @@ -48,7 +47,7 @@ export class ChangeBlmRangeHandler
}

const calculator = this.blmPolicyFactory.get();
const blmValues = calculator.with(range, result.right);
const blmValues = calculator.with(range);

const updateResult = await this.blmRepository.update(
projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,39 @@ describe('blm-values-calculator', () => {
const blmValuesCalculator = new BlmValuesCalculator();

it('should calculate the correct default values', async () => {
const defaultValues = blmValuesCalculator.withDefaultRange(1500);
const defaultValues = blmValuesCalculator.withDefaultRange();

expect(defaultValues).toStrictEqual([
0.03872983346207417,
606.799665767047,
1213.5606017006319,
1820.3215376342168,
2427.0824735678016,
3033.8434095013868,
0.001,
20.0008,
40.0006,
60.0004,
80.0002,
100,
]);
});

it('should calculate the correct values with a given range', async () => {
const values = blmValuesCalculator.with([1, 50], 1500);
const values = blmValuesCalculator.with([0, 50]);

expect(values).toStrictEqual([
38.72983346207417,
316.2936399402723,
593.8574464184705,
871.4212528966688,
1148.985059374867,
1426.548865853065,
]);
expect(values).toStrictEqual([0, 10, 20, 30, 40, 50]);
});

it('should return values within the given range', async () => {
const ranges: [number, number][] = [
[0, 1],
[0, 10],
[10, 20],
[100, 1000],
[0, 100000],
];

ranges.forEach(([min, max]) => {
const values = blmValuesCalculator.with([min, max]);
values.forEach((value) => {
expect(value).toBeGreaterThanOrEqual(min);
expect(value).toBeLessThanOrEqual(max);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ export class BlmValuesCalculator {
private range: [number, number] = [0.001, 100],
) {}

private execute(area: number) {
private execute() {
const [min, max] = this.range;
const initialArray = Array(this.cardinality - 1)
.fill(0)
.map((_, i) => i + 1);

const formulaResults = initialArray.map(
(i) => min + ((max - min) / this.cardinality - 1) * i,
(i) => min + ((max - min) / (this.cardinality - 1)) * i,
);
const blmValues = [min, ...formulaResults];

return blmValues.map((value) => value * Math.sqrt(area));
return blmValues.map((value) => value);
}

with(range: [number, number], area: number) {
with(range: [number, number]) {
this.range = range;
return this.execute(area);
return this.execute();
}

withDefaultRange(area: number) {
return this.execute(area);
withDefaultRange() {
return this.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Logger } from '@nestjs/common';

import { SetProjectBlm } from './set-project-blm';
import { ProjectBlmRepo } from '@marxan-api/modules/blm';
import { PlanningUnitAreaFetcher } from '@marxan-api/modules/projects/blm/planning-unit-area-fetcher';
import { PlanningUnitAreaFetcher } from './planning-unit-area-fetcher';
import { BlmValuesPolicyFactory } from './blm-values-policy-factory';

@CommandHandler(SetProjectBlm)
Expand All @@ -29,7 +29,7 @@ export class SetProjectBlmHandler
return;
}
const calculator = this.blmPolicyFactory.get();
const defaultBlm = calculator.withDefaultRange(areaResult.right);
const defaultBlm = calculator.withDefaultRange();

const result = await this.blmRepository.create(projectId, defaultBlm);
if (isLeft(result))
Expand Down

0 comments on commit 9ecda96

Please sign in to comment.