Skip to content

Commit

Permalink
feat: change method of query sutdy
Browse files Browse the repository at this point in the history
- Change `dicomJson.${tag}` to `${tag}`
> The tag will in ROOT path
- Change, query attributes of study level from `dicomStudy` collection
  • Loading branch information
Chinlinlee committed May 1, 2022
1 parent e7684c1 commit 9e3c0fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 78 deletions.
102 changes: 26 additions & 76 deletions api/dicom-web/controller/QIDO-RS/queryAllStudies.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');
const mongoose = require('mongoose');
const moment = require('moment');
const {
const {
convertAllQueryToDICOMTag,
convertRequestQueryToMongoQuery,
getStudyLevelFields
Expand Down Expand Up @@ -36,113 +36,63 @@ const {
*/
module.exports = async function (req, res) {
try {
let limit = req.query.limit || 100 ;
let limit = req.query.limit || 100;
let skip = req.query.offset || 0;
delete req.query["limit"];
delete req.query["offset"];
let query = _.cloneDeep(req.query);
let queryKeys = Object.keys(query).sort();
for ( let i = 0 ; i < queryKeys.length ; i++) {
for (let i = 0; i < queryKeys.length; i++) {
let queryKey = queryKeys[i];
if (!query[queryKey]) delete query[queryKey];
}

let dicomTagQuery = convertAllQueryToDICOMTag(query);
let studiesJson = await getStudyDicomJson(dicomTagQuery, limit, skip);
let studiesJson = await getStudyDicomJson(dicomTagQuery, limit, skip, req);
res.writeHead(200, {
"Content-Type": "application/dicom+json"
});
res.end(JSON.stringify(studiesJson.data));
} catch(e) {
} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
logger.error(`[QIDO-RS] [Error: ${errorStr}]`);
}
}

async function getStudyDicomJson(iQuery , limit , skip) {
async function getStudyDicomJson(iQuery, limit, skip, req) {
logger.info(`[QIDO-RS] [Query Study Level]`);
let result = {
data : '' ,
data: '',
status: false
}
let protocol = req.secure ? "https" : "http";
let retrieveUrl = `${protocol}://${req.headers.host}/${process.env.DICOMWEB_API}/studies`;
try {
iQuery = await convertRequestQueryToMongoQuery(iQuery);
// iQuery = iQuery.$match;
logger.info(`[QIDO-RS] [Query for MongoDB: ${JSON.stringify(iQuery)}]`);
let studyFields = getStudyLevelFields();
let aggregateQuery = [
{
$sort: {
studyUID: 1
}
},
iQuery,
{
$limit: limit + skip
},
{
$skip: skip
},
{
$group: {
_id: "$0020000D",
modalitiesInStudy: {
$addToSet: "$00080060.Value"
},
dicomJson: {
$addToSet: "$$ROOT"
}
}
},
{
$project: {
...studyFields,
"dicomJson.00080061": {
"vr": "CS",
"Value": {
$reduce: {
input: "$modalitiesInStudy",
initialValue: [],
in: {
$concatArrays : ["$$value", "$$this"]
}
}
}
}
}
},
{
$project: {
dicomJsonObj: {
$mergeObjects: "$$ROOT.dicomJson"
}
}
},
{
$replaceWith: "$dicomJsonObj"
},
{
$sort: {
"0020000D": 1
}
}
];
let docs = await mongoose.model("dicom").aggregate(aggregateQuery).exec();
let docs = await mongoose.model("dicomStudy")
.find(iQuery.$match, studyFields)
.limit(limit)
.skip(skip)
.exec();
result.data = docs.map(v => {
let studyDate = _.get(v , "00080020.Value");
if (studyDate) {
for (let j in studyDate) {
let studyDateYYYYMMDD = moment(studyDate[j]).format("YYYYMMDD").toString();
studyDate[j] = studyDateYYYYMMDD;
}
_.set(v , "00080020.Value" , studyDate);
}
return v;
let obj = v.toObject();
delete obj._id;
delete obj.id;
obj["00801190"] = {
vr: "UR",
Value: [
`${retrieveUrl}/${obj["0020000D"]["Value"][0]}`
]
};
return obj;
});
result.status = true;
return result;
} catch (e) {
console.error("get Study DICOM error" , e);
console.error("get Study DICOM error", e);
result.data = e;
result.status = false;
return result;
Expand Down
13 changes: 11 additions & 2 deletions api/dicom-web/controller/QIDO-RS/service/QIDO-RS.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,15 @@ async function convertRequestQueryToMongoQuery(iQuery) {
function getStudyLevelFields() {
let fields = {};
for (let tag in tagsOfRequiredMatching.Study) {
fields[`dicomJson.${tag}`] = 1;
fields[tag] = 1;
}
return fields;
}

function getSeriesLevelFields() {
let fields = {};
for (let tag in tagsOfRequiredMatching.Series) {
fields[tag] = 1;
}
return fields;
}
Expand All @@ -159,4 +167,5 @@ const vrQueryLookup = {

module.exports.convertAllQueryToDICOMTag = convertAllQueryToDICOMTag;
module.exports.convertRequestQueryToMongoQuery = convertRequestQueryToMongoQuery;
module.exports.getStudyLevelFields = getStudyLevelFields;
module.exports.getStudyLevelFields = getStudyLevelFields;
module.exports.getSeriesLevelFields = getSeriesLevelFields;

0 comments on commit 9e3c0fd

Please sign in to comment.