Skip to content

Commit

Permalink
Merge pull request #478 from Duke-MatSci/#472_Enhancements
Browse files Browse the repository at this point in the history
Refactoring ECS
  • Loading branch information
tholulomo authored Feb 19, 2024
2 parents 2161eef + a4c4542 commit bb999bf
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 34 deletions.
96 changes: 76 additions & 20 deletions resfulservice/src/controllers/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ const _loadBulkElasticSearch = async (req, res, next) => {
const data = body?.data;

if (!type || !data.length) {
return next(errorWriter(req, 'Category type or doc array is missing', '_loadBulkElasticSearch', 422));
return next(
errorWriter(
req,
'Category type or doc array is missing',
'_loadBulkElasticSearch',
422
)
);
}

try {
Expand All @@ -71,10 +78,12 @@ const _loadBulkElasticSearch = async (req, res, next) => {
/** Users will always pass limit & offset, if submitting multi bulk entries.
* If it's missing in request header delete existing docs
* Only clear if it is a one time bulk entry.
*/
*/
if ((!req.query.offset || +req.query.offset === 0) && !!total) {
await elasticSearch.deleteIndexDocs(type);
log.info(`_loadBulkElasticSearch(): Successfully deleted ${type} indices`);
await elasticSearch.deleteIndexDocs(req, type);
log.info(
`_loadBulkElasticSearch(): Successfully deleted ${type} indices`
);
} else {
log.info(`_loadBulkElasticSearch(): Skipped deleting ${type} indexes`);
}
Expand All @@ -83,7 +92,9 @@ const _loadBulkElasticSearch = async (req, res, next) => {
const response = await elasticSearch.indexDocument(req, type, item);

if (!response) {
log.debug(`_loadBulkElasticSearch()::error: rejected - ${response.statusText}`);
log.debug(
`_loadBulkElasticSearch()::error: rejected - ${response.statusText}`
);
rejected = rejected + 1;
}
}
Expand Down Expand Up @@ -116,7 +127,14 @@ exports.loadElasticSearch = async (req, res, next) => {
const doc = body?.doc;

if (!type || !doc) {
return next(errorWriter(req, 'Category type or doc is missing', 'loadElasticSearch', 422));
return next(
errorWriter(
req,
'Category type or doc is missing',
'loadElasticSearch',
422
)
);
}

try {
Expand All @@ -125,11 +143,15 @@ exports.loadElasticSearch = async (req, res, next) => {
validateIsAdmin(req, res, next);

log.info(`loadElasticSearch(): deleting ${type} matching ${doc}`);
response = await elasticSearch.deleteSingleDoc(type, doc);
log.info(`loadElasticSearch(): successfully deleted ${response.deleted} doc(s)`);
response = await elasticSearch.deleteSingleDoc(req, type, doc);
log.info(
`loadElasticSearch(): successfully deleted ${response.deleted} doc(s)`
);
} else {
response = await elasticSearch.indexDocument(req, type, doc);
log.info(`loadElasticSearch(): successfully added ${JSON.stringify(doc)} doc`);
log.info(
`loadElasticSearch(): successfully added ${JSON.stringify(doc)} doc`
);
}

await elasticSearch.refreshIndices(req, type);
Expand Down Expand Up @@ -182,7 +204,7 @@ exports.dataDump = async (req, res, next) => {
try {
if (req.method === 'DELETE') {
const type = req.query.type;
await elasticSearch.deleteIndexDocs(type);
await elasticSearch.deleteIndexDocs(req, type);
return res.status(200);
}

Expand Down Expand Up @@ -245,7 +267,11 @@ exports.populateDatasetIds = async (req, res, next) => {
const datasetId = new DatasetId({ user });
const createdDataset = await datasetId.save();

successWriter(req, { message: `Successfully created DatasetId: ${createdDataset._id}` }, 'populateDatasetIds');
successWriter(
req,
{ message: `Successfully created DatasetId: ${createdDataset._id}` },
'populateDatasetIds'
);
return res.status(201).json({
id: createdDataset._id,
samples: createdDataset.samples,
Expand Down Expand Up @@ -279,13 +305,22 @@ exports.populateDatasetProperties = async (req, res, next) => {
});
if (response.status === 200) {
response = response.data.results.bindings.map(
({ Attribute, Label }) => ({ attribute: Attribute?.value, label: Label?.value })
({ Attribute, Label }) => ({
attribute: Attribute?.value,
label: Label?.value
})
);
await DatasetProperty.insertMany(response);
}
}
successWriter(req, { message: 'Successfully updated dataset properties' }, 'populateDatasetProperties');
return res.status(201).json({ message: 'Successfully updated dataset properties' });
successWriter(
req,
{ message: 'Successfully updated dataset properties' },
'populateDatasetProperties'
);
return res
.status(201)
.json({ message: 'Successfully updated dataset properties' });
} catch (err) {
next(errorWriter(req, err, 'populateDatasetProperties'));
}
Expand All @@ -304,22 +339,43 @@ exports.getDatasetProperties = async (req, res, next) => {
try {
const searchQuery = req.query?.search;
if (!searchQuery) {
return next(errorWriter(req, 'search query params is missing', 'getDatasetProperties', 400));
return next(
errorWriter(
req,
'search query params is missing',
'getDatasetProperties',
400
)
);
}
let filter = {};
const searchQueryArr = searchQuery.split(' ');
if (searchQueryArr.length >= 2) {
filter = {
$or: [
{ label: { $regex: new RegExp(`^${searchQuery}|${searchQuery}$`, 'gi') } },
{ label: { $regex: new RegExp(`^${searchQueryArr[0]}|${searchQueryArr[0]}$`, 'gi') } }
{
label: {
$regex: new RegExp(`^${searchQuery}|${searchQuery}$`, 'gi')
}
},
{
label: {
$regex: new RegExp(
`^${searchQueryArr[0]}|${searchQueryArr[0]}$`,
'gi'
)
}
}
]
};
} else {
filter = { label: { $regex: new RegExp(`^${searchQuery}|${searchQuery}$`, 'gi') } };
filter = {
label: { $regex: new RegExp(`^${searchQuery}|${searchQuery}$`, 'gi') }
};
}
const datasetProperties = await DatasetProperty.find(filter)
.select('attribute label -_id');
const datasetProperties = await DatasetProperty.find(filter).select(
'attribute label -_id'
);
return res.status(200).json({ data: datasetProperties });
} catch (err) {
next(errorWriter(req, err, 'getDatasetProperties'));
Expand Down
2 changes: 1 addition & 1 deletion resfulservice/src/controllers/curationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ exports.bulkXlsxCurations = async (req, res, next) => {

const processFolders = async (bulkCurations, bulkErrors, folder, req) => {
const { folders, masterTemplates, curationFiles } =
XlsxFileManager.readFolder(folder);
XlsxFileManager.readFolder(folder, req.logger);
await processSingleCuration(
masterTemplates,
curationFiles,
Expand Down
29 changes: 19 additions & 10 deletions resfulservice/src/controllers/kgWrapperController.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,27 @@ const _outboundRequest = async (req, next) => {
preparedRequest.withCredentials = 'true';
}
}

const response = await axios(preparedRequest);
return {
type,
data: response?.data
};
try {
const response = await axios(preparedRequest);
return {
type,
data: response?.data
};
} catch (err) {
log.error(`_outboundRequest(): ${err.status || 500} - ${err}`);
throw err;
}
};

exports.outboundRequest = async (req, next) => {
const log = req.logger;
log.info('outboundRequest(): Function entry');

return _outboundRequest(req, next);
try {
return _outboundRequest(req, next);
} catch (err) {
log.error(`outboundRequest(): ${err.status || 500} - ${err}`);
throw err;
}
};

/**
Expand Down Expand Up @@ -257,7 +265,7 @@ exports.getAllDatasets = async (req, res, next) => {
const pageSize = parseInt(req?.query?.pageSize) || 10;

try {
const response = await elasticSearch.loadAllDatasets(page, pageSize);
const response = await elasticSearch.loadAllDatasets(req, page, pageSize);
successWriter(req, { message: 'success' }, 'getAllDatasets');
return res.status(200).json({
data: response?.data?.hits?.hits || [],
Expand Down Expand Up @@ -293,7 +301,8 @@ exports.getInstanceFromKnowledgeGraph = async (req, res, next) => {
'Content-Length': response.data.length
});
res.send(response.data);
});
})
.catch((err) => next(errorWriter(req, err, 'getAllCharts')));
} catch (err) {
next(errorWriter(req, err, 'getAllCharts'));
}
Expand Down
13 changes: 10 additions & 3 deletions resfulservice/src/controllers/searchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports.explorerSearch = async (req, res, next) => {
successWriter(req, 'success', 'explorerSearch');
return res.status(201).json();
}
const response = await elasticSearch.search(request.search);
const response = await elasticSearch.search(req, request.search);
successWriter(req, 'success', 'explorerSearch');
return res.status(200).json({
data: response.data?.hits
Expand All @@ -41,7 +41,7 @@ exports.autoSuggestSearch = async (req, res, next) => {
successWriter(req, 'success', 'autoSuggestSearch');
return res.status(201).json();
}
const response = await elasticSearch.search(request.search, true);
const response = await elasticSearch.search(req, request.search, true);
successWriter(req, 'success', 'autoSuggestSearch');
return res.status(200).json({
data: response.data?.hits
Expand All @@ -66,7 +66,14 @@ exports.typeSearch = async (req, res, next) => {
successWriter(req, 'success', 'explorerSearch');
return res.status(201).json();
}
const response = await elasticSearch.searchType(search, field, type, page, pageSize);
const response = await elasticSearch.searchType(
req,
search,
field,
type,
page,
pageSize
);
successWriter(req, 'success', 'explorerSearch');
return res.status(200).json({
data: response.data?.hits
Expand Down

0 comments on commit bb999bf

Please sign in to comment.