Skip to content

Commit

Permalink
Adding logging to the db export service (#227)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-3968

This pull request is just a small part of a larger project that involves exporting all our database schemas, converting them into CSV files, and uploading them to our Amazon S3 bucket. This PR's primary focus is to add logging to the db-export service. To expedite the export process and see the output sooner, we are using a vertical slicing approach, rather than a horizontal one, which means exporting a single table at a time from each schema.
  • Loading branch information
Beckyrose200 authored May 17, 2023
1 parent 2c3d0b1 commit f778ee4
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 76 deletions.
10 changes: 3 additions & 7 deletions app/controllers/data/data.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ async function tearDown (_request, h) {
/**
* Triggers export of all relevant tables to CSV and then uploads them to S3
*/
async function dbExport (_request, _h) {
global.GlobalNotifier.omg('Starting db export service ')

async function dbExport (_request, h) {
try {
await DbExportService.go()

global.GlobalNotifier.omg('Finished export service')
return { status: 'successful' }
return h.response().code(204)
} catch (error) {
global.GlobalNotifier.omfg(`Error: ${error.message}`)
return { status: `Error: ${error.message}` }
return Boom.badImplementation(error.message)
}
}

Expand Down
3 changes: 0 additions & 3 deletions app/services/db-export/compress-files.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ const zlib = require('node:zlib')
*/
async function go (filePath) {
if (!fs.existsSync(filePath)) {
global.GlobalNotifier.omfg(`ERROR: ${filePath} did not successfully compress to gzip.`)

return false
}

await _compressFile(filePath)
global.GlobalNotifier.omg(`${filePath} successfully compressed to gzip.`)

return `${filePath}.gz`
}
Expand Down
3 changes: 0 additions & 3 deletions app/services/db-export/export-data-files.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ async function go (tableConvertedToCsv, tableName) {

try {
await fs.writeFile(filePath, tableConvertedToCsv)
global.GlobalNotifier.omg(`${tableName} exported successfully`)

return filePath
} catch (error) {
global.GlobalNotifier.omfg(`${tableName} Export request errored`, error)

return false
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/services/db-export/send-to-s3-bucket.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ async function _uploadToBucket (params, fileName) {

try {
await s3Client.send(command)
global.GlobalNotifier.omg(`The file ${fileName} was uploaded successfully`)

return true
} catch (error) {
global.GlobalNotifier.omfg(`ERROR uploading file: ${error.message}`)
return false
}
}
Expand Down
8 changes: 2 additions & 6 deletions test/controllers/data/data.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ describe('Data controller', () => {

it('displays the correct message', async () => {
const response = await server.inject(options)
const payload = JSON.parse(response.payload)

expect(response.statusCode).to.equal(200)
expect(payload.status).to.equal('successful')
expect(response.statusCode).to.equal(204)
})
})

Expand All @@ -89,10 +87,8 @@ describe('Data controller', () => {

it('displays the error message', async () => {
const response = await server.inject(options)
const payload = JSON.parse(response.payload)

expect(payload.status).to.equal('Error: Error')
expect(response.statusCode).to.equal(200)
expect(response.statusCode).to.equal(500)
})
})
})
Expand Down
14 changes: 0 additions & 14 deletions test/services/db-export/compress-files.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code
Expand All @@ -15,19 +14,8 @@ const fs = require('fs')
const CompressFilesService = require('../../../app/services/db-export/compress-files.service.js')

describe('Compress files service', () => {
let notifierStub
let filePath

beforeEach(() => {
notifierStub = { omg: Sinon.stub(), omfg: Sinon.stub() }
global.GlobalNotifier = notifierStub
})

afterEach(() => {
Sinon.restore()
delete global.GlobalNotifier
})

describe('when successful', () => {
beforeEach(() => {
filePath = 'test/fixtures/compress-files.service.csv'
Expand All @@ -43,7 +31,6 @@ describe('Compress files service', () => {

expect(result).to.equal(`${filePath}.gz`)
expect(fs.existsSync(`${filePath}.gz`)).to.equal(true)
expect(notifierStub.omg.calledWith(`${filePath} successfully compressed to gzip.`)).to.be.true()
})
})

Expand All @@ -57,7 +44,6 @@ describe('Compress files service', () => {

expect(result).to.equal(false)
expect(fs.existsSync((`${filePath}.gz`))).to.equal(false)
expect(notifierStub.omfg.calledWith(`ERROR: ${filePath} did not successfully compress to gzip.`)).to.be.true()
})
})
})
14 changes: 0 additions & 14 deletions test/services/db-export/export-data-files.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code
Expand Down Expand Up @@ -49,17 +48,6 @@ const csvValues = [

describe('Export data files service', () => {
const tableName = 'billing_charge_categories'
let notifierStub

beforeEach(() => {
notifierStub = { omg: Sinon.stub(), omfg: Sinon.stub() }
global.GlobalNotifier = notifierStub
})

afterEach(() => {
Sinon.restore()
delete global.GlobalNotifier
})

let filePath

Expand All @@ -82,7 +70,6 @@ describe('Export data files service', () => {

expect(fs.existsSync(filePath)).to.equal(true)
expect(returnedResult).to.equal('/tmp/billing_charge_categories.csv')
expect(notifierStub.omg.calledWith('billing_charge_categories exported successfully')).to.be.true()
})
})

Expand All @@ -92,7 +79,6 @@ describe('Export data files service', () => {
const result = await ExportDataFilesService.go(data, tableName)

expect(result).to.equal(false)
expect(notifierStub.omfg.calledWith('billing_charge_categories Export request errored')).to.be.true()
})
})
})
31 changes: 4 additions & 27 deletions test/services/db-export/send-to-s3-bucket.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const Sinon = require('sinon')
const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const path = require('path')

// Things we need to stub
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3')

Expand All @@ -19,26 +16,18 @@ const SendToS3BucketService = require('../../../app/services/db-export/send-to-s

describe('Send to S3 bucket service', () => {
let s3Stub
let notifierStub

beforeEach(() => {
notifierStub = { omg: Sinon.stub(), omfg: Sinon.stub() }
global.GlobalNotifier = notifierStub
})

afterEach(() => {
Sinon.restore()
delete global.GlobalNotifier
})

describe('when successful', () => {
beforeEach(() => {
// Stub the S3 Client's send method, which is used to run the 'put object' command
s3Stub = Sinon.stub(S3Client.prototype, 'send')
})

afterEach(() => {
Sinon.restore()
})

const filePath = 'test/fixtures/compress-files.service.csv'
const fileName = path.basename(filePath)

it('uploads a file to the S3 bucket', async () => {
await SendToS3BucketService.go(filePath)
Expand All @@ -56,12 +45,6 @@ describe('Send to S3 bucket service', () => {

expect(result).to.equal(true)
})

it('logs a success message', async () => {
await SendToS3BucketService.go(filePath)

expect(notifierStub.omg.calledWith(`The file ${fileName} was uploaded successfully`)).to.be.true()
})
})

describe('when unsuccessful', () => {
Expand Down Expand Up @@ -89,12 +72,6 @@ describe('Send to S3 bucket service', () => {

expect(result).to.equal(false)
})

it('logs the error', async () => {
await SendToS3BucketService.go(filePath)

expect(notifierStub.omfg.calledWith('ERROR uploading file: Error')).to.be.true()
})
})
})
})

0 comments on commit f778ee4

Please sign in to comment.