Skip to content

Commit

Permalink
refactor: mock glofas stations AB#26938
Browse files Browse the repository at this point in the history
  • Loading branch information
arsforza committed Mar 14, 2024
1 parent fa94212 commit 27f2bd6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
16 changes: 3 additions & 13 deletions services/API-service/src/scripts/mock.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ export class MockController {
return res.status(HttpStatus.FORBIDDEN).send('Not allowed');
}
const result = await this.mockService.mock(
body.countryCodeISO3,
body,
DisasterType.Floods,
body.scenario,
false,
body.date,
);

return res.status(HttpStatus.ACCEPTED).send(result);
Expand All @@ -134,11 +132,9 @@ export class MockController {
return res.status(HttpStatus.FORBIDDEN).send('Not allowed');
}
const result = await this.mockService.mock(
body.countryCodeISO3,
body,
DisasterType.FlashFloods,
body.scenario,
false,
body.date,
);

return res.status(HttpStatus.ACCEPTED).send(result);
Expand All @@ -165,13 +161,7 @@ export class MockController {
body.countryCodeISO3 === 'PHL'
? DisasterType.Dengue
: DisasterType.Malaria;
const result = await this.mockService.mock(
body.countryCodeISO3,
disasterType,
body.scenario,
false,
body.date,
);
const result = await this.mockService.mock(body, disasterType, false);

return res.status(HttpStatus.ACCEPTED).send(result);
}
Expand Down
94 changes: 77 additions & 17 deletions services/API-service/src/scripts/mock.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import { UploadDynamicPointDataDto } from '../api/point-data/dto/upload-asset-ex
import { PointDataEnum } from '../api/point-data/point-data.entity';
import { LinesDataService } from '../api/lines-data/lines-data.service';
import { PointDataService } from '../api/point-data/point-data.service';
import { GlofasStationService } from '../api/glofas-station/glofas-station.service';
import {
MockEpidemicsScenario,
MockFlashFloodsScenario,
MockFloodsScenario,
} from './mock.controller';

class Scenario {
scenarioName: string;
Expand All @@ -29,40 +35,50 @@ export class MockService {
private eventService: EventService,
private linesDataService: LinesDataService,
private pointDataService: PointDataService,
private glofasStationService: GlofasStationService,
) {}

public async mock(
countryCodeISO3: string,
mockBody:
| MockFloodsScenario
| MockEpidemicsScenario
| MockFlashFloodsScenario,
disasterType: DisasterType,
scenarioName: string,
defaultScenario: boolean,
date: Date,
) {
if (mockBody.removeEvents) {
console.log('mockBody.removeEvents: ', mockBody.removeEvents);
// this.removeEvents()
}

const selectedCountry = countries.find((country): any => {
if (countryCodeISO3 === country.countryCodeISO3) {
if (mockBody.countryCodeISO3 === country.countryCodeISO3) {
return country;
}
});

const scenario = await this.getScenario(
disasterType,
countryCodeISO3,
scenarioName,
mockBody.countryCodeISO3,
mockBody.scenario,
defaultScenario,
);

const adminLevels = selectedCountry.countryDisasterSettings.find(
(s) => s.disasterType === disasterType,
).adminLevels;

const indicators = await this.getIndicators(countryCodeISO3, disasterType);
const indicators = await this.getIndicators(
mockBody.countryCodeISO3,
disasterType,
);

for (const event of scenario.events) {
for (const indicator of indicators) {
for (const adminLevel of adminLevels) {
const exposurePlaceCodes = this.getIndicatorPlaceCodes(
disasterType,
countryCodeISO3,
mockBody.countryCodeISO3,
scenario.scenarioName,
event.eventName,
indicator,
Expand All @@ -75,43 +91,52 @@ export class MockService {
}

await this.adminAreaDynamicDataService.exposure({
countryCodeISO3: countryCodeISO3,
countryCodeISO3: mockBody.countryCodeISO3,
exposurePlaceCodes,
leadTime: event.leadTime as LeadTime,
dynamicIndicator: indicator,
adminLevel: adminLevel,
disasterType: disasterType,
eventName: event.eventName,
date,
date: mockBody.date,
});
}
}

if (disasterType === DisasterType.Floods) {
if (this.shouldMockTriggerPerLeadTime(disasterType)) {
const triggersPerLeadTime = this.getFile(
`./src/scripts/mock-data/${disasterType}/${countryCodeISO3}/${scenario.scenarioName}/${event.eventName}/triggers-per-leadtime.json`,
`./src/scripts/mock-data/${disasterType}/${mockBody.countryCodeISO3}/${scenario.scenarioName}/${event.eventName}/triggers-per-leadtime.json`,
);
await this.eventService.uploadTriggerPerLeadTime({
countryCodeISO3: countryCodeISO3,
countryCodeISO3: mockBody.countryCodeISO3,
triggersPerLeadTime,
disasterType: DisasterType.Floods,
eventName: event.eventName,
date,
date: mockBody.date,
});
}

if (this.shouldMockGlofasStations(disasterType)) {
await this.mockGlofasStations(
selectedCountry,
DisasterType.Floods,
true,
mockBody.date,
);
}

if (disasterType === DisasterType.FlashFloods) {
// TODO: the below methods are now duplidated between mock.service and scrpts.service
// TODO: the below methods still assume hard-coded leadTimes and is not flexible
await this.mockExposedAssets(
selectedCountry.countryCodeISO3,
true, // TODO: assume triggered, no-trigger for now done via old endpoint
date,
mockBody.date,
);
await this.mockDynamicPointData(
selectedCountry.countryCodeISO3,
DisasterType.FlashFloods,
date,
mockBody.date,
);

// TODO: raster-file
Expand Down Expand Up @@ -166,7 +191,7 @@ export class MockService {
);
}

getIndicatorPlaceCodes(
private getIndicatorPlaceCodes(
disasterType: DisasterType,
countryCodeISO3: string,
scenarioName: string,
Expand All @@ -179,6 +204,33 @@ export class MockService {
);
}

private async mockGlofasStations(
selectedCountry,
disasterType: DisasterType,
triggered: boolean,
date: Date,
) {
const stations = this.getFile(
`./src/api/point-data/dto/example/glofas-stations/glofas-stations-${
selectedCountry.countryCodeISO3
}${triggered ? '-triggered' : ''}.json`,
);

for (const activeLeadTime of selectedCountry.countryDisasterSettings.find(
(s) => s.disasterType === disasterType,
).activeLeadTimes) {
console.log(
`Seeding Glofas stations for country: ${selectedCountry.countryCodeISO3} for leadtime: ${activeLeadTime}`,
);
await this.glofasStationService.uploadTriggerDataPerStation({
countryCodeISO3: selectedCountry.countryCodeISO3,
stationForecasts: stations,
leadTime: activeLeadTime as LeadTime,
date,
});
}
}

private async mockExposedAssets(
countryCodeISO3: string,
triggered: boolean,
Expand Down Expand Up @@ -279,4 +331,12 @@ export class MockService {
await this.pointDataService.uploadDynamicPointData(payload);
}
}

private shouldMockGlofasStations(disasterType: DisasterType): boolean {
return disasterType === DisasterType.Floods;
}

private shouldMockTriggerPerLeadTime(disasterType: DisasterType): boolean {
return disasterType === DisasterType.Floods;
}
}

0 comments on commit 27f2bd6

Please sign in to comment.