Skip to content

Commit

Permalink
Merge branch 'main' into update-department-name
Browse files Browse the repository at this point in the history
  • Loading branch information
rosado authored Aug 14, 2024
2 parents d3652e7 + b0c4e33 commit c1d677b
Show file tree
Hide file tree
Showing 17 changed files with 298 additions and 214 deletions.
37 changes: 17 additions & 20 deletions src/controllers/OrganisationsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import performanceDbApi from '../services/performanceDbApi.js' // Assume you hav
import logger from '../utils/logger.js'
import { types } from '../utils/logging.js'
import { dataSubjects } from '../utils/utils.js'
import { statusToTagClass } from '../filters/filters.js'

// get a list of available datasets
const availableDatasets = Object.values(dataSubjects)
Expand All @@ -19,17 +20,10 @@ const availableDatasets = Object.values(dataSubjects)
* @returns {object} - An object with a `tag` property containing the text label and CSS class.
*/
function getStatusTag (status) {
const statusToTagClass = {
Error: 'govuk-tag--red',
'Needs fixing': 'govuk-tag--yellow',
Warning: 'govuk-tag--blue',
Issue: 'govuk-tag--blue'
}

return {
tag: {
text: status,
classes: statusToTagClass[status]
classes: statusToTagClass(status)
}
}
}
Expand All @@ -46,6 +40,9 @@ const organisationsController = {
try {
const lpa = req.params.lpa

const organisationResult = await datasette.runQuery(`SELECT name, organisation FROM organisation WHERE organisation = '${lpa}'`)
const organisation = organisationResult.formattedData[0]

const datasetsFilter = ['article-4-direction',
'article-4-direction-area',
'conservation-area',
Expand Down Expand Up @@ -73,24 +70,23 @@ const organisationsController = {
if (!keys.includes(dataset)) {
datasets.push({
slug: dataset,
endpoint: null
endpoint: null,
issue_count: 0,
status: 'Not submitted'
})
}
})

const totalDatasets = datasets.length
const [datasetsWithEndpoints, datasetsWithIssues, datasetsWithErrors] = datasets.reduce((accumulator, dataset) => {
if (dataset.endpoint !== null) accumulator[0]++
if (dataset.issue) accumulator[1]++
if (dataset.error) accumulator[2]++
if (dataset.endpoint) accumulator[0]++
if (dataset.status === 'Needs fixing') accumulator[1]++
if (dataset.status === 'Error') accumulator[2]++
return accumulator
}, [0, 0, 0])

const params = {
organisation: {
name: lpaOverview.name,
organisation: lpaOverview.organisation
},
organisation,
datasets,
totalDatasets,
datasetsWithEndpoints,
Expand All @@ -100,7 +96,7 @@ const organisationsController = {

res.render('organisations/overview.html', params)
} catch (error) {
logger.error(error)
logger.warn('organisationsController.getOverview(): ' + error.message ?? error.errorMessage, { type: types.App })
next(error)
}
},
Expand Down Expand Up @@ -130,7 +126,7 @@ const organisationsController = {

res.render('organisations/find.html', { alphabetisedOrgs })
} catch (err) {
logger.warn(err)
logger.warn('organisationsController.getOrganisations(): ' + err.message ?? err.errorMessage, { type: types.App })
next(err)
}
},
Expand Down Expand Up @@ -183,7 +179,7 @@ const organisationsController = {
const datasetId = req.params.dataset

try {
const organisationResult = await datasette.runQuery(`SELECT name FROM organisation WHERE organisation = '${lpa}'`)
const organisationResult = await datasette.runQuery(`SELECT name, organisation FROM organisation WHERE organisation = '${lpa}'`)
const organisation = organisationResult.formattedData[0]

const datasetResult = await datasette.runQuery(`SELECT name FROM dataset WHERE dataset = '${datasetId}'`)
Expand Down Expand Up @@ -377,7 +373,8 @@ const organisationsController = {
dataset,
errorHeading,
issueItems,
entry
entry,
issueType
}

res.render('organisations/issueDetails.html', params)
Expand Down
18 changes: 18 additions & 0 deletions src/filters/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import prettifyColumnName from './prettifyColumnName.js'
import getFullServiceName from './getFullServiceName.js'
import { makeDatasetSlugToReadableNameFilter } from './makeDatasetSlugToReadableNameFilter.js'

/** maps dataset status (as returned by performanceDbApi/getLpaOverview()) to a
* CSS class used by the govuk-tag component
*/
const statusToTagClassMapping = {
Error: 'govuk-tag--red',
'Not submitted': 'govuk-tag--red',
'Needs fixing': 'govuk-tag--yellow',
Warning: 'govuk-tag--blue',
Issue: 'govuk-tag--blue', // deprecated
Live: 'govuk-tag--green'
}

export function statusToTagClass (status) {
console.assert(status in statusToTagClassMapping, `statusToTagClass: unknown status ${status}`)
return statusToTagClassMapping[status]
}

const { govukMarkdown, govukDateTime } = xGovFilters

const addFilters = (nunjucksEnv, { datasetNameMapping }) => {
Expand All @@ -20,6 +37,7 @@ const addFilters = (nunjucksEnv, { datasetNameMapping }) => {
nunjucksEnv.addFilter('toErrorList', toErrorList)
nunjucksEnv.addFilter('prettifyColumnName', prettifyColumnName)
nunjucksEnv.addFilter('getFullServiceName', getFullServiceName)
nunjucksEnv.addFilter('statusToTagClass', statusToTagClass)
}

export default addFilters
2 changes: 1 addition & 1 deletion src/services/datasette.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
* Executes a SQL query on the Datasette instance and returns the results.
*
* @param {string} query - The SQL query to execute.
* @returns {Promise<{data: object, formattedData: object}>} - A promise that resolves to an object with the following properties:
* @returns {Promise<{data: object, formattedData: object[]}>} - A promise that resolves to an object with the following properties:
* - `data`: The raw data returned by Datasette.
* - `formattedData`: The formatted data, with columns and rows parsed into a usable format.
* @throws {Error} If the query fails or there is an error communicating with Datasette.
Expand Down
85 changes: 38 additions & 47 deletions src/services/performanceDbApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Performance DB API service
*/
import datasette from './datasette.js'
import logger from '../utils/logger.js'

// ===========================================

Expand Down Expand Up @@ -41,14 +42,14 @@ fs.createReadStream('src/content/entityIssueMessages.csv')

/**
* @typedef {object} Dataset
* @property {'Not submitted' | 'Error' | 'Needs fixing' | 'Warning' | 'Live' } status
* @property {string} endpoint
* @property {number} issue_count
* @property {?string} error
* @property {?string} issue
*/

/**
* @typedef {object} LpaOverview
* @property {string} name
* @property {{ [dataset: string]: Dataset }} datasets
*/

Expand Down Expand Up @@ -80,27 +81,32 @@ export default {
rle.resource,
rle.exception,
rle.status as http_status,
case
when (rle.status != '200') then 'Error'
when (it.severity = 'error') then 'Issue'
when (it.severity = 'warning') then 'Warning'
else 'No issues'
end as status,
case
when (rle.status is null) then 'Not Submitted'
when (rle.status != '200') then 'Error'
when (it.severity = 'error') then 'Needs fixing'
when (it.severity = 'warning') then 'Warning'
else 'Live'
end as status,
case
when (it.severity = 'info') then ''
else i.issue_type
end as issue_type,
case
when (it.severity = 'info') then ''
else it.severity
end as severity,
it.responsibility,
COUNT(
case
when it.severity != 'info' then 1
else null
end
) as issue_count
when ((cast(rle.status as integer) > 200)) then format('There was a %s error accessing the data URL', rle.status)
else null
end as error,
case
when (it.severity = 'info') then ''
else i.issue_type
end as issue_type,
case
when (it.severity = 'info') then ''
else it.severity
end as severity,
it.responsibility,
COUNT(
case
when it.severity != 'info' then 1
else null
end
) as issue_count
FROM
provision p
LEFT JOIN
Expand All @@ -126,33 +132,22 @@ ORDER BY
p.organisation,
o.name;
`

const result = await datasette.runQuery(query)
if (result.formattedData.length === 0) {
logger.info(`No records found for LPA=${lpa}`)
}

const datasets = result.formattedData.reduce((accumulator, row) => {
let error
if (row.http_status !== '200' || row.exception) {
error = row.exception ? row.exception : `endpoint returned with a status of ${row.http_status}`
}

let issue
if (row.issue_count > 0) {
issue = `There are ${row.issue_count} issues in this dataset`
}

accumulator[row.dataset] = {
endpoint: row.endpoint,
error,
issue
status: row.status,
issue_count: row.issue_count,
error: row.error
}
return accumulator
}, {})

return {
name: result.formattedData[0].name,
organisation: result.formattedData[0].organisation,
datasets
}
return { datasets }
},

getResourceStatus: async (lpa, datasetId) => {
Expand Down Expand Up @@ -206,13 +201,9 @@ ORDER BY
ORDER BY it.severity`

const result = await datasette.runQuery(sql)
return result.formattedData.map((row) => {
return {
num_issues: row.num_issues,
issue_type: row.issue_type,
resource: row.resource,
status: row.status
}
/* eslint camelcase: "off" */
return result.formattedData.map(({ num_issues, issue_type, resource, status }) => {
return { num_issues, issue_type, resource, status }
})
},

Expand Down
24 changes: 23 additions & 1 deletion src/views/organisations/datasetTaskList.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% from "govuk/components/breadcrumbs/macro.njk" import govukBreadcrumbs %}

{% extends "layouts/main.html" %}

{% from 'govuk/components/task-list/macro.njk' import govukTaskList %}
Expand All @@ -10,6 +12,26 @@

{{ super() }}

{{ govukBreadcrumbs({
items: [
{
text: "Home",
href: "/manage"
},
{
text: "Organisations",
href: '/organisations'
},
{
text: organisation.name,
href: '/organisations/' + organisation.organisation
},
{
text: dataset.name
}
]
}) }}

{% endblock %}

{% block content %}
Expand Down Expand Up @@ -45,7 +67,7 @@ <h3 class="govuk-heading-m">

<ol class="govuk-list govuk-list--number">
<li>Fix the errors indicated</li>
<li>Use the <a href="https://check.staging.digital-land.info/">check service</a> to make sure the data meets
<li>Use the <a href="/">check service</a> to make sure the data meets
the standard</li>
<li>Publish the updated data on the data URL</li>
</ol>
Expand Down
14 changes: 14 additions & 0 deletions src/views/organisations/find.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% from "govuk/components/breadcrumbs/macro.njk" import govukBreadcrumbs %}

{% extends "layouts/main.html" %}

{% set serviceType = 'Submit' %}
Expand All @@ -7,6 +9,18 @@

{{ super() }}

{{ govukBreadcrumbs({
items: [
{
text: "Home",
href: "/manage"
},
{
text: "Organisations"
}
]
}) }}

{% endblock %}

{% block content %}
Expand Down
Loading

0 comments on commit c1d677b

Please sign in to comment.