Skip to content

Commit

Permalink
get data from datasette
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Jul 12, 2024
1 parent 7901b6f commit 9f28f4e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 45 deletions.
7 changes: 3 additions & 4 deletions src/controllers/LpaOverviewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const LpaOverviewController = {
try {
const lpa = req.params.lpa

const response = await performanceDbApi.getLpaOverview(lpa) // Make API request
const data = response.data
const lpaOverview = await performanceDbApi.getLpaOverview(lpa) // Make API request

const datasets = Object.entries(data.datasets).map(([key, value]) => {
const datasets = Object.entries(lpaOverview.datasets).map(([key, value]) => {
return { ...value, slug: key }
})
const totalDatasets = datasets.length
Expand All @@ -22,7 +21,7 @@ const LpaOverviewController = {

const params = {
organisation: {
name: data.name
name: lpaOverview.name
},
datasets,
totalDatasets,
Expand Down
7 changes: 6 additions & 1 deletion src/filters/makeDatasetSlugToReadableNameFilter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logger from '../utils/logger.js'

/**
* Creates a filter function that takes a dataset slug as input and returns its corresponding readable name.
* The filter function uses a provided dataset name mapping to look up the readable name.
Expand All @@ -16,7 +18,10 @@ export const makeDatasetSlugToReadableNameFilter = (datasetNameMapping) => {
return (slug) => {
const name = datasetNameMapping.get(slug)
if (!name) {
throw new Error(`Can't find a name for ${slug}`)
// throw new Error(`Can't find a name for ${slug}`)
// ToDo: work out what to do here? potentially update it with data from datasette
logger.error(`can't find a name for ${slug}`)
return slug
}
return name
}
Expand Down
19 changes: 19 additions & 0 deletions src/services/datasette.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios from 'axios'
import logger from '../utils/logger.js'

const datasetteUrl = 'https://datasette.planning.data.gov.uk'
const database = 'digital-land'

export default {
runQuery: async (query) => {
const encodedQuery = encodeURIComponent(query)
const url = `${datasetteUrl}/${database}.json?sql=${encodedQuery}`
try {
const response = await axios.get(url)
return response.data
} catch (error) {
logger.error(error)
throw error
}
}
}
119 changes: 79 additions & 40 deletions src/services/performanceDbApi.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,86 @@
import datasette from './datasette.js'

/*
*/

export default {
getLpaOverview: async (lpa) => {
getLpaOverviewOld: async (lpa) => {
return {
data: {
name: 'Borechester City Council',
datasets: {
'article-4-direction': {
endpoint: null
},
'article-4-direction-area': {
endpoint: null
},
'conservation-area': {
endpoint: 'http://conservation-area.json',
error: null,
issue: 'Endpoint has not been updated since 21 May 2023'
},
'conservation-area-document': {
endpoint: 'http://conservation-area-document.json',
error: null,
issue: null
},
'listed-building-outline': {
endpoint: 'http://listed-building-outline.json',
error: null,
issue: null
},
tree: {
endpoint: 'http://tree.json',
error: null,
issue: 'There are 20 issues in this dataset'
},
'tree-preservation-order': {
endpoint: 'http://tree-preservation-order.json',
error: 'Error connecting to endpoint',
issue: null
},
'tree-preservation-zone': {
endpoint: 'http://tree-preservation-zone.json',
error: 'Error connecting to endpoint',
issue: null
}
name: 'Borechester City Council',
datasets: {
'article-4-direction': {
endpoint: null
},
'article-4-direction-area': {
endpoint: null
},
'conservation-area': {
endpoint: 'http://conservation-area.json',
error: null,
issue: 'Endpoint has not been updated since 21 May 2023'
},
'conservation-area-document': {
endpoint: 'http://conservation-area-document.json',
error: null,
issue: null
},
'listed-building-outline': {
endpoint: 'http://listed-building-outline.json',
error: null,
issue: null
},
tree: {
endpoint: 'http://tree.json',
error: null,
issue: 'There are 20 issues in this dataset'
},
'tree-preservation-order': {
endpoint: 'http://tree-preservation-order.json',
error: 'Error connecting to endpoint',
issue: null
},
'tree-preservation-zone': {
endpoint: 'http://tree-preservation-zone.json',
error: 'Error connecting to endpoint',
issue: null
}
}
}
},

getLpaOverview: async (lpa) => {
const query =
`SELECT
p.organisation,
o.name,
p.dataset,
rle.endpoint
FROM
provision p
INNER JOIN
organisation o ON o.organisation = p.organisation
LEFT JOIN
reporting_latest_endpoints rle
ON REPLACE(rle.organisation, '-eng', '') = p.organisation
AND rle.pipeline = p.dataset
WHERE p.organisation = '${lpa}'
ORDER BY
p.organisation,
o.name`

const result = await datasette.runQuery(query)

const datasets = result.rows.reduce((accumulator, row) => {
accumulator[row[2]] = {
endpoint: row[3]
}
return accumulator
}, {})

return {
name: result.rows[0][1],
datasets
}
}
}

0 comments on commit 9f28f4e

Please sign in to comment.