Skip to content

Commit

Permalink
Merge pull request #899 from HDRUK/hotfix/GAT-1932-loki
Browse files Browse the repository at this point in the history
GAT-1932 fixes api crash due to excessive data loading
  • Loading branch information
reubensamuel authored Jan 24, 2023
2 parents b17baf7 + c5a3975 commit 7fe459e
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions src/resources/dataUseRegister/dataUseRegister.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,25 @@ export default class DataUseRegisterController extends Controller {
as: 'publisherDetails',
},
},
{
$lookup: {
from: 'tools',
localField: 'gatewayOutputsTools',
foreignField: 'id',
as: 'gatewayOutputsToolsInfo',
},
},
{
$lookup: {
from: 'tools',
localField: 'gatewayOutputsPapers',
foreignField: 'id',
as: 'gatewayOutputsPapersInfo',
},
},
// Removed for now, see comments on:
// https://hdruk.atlassian.net/browse/GAT-1932
//
// {
// $lookup: {
// from: 'tools',
// localField: 'gatewayOutputsTools',
// foreignField: 'id',
// as: 'gatewayOutputsToolsInfo',
// },
// },
// {
// $lookup: {
// from: 'tools',
// localField: 'gatewayOutputsPapers',
// foreignField: 'id',
// as: 'gatewayOutputsPapersInfo',
// },
// },
{
$lookup: {
from: 'users',
Expand Down Expand Up @@ -350,9 +353,28 @@ export default class DataUseRegisterController extends Controller {
aggregateQuery.unshift({ $match: { $text: { $search: searchString } } });
}

const result = await DataUseRegister.aggregate(aggregateQuery);
const results = await DataUseRegister.aggregate(aggregateQuery);
let newPayload = [];

// Due to the excessive size of laySummary and publicBenefitStatement
// we have to truncate the content to avoid running out of memory
//
// TODO - Needs refactoring on the whole
results.forEach(result => {
if (result.laySummary) {
result.laySummary = result.laySummary.substring(0, 200);
result.laySummary += '...';
}

return res.status(200).json({ success: true, result });
if (result.publicBenefitStatement) {
result.publicBenefitStatement = result.publicBenefitStatement.substring(0, 200)
result.publicBenefitStatement += '...';
}

newPayload.push(result);
});

return res.status(200).json({ success: true, newPayload });
} catch (err) {
//Return error response if something goes wrong
logger.logError(err, logCategory);
Expand Down

0 comments on commit 7fe459e

Please sign in to comment.