Skip to content

Commit

Permalink
Modified export algorithm to export data only in legacy collections
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed Mar 28, 2024
1 parent 535380a commit 3e45022
Showing 1 changed file with 9 additions and 46 deletions.
55 changes: 9 additions & 46 deletions lib/api/consumers/export-to-exploitation-db-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,9 @@ const ADDRESS_BBOX_BUFFER = 50

// Collections names
const EXPLOITATION_DB_COLLECTION_NAMES = {
legacy: {
district: 'communes',
commonToponym: 'voies',
address: 'numeros'
},
banID: {
district: 'district',
commonToponym: 'common_toponym',
address: 'address'
}
district: 'communes',
commonToponym: 'voies',
address: 'numeros'
}

// QUERIES & POSTGIS FUNCTIONS
Expand Down Expand Up @@ -132,7 +125,6 @@ export default async function exportToExploitationDB({data}) {
// Clean collections
// Delete all data related to the district (legacy and banID)
await deleteAllLegacyDataRelatedToCOG(cog)
await deleteAllDataRelatedToDistrict(districtID)

// CommonToponym
// Count the total number of common toponyms and pages to process
Expand All @@ -157,12 +149,10 @@ export default async function exportToExploitationDB({data}) {
})
// Format the data and calculate the fantoir code, tiles and postal code
const pageDataWithExtraDataCalculation = pageData.map(commonToponym => calculateExtraDataForCommonToponym(commonToponym, cog, fantoirFinder, commonToponymIDFantoirCodeMap))
const formatedPageData = pageDataWithExtraDataCalculation.map(commonToponym => formatCommonToponym(commonToponym))
const formatedPageDataForLegacy = pageDataWithExtraDataCalculation.map(commonToponym => formatCommonToponymDataForLegacy(commonToponym, district, pseudoCodeVoieGenerator, commonToponymIDlegacyCommonToponymIDMap))

// Insert the data in the collection (legacy and banID)
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.legacy.commonToponym).insertMany(formatedPageDataForLegacy, {ordered: false})
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.banID.commonToponym).insertMany(formatedPageData, {ordered: false})
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.commonToponym).insertMany(formatedPageDataForLegacy, {ordered: false})
}

const commonToponymsExportPromises = []
Expand Down Expand Up @@ -194,12 +184,10 @@ export default async function exportToExploitationDB({data}) {

// Format the data and calculate the fantoir code, tiles and postal code
const pageDataWithExtraDataCalculation = pageData.map(address => calculateExtraDataForAddress(address, cog, commonToponymIDFantoirCodeMap))
const formatedPageData = pageDataWithExtraDataCalculation.map(address => formatAddress(address))
const formatedPageDataForLegacy = pageDataWithExtraDataCalculation.map(address => formatAddressDataForLegacy(address, district, commonToponymIDlegacyCommonToponymIDMap))

// Insert the data in the collection (legacy and banID)
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.legacy.address).insertMany(formatedPageDataForLegacy, {ordered: false})
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.banID.address).insertMany(formatedPageData, {ordered: false})
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.address).insertMany(formatedPageDataForLegacy, {ordered: false})
}

const addressesExportPromises = []
Expand All @@ -212,10 +200,7 @@ export default async function exportToExploitationDB({data}) {
// District
// For Legacy collections
const districtFormatedForLegacy = await formatDistrictDataForLegacy(district, totalCommonToponymRecords, totalAddressRecords, transaction)
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.legacy.district).insertOne(districtFormatedForLegacy)

// For BanID collections
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.banID.district).insertOne(district)
await mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.district).insertOne(districtFormatedForLegacy)

// Pseudo code voie generator saving data
await pseudoCodeVoieGenerator.save()
Expand All @@ -232,37 +217,15 @@ export default async function exportToExploitationDB({data}) {
// Helpers

// Helpers for exploitation DB
const deleteAllDataRelatedToDistrict = async districtID => {
await Promise.all([
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.banID.district).deleteMany({id: districtID}),
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.banID.commonToponym).deleteMany({districtID}),
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.banID.address).deleteMany({districtID})
])
}

const deleteAllLegacyDataRelatedToCOG = async cog => {
await Promise.all([
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.legacy.district).deleteMany({codeCommune: cog}),
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.legacy.commonToponym).deleteMany({codeCommune: cog}),
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.legacy.address).deleteMany({codeCommune: cog}),
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.district).deleteMany({codeCommune: cog}),
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.commonToponym).deleteMany({codeCommune: cog}),
mongo.db.collection(EXPLOITATION_DB_COLLECTION_NAMES.address).deleteMany({codeCommune: cog}),
])
}

// Helpers for formatting data
export const formatCommonToponym = commonToponym => {
// To-do : define the format for the common toponym
// For now, we remove data calculation used for the legacy format (centroid, addressCount, certifiedAddressCount, bbox)
const {centroid, addressCount, certifiedAddressCount, addressBbox, bbox, ...rest} = commonToponym
return rest
}

const formatAddress = address => {
// To-do : define the format for the address
// For now, we remove data calculation used for the legacy format (bbox)
const {bbox, ...rest} = address
return rest
}

// Helpers for calculation
export const calculateExtraDataForCommonToponym = (commonToponym, cog, fantoirFinder, commonToponymIDFantoirCodeMap) => {
// Calculate the fantoir code for each common toponym
Expand Down

0 comments on commit 3e45022

Please sign in to comment.