-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added report routes, storage and batch processing
- Loading branch information
1 parent
ae588a6
commit e4197ed
Showing
7 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import mongo from '../../util/mongo.cjs' | ||
import {formatAndUpdateReports} from '../report/utils.js' | ||
|
||
const buildReportsConsumer = async () => { | ||
try { | ||
console.log('Building reports...') | ||
const filters = { | ||
status: null, | ||
preProcessingStatusKey: 0, | ||
'meta.targetedPlateform': 'ban', | ||
preProcessingResponse: {$ne: {}} | ||
} | ||
|
||
const reportsNotCompletelyBuilt = await mongo.db.collection('processing_reports').find({...filters}).toArray() | ||
await formatAndUpdateReports(reportsNotCompletelyBuilt) | ||
console.log('Reports built successfully') | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
export default buildReportsConsumer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import | ||
import express from 'express' | ||
import {getDistrict} from '../district/models.js' | ||
import mongo from '../../util/mongo.cjs' | ||
import auth from '../../middleware/auth.js' | ||
import analyticsMiddleware from '../../middleware/analytics.js' | ||
import {formatAndUpdateReports, formatReportToInsert} from './utils.js' | ||
|
||
const app = new express.Router() | ||
|
||
app.route('/district/:districtID') | ||
.get(auth, analyticsMiddleware, async (req, res) => { | ||
let response | ||
try { | ||
const {districtID} = req.params | ||
const reports = await mongo.db.collection('processing_reports') | ||
.find({districtID}) | ||
.sort({preProcessingDate: -1}) | ||
.toArray() | ||
const formattedReports = await formatAndUpdateReports(reports) | ||
res.send(formattedReports) | ||
} catch (error) { | ||
const {message} = error | ||
response = { | ||
date: new Date(), | ||
status: 'error', | ||
message, | ||
response: {}, | ||
} | ||
} | ||
|
||
res.send(response) | ||
}) | ||
|
||
app.route('/district/cog/:cog') | ||
.get(auth, analyticsMiddleware, async (req, res) => { | ||
let response | ||
try { | ||
const {cog} = req.params | ||
const reports = await mongo.db.collection('processing_reports') | ||
.find({'meta.cog': cog}) | ||
.sort({preProcessingDate: 1}) | ||
.toArray() | ||
const formattedReports = await formatAndUpdateReports(reports) | ||
res.send(formattedReports) | ||
} catch (error) { | ||
const {message} = error | ||
response = { | ||
date: new Date(), | ||
status: 'error', | ||
message, | ||
response: {}, | ||
} | ||
} | ||
|
||
res.send(response) | ||
}) | ||
|
||
app.route('/district/:districtID') | ||
.post(auth, analyticsMiddleware, async (req, res) => { | ||
let response | ||
try { | ||
const {districtID} = req.params | ||
const district = await getDistrict(districtID) | ||
|
||
if (!district) { | ||
res.status(404).send('Request ID unknown') | ||
return | ||
} | ||
|
||
// Count the number of documents with the same districtID | ||
const count = await mongo.db.collection('processing_reports').countDocuments({districtID}) | ||
|
||
// If the count is 5 or more, delete the oldest document based on preProcessingDate | ||
if (count >= 5) { | ||
await mongo.db.collection('processing_reports').findOneAndDelete({districtID}, {sort: {preProcessingDate: 1}}).toArray() | ||
} | ||
|
||
const report = req.body | ||
const formatedReportToInsert = formatReportToInsert(districtID, report) | ||
|
||
await mongo.db.collection('processing_reports').insertOne(formatedReportToInsert, {upsert: true}) | ||
response = { | ||
date: new Date(), | ||
status: 'success', | ||
message: 'Processing report created successfully', | ||
response: {}, | ||
} | ||
} catch (error) { | ||
const {message} = error | ||
response = { | ||
date: new Date(), | ||
status: 'error', | ||
message, | ||
response: {}, | ||
} | ||
} | ||
|
||
res.send(response) | ||
}) | ||
|
||
export default app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import {PromisePool} from '@supercharge/promise-pool' | ||
import mongo from '../../util/mongo.cjs' | ||
import {getJobStatus} from '../job-status/models.js' | ||
|
||
const categories = ['addresses', 'commonToponyms', 'districts'] | ||
const operations = ['add', 'update', 'delete'] | ||
|
||
// Status key: 0 = success, 1 = error, -1 = unknown | ||
|
||
export const formatReportToInsert = (districtID, report) => { | ||
const {preProcessingDate, ...rest} = report | ||
return { | ||
districtID, | ||
...rest, | ||
...(preProcessingDate ? {preProcessingDate: new Date(preProcessingDate)} : {}), | ||
} | ||
} | ||
|
||
export const formatAndUpdateReports = async reports => { | ||
const {results} = await PromisePool | ||
.withConcurrency(10) | ||
.for(reports) | ||
.process(formatAndUpdateReport) | ||
return results | ||
} | ||
|
||
const formatAndUpdateReport = async report => { | ||
const {_id, ...reportRest} = report | ||
const {status, preProcessingStatusKey, preProcessingResponse, meta: {targetedPlateform}} = reportRest | ||
// If the report does not have a final status yet and the pre-processing status is in success, we need to reconstruct. | ||
// the flag 'targetedPlateform' is used to determine if the pre-processing data has been sent to the ban APIs or the legacy compose API | ||
// Our ban APIs are asynchronous so the preprocessing report need to be reconstructed to get the final status | ||
if (!status && preProcessingStatusKey === 0 | ||
&& targetedPlateform === 'ban' | ||
&& Object.keys(preProcessingResponse).length > 0 | ||
) { | ||
try { | ||
let errorCount = 0 | ||
let mostRecentDate = new Date(0) | ||
const formattedResponse = {} | ||
|
||
for (const category of categories) { | ||
for (const operation of operations) { | ||
if (!preProcessingResponse[category] || !preProcessingResponse[category][operation]) { // eslint-disable-line max-depth | ||
continue | ||
} | ||
|
||
formattedResponse[category] ??= {} | ||
const jobStatusArr = await Promise.all( // eslint-disable-line no-await-in-loop | ||
preProcessingResponse[category][operation].map(async report => { | ||
const statusID = report?.response?.statusID | ||
if (!statusID) { | ||
throw new Error('Missing statusID in pre-processing report response') | ||
} | ||
|
||
const jobStatus = await getJobStatus(statusID) | ||
if (!jobStatus) { | ||
throw new Error(`Job status ${report.response.statusID} not found : either pending or expired`) | ||
} | ||
|
||
return jobStatus | ||
}) | ||
) | ||
|
||
formattedResponse[category][operation] = jobStatusArr.reduce((acc, jobStatus) => { | ||
const {status, count, report, updatedAt} = jobStatus | ||
|
||
const date = new Date(updatedAt) | ||
if (date > mostRecentDate) { | ||
mostRecentDate = date | ||
} | ||
|
||
if (status === 'error') { | ||
errorCount++ | ||
acc.error = { | ||
count: acc.error ? acc.error.count + count : count, | ||
report: [...acc.error.report, report], | ||
} | ||
} | ||
|
||
if (status === 'success') { | ||
acc.success = { | ||
count: acc.success ? acc.success.count + count : count, | ||
} | ||
} | ||
|
||
return acc | ||
}, { | ||
success: { | ||
count: 0, | ||
}, | ||
error: { | ||
count: 0, | ||
report: [] | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
const processingReport = { | ||
statusKey: errorCount ? 1 : 0, | ||
status: errorCount ? 'error' : 'success', | ||
message: errorCount ? 'Processed with errors' : 'Processed successfully', | ||
response: formattedResponse, | ||
date: mostRecentDate | ||
} | ||
|
||
// Update the report with the processing report | ||
await mongo.db.collection('processing_reports').updateOne({_id}, {$set: {...processingReport}}) | ||
|
||
return { | ||
...reportRest, | ||
...processingReport | ||
} | ||
} catch (error) { | ||
const message = `Could not process the report : ${error.message}` | ||
console.log(message) | ||
const processingReport = { | ||
statusKey: -1, | ||
status: 'error', | ||
message, | ||
response: {}, | ||
date: new Date() | ||
} | ||
|
||
return { | ||
...reportRest, | ||
...processingReport | ||
} | ||
} | ||
} | ||
|
||
return reportRest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters