Skip to content

Commit

Permalink
Refactored and cleaned export postres to mongo algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed Sep 10, 2024
1 parent 263dc0b commit d9d853a
Showing 1 changed file with 60 additions and 51 deletions.
111 changes: 60 additions & 51 deletions lib/api/consumers/export-to-exploitation-db-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const createAddressTempTableQuery = tempTableName => `
WHERE AV."districtID" = :districtID
`

const commonToponymPageQuery = tempTableName => `
const pageQuery = tempTableName => `
SELECT
*
FROM
Expand All @@ -63,23 +63,7 @@ const commonToponymPageQuery = tempTableName => `
LIMIT :limit
`

const addressPageQuery = tempTableName => `
SELECT
*
FROM
${tempTableName}
OFFSET :offset
LIMIT :limit
`

const commonToponymTempTableCountQuery = tempTableName => `
SELECT
COUNT(*)
FROM
${tempTableName}
`

const addressTempTableCountQuery = tempTableName => `
const countQuery = tempTableName => `
SELECT
COUNT(*)
FROM
Expand Down Expand Up @@ -181,34 +165,49 @@ export default async function exportToExploitationDB({data}) {
await deleteTempCollections([tempCommonToponymCollection, tempAddressCollection, tempDistrictCollection])

// Create temporary tables
await sequelize.query(createCommonToponymTempTableQuery(tempCommonToponymTableName), {
replacements: {districtID},
transaction,
})
console.log(`Temporary table ${tempCommonToponymTableName} created`)
await sequelize.query(createAddressTempTableQuery(tempAddressTableName), {
replacements: {districtID},
transaction,
})
console.log(`Temporary table ${tempAddressTableName} created`)
await sequelize.query(
createCommonToponymTempTableQuery(tempCommonToponymTableName),
{
replacements: {
districtID
},
transaction,
})
await sequelize.query(
createAddressTempTableQuery(tempAddressTableName),
{
replacements: {
tempTable: tempAddressTableName,
districtID
},
transaction,
})

// Commit the transaction once the temporary tables are created
await transaction.commit()

// CommonToponym
// Count the total number of common toponyms and pages to process
const [commonToponymTempTableCountQueryResult] = await sequelize.query(commonToponymTempTableCountQuery(tempCommonToponymTableName), {transaction})
const commonToponymTempTableCountQueryResult = await sequelize.query(
countQuery(tempCommonToponymTableName),
{
type: sequelize.QueryTypes.SELECT,
})
const totalCommonToponymTempTableRecordsResult = Number(commonToponymTempTableCountQueryResult?.[0]?.count)
const totalCommonToponymPages = Math.ceil(totalCommonToponymTempTableRecordsResult / PAGE_SIZE)

const fetchAndExportDataFromCommonToponymPage = async pageNumber => {
try {
const offset = (pageNumber - 1) * PAGE_SIZE
const [pageData] = await sequelize.query(commonToponymPageQuery(tempCommonToponymTableName), {
replacements: {
offset,
limit: PAGE_SIZE
},
transaction,
raw: true,
})
const pageData = await sequelize.query(
pageQuery(tempCommonToponymTableName),
{
replacements: {
offset,
limit: PAGE_SIZE
},
type: sequelize.QueryTypes.SELECT,
})
// Format the data and calculate the fantoir code, tiles and postal code
const pageDataWithExtraDataCalculation = pageData.map(commonToponym => calculateExtraDataForCommonToponym(commonToponym, cog, fantoirFinder, commonToponymIDFantoirCodeMap))
const formatedPageDataForLegacy = pageDataWithExtraDataCalculation.map(commonToponym => formatCommonToponymDataForLegacy(commonToponym, district, pseudoCodeVoieGenerator, commonToponymLegacyIDCommonToponymIDMap, commonToponymLegacyIDSet))
Expand All @@ -230,20 +229,25 @@ export default async function exportToExploitationDB({data}) {

// Address
// Count the total number of addresses and pages to process
const [addressTempTableCountQueryResult] = await sequelize.query(addressTempTableCountQuery(tempAddressTableName), {transaction})
const addressTempTableCountQueryResult = await sequelize.query(
countQuery(tempAddressTableName),
{
type: sequelize.QueryTypes.SELECT,
})
const totalAddressRecords = Number(addressTempTableCountQueryResult?.[0]?.count)
const totalAddressPages = Math.ceil(totalAddressRecords / PAGE_SIZE)

const fetchAndExportDataFromAddressPage = async pageNumber => {
const offset = (pageNumber - 1) * PAGE_SIZE
const [pageData] = await sequelize.query(addressPageQuery(tempAddressTableName), {
replacements: {
offset,
limit: PAGE_SIZE
},
transaction,
raw: true,
})
const pageData = await sequelize.query(
pageQuery(tempAddressTableName),
{
replacements: {
offset,
limit: PAGE_SIZE
},
type: sequelize.QueryTypes.SELECT,
})

// Format the data and calculate the fantoir code, tiles and postal code
const pageDataWithExtraDataCalculation = pageData.map(address => calculateExtraDataForAddress(address, cog, commonToponymIDFantoirCodeMap))
Expand All @@ -262,16 +266,21 @@ export default async function exportToExploitationDB({data}) {

// District
// Count the total number of "lieu-dit" common toponym used for the district legacy format
const [specificCommonToponymTempTableCountQueryResult] = await sequelize.query(specificCommonToponymTempTableCountQuery(tempCommonToponymTableName), {transaction})
const specificCommonToponymTempTableCountQueryResult = await sequelize.query(
specificCommonToponymTempTableCountQuery(tempCommonToponymTableName),
{
type: sequelize.QueryTypes.SELECT,
})
const totalSpecifCommonToponymRecords = Number(specificCommonToponymTempTableCountQueryResult?.[0]?.count)

// Count the total number of certified address used for the district legacy format
const [addressCertifiedTempTableCountQueryResult] = await sequelize.query(addressCertifiedTempTableCountQuery(tempAddressTableName), {transaction})
const addressCertifiedTempTableCountQueryResult = await sequelize.query(
addressCertifiedTempTableCountQuery(tempAddressTableName),
{
type: sequelize.QueryTypes.SELECT,
})
const totalAddressCertifiedRecords = Number(addressCertifiedTempTableCountQueryResult?.[0]?.count)

// Commit the transaction
await transaction.commit()

// Format the district data for the legacy format
const districtFormatedForLegacy = await formatDistrictDataForLegacy(district, {totalCommonToponymRecords, totalSpecifCommonToponymRecords, totalAddressRecords, totalAddressCertifiedRecords})

Expand Down

0 comments on commit d9d853a

Please sign in to comment.