Skip to content

Commit

Permalink
refactor: move getInstanceFrameObj to InstanceModel
Browse files Browse the repository at this point in the history
- This function belongs to InstanceModel and is more reasonable.
  • Loading branch information
Chinlinlee committed Jan 8, 2024
1 parent 7e275f2 commit a389efa
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 61 deletions.
65 changes: 5 additions & 60 deletions api/dicom-web/controller/WADO-RS/service/rendered.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require("path");
const mongoose = require("mongoose");
const { InstanceModel } = require("@dbModels/instance.model");
const fs = require("fs");
const sharp = require("sharp");
const _ = require("lodash");
Expand Down Expand Up @@ -155,7 +156,7 @@ class FramesWriter {
async write() {
let multipartWriter = new MultipartWriter([], this.request, this.response);
for(let imagePathObj of this.imagePaths) {
let instanceFramesObj = await getInstanceFrameObj(imagePathObj);
let instanceFramesObj = await InstanceModel.getInstanceFrame(imagePathObj);
if(_.isUndefined(instanceFramesObj)) continue;
let dicomNumberOfFrames = _.get(instanceFramesObj, "00280008.Value.0", 1);
dicomNumberOfFrames = parseInt(dicomNumberOfFrames);
Expand Down Expand Up @@ -188,7 +189,7 @@ class InstanceFramesWriter extends FramesWriter {

async write() {
let multipartWriter = new MultipartWriter([], this.request, this.response);
let instanceFramesObj = await getInstanceFrameObj(this.imagePaths[0]);
let instanceFramesObj = await InstanceModel.getInstanceFrame(this.imagePaths[0]);
if (_.isUndefined(instanceFramesObj)) {
return this.response.status(400).json(
errorResponse.getBadRequestErrorMessage(`instance: ${this.request.params.instanceUID} doesn't have pixel data`)
Expand All @@ -211,7 +212,7 @@ class InstanceFramesListWriter extends FramesWriter {
async write() {
let {frameNumber} = this.request.params;

this.instanceFramesObj = await getInstanceFrameObj(this.imagePaths[0]);
this.instanceFramesObj = await InstanceModel.getInstanceFrame(this.imagePaths[0]);
if (_.isUndefined(this.instanceFramesObj)) {
return this.response.status(400).json(
errorResponse.getBadRequestErrorMessage(`instance: ${this.request.params.instanceUID} doesn't have pixel data`)
Expand Down Expand Up @@ -265,62 +266,6 @@ This instance NumberOfFrames is : ${this.dicomNumberOfFrames} , But request ${JS
}
}

/**
*
* @param {Object} iParam
* @return { Promise<import("../../../../../utils/typeDef/WADO-RS/WADO-RS.def").InstanceFrameObj> | Promise<undefined> }
*/
async function getInstanceFrameObj(iParam) {
let { studyUID, seriesUID, instanceUID } = iParam;
try {
/** @type { import("mongoose").FilterQuery<any> } */
let query = {
$and: [
{
studyUID: studyUID
},
{
seriesUID: seriesUID
},
{
instanceUID: instanceUID
},
{
"00080016.Value": {
$nin: notImageSOPClass
}
},
{
deleteStatus: {
$eq: 0
}
}
]
};
let doc = await mongoose.model("dicom").findOne(query, {
studyUID: 1,
seriesUID: 1,
instanceUID: 1,
instancePath: 1,
"00280008": 1, //number of frames
"00020010": 1, //transfer syntax UID
"00281050": 1, // Window Center
"00281051": 1 // Window Width
}).exec();
if (doc) {
let docObj = doc.toObject();
docObj.instancePath = path.join(
raccoonConfig.dicomWebConfig.storeRootPath,
docObj.instancePath
);
return docObj;
}
return undefined;
} catch (e) {
throw e;
}
}

/**
*
* @param {import("http").IncomingMessage} req
Expand Down Expand Up @@ -411,7 +356,7 @@ async function writeRenderedImages(req, dicomNumberOfFrames, instanceFramesObj,
module.exports.handleImageQuality = handleImageQuality;
module.exports.handleImageICCProfile = handleImageICCProfile;
module.exports.handleViewport = handleViewport;
module.exports.getInstanceFrameObj = getInstanceFrameObj;
module.exports.getInstanceFrameObj = InstanceModel.getInstanceFrame;
module.exports.postProcessFrameImage = postProcessFrameImage;
module.exports.writeRenderedImages = writeRenderedImages;
module.exports.RenderedImageMultipartWriter = RenderedImageMultipartWriter;
Expand Down
62 changes: 61 additions & 1 deletion models/mongodb/models/instance.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { logger } = require("../../../utils/logs/log");
const { raccoonConfig } = require("@root/config-class");
const { dictionary } = require("@models/DICOM/dicom-tags-dic");
const { DicomSchemaOptionsFactory, InstanceDocDicomJsonHandler } = require("../schema/dicom.schema");
const notImageSOPClass = require("@models/DICOM/dicomWEB/notImageSOPClass");


let verifyingObserverSchema = new mongoose.Schema(
Expand Down Expand Up @@ -149,7 +150,7 @@ let dicomSchemaOptions = _.merge(
}
]
});

return await mongoose.model("dicom").findOne(query, {
studyUID: 1,
seriesUID: 1,
Expand All @@ -163,6 +164,65 @@ let dicomSchemaOptions = _.merge(
.skip(instanceCountOfStudy >> 1)
.limit(1)
.exec();
},
/**
*
* @param {object} iParam
* @param {string} iParam.studyUID
* @param {string} iParam.seriesUID
* @param {string} iParam.instanceUID
* @returns { Promise<import("@root/utils/typeDef/WADO-RS/WADO-RS.def").InstanceFrameObj> | Promise<undefined> }
*/
getInstanceFrame: async function (iParam) {
let { studyUID, seriesUID, instanceUID } = iParam;

try {
/** @type { import("mongoose").FilterQuery<any> } */
let query = {
$and: [
{
studyUID: studyUID
},
{
seriesUID: seriesUID
},
{
instanceUID: instanceUID
},
{
"00080016.Value": {
$nin: notImageSOPClass
}
},
{
deleteStatus: {
$eq: 0
}
}
]
};
let doc = await mongoose.model("dicom").findOne(query, {
studyUID: 1,
seriesUID: 1,
instanceUID: 1,
instancePath: 1,
"00280008": 1, //number of frames
"00020010": 1, //transfer syntax UID
"00281050": 1, // Window Center
"00281051": 1 // Window Width
}).exec();
if (doc) {
let docObj = doc.toObject();
docObj.instancePath = path.join(
raccoonConfig.dicomWebConfig.storeRootPath,
docObj.instancePath
);
return docObj;
}
return undefined;
} catch (e) {
throw e;
}
}
}
}
Expand Down

0 comments on commit a389efa

Please sign in to comment.