Skip to content

Commit

Permalink
Create Service Status holding page (#7)
Browse files Browse the repository at this point in the history
DEFRA/water-abstraction-team#54

Here we have the addition of a status page to the service repo. This allows us to see the different status of the service extensions (virus checker, database etc).

This change just creates the initial holding page. We will add the additional functionality in separate PRs
  • Loading branch information
Beckyrose200 authored Oct 19, 2022
1 parent 83b6fa1 commit ce5c536
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/controllers/root.controller.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
'use strict'

const ServiceStatusService = require('../services/service_status.service')

class RootController {
static async index (_req, _h) {
return { status: 'alive' }
}

static async helloWorld (_req, h) {
static helloWorld (_req, h) {
return h.view('home.njk', {
title: 'Hello',
message: 'World',
pageTitle: 'Hello World!'
})
}

static async serviceStatus (_req, h) {
const pageData = await ServiceStatusService.go()

return h.view('service_status.njk', {
pageTitle: 'Service Status',
...pageData
})
}
}

module.exports = RootController
8 changes: 8 additions & 0 deletions app/routes/root.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const routes = [
options: {
auth: false
}
},
{
method: 'GET',
path: '/service-status',
handler: RootController.serviceStatus,
options: {
auth: false
}
}
]

Expand Down
107 changes: 107 additions & 0 deletions app/services/service_status.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict'

/**
* @module ServiceStatusService
*/

/**
* Returns data required to populate our `/service-status` page, eg. task activity status, virus checker status, service
* version numbers, etc.
*
* Each data set is returned in the format needed to populate the gov.uk table elements ie. an array containing one
* array per row, where each row array contains multiple `{ text: '...' }` elements, one for each cell in the row.
*/
class ServiceStatusService {
static async go () {
const importData = await this._getImportData()
const virusScannerData = await this._getVirusScannerData()
const cacheConnectivityData = await this._getCacheConnectivityData()
const serviceVersionsData = await this._getServiceVersionsData()

return {
importRows: this._mapArrayToTextCells(importData),
virusScannerRows: this._mapArrayToStatusCells(virusScannerData),
cacheConnectivityRows: this._mapArrayToStatusCells(cacheConnectivityData),
serviceVersionsRows: this._mapArrayToStatusCells(serviceVersionsData)
}
}

/**
* Receives an array and returns it in the format required by the nunjucks template in the view.
*/
static _mapArrayToTextCells (rows) {
return rows.map(row => {
return row.map(cell => {
return { text: cell }
})
})
}

/**
* Receives an array of statuses and returns it in the format required by the nunjucks template in the view.
*/
static _mapArrayToStatusCells (rows) {
// Map each row in the array we've received
return rows.map(row => {
// A status row has only two elements:
// * The thing having its status reported, which is a standard text cell;
// * Its status, which is formatted numeric so that it's right justified on its row.
return [
{ text: row[0] },
{ text: row[1], format: 'numeric' }
]
})
}

static async _getImportData () {
return [
[
'Cell 1.1',
'Cell 1.2',
'Cell 1.3',
'Cell 1.4',
'Cell 1.5'
],
[
'Cell 2.1',
'Cell 2.2',
'Cell 2.3',
'Cell 2.4',
'Cell 2.5'
]
]
}

static async _getVirusScannerData () {
return [
[
'Status',
'OK'
]
]
}

static async _getCacheConnectivityData () {
return [
[
'Status',
'Connected'
]
]
}

static async _getServiceVersionsData () {
return [
[
'Water service',
'3.0.1'
],
[
'IDM',
'2.25.1'
]
]
}
}

module.exports = ServiceStatusService
54 changes: 54 additions & 0 deletions app/views/service_status.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{% extends 'layout.njk' %}

{% from "govuk/components/table/macro.njk" import govukTable %}

{% block content %}
<div class="govuk-body">
<h1 class="govuk-heading-l">Service Status</h1>

{{ govukTable({
caption: "Imports",
captionClasses: "govuk-table__caption--m",
firstCellIsHeader: true,
head: [
{
text: "Import task name"
},
{
text: "Success rate (last 3 days)"
},
{
text: "Active"
},
{
text: "Last updated"
},
{
text: "Last checked"
}
],
rows: importRows
}) }}

{{ govukTable({
caption: "Virus scanner",
captionClasses: "govuk-table__caption--m",
firstCellIsHeader: true,
rows: virusScannerRows
}) }}

{{ govukTable({
caption: "Cache connectivity",
captionClasses: "govuk-table__caption--m",
firstCellIsHeader: true,
rows: cacheConnectivityRows
}) }}

{{ govukTable({
caption: "Service versions",
captionClasses: "govuk-table__caption--m",
firstCellIsHeader: true,
rows: serviceVersionsRows
}) }}
</div>
{% endblock %}
40 changes: 40 additions & 0 deletions test/services/service_status.service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const ServiceStatusService = require('../../app/services/service_status.service.js')

const firstRow = [
{ text: 'Cell 1.1' },
{ text: 'Cell 1.2' },
{ text: 'Cell 1.3' },
{ text: 'Cell 1.4' },
{ text: 'Cell 1.5' }
]

const secondRow = [
{ text: 'Cell 2.1' },
{ text: 'Cell 2.2' },
{ text: 'Cell 2.3' },
{ text: 'Cell 2.4' },
{ text: 'Cell 2.5' }
]

describe('Service Status service', () => {
describe('importRows data', () => {
it('returns data in the expected format', async () => {
const { importRows } = await ServiceStatusService.go()

expect(importRows).to.equal([
firstRow,
secondRow
])
})
})
})

0 comments on commit ce5c536

Please sign in to comment.