Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/constructed-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ module.exports = {
`
return Query.fromString(sql)
},
updateTelemetryHistory: () => {
const sql = 'SELECT u_flood.update_telemetry_history();'
return Query.fromString(sql)
},
refreshStationMviews: () => {
const sql = `
REFRESH MATERIALIZED VIEW CONCURRENTLY u_flood.telemetry_context_mview WITH DATA;
Expand Down
10 changes: 10 additions & 0 deletions lib/functions/telemetry-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { Pool } = require('../helpers/pool')

module.exports.handler = async () => {
const pool = new Pool({ connectionString: process.env.LFW_DATA_DB_CONNECTION })
try {
await pool.query('updateTelemetryHistory')
} finally {
await pool.end()
}
}
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ brings in the observed telemtery data from the S3 bucket and processes it into t

refreshes the materialized views and clears out the old telemetry.

### telemetry-history

Runs `u_flood.update_telemetry_history()` to maintain historical telemetry. Its schedule is configured separately in Terraform and should run at every day but at a minimum must run at least once every 5 days. This function will also clear any historical data over 3 years old.

### station-process

Uses the exports context file from sharepoint to update the stations in the database.
Expand Down Expand Up @@ -91,6 +95,7 @@ Cron configurations (e.g., thresholds, data syncs) are managed per environment v
export LFW_DATA_IMTD_THRESHOLD_SCHEDULE=
export LFW_DATA_DTS_SCHEDULE=
export LFW_DATA_FWIS_SCHEDULE=
export LFW_DATA_TELEMETRY_HISTORY_SCHEDULE=
```

These are defined in the Terraform modules and should be updated through the infrastructure as code process.
Expand Down
13 changes: 12 additions & 1 deletion test/unit/constructed-queries.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('@hapi/code')
const Lab = require('@hapi/lab')
const {
slsTelemetryValues, slsTelemetryStation, slsTelemetryValueParent, deleteOldTelemetry, refreshStationMviews,
slsTelemetryValues, slsTelemetryStation, slsTelemetryValueParent, deleteOldTelemetry, updateTelemetryHistory, refreshStationMviews,
deleteStations, insertStations, upsertFfoiMax, deleteCurrentFwis, insertFloodWarnings, refreshFloodWarningsMview,
updateTimestamp
} = require('../../lib/constructed-queries')
Expand Down Expand Up @@ -101,6 +101,17 @@ describe('constructedQueries', () => {
})
})

describe('updateTelemetryHistory', () => {
it('returns the history update sql in a query object', () => {
// Act
const actual = updateTelemetryHistory()

// Assert
expect(actual.text).to.equal('SELECT u_flood.update_telemetry_history();')
expect(actual.values).to.be.undefined()
})
})

describe('refreshStationMviews', () => {
it('returns appropriate sql in a query object', () => {
// Act
Expand Down
38 changes: 38 additions & 0 deletions test/unit/functions/telemetry-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const Lab = require('@hapi/lab')
const lab = exports.lab = Lab.script()
const Code = require('@hapi/code')
const sinon = require('sinon')
const proxyquire = require('proxyquire')

lab.experiment('telemetry history', () => {
lab.test('runs the history update query before ending the pool', async () => {
const pool = {
query: sinon.stub().resolves(),
end: sinon.stub().resolves()
}
const Pool = sinon.stub().returns(pool)
const { handler } = proxyquire('../../../lib/functions/telemetry-history', {
'../helpers/pool': { Pool }
})

await handler()

sinon.assert.calledOnceWithExactly(pool.query, 'updateTelemetryHistory')
sinon.assert.callOrder(pool.query, pool.end)
})

lab.test('ends the pool and propagates a history update failure', async () => {
const error = new Error('history update failed')
const pool = {
query: sinon.stub().rejects(error),
end: sinon.stub().resolves()
}
const Pool = sinon.stub().returns(pool)
const { handler } = proxyquire('../../../lib/functions/telemetry-history', {
'../helpers/pool': { Pool }
})

await Code.expect(handler()).to.reject()
sinon.assert.calledOnce(pool.end)
})
})
Loading