Skip to content

Commit

Permalink
LANDGRIF-162 - Add tmp logging to task for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Feb 23, 2025
1 parent 32e2626 commit da1ff18
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/src/modules/indicator-records/indicator-records.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CachedDataModule } from 'modules/cached-data/cached-data.module';
import { ImpactCalculator } from 'modules/indicator-records/services/impact-calculator.service';
import { ImpactQueryBuilder } from 'modules/indicator-records/services/indicator-dependency-manager.service';
import { IndicatorRecordRepository } from 'modules/indicator-records/indicator-record.repository';
import { TasksModule } from '../tasks/tasks.module';

@Module({
imports: [
Expand All @@ -20,6 +21,7 @@ import { IndicatorRecordRepository } from 'modules/indicator-records/indicator-r
MaterialsModule,
SourcingRecordsModule,
CachedDataModule,
TasksModule,
],
controllers: [IndicatorRecordsController],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ImpactCalculationProgressTracker } from 'modules/impact/progress-tracke
import { ImportProgressTrackerFactory } from 'modules/events/import-data-progress/import-progress.tracker.factory';
import { SourcingLocation } from 'modules/sourcing-locations/sourcing-location.entity';
import { AppConfig } from 'utils/app.config';
import { TasksService } from '../../tasks/tasks.service';

/**
* @description: This is PoC (Proof of Concept) for the updated LG methodology v0.1
Expand All @@ -56,6 +57,7 @@ export class ImpactCalculator {
private readonly cachedDataService: CachedDataService,
private readonly dataSource: DataSource,
private readonly importProgressTrackerFactory: ImportProgressTrackerFactory,
private readonly taskService: TasksService,
) {}

async calculateImpactForAllSourcingRecords(
Expand Down Expand Up @@ -519,6 +521,13 @@ export class ImpactCalculator {
SourcingRecordsWithIndicatorRawData[]
> = new Map<string, SourcingRecordsWithIndicatorRawData[]>();
const repository = this.dataSource.getRepository(SourcingLocation);
// For testing purposes, track locations with no production in task, will remove this later
const locationIdsWithNoProduction: string[] = [];
const { id } = await this.taskService.taskRepository.findOneOrFail({
order: {
updatedAt: 'DESC',
},
});

// Group records by location where production is 0 or null
// TODO: We must apply this when harvesting is 0 as well, but given the use of this approach is not straightforward, and how to apply the new values
Expand All @@ -527,10 +536,17 @@ export class ImpactCalculator {
if (!record.production || record.production === 0) {
if (!recordsPorLocation.has(record.sourcingLocationId)) {
recordsPorLocation.set(record.sourcingLocationId, []);
locationIdsWithNoProduction.push(record.sourcingLocationId);
}
recordsPorLocation.get(record.sourcingLocationId)!.push(record);
}
}
if (locationIdsWithNoProduction.length) {
await this.taskService.updateImportTask({
taskId: id,
newData: { locationsWithNoProduction: locationIdsWithNoProduction },
});
}
const dataArray = Array.from(recordsPorLocation.entries());
const promises: Promise<any>[] = dataArray.map(async (elem) => {
// For each location that has no production value, get its geo region
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IndicatorsService } from '../../../../src/modules/indicators/indicators
import { ImpactQueryBuilder } from '../../../../src/modules/indicator-records/services/indicator-dependency-manager.service';
import { CachedDataService } from '../../../../src/modules/cached-data/cached-data.service';
import { ImportProgressTrackerFactory } from '../../../../src/modules/events/import-data-progress/import-progress.tracker.factory';
import { TasksService } from '../../../../src/modules/tasks/tasks.service';

const dummyProvider = { useValue: {} };

Expand All @@ -34,6 +35,17 @@ describe('ImpactCalculator.updateDistributedImpactOverGeoRegion', () => {
providers: [
ImpactCalculator,
{ provide: DataSource, useValue: dataSourceMock },
{
provide: TasksService,
useValue: {
taskRepository: {
findOneOrFail: () => ({
id: 1,
}),
},
updateImportTask: jest.fn(),
},
},
{ provide: IndicatorRecordRepository, ...dummyProvider },
{ provide: MaterialsToH3sService, ...dummyProvider },
{ provide: IndicatorsService, ...dummyProvider },
Expand Down

0 comments on commit da1ff18

Please sign in to comment.