Skip to content

Commit

Permalink
Refactor feature csv upload service
Browse files Browse the repository at this point in the history
  • Loading branch information
yulia-bel committed Oct 19, 2023
1 parent 7172b6a commit 4268a88
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
20 changes: 17 additions & 3 deletions api/apps/api/src/modules/geo-features/geo-features.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,22 @@ export class GeoFeaturesService extends AppBaseService<
.groupBy('puvspr.feature_id')
.getRawMany();

await this.geoFeaturesRepository.upsert(minAndMaxAmountsForFeatures, [
'id',
]);
const minMaxSqlValueStringForFeatures = minAndMaxAmountsForFeatures
.map(
(feature) =>
`(uuid('${feature.id}'), ${feature.amountMin}, ${feature.amountMax})`,
)
.join(', ');

const query = `
update features set
amount_min = minmax.min,
amount_max = minmax.max
from (
values
${minMaxSqlValueStringForFeatures}
) as minmax(feature_id, min, max)
where features.id = minmax.feature_id;`;
await this.geoFeaturesRepository.query(query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ export class FeatureAmountUploadService {

this.logger.log(`Saving min and max amounts for new features...`);

await this.geoFeaturesService.saveAmountRangeForFeatures(
newFeaturesFromCsvUpload.map((feature) => feature.id),
);

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

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

await this.geoFeaturesService.saveAmountRangeForFeatures(
newFeaturesFromCsvUpload.map((feature) => feature.id),
);
} catch (err) {
await this.events.failEvent(err);
await apiQueryRunner.rollbackTransaction();
Expand Down Expand Up @@ -334,6 +334,10 @@ export class FeatureAmountUploadService {
`,
parameters,
);
await geoQueryRunner.manager.query(
` INSERT INTO puvspr_calculations (project_id, feature_id, amount, project_pu_id) select $1, $2, amount, project_pu_id from features_data where feature_id = $2`,
[projectId, newFeature.id],
);
this.logger.log(
`Chunk with index ${amountIndex} saved to (geoDB).features_data`,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
puid,feat_1d666bd,feat_28135ef
1,4.245387225,0
2,4.245387225,0
3,4.245387225,0
3,3.245387225,0
7 changes: 6 additions & 1 deletion api/apps/api/test/upload-feature/upload-feature.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ export const getFixtures = async () => {
expect(newFeaturesAdded[0].projectId).toBe(projectId);
expect(newFeaturesAdded[0].isLegacy).toBe(true);
expect(newFeaturesAdded[1].isLegacy).toBe(true);
expect(newFeaturesAdded[0].amountMin).toEqual(3.245387225);
expect(newFeaturesAdded[0].amountMax).toEqual(4.245387225);
},

ThenNewFeaturesAmountsAreCreated: async () => {
Expand All @@ -322,6 +324,9 @@ export const getFixtures = async () => {
});
const newFeature1Amounts = await featuresAmounsGeoDbRepository.find({
where: { featureId: newFeatures1?.id },
order: {
amount: 'DESC',
},
});
const newFeature2Amounts = await featuresAmounsGeoDbRepository.find({
where: { featureId: newFeatures2?.id },
Expand All @@ -331,7 +336,7 @@ export const getFixtures = async () => {
expect(newFeature2Amounts).toHaveLength(3);
expect(newFeature1Amounts[0].amount).toBe(4.245387225);
expect(newFeature1Amounts[1].amount).toBe(4.245387225);
expect(newFeature1Amounts[2].amount).toBe(4.245387225);
expect(newFeature1Amounts[2].amount).toBe(3.245387225);

expect(newFeature2Amounts[0].amount).toBe(0);
expect(newFeature2Amounts[1].amount).toBe(0);
Expand Down

0 comments on commit 4268a88

Please sign in to comment.