Skip to content

Commit

Permalink
fix: move code to new service AB#23762
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisvisser committed Sep 25, 2023
1 parent 2cb1de9 commit ffbd9be
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EventModule } from '../event/event.module';
import { DisasterEntity } from '../disaster/disaster.entity';
import { CountryEntity } from '../country/country.entity';
import { HelperService } from '../../shared/helper.service';
import { AdminAreaModule } from '../admin-area/admin-area.module';

@Module({
imports: [
Expand All @@ -22,6 +23,7 @@ import { HelperService } from '../../shared/helper.service';
UserModule,
EventModule,
CountryModule,
AdminAreaModule,
],
providers: [AdminAreaDynamicDataService, HelperService],
controllers: [AdminAreaDynamicDataController],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { DisasterType } from '../disaster/disaster-type.enum';
import fs from 'fs';
import { CountryEntity } from '../country/country.entity';
import { HelperService } from '../../shared/helper.service';
import { DateDto } from '../event/dto/date.dto';
import { IndicatorDto } from '../metadata/dto/add-indicators.dto';
import { EventAreaService } from '../admin-area/services/event-area.service';

@Injectable()
export class AdminAreaDynamicDataService {
Expand All @@ -28,6 +27,7 @@ export class AdminAreaDynamicDataService {

public constructor(
private eventService: EventService,
private eventAreaService: EventAreaService,
private helperService: HelperService,
private dataSource: DataSource,
) {}
Expand Down Expand Up @@ -156,46 +156,6 @@ export class AdminAreaDynamicDataService {
return false;
}

private async getEventAreaAggregates(
countryCodeISO3: string,
disasterType: DisasterType,
indicator: DynamicIndicator,
lastTriggeredDate: DateDto,
): Promise<AdminDataReturnDto[]> {
const events = await this.eventService.getEventSummary(
countryCodeISO3,
disasterType,
);

const records = [];
for await (const event of events.filter((e) => e.activeTrigger)) {
const aggregateValue = await this.adminAreaDynamicDataRepo
.createQueryBuilder('dynamic')
.select(
`CASE WHEN dynamic."indicator" = 'alert_threshold' THEN MAX(value) ELSE SUM(value) END as "value"`,
)
.where({
timestamp: MoreThanOrEqual(
this.helperService.getUploadCutoffMoment(
disasterType,
lastTriggeredDate.timestamp,
),
),
disasterType: disasterType,
indicator: indicator,
eventName: event.eventName,
})
.groupBy('dynamic."indicator"')
.getRawOne();

const record = new AdminDataReturnDto();
record.placeCode = event.eventName;
record.value = aggregateValue.value;
records.push(record);
}
return records;
}

public async getAdminAreaDynamicData(
countryCodeISO3: string,
adminLevel: string,
Expand All @@ -210,7 +170,7 @@ export class AdminAreaDynamicDataService {
);

if (disasterType === DisasterType.FlashFloods && !eventName) {
return await this.getEventAreaAggregates(
return await this.eventAreaService.getEventAreaDynamicData(
countryCodeISO3,
disasterType,
indicator,
Expand Down
5 changes: 3 additions & 2 deletions services/API-service/src/api/admin-area/admin-area.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AdminAreaService } from './admin-area.service';
import { DisasterEntity } from '../disaster/disaster.entity';
import { AdminAreaDynamicDataEntity } from '../admin-area-dynamic-data/admin-area-dynamic-data.entity';
import { HttpModule } from '@nestjs/axios';
import { EventAreaService } from './services/event-area.service';

@Module({
imports: [
Expand All @@ -25,8 +26,8 @@ import { HttpModule } from '@nestjs/axios';
EventModule,
CountryModule,
],
providers: [AdminAreaService, HelperService],
providers: [AdminAreaService, EventAreaService, HelperService],
controllers: [AdminAreaController],
exports: [AdminAreaService],
exports: [AdminAreaService, EventAreaService],
})
export class AdminAreaModule {}
97 changes: 4 additions & 93 deletions services/API-service/src/api/admin-area/admin-area.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { DisasterType } from '../disaster/disaster-type.enum';
import { DisasterEntity } from '../disaster/disaster.entity';
import { DynamicIndicator } from '../admin-area-dynamic-data/enum/dynamic-data-unit';
import { LeadTime } from '../admin-area-dynamic-data/enum/lead-time.enum';
import { CountryService } from '../country/country.service';
import { DateDto } from '../event/dto/date.dto';
import { EventAreaService } from './services/event-area.service';

@Injectable()
export class AdminAreaService {
Expand All @@ -27,7 +26,7 @@ export class AdminAreaService {
public constructor(
private helperService: HelperService,
private eventService: EventService,
private countryService: CountryService,
private eventAreaService: EventAreaService,
) {}

public async addOrUpdateAdminAreas(
Expand Down Expand Up @@ -197,7 +196,7 @@ export class AdminAreaService {
disasterType,
);
if (disasterType === DisasterType.FlashFloods && !eventName) {
return await this.getEventAreaAggregates(
return await this.eventAreaService.getEventAreaAggregates(
countryCodeISO3,
disasterType,
lastTriggeredDate,
Expand Down Expand Up @@ -291,94 +290,6 @@ export class AdminAreaService {
});
}

private async getEventAreaAggregates(
countryCodeISO3: string,
disasterType: DisasterType,
lastTriggeredDate: DateDto,
): Promise<AggregateDataRecord[]> {
const events = await this.eventService.getEventSummary(
countryCodeISO3,
disasterType,
);

const aggregateRecords = [];
for await (const event of events.filter((e) => e.activeTrigger)) {
const aggregateValues = await this.adminAreaDynamicDataRepo
.createQueryBuilder('dynamic')
.select('dynamic."indicator"', 'indicator')
.addSelect(
`CASE WHEN dynamic."indicator" = 'alert_threshold' THEN MAX(value) ELSE SUM(value) END as "value"`,
)
.where({
timestamp: MoreThanOrEqual(
this.helperService.getUploadCutoffMoment(
disasterType,
lastTriggeredDate.timestamp,
),
),
disasterType: disasterType,
eventName: event.eventName,
})
.groupBy('dynamic."indicator"')
.getRawMany();

for (const indicator of aggregateValues) {
const aggregateRecord = new AggregateDataRecord();
aggregateRecord.placeCode = event.eventName;
aggregateRecord.indicator = indicator.indicator;
aggregateRecord.value = indicator.value;
aggregateRecords.push(aggregateRecord);
}
}
return aggregateRecords;
}

private async getEventAreas(
countryCodeISO3: string,
disaster: DisasterEntity,
lastTriggeredDate: DateDto,
): Promise<GeoJson> {
const events = await this.eventService.getEventSummary(
countryCodeISO3,
disaster.disasterType,
);
const country = await this.countryService.findOne(countryCodeISO3, [
'countryDisasterSettings',
]);
const geoJson: GeoJson = {
type: 'FeatureCollection',
features: [],
};
for await (const event of events.filter((e) => e.activeTrigger)) {
const eventArea = country.countryDisasterSettings.find(
(d) => d.disasterType === disaster.disasterType,
).eventAreas[event.eventName];
eventArea['properties'] = {};
eventArea['properties']['eventName'] = event.eventName;
eventArea['properties']['placeCode'] = event.eventName;

const aggregateValue = await this.adminAreaDynamicDataRepo
.createQueryBuilder('dynamic')
.select('SUM(value)', 'value') // TODO: facilitate other aggregate-cases than SUM
.where({
timestamp: MoreThanOrEqual(
this.helperService.getUploadCutoffMoment(
disaster.disasterType,
lastTriggeredDate.timestamp,
),
),
disasterType: disaster.disasterType,
indicator: disaster.actionsUnit,
eventName: event.eventName,
})
.getRawOne();

eventArea['properties'][disaster.actionsUnit] = aggregateValue.value;
geoJson.features.push(eventArea);
}
return geoJson;
}

public async getAdminAreas(
countryCodeISO3: string,
disasterType: DisasterType,
Expand All @@ -393,7 +304,7 @@ export class AdminAreaService {
);

if (disasterType === DisasterType.FlashFloods && !eventName) {
return await this.getEventAreas(
return await this.eventAreaService.getEventAreas(
countryCodeISO3,
disaster,
lastTriggeredDate,
Expand Down
Loading

0 comments on commit ffbd9be

Please sign in to comment.