Skip to content

Commit

Permalink
Add points import step to water process
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruikshanks committed Sep 16, 2024
1 parent 5cc4390 commit 1182461
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/modules/water/process-steps.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
'use strict'

const ImportStep = require('./steps/import.js')
const LicenceImportStep = require('./steps/licence-import.js')
const PointsImportStep = require('./steps/points-import.js')

const { calculateAndLogTimeTaken, currentTimeInNanoseconds } = require('../../lib/general.js')

async function go () {
let processComplete = false
let licenceImportCounts = {}

try {
global.GlobalNotifier.omg('water started')

const startTime = currentTimeInNanoseconds()

await ImportStep.go()
licenceImportCounts = await LicenceImportStep.go()
await PointsImportStep.go()

processComplete = true

Expand All @@ -21,7 +24,7 @@ async function go () {
global.GlobalNotifier.oops('water failed')
}

return processComplete
return { processComplete, licenceImportCounts }
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function go () {
let rejected = 0

try {
global.GlobalNotifier.omg('water.import started')
global.GlobalNotifier.omg('water.licence-import started')

const startTime = currentTimeInNanoseconds()

Expand All @@ -21,9 +21,9 @@ async function go () {

rejected = await _import(licences, count)

calculateAndLogTimeTaken(startTime, 'water.import complete', { count, rejected })
calculateAndLogTimeTaken(startTime, 'water.licence-import complete', { count, rejected })
} catch (error) {
global.GlobalNotifier.omfg('water.import errored', error, { count, rejected })
global.GlobalNotifier.omfg('water.licence-import errored', error, { count, rejected })
throw error
}

Expand All @@ -39,13 +39,12 @@ async function _import (licences, count) {
for (let i = 0; i < count; i += batchSize) {
if (i === progress) {
progress = progress + PROGRESS_TICK
global.GlobalNotifier.omg(`water.import progress (${i} of ${count})`)
global.GlobalNotifier.omg(`water.licence-import progress (${i} of ${count})`)
}

const licencesToProcess = licences.slice(i, i + batchSize)

const processes = licencesToProcess.map((licenceToProcess) => {
// return Loader.go(licenceToProcess.LIC_NO)
return Loader.go(licenceToProcess)
})

Expand Down
76 changes: 76 additions & 0 deletions src/modules/water/steps/points-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict'

const db = require('../../../lib/connectors/db.js')
const { calculateAndLogTimeTaken, currentTimeInNanoseconds } = require('../../../lib/general.js')

async function go () {
try {
global.GlobalNotifier.omg('water.points-import started')

const startTime = currentTimeInNanoseconds()

await db.query(_query())

calculateAndLogTimeTaken(startTime, 'water.points-import complete')
} catch (error) {
global.GlobalNotifier.omfg('water.points-import errored', error)
throw error
}
}

function _query () {
return `
INSERT INTO water.licence_version_purpose_points (
licence_version_purpose_id,
description,
ngr_1,
ngr_2,
ngr_3,
ngr_4,
external_id,
nald_point_id
)
SELECT
lvp.licence_version_purpose_id,
np."LOCAL_NAME" AS description,
concat_ws(' ', np."NGR1_SHEET", np."NGR1_EAST", np."NGR1_NORTH") AS ngr_1,
(
CASE np."NGR2_SHEET"
WHEN 'null' THEN NULL
ELSE concat_ws(' ', np."NGR2_SHEET", np."NGR2_EAST", np."NGR2_NORTH")
END
) AS ngr_2,
(
CASE np."NGR3_SHEET"
WHEN 'null' THEN NULL
ELSE concat_ws(' ', np."NGR3_SHEET", np."NGR3_EAST", np."NGR3_NORTH")
END
) AS ngr_3,
(
CASE np."NGR4_SHEET"
WHEN 'null' THEN NULL
ELSE concat_ws(' ', np."NGR4_SHEET", np."NGR4_EAST", np."NGR4_NORTH")
END
) AS ngr_4,
(concat_ws(':', napp."FGAC_REGION_CODE", napp."AABP_ID", napp."AAIP_ID")) AS external_id,
napp."AAIP_ID"::integer AS nald_point_id
FROM
"import"."NALD_ABS_PURP_POINTS" napp
INNER JOIN water.licence_version_purposes lvp
ON napp."FGAC_REGION_CODE" = split_part(lvp.external_id, ':', 1) AND napp."AABP_ID" = split_part(lvp.external_id, ':', 2)
INNER JOIN import."NALD_POINTS" np
ON np."ID" = napp."AAIP_ID" AND np."FGAC_REGION_CODE" = napp."FGAC_REGION_CODE"
ON CONFLICT(external_id)
DO UPDATE
SET
description = excluded.description,
ngr_1 = excluded.ngr_1,
ngr_2 = excluded.ngr_2,
ngr_3 = excluded.ngr_3,
ngr_4 = excluded.ngr_4;
`
}

module.exports = {
go
}

0 comments on commit 1182461

Please sign in to comment.