Skip to content

Commit

Permalink
feature(FeatureAmounts): Add Feature Amount to shapefile upload calcu…
Browse files Browse the repository at this point in the history
…lation PR Tweaks
  • Loading branch information
KevSanchez committed Nov 3, 2023
1 parent f7b4656 commit e94838f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
24 changes: 16 additions & 8 deletions api/apps/api/src/modules/geo-features/geo-features.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
FeatureAmountsPerPlanningUnitService,
} from '@marxan/feature-amounts-per-planning-unit';
import { ComputeFeatureAmountPerPlanningUnit } from '@marxan/feature-amounts-per-planning-unit/feature-amounts-per-planning-units.service';
import { CHUNK_SIZE_FOR_BATCH_GEODB_OPERATIONS } from '@marxan-geoprocessing/utils/chunk-size-for-batch-geodb-operations';
import { CHUNK_SIZE_FOR_BATCH_APIDB_OPERATIONS } from '@marxan-api/utils/chunk-size-for-batch-apidb-operations';

const geoFeatureFilterKeyNames = [
'featureClassName',
Expand Down Expand Up @@ -431,10 +431,14 @@ export class GeoFeaturesService extends AppBaseService<
computedFeatureAmounts,
);

await this.saveAmountRangeForFeatures(
apiQueryRunner.manager,
geoQueryRunner.manager,
[geoFeature.id],
);

await apiQueryRunner.commitTransaction();
await geoQueryRunner.commitTransaction();

await this.saveAmountRangeForFeatures([geoFeature.id]);
} catch (err) {
await apiQueryRunner.rollbackTransaction();
await geoQueryRunner.rollbackTransaction();
Expand Down Expand Up @@ -468,7 +472,7 @@ export class GeoFeaturesService extends AppBaseService<
amount,
projectPuId,
})),
{ chunk: CHUNK_SIZE_FOR_BATCH_GEODB_OPERATIONS },
{ chunk: CHUNK_SIZE_FOR_BATCH_APIDB_OPERATIONS },
);
}

Expand Down Expand Up @@ -882,10 +886,14 @@ export class GeoFeaturesService extends AppBaseService<
} as GeoFeature;
}

async saveAmountRangeForFeatures(featureIds: string[]) {
async saveAmountRangeForFeatures(
apiEntityManager: EntityManager,
geoEntityManager: EntityManager,
featureIds: string[],
) {
this.logger.log(`Saving min and max amounts for new features...`);

const minAndMaxAmountsForFeatures = await this.geoEntityManager
const minAndMaxAmountsForFeatures = await geoEntityManager
.createQueryBuilder()
.select('feature_id', 'id')
.addSelect('MIN(amount)', 'amountMin')
Expand All @@ -896,7 +904,7 @@ export class GeoFeaturesService extends AppBaseService<
.getRawMany();

if (minAndMaxAmountsForFeatures.length === 0) {
return;
throw new Error('Error saving Min/Max amounts for given features ');
}

const minMaxSqlValueStringForFeatures = minAndMaxAmountsForFeatures
Expand All @@ -915,7 +923,7 @@ export class GeoFeaturesService extends AppBaseService<
${minMaxSqlValueStringForFeatures}
) as minmax(feature_id, min, max)
where features.id = minmax.feature_id;`;
await this.geoFeaturesRepository.query(query);
await apiEntityManager.query(query);
}

async checkProjectFeatureVisibility(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { UploadedFeatureAmount } from '@marxan-api/modules/geo-features/import/f
@Injectable()
export class FeatureAmountUploadService {
private readonly logger = new Logger(this.constructor.name);

constructor(
@InjectDataSource(DbConnections.default)
private readonly apiDataSource: DataSource,
Expand Down Expand Up @@ -107,15 +108,17 @@ export class FeatureAmountUploadService {
`Upload temporary data removed from apiDB uploads tables`,
);

this.logger.log(`Csv file upload process finished successfully`);
// Committing transaction
await apiQueryRunner.commitTransaction();
await geoQueryRunner.commitTransaction();

this.logger.log(`Saving min and max amounts for new features...`);
await this.geoFeaturesService.saveAmountRangeForFeatures(
apiQueryRunner.manager,
geoQueryRunner.manager,
newFeaturesFromCsvUpload.map((feature) => feature.id),
);

this.logger.log(`Csv file upload process finished successfully`);
// Committing transaction
await apiQueryRunner.commitTransaction();
await geoQueryRunner.commitTransaction();
} catch (err) {
await this.events.failEvent(err);
await apiQueryRunner.rollbackTransaction();
Expand All @@ -134,6 +137,7 @@ export class FeatureAmountUploadService {
}
return right(newFeaturesFromCsvUpload);
}

async saveCsvToRegistry(
data: {
fileBuffer: Buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ export class FeatureAmountsPerPlanningUnitService {
public async computeMarxanAmountPerPlanningUnit(
featureId: string,
projectId: string,
geoEntityManager?: EntityManager,
geoEntityManager: EntityManager = this.geoEntityManager,
): Promise<ComputeFeatureAmountPerPlanningUnit[]> {
/**
* @TODO further performance savings: limiting scans to planning_units_geom
* by partition (we need to get the grid shape from the parent project); use
* && operator instead of st_intersects() for bbox-based calculation of
* intersections.
*/
geoEntityManager = geoEntityManager
? geoEntityManager
: this.geoEntityManager;

const rows: {
featureid: string;
puid: number;
Expand Down

1 comment on commit e94838f

@vercel
Copy link

@vercel vercel bot commented on e94838f Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

marxan – ./

marxan23.vercel.app
marxan-git-develop-vizzuality1.vercel.app
marxan-vizzuality1.vercel.app

Please sign in to comment.