-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AB#26925 Sync stores and layers to geoserver
- Loading branch information
Ruben
committed
Mar 18, 2024
1 parent
731de25
commit 5158c49
Showing
11 changed files
with
449 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
services/API-service/migration/1710512991479-rename-mock-rasters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
export class RenameMockRasters1710512991479 implements MigrationInterface { | ||
public async up(_queryRunner: QueryRunner): Promise<void> { | ||
const directoryPath = './geoserver-volume/raster-files/mock-output/'; | ||
const ugandaFloodsBasicPath = `${directoryPath}/flood_extent_day_UGA.tif`; | ||
if (fs.existsSync(ugandaFloodsBasicPath)) { | ||
// Do not run the migration another time if it has already been run on test servers | ||
return; | ||
} | ||
|
||
if (fs.existsSync(directoryPath)) { | ||
const files = fs.readdirSync(directoryPath); | ||
console.log('🚀 ~ RenameMockRasters1710512991479 ~ up ~ files:', files); | ||
|
||
files.forEach((file) => { | ||
if (!file.includes('hour_MWI')) { | ||
const newFilename = file.replace( | ||
/_[0-9]+-(hour|day|month)_/g, | ||
'_$1_', | ||
); | ||
fs.renameSync( | ||
path.join(directoryPath, file), | ||
path.join(directoryPath, newFilename), | ||
); | ||
} | ||
}); | ||
} else { | ||
console.log(`Directory ${directoryPath} does not exist`); | ||
} | ||
} | ||
|
||
public async down(_queryRunner: QueryRunner): Promise<void> { | ||
// If you want to revert the renaming, you would need to implement the logic here | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
services/API-service/src/scripts/disaster-type-geoserver-file.mapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { DisasterType } from '../api/disaster/disaster-type.enum'; | ||
|
||
export class DisasterTypeGeoServerMapper { | ||
static getLayerStorePrefixForDisasterType( | ||
disasterType: DisasterType | ||
): string { | ||
if (disasterType === DisasterType.Floods) { | ||
return 'flood_extent'; | ||
} else if (disasterType === DisasterType.HeavyRain) { | ||
return 'rainfall_extent'; | ||
} else if (disasterType === DisasterType.Drought) { | ||
return 'rainfall_forecast'; | ||
} else if (disasterType === DisasterType.FlashFloods) { | ||
return 'flood_extent'; | ||
} | ||
return ''; | ||
} | ||
|
||
static getDestFilePrefixForDisasterType( | ||
disasterType: DisasterType, | ||
countryCode: string, | ||
): string { | ||
if (disasterType === DisasterType.Floods) { | ||
return 'flood_extent'; | ||
} else if (disasterType === DisasterType.HeavyRain) { | ||
if (countryCode === 'EGY') { | ||
return 'rain_rp'; | ||
} else if (countryCode === 'UGA') { | ||
return 'rainfall_extent'; | ||
} | ||
} else if (disasterType === DisasterType.Drought) { | ||
return 'rain_rp'; | ||
} else if (disasterType === DisasterType.FlashFloods) { | ||
return 'flood_extent'; | ||
} | ||
return ''; | ||
} | ||
|
||
static getSubfolderForDisasterType(disasterType: DisasterType): string { | ||
let subfolder = ''; | ||
if ( | ||
[DisasterType.Floods, DisasterType.FlashFloods].includes(disasterType) | ||
) { | ||
subfolder = 'flood_extents'; | ||
} else if ( | ||
[DisasterType.HeavyRain, DisasterType.Drought].includes(disasterType) | ||
) { | ||
subfolder = 'rainfall_extents'; | ||
} | ||
return subfolder; | ||
} | ||
|
||
// DOES not work for heavy rain as it will be phased out | ||
static getStyleForCountryAndDisasterType( | ||
countryCode: string, | ||
disasterType: DisasterType, | ||
): string { | ||
if (disasterType === DisasterType.Drought) { | ||
return 'rainfall_extent_drought'; | ||
} else if (disasterType === DisasterType.FlashFloods) { | ||
return 'flood_extent_MWI_flash-floods'; | ||
} else if (disasterType === DisasterType.Floods) { | ||
if (countryCode === 'PHL') { | ||
return 'flood_extent_PHL'; | ||
} else { | ||
return 'flood_extent'; | ||
} | ||
} | ||
throw new Error( | ||
`No style found for disaster type' ${disasterType} and country code ${countryCode}`, | ||
); | ||
} | ||
} |
206 changes: 206 additions & 0 deletions
206
services/API-service/src/scripts/geoserver-sync.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import { HttpService } from '@nestjs/axios'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { firstValueFrom, map } from 'rxjs'; | ||
import { INTERNAL_GEOSERVER_API_URL } from '../config'; | ||
import countries from './json/countries.json'; | ||
import { DisasterType } from '../api/disaster/disaster-type.enum'; | ||
import { DisasterTypeGeoServerMapper } from './disaster-type-geoserver-file.mapper'; | ||
import * as fs from 'fs'; | ||
|
||
const workspaceName = 'ibf-system'; | ||
|
||
class RecourceNameObject { | ||
resourceName: string; | ||
disasterType: DisasterType; | ||
countryCodeISO3: string; | ||
} | ||
|
||
@Injectable() | ||
export class GeoseverSyncService { | ||
constructor(private httpService: HttpService) {} | ||
|
||
public async sync( | ||
countryCodeISO3?: string, | ||
disasterType?: DisasterType, | ||
): Promise<void> { | ||
const filteredCountries = countries.filter((country: any) => { | ||
return countryCodeISO3 | ||
? country.countryCodeISO3 === countryCodeISO3 | ||
: true; | ||
}); | ||
// also filter by disaster type | ||
for (const country of filteredCountries) { | ||
const disasterSettings = country.countryDisasterSettings.filter( | ||
(disasterSetting: any) => { | ||
return disasterType | ||
? disasterSetting.disasterType === disasterType | ||
: true; | ||
}, | ||
); | ||
country.countryDisasterSettings = disasterSettings; | ||
} | ||
const geoserverResourceNameObjects = | ||
this.generateGeoserverResourceNames(filteredCountries); | ||
await this.syncStores(geoserverResourceNameObjects); | ||
await this.syncLayers(geoserverResourceNameObjects); | ||
} | ||
|
||
private async syncStores(expectedStoreNameObjects: RecourceNameObject[]) { | ||
const foundStoreNames = await this.getStoreNamesFromGeoserver( | ||
workspaceName, | ||
); | ||
const missingStoreNames = expectedStoreNameObjects.filter( | ||
(o) => !foundStoreNames.includes(o.resourceName), | ||
); | ||
await this.postStoreNamesToGeoserver(missingStoreNames); | ||
} | ||
|
||
private generateGeoserverResourceNames( | ||
filteredCountries: any[], | ||
): RecourceNameObject[] { | ||
const resourceNameObjects = []; | ||
for (const country of filteredCountries) { | ||
resourceNameObjects.push(...this.generateStoreNameForCountry(country)); | ||
} | ||
return resourceNameObjects; | ||
} | ||
|
||
private generateStoreNameForCountry(country: any): RecourceNameObject[] { | ||
const resourceNameObjects = []; | ||
const countryCode = country.countryCodeISO3; | ||
for (const disasterSetting of country.countryDisasterSettings) { | ||
if (disasterSetting.disasterType == DisasterType.Floods) { | ||
for (const leadTime of disasterSetting.activeLeadTimes) { | ||
const disasterTypeStorePrefix = | ||
DisasterTypeGeoServerMapper.getLayerStorePrefixForDisasterType( | ||
disasterSetting.disasterType | ||
); | ||
const resourceName = `${disasterTypeStorePrefix}_${leadTime}_${countryCode}`; | ||
resourceNameObjects.push({ | ||
resourceName: resourceName, | ||
disasterType: disasterSetting.disasterType, | ||
countryCodeISO3: countryCode, | ||
}); | ||
} | ||
} | ||
} | ||
return resourceNameObjects; | ||
} | ||
|
||
private async getStoreNamesFromGeoserver(workspaceName: string) { | ||
const data = await this.get(`workspaces/${workspaceName}/coveragestores`); | ||
const storeNames = data.coverageStores.coverageStore.map( | ||
(store: any) => store.name, | ||
); | ||
return storeNames; | ||
} | ||
|
||
private async postStoreNamesToGeoserver( | ||
resourceNameObjects: RecourceNameObject[], | ||
) { | ||
for (const resourceNameObject of resourceNameObjects) { | ||
const subfolder = DisasterTypeGeoServerMapper.getSubfolderForDisasterType( | ||
resourceNameObject.disasterType, | ||
); | ||
const url = `workspaces/${workspaceName}/coveragestores`; // replace with the correct API endpoint | ||
const body = { | ||
coverageStore: { | ||
name: resourceNameObject.resourceName, | ||
workspace: workspaceName, | ||
enabled: true, | ||
type: 'GeoTIFF', | ||
url: `file:workspaces/ibf-system/ibf-pipeline/output/${subfolder}/${resourceNameObject.resourceName}.tif`, | ||
}, | ||
}; | ||
const result = await this.post(url, body); | ||
console.log( | ||
'Updated geoserver with ', | ||
result, | ||
'please commit the resulting config changes of geoserver to git.', | ||
); | ||
} | ||
} | ||
|
||
public async syncLayers(expectedLayerNames: RecourceNameObject[]) { | ||
const foundLayerNames = await this.getLayerNamesFromGeoserver( | ||
workspaceName, | ||
); | ||
const missingLayerNames = expectedLayerNames.filter( | ||
(o) => !foundLayerNames.includes(o.resourceName), | ||
); | ||
await this.postLayerNamesToGeoserver(missingLayerNames); | ||
} | ||
|
||
private async getLayerNamesFromGeoserver(workspaceName: string) { | ||
const data = await this.get(`workspaces/${workspaceName}/layers`); | ||
const layerNames = data.layers.layer.map((layer: any) => layer.name); | ||
return layerNames; | ||
} | ||
|
||
private async postLayerNamesToGeoserver( | ||
resourceNameObjects: RecourceNameObject[], | ||
) { | ||
for (const resourceNameObject of resourceNameObjects) { | ||
const publishLayerUrl = `workspaces/${workspaceName}/coveragestores/${resourceNameObject.resourceName}/coverages`; | ||
const publishLayerBody = { | ||
coverage: { | ||
name: resourceNameObject.resourceName, | ||
title: resourceNameObject.resourceName, | ||
nativeName: resourceNameObject.resourceName, | ||
}, | ||
}; | ||
await this.post(publishLayerUrl, publishLayerBody); | ||
// Set the default style for the layer | ||
const styleName = | ||
DisasterTypeGeoServerMapper.getStyleForCountryAndDisasterType( | ||
resourceNameObject.countryCodeISO3, | ||
resourceNameObject.disasterType, | ||
); | ||
const styleUrl = `layers/${resourceNameObject.resourceName}`; | ||
const body = { | ||
layer: { | ||
defaultStyle: { | ||
name: `${workspaceName}:${styleName}`, | ||
}, | ||
}, | ||
}; | ||
await this.put(styleUrl, body); | ||
} | ||
} | ||
|
||
private async post(path: string, body: any) { | ||
const url = `${INTERNAL_GEOSERVER_API_URL}/${path}`; | ||
const headers = this.getHeaders(); | ||
const result = await firstValueFrom( | ||
this.httpService.post(url, body, { headers }), | ||
); | ||
return result.data; | ||
} | ||
|
||
private async put(path: string, body: any) { | ||
const url = `${INTERNAL_GEOSERVER_API_URL}/${path}`; | ||
const headers = this.getHeaders(); | ||
const result = await firstValueFrom( | ||
this.httpService.put(url, body, { headers }), | ||
); | ||
return result.data; | ||
} | ||
|
||
private async get(path: string) { | ||
const url = `${INTERNAL_GEOSERVER_API_URL}/${path}`; | ||
const headers = this.getHeaders(); | ||
const result = await firstValueFrom(this.httpService.get(url, { headers })); | ||
return result.data; | ||
} | ||
|
||
private getHeaders() { | ||
const username = 'admin'; | ||
return { | ||
Authorization: | ||
'Basic ' + | ||
Buffer.from( | ||
username + ':' + process.env.GEOSERVER_ADMIN_PASSWORD, | ||
).toString('base64'), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.