Skip to content

Commit

Permalink
Merge pull request #1454 from rodekruis/refactor.disaster-type-per-layer
Browse files Browse the repository at this point in the history
Refactor.disaster type per layer
  • Loading branch information
jannisvisser authored Sep 18, 2023
2 parents a2939b9 + 3e3fa62 commit f31042b
Show file tree
Hide file tree
Showing 17 changed files with 309 additions and 477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ export class AggregatesComponent implements OnInit, OnDestroy {
};

private onIndicatorChange = (newIndicators: Indicator[]) => {
// clean data to avoid these inefficient filters and loops
const filterAggregateIndicators = (indicator: Indicator) =>
indicator.aggregateIndicator.includes(this.country.countryCodeISO3);
indicator.countryDisasterTypes[this.country.countryCodeISO3][
this.disasterType.disasterType
].includes('aggregate');

this.indicators = newIndicators.filter(filterAggregateIndicators);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class MatrixComponent implements OnDestroy {
}

private onLayerChange = (newLayer: IbfLayer) => {
if (newLayer && newLayer.name === 'alert_threshold') {
if (newLayer && newLayer.name === IbfLayerName.alertThreshold) {
return;
}

Expand Down
24 changes: 2 additions & 22 deletions interfaces/IBF-dashboard/src/app/services/map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,6 @@ export class MapService {
private disasterType: DisasterType;
private placeCode: PlaceCode;

private aggregatesToExclude = {
MWI: {
[DisasterTypeKey.flashFloods]: [
'nr_affected_roads',
'nr_affected_schools',
'nr_affected_clinics',
'nr_affected_waterpoints',
'nr_affected_buildings',
],
},
};

constructor(
private countryService: CountryService,
private adminLevelService: AdminLevelService,
Expand Down Expand Up @@ -419,18 +407,10 @@ export class MapService {
return;
}

Object.keys(this.aggregatesToExclude).includes(
this.country.countryCodeISO3,
);

if (
!this.aggregatesToExclude[this.country.countryCodeISO3] ||
!this.aggregatesToExclude[this.country.countryCodeISO3][
this.disasterType.disasterType
] ||
!this.aggregatesToExclude[this.country.countryCodeISO3][
indicator.countryDisasterTypes[this.country.countryCodeISO3][
this.disasterType.disasterType
].includes(indicator.name)
].includes('map')
) {
const layerActive = this.adminLevelService.activeLayerNames.length
? this.adminLevelService.activeLayerNames.includes(indicator.name)
Expand Down
2 changes: 1 addition & 1 deletion interfaces/IBF-dashboard/src/app/types/indicator-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { LayerActivation } from '../models/layer-activation.enum';
import { ColorBreaks, IbfLayerLabel, IbfLayerName } from './ibf-layer';

export class Indicator {
countryDisasterTypes: JSON;
name: IbfLayerName;
label: IbfLayerLabel;
icon: string;
active: LayerActivation;
numberFormatMap: NumberFormat;
numberFormatAggregate: NumberFormat;
aggregateIndicator: string;
weightedAvg: boolean;
weightVar: IbfLayerName;
colorBreaks?: ColorBreaks;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class RemoveLayerCountryDisasterType1693224120772
implements MigrationInterface
{
name = 'RemoveLayerCountryDisasterType1693224120772';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "IBF-app"."indicator-metadata" DROP COLUMN "countryCodes"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."layer-metadata" DROP COLUMN "countryCodes"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."indicator-metadata" DROP COLUMN "aggregateIndicator"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."indicator-metadata" ADD "countryDisasterTypes" json NOT NULL DEFAULT '{}'`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."layer-metadata" ADD "countryDisasterTypes" json NOT NULL DEFAULT '{}'`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "IBF-app"."indicator-metadata" DROP COLUMN "countryDisasterTypes"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."layer-metadata" DROP COLUMN "countryDisasterTypes"`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."indicator-metadata" ADD "aggregateIndicator" character varying`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."layer-metadata" ADD "countryCodes" character varying`,
);
await queryRunner.query(
`ALTER TABLE "IBF-app"."indicator-metadata" ADD "countryCodes" character varying`,
);
}
}
2 changes: 1 addition & 1 deletion services/API-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"test:dev": "npm test -- --watchAll",
"test:coverage": "npm test -- --coverage --coverageDirectory=coverage",
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
"migration:generate": "npm run typeorm migration:generate -- -d ./appdatasource.ts -n",
"migration:generate": "npm run typeorm migration:generate -- -d ./appdatasource.ts",
"migration:run": "npm run typeorm migration:run -- -d ./appdatasource.ts",
"migration:revert": "npm run typeorm migration:revert -- -d ./appdatasource.ts"
},
Expand Down
16 changes: 0 additions & 16 deletions services/API-service/src/api/disaster/disaster.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
PrimaryGeneratedColumn,
} from 'typeorm';
import { DisasterType } from './disaster-type.enum';
import { IndicatorMetadataEntity } from '../metadata/indicator-metadata.entity';
import { LayerMetadataEntity } from '../metadata/layer-metadata.entity';
import { ApiProperty } from '@nestjs/swagger';
import {
LeadTime,
Expand Down Expand Up @@ -62,20 +60,6 @@ export class DisasterEntity {
@Column({ nullable: true })
public maxLeadTime: LeadTime;

@ManyToMany(
(): typeof IndicatorMetadataEntity => IndicatorMetadataEntity,
(indicators): DisasterEntity[] => indicators.disasterTypes,
)
@JoinTable()
public indicators: IndicatorMetadataEntity[];

@ManyToMany(
(): typeof LayerMetadataEntity => LayerMetadataEntity,
(layers): DisasterEntity[] => layers.disasterTypes,
)
@JoinTable()
public layers: LayerMetadataEntity[];

@ManyToMany(
(): typeof UserEntity => UserEntity,
(user): DisasterEntity[] => user.disasterTypes,
Expand Down
20 changes: 8 additions & 12 deletions services/API-service/src/api/metadata/dto/add-indicators.dto.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import {
IsBoolean,
IsEnum,
IsIn,
IsNotEmpty,
IsNumber,
IsString,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { DisasterType } from '../../disaster/disaster-type.enum';

export class IndicatorDto {
@ApiProperty({ example: process.env.COUNTRIES })
@IsString()
public countryCodes: string;

@ApiProperty({ example: [{ disasterType: DisasterType.Floods }] })
@IsEnum(DisasterType)
public disasterTypes: DisasterType[];
@ApiProperty({
example: {
EGY: {
'heavy-rain': ['map', 'aggregate'],
},
},
})
public countryDisasterTypes: JSON;

@ApiProperty()
@IsString()
Expand Down Expand Up @@ -57,9 +56,6 @@ export class IndicatorDto {
@IsString()
public numberFormatMap: string;

@ApiProperty({ example: process.env.COUNTRIES })
public aggregateIndicator: string | null;

@ApiProperty({ example: 'decimal0' })
@IsIn(['decimal0', 'decimal2', 'perc'])
public numberFormatAggregate: string;
Expand Down
22 changes: 5 additions & 17 deletions services/API-service/src/api/metadata/indicator-metadata.entity.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from 'typeorm';
import { DisasterType } from '../disaster/disaster-type.enum';
import { DisasterEntity } from '../disaster/disaster.entity';
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity('indicator-metadata')
export class IndicatorMetadataEntity {
@ApiProperty({ example: '6b9b7669-4839-4fdb-9645-9070a27bda86' })
@PrimaryGeneratedColumn('uuid')
public id: string;

@ApiProperty({ example: process.env.COUNTRIES })
@Column()
public countryCodes: string;

@ApiProperty({ example: [{ disasterType: DisasterType.Floods }] })
@ManyToMany(
(): typeof DisasterEntity => DisasterEntity,
(disasterTypes): IndicatorMetadataEntity[] => disasterTypes.indicators,
)
public disasterTypes: DisasterEntity[];
@Column('json', {
default: {},
})
public countryDisasterTypes: JSON;

@ApiProperty()
@Column()
Expand Down Expand Up @@ -60,10 +52,6 @@ export class IndicatorMetadataEntity {
@Column()
public numberFormatMap: string;

@ApiProperty({ example: process.env.COUNTRIES })
@Column()
public aggregateIndicator: string | null;

@ApiProperty({ example: 'decimal0' })
@Column()
public numberFormatAggregate: string;
Expand Down
18 changes: 5 additions & 13 deletions services/API-service/src/api/metadata/layer-metadata.entity.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsIn } from 'class-validator';
import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from 'typeorm';
import { DisasterType } from '../disaster/disaster-type.enum';
import { DisasterEntity } from '../disaster/disaster.entity';
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity('layer-metadata')
export class LayerMetadataEntity {
@ApiProperty({ example: '6b9b7669-4839-4fdb-9645-9070a27bda86' })
@PrimaryGeneratedColumn('uuid')
public id: string;

@ApiProperty({ example: process.env.COUNTRIES })
@Column()
public countryCodes: string;

@ApiProperty({ example: [{ disasterType: DisasterType.Floods }] })
@ManyToMany(
(): typeof DisasterEntity => DisasterEntity,
(disasterTypes): LayerMetadataEntity[] => disasterTypes.layers,
)
public disasterTypes: DisasterEntity[];
@Column('json', {
default: {},
})
public countryDisasterTypes: JSON;

@ApiProperty()
@Column()
Expand Down
43 changes: 9 additions & 34 deletions services/API-service/src/api/metadata/metadata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { DisasterType } from '../disaster/disaster-type.enum';
import { DisasterEntity } from '../disaster/disaster.entity';
import { AddIndicatorsDto, IndicatorDto } from './dto/add-indicators.dto';
import { AddLayersDto, LayerDto } from './dto/add-layers.dto';
import { IndicatorMetadataEntity } from './indicator-metadata.entity';
Expand All @@ -14,8 +13,6 @@ export class MetadataService {
private readonly indicatorRepository: Repository<IndicatorMetadataEntity>;
@InjectRepository(LayerMetadataEntity)
private readonly layerRepository: Repository<LayerMetadataEntity>;
@InjectRepository(DisasterEntity)
private readonly disasterRepository: Repository<DisasterEntity>;

public async addOrUpdateIndicators(
indicators: AddIndicatorsDto,
Expand Down Expand Up @@ -48,14 +45,9 @@ export class MetadataService {
indicatorEntity: IndicatorMetadataEntity,
indicator: IndicatorDto,
): Promise<IndicatorMetadataEntity> {
indicatorEntity.countryCodes = indicator.countryCodes;
indicatorEntity.disasterTypes = await this.disasterRepository.find({
where: indicator.disasterTypes.map(
(countryDisasterType: DisasterType): object => {
return { disasterType: countryDisasterType };
},
),
});
indicatorEntity.description = JSON.parse(
JSON.stringify(indicator.countryDisasterTypes || {}),
);
indicatorEntity.label = indicator.label;
indicatorEntity.icon = indicator.icon;
indicatorEntity.weightedAvg = indicator.weightedAvg;
Expand All @@ -65,7 +57,6 @@ export class MetadataService {
JSON.stringify(indicator.colorBreaks),
);
indicatorEntity.numberFormatMap = indicator.numberFormatMap;
indicatorEntity.aggregateIndicator = indicator.aggregateIndicator;
indicatorEntity.numberFormatAggregate = indicator.numberFormatAggregate;
indicatorEntity.order = indicator.order;
indicatorEntity.dynamic = indicator.dynamic;
Expand Down Expand Up @@ -106,14 +97,6 @@ export class MetadataService {
layerEntity: LayerMetadataEntity,
layer: LayerDto,
): Promise<LayerMetadataEntity> {
layerEntity.countryCodes = layer.countryCodes;
layerEntity.disasterTypes = await this.disasterRepository.find({
where: layer.disasterTypes.map(
(countryDisasterType: DisasterType): object => {
return { disasterType: countryDisasterType };
},
),
});
layerEntity.label = layer.label;
layerEntity.type = layer.type;
layerEntity.legendColor = layer.legendColor
Expand All @@ -132,32 +115,24 @@ export class MetadataService {
countryCodeISO3: string,
disasterType: DisasterType,
): Promise<IndicatorMetadataEntity[]> {
const indicators = await this.indicatorRepository.find({
relations: ['disasterTypes'],
});
const indicators = await this.indicatorRepository.find();
return indicators.filter(
(metadata: IndicatorMetadataEntity): boolean =>
metadata.countryCodes.split(',').includes(countryCodeISO3) &&
metadata.disasterTypes
.map((d) => d.disasterType)
.includes(disasterType),
metadata.countryDisasterTypes?.[countryCodeISO3] &&
metadata.countryDisasterTypes?.[countryCodeISO3][disasterType],
);
}

public async getLayersByCountryAndDisaster(
countryCodeISO3: string,
disasterType: DisasterType,
): Promise<LayerMetadataEntity[]> {
const layers = await this.layerRepository.find({
relations: ['disasterTypes'],
});
const layers = await this.layerRepository.find();

return layers.filter(
(metadata: LayerMetadataEntity): boolean =>
metadata.countryCodes.split(',').includes(countryCodeISO3) &&
metadata.disasterTypes
.map((d) => d.disasterType)
.includes(disasterType),
metadata.description?.[countryCodeISO3] &&
metadata.description?.[countryCodeISO3][disasterType],
);
}
}
Loading

0 comments on commit f31042b

Please sign in to comment.