|
| 1 | +import fetchDatasetsRequiredForLocalAuthority from './datasetteQueries/fetchDatasetsFromProvisions.js' |
| 2 | +import { datasetSlugToReadableName } from './datasetSlugToReadableName.js' |
| 3 | +import logger from './logger.js' |
| 4 | +import config from '../../config/index.js' |
| 5 | +import { getDatasetCollectionSlugNameMapping } from './datasetteQueries/fetchDatasetCollections.js' |
| 6 | +import { getRedisClient } from './datasetLoader.js' |
| 7 | + |
| 8 | +// Use a short TTL in development |
| 9 | +const CACHE_TTL = (['development', 'local'].includes(config.environment)) ? 60 : 60 * 60 // 1 minute or 1 hour depending on environment |
| 10 | + |
| 11 | +const fallbackDataSubjects = { |
| 12 | + 'article-4-direction': { |
| 13 | + available: true, |
| 14 | + dataSets: [ |
| 15 | + { |
| 16 | + value: 'article-4-direction', |
| 17 | + text: 'Article 4 direction', |
| 18 | + available: true |
| 19 | + }, |
| 20 | + { |
| 21 | + value: 'article-4-direction-area', |
| 22 | + text: 'Article 4 direction area', |
| 23 | + available: true |
| 24 | + } |
| 25 | + ] |
| 26 | + }, |
| 27 | + 'brownfield-land': { |
| 28 | + available: true, |
| 29 | + dataSets: [ |
| 30 | + { |
| 31 | + value: 'brownfield-land', |
| 32 | + text: 'Brownfield land', |
| 33 | + available: true |
| 34 | + }, |
| 35 | + { |
| 36 | + value: 'brownfield-site', |
| 37 | + text: 'Brownfield site', |
| 38 | + available: false |
| 39 | + } |
| 40 | + ] |
| 41 | + }, |
| 42 | + 'conservation-area': { |
| 43 | + available: true, |
| 44 | + dataSets: [ |
| 45 | + { |
| 46 | + value: 'conservation-area', |
| 47 | + text: 'Conservation area', |
| 48 | + available: true |
| 49 | + }, |
| 50 | + { |
| 51 | + value: 'conservation-area-document', |
| 52 | + text: 'Conservation area document', |
| 53 | + available: true |
| 54 | + } |
| 55 | + ] |
| 56 | + }, |
| 57 | + 'listed-building': { |
| 58 | + available: true, |
| 59 | + dataSets: [ |
| 60 | + { |
| 61 | + value: 'listed-building', |
| 62 | + text: 'Listed building', |
| 63 | + available: false |
| 64 | + }, |
| 65 | + { |
| 66 | + value: 'listed-building-grade', |
| 67 | + text: 'Listed building grade', |
| 68 | + available: false |
| 69 | + }, |
| 70 | + { |
| 71 | + value: 'listed-building-outline', |
| 72 | + text: 'Listed building outline', |
| 73 | + available: true |
| 74 | + } |
| 75 | + ] |
| 76 | + }, |
| 77 | + 'tree-preservation-order': { |
| 78 | + available: true, |
| 79 | + dataSets: [ |
| 80 | + { |
| 81 | + value: 'tree', |
| 82 | + text: 'Tree', |
| 83 | + available: true, |
| 84 | + requiresGeometryTypeSelection: true |
| 85 | + }, |
| 86 | + { |
| 87 | + value: 'tree-preservation-order', |
| 88 | + text: 'Tree preservation order', |
| 89 | + available: true |
| 90 | + }, |
| 91 | + { |
| 92 | + value: 'tree-preservation-zone', |
| 93 | + text: 'Tree preservation zone', |
| 94 | + available: true |
| 95 | + } |
| 96 | + ] |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/* Assign datasets to their collections if a mapping exists; otherwise, group them under 'other', also a check here is done to see if dataset table exists. |
| 101 | +* Special handling: e.g., the 'tree' dataset requires geometry type selection |
| 102 | +* The fallbackDataSubjects demos what is being built. |
| 103 | +*/ |
| 104 | +export async function makeDatasetSubjectMap (nameMap) { |
| 105 | + const dataSubjectMapping = await getDatasetCollectionSlugNameMapping(nameMap) |
| 106 | + |
| 107 | + if (!dataSubjectMapping) { |
| 108 | + logger.warn('Failed to fetch dataset collection mapping, using fallback') |
| 109 | + return fallbackDataSubjects |
| 110 | + } |
| 111 | + |
| 112 | + const subjects = {} |
| 113 | + for (const key of Object.keys(nameMap)) { |
| 114 | + const dataset = { |
| 115 | + value: key, |
| 116 | + text: nameMap[key], |
| 117 | + available: true |
| 118 | + } |
| 119 | + |
| 120 | + // Add special handling for any: currenlty only for tree dataset |
| 121 | + if (key === 'tree') { |
| 122 | + dataset.requiresGeometryTypeSelection = true |
| 123 | + } |
| 124 | + |
| 125 | + // Only add if there is a collection mapping, if mapping is to empty collection '', make key ='other' |
| 126 | + const mapping = dataSubjectMapping.get(key) |
| 127 | + if (mapping === undefined) { |
| 128 | + continue |
| 129 | + } |
| 130 | + const subjectKey = mapping === '' ? 'other' : mapping |
| 131 | + |
| 132 | + // Initialize the subject if it doesn't exist |
| 133 | + if (!subjects[subjectKey]) { |
| 134 | + subjects[subjectKey] = { |
| 135 | + available: true, |
| 136 | + dataSets: [] |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + subjects[subjectKey].dataSets.push(dataset) |
| 141 | + } |
| 142 | + |
| 143 | + return subjects |
| 144 | +} |
| 145 | + |
| 146 | +// Build data subjects by fetching dataset keys from provision table instead of hard coding. |
| 147 | +export async function buildDataSubjects () { |
| 148 | + // First try to fetch dataset keys(slugs) from provisions table (i.e.the keys(datasets) that the LA is expected to provide based on provision rules) |
| 149 | + let datasetKeys = [] |
| 150 | + try { |
| 151 | + datasetKeys = await fetchDatasetsRequiredForLocalAuthority() |
| 152 | + } catch (error) { |
| 153 | + logger.warn('buildDataSubjects: Error fetching dataset keys roll back to defaults') |
| 154 | + datasetKeys = Object.keys(config.datasetsConfig) |
| 155 | + } |
| 156 | + |
| 157 | + // Use existing datasetSlugToReadableName to create lookup of dataset keys to readable names |
| 158 | + const nameMap = {} |
| 159 | + for (const key of datasetKeys) { |
| 160 | + nameMap[key] = datasetSlugToReadableName(key) |
| 161 | + } |
| 162 | + |
| 163 | + return makeDatasetSubjectMap(nameMap) |
| 164 | +} |
| 165 | + |
| 166 | +// Get data subject map, with Redis caching |
| 167 | +export async function getDataSubjectMap () { |
| 168 | + let dataSubjectMap = {} |
| 169 | + |
| 170 | + const cacheKey = 'dataset:dataSubjectMap' |
| 171 | + const client = await getRedisClient() |
| 172 | + |
| 173 | + if (client) { |
| 174 | + try { |
| 175 | + const cached = await client.get(cacheKey) |
| 176 | + if (cached) { |
| 177 | + dataSubjectMap = JSON.parse(cached) |
| 178 | + return dataSubjectMap |
| 179 | + } |
| 180 | + } catch (err) { |
| 181 | + logger.warn(`datasetSubjectLoader/redis get error: ${err.message}`) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // fallback → fetch fresh |
| 186 | + logger.info('getDataSubjectMap: Dataset subject map cache miss, rebuilding data subject map') |
| 187 | + dataSubjectMap = await buildDataSubjects() |
| 188 | + |
| 189 | + if (client) { |
| 190 | + try { |
| 191 | + await client.setEx(cacheKey, CACHE_TTL, JSON.stringify(dataSubjectMap)) |
| 192 | + } catch (err) { |
| 193 | + logger.warn(`datasetSubjectLoader/redis set error: ${err.message}`) |
| 194 | + } |
| 195 | + } |
| 196 | + return dataSubjectMap |
| 197 | +} |
0 commit comments