11const parseThresholds = require ( '../models/parse-thresholds' )
2- const axios = require ( 'axios' )
32const logger = require ( '../helpers/logging' )
43const pg = require ( '../helpers/db' )
54const invokeLambda = require ( '../helpers/invoke-lambda' )
6-
7- async function deleteThresholds ( stationId ) {
8- try {
9- await pg ( 'station_imtd_threshold' ) . where ( { station_id : stationId } ) . delete ( )
10- logger . info ( `Deleted thresholds for RLOI id ${ stationId } ` )
11- } catch ( error ) {
12- logger . error ( `Error deleting thresholds for station ${ stationId } ` , error )
13- throw error
14- }
15- }
5+ const deleteThresholds = require ( '../helpers/imtd-api' ) . deleteStation
6+ const { getRloiIds, getImtdApiResponse } = require ( '../helpers/imtd-api' )
7+ const tableName = 'station_imtd_threshold'
168
179async function insertThresholds ( stationId , thresholds ) {
1810 try {
@@ -37,21 +29,6 @@ async function insertThresholds (stationId, thresholds) {
3729 }
3830}
3931
40- async function getImtdApiResponse ( stationId ) {
41- const hostname = 'imfs-prd1-thresholds-api.azurewebsites.net'
42- try {
43- return await axios . get ( `https://${ hostname } /Location/${ stationId } ?version=2` )
44- } catch ( error ) {
45- if ( error . response ?. status === 404 ) {
46- logger . info ( `Station ${ stationId } not found (HTTP Status: 404)` )
47- } else {
48- const message = error . response ?. status ? `HTTP Status: ${ error . response . status } ` : `Error: ${ error . message } `
49- throw Error ( `IMTD API request for station ${ stationId } failed (${ message } )` )
50- }
51- return { }
52- }
53- }
54-
5532async function getIMTDThresholds ( stationId ) {
5633 const response = await getImtdApiResponse ( stationId )
5734 if ( response . data ) {
@@ -66,36 +43,23 @@ async function getData (stationId) {
6643 if ( thresholds . length > 0 ) {
6744 await insertThresholds ( stationId , thresholds )
6845 } else {
69- await deleteThresholds ( stationId )
46+ await deleteThresholds ( stationId , tableName )
47+ logger . info ( `Deleted data for RLOI id ${ stationId } ` )
7048 }
7149 } catch ( error ) {
7250 logger . error ( `Could not process data for station ${ stationId } (${ error . message } )` )
7351 }
7452}
7553
76- async function getRloiIds ( { limit, offset } = { } ) {
77- try {
78- logger . info ( `Retrieving up to ${ limit } rloi_ids with an offset of ${ offset } ` )
79- const result = await pg ( 'rivers_mview' )
80- . distinct ( 'rloi_id' )
81- . whereNotNull ( 'rloi_id' )
82- . orderBy ( 'rloi_id' , 'asc' )
83- . limit ( limit )
84- . offset ( offset )
85- logger . info ( `Retrieved ${ result . length } rloi_ids` )
86- return result
87- } catch ( error ) {
88- throw Error ( `Could not get list of id's from database (Error: ${ error . message } )` )
89- }
90- }
91-
9254async function handler ( { offset = 0 } = { } ) {
9355 const BATCH_SIZE = parseInt ( process . env . IMTD_BATCH_SIZE || '500' )
9456
57+ logger . info ( `Retrieving up to ${ BATCH_SIZE } rloi_ids with an offset of ${ offset } ` )
9558 const stations = await getRloiIds ( {
9659 offset,
9760 limit : BATCH_SIZE
9861 } )
62+ logger . info ( `Retrieved ${ stations . length } rloi_ids` )
9963
10064 for ( const station of stations ) {
10165 await getData ( station . rloi_id )
0 commit comments