Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging #31

Merged
merged 9 commits into from
Dec 6, 2023
11 changes: 0 additions & 11 deletions src/controllers/MyController.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/controllers/datasetController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

import MyController from './MyController.js'
import PageController from './pageController.js'

// ToDo: we shouldn't hardcode these values here, should we get them from the API
// maybe take from specification
import { dataSubjects } from '../utils/utils.js'

class DatasetController extends MyController {
class DatasetController extends PageController {
get (req, res, next) {
// const dataset = req.sessionModel.get('data-subject')
// const options = datasetOptions[dataset]
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/errorsController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

import MyController from './MyController.js'
import PageController from './pageController.js'

import { severityLevels, dataSubjects } from '../utils/utils.js'

class ErrorsController extends MyController {
class ErrorsController extends PageController {
get (req, res, next) {
const validationResult = req.sessionModel.get('validationResult')

Expand Down
23 changes: 23 additions & 0 deletions src/controllers/pageController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import hmpoFormWizard from 'hmpo-form-wizard'
import logger from '../utils/logger.js'
const { Controller } = hmpoFormWizard

class PageController extends Controller {
locals (req, res, callback) {
req.form.options.lastPage = req.journeyModel.get('lastVisited')
super.locals(req, res, callback)
}

get (req, res, next) {
logger.info({
type: 'PageView',
pageRoute: this.options.route,
message: `page view occurred for page: ${req.originalUrl}`,
sessionId: req.sessionID,
ipAddress: req.ip
})
super.get(req, res, next)
}
}

export default PageController
12 changes: 8 additions & 4 deletions src/controllers/uploadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import multer from 'multer'
import axios from 'axios'
import { readFile } from 'fs/promises'
import { lookup } from 'mime-types'
import MyController from './MyController.js'
import PageController from './pageController.js'
import config from '../../config/index.js'

import { severityLevels } from '../utils/utils.js'
Expand All @@ -13,7 +13,7 @@ const upload = multer({ dest: 'uploads/' })

const apiRoute = config.api.url + config.api.validationEndpoint

class UploadController extends MyController {
class UploadController extends PageController {
middlewareSetup () {
super.middlewareSetup()
this.use('/upload', upload.single('datafile'))
Expand All @@ -27,7 +27,9 @@ class UploadController extends MyController {
fileName: req.file.originalname,
dataset: req.sessionModel.get('dataset'),
dataSubject: req.sessionModel.get('data-subject'),
organisation: 'local-authority-eng:CAT' // ToDo: this needs to be dynamic, not collected in the prototype, should it be?
organisation: 'local-authority-eng:CAT', // ToDo: this needs to be dynamic, not collected in the prototype, should it be?
sessionId: req.sessionID,
ipAddress: req.ip
})
this.errorCount = jsonResult['issue-log'].filter(issue => issue.severity === severityLevels.error).length
req.body.datafile = req.file
Expand All @@ -39,11 +41,13 @@ class UploadController extends MyController {
super.post(req, res, next)
}

async validateFile ({ filePath, fileName, dataset, dataSubject, organisation }) {
async validateFile ({ filePath, fileName, dataset, dataSubject, organisation, sessionId, ipAddress }) {
const formData = new FormData()
formData.append('dataset', dataset)
formData.append('collection', dataSubject)
formData.append('organisation', organisation)
formData.append('sessionId', sessionId)
formData.append('ipAddress', ipAddress)

const file = new Blob([await readFile(filePath)], { type: lookup(filePath) })

Expand Down
4 changes: 2 additions & 2 deletions src/routes/form-wizard/steps.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import MyController from '../../controllers/MyController.js'
import PageController from '../../controllers/pageController.js'
import datasetController from '../../controllers/datasetController.js'
import uploadController from '../../controllers/uploadController.js'
import errorsController from '../../controllers/errorsController.js'

const baseSettings = {
controller: MyController,
controller: PageController,
editable: true,
editBackStep: 'check'
}
Expand Down
41 changes: 41 additions & 0 deletions test/unit/MyController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import PageController from '../../src/controllers/pageController.js'

import { describe, it, vi, expect } from 'vitest'

import logger from '../../src/utils/logger.js'

describe('PageController', () => {
const loggerInfoMock = vi.fn()

vi.mock('../utils/logger.js', () => {
return {
info: loggerInfoMock
}
})

const loggerInfoSpy = vi.spyOn(logger, 'info')

it('Correctly creates a log when the page is viewed', () => {
const req = {
originalUrl: '/dataset',
sessionID: '123',
ip: '1234'
}
const pageController = new PageController({
route: '/dataset'
})
// pageController.super.get = vi.fn();
pageController.get(req, {}, vi.fn())
expect(loggerInfoSpy).toHaveBeenCalledOnce()

const callArgs = loggerInfoSpy.mock.calls[0][0]

expect(callArgs.type).toEqual('PageView')
expect(callArgs.pageRoute).toEqual('/dataset')
expect(callArgs.message).toEqual('page view occurred for page: /dataset')
expect(callArgs.sessionId).toEqual('123')
expect(callArgs.ipAddress).toEqual('1234')
expect(callArgs.level).toEqual('info')
expect(callArgs.service).toEqual('lpa-data-validation-frontend')
})
})