Skip to content

Commit

Permalink
feat: global subscription, event for creating
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinlinlee committed May 30, 2023
1 parent f819367 commit c985d94
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 8 deletions.
74 changes: 71 additions & 3 deletions api/dicom-web/controller/UPS-RS/service/base-workItem.service.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const _ = require("lodash");
const { WorkItemEvent } = require("./workItem-event");
const { DicomJsonModel } = require("@models/DICOM/dicom-json-model");
const subscriptionModel = require("@models/mongodb/models/upsSubscription");
const { findWsArrayByAeTitle } = require("@root/websocket");
const { SUBSCRIPTION_STATE } = require("@models/DICOM/ups");


const { convertRequestQueryToMongoQuery } = require("../../QIDO-RS/service/QIDO-RS.service");
const globalSubscriptionModel = require("@models/mongodb/models/upsGlobalSubscription");
const subscriptionModel = require("@models/mongodb/models/upsSubscription");
const workItemModel = require("@models/mongodb/models/workItems");
const { dictionary } = require("@models/DICOM/dicom-tags-dic");
class BaseWorkItemService {

constructor(req, res) {
Expand Down Expand Up @@ -45,6 +47,7 @@ class BaseWorkItemService {
}

/**
* Use for getting event information
* @param {DicomJsonModel} workItem
*/
stateReportOf(workItem) {
Expand Down Expand Up @@ -85,6 +88,71 @@ class BaseWorkItemService {
return subscription.subscribed === SUBSCRIPTION_STATE.SUBSCRIBED_LOCK ||
subscription.subscribed === SUBSCRIPTION_STATE.SUBSCRIBED_NO_LOCK;
}

async getGlobalSubscriptionsCursor() {
return globalSubscriptionModel.find({}).cursor();
}

/**
* @param {DicomJsonModel} workItem
*/
async getHitGlobalSubscriptions(workItem) {
let globalSubscriptionsCursor = await this.getGlobalSubscriptionsCursor();
let hitGlobalSubscriptions = [];
let globalSubscription = await globalSubscriptionsCursor.next();
while(globalSubscription) {
if (!globalSubscription.queryKeys) {
hitGlobalSubscriptions.push(globalSubscription);
} else {
let { $match } = await convertRequestQueryToMongoQuery(globalSubscription.queryKeys);
$match.$and.push({
upsInstanceUID: workItem.dicomJson.upsInstanceUID
});
let count = await workItemModel.countDocuments({
...$match
});
if (count > 0)
hitGlobalSubscriptions.push(globalSubscription);
}
globalSubscription = await globalSubscriptionsCursor.next();
}
return hitGlobalSubscriptions;
}

async getHitSubscriptions(workItem) {
let hitSubscriptions = await subscriptionModel.find({
workItems: workItem.dicomJson._id
});

return hitSubscriptions;
}

async getAssignedEventInformationArray(workItem, stationNameUpdated, performerUpdated) {
if (!performerUpdated) {
let scheduledStationCodeSeq = _.get(workItem.dicomJson, `${dictionary.keyword.ScheduledStationNameCodeSequence}.Value`);
return stationNameUpdated ? scheduledStationCodeSeq : [];
}
/** @type {Array<any>} */
let scheduledHumanPerformersSeq = _.get(workItem.dicomJson, `${dictionary.keyword.ScheduledHumanPerformersSequence}.Value`);
let assignedEventInfo = scheduledHumanPerformersSeq.map(item => this.getAssignedOf(workItem, stationNameUpdated, item));
return assignedEventInfo;
}

/**
*
* @param {*} workItem
* @param {boolean} stationNameUpdated
* @param {*} seqItem
*/
getAssignedOf(workItem, stationNameUpdated, seqItem) {
let eventInformation = {};
_.set(eventInformation, `${dictionary.keyword.HumanPerformerCodeSequence}`, _.get(seqItem, `${dictionary.keyword.HumanPerformerCodeSequence}`));
_.set(eventInformation, `${dictionary.keyword.HumanPerformerOrganization}`, _.get(seqItem, `${dictionary.keyword.HumanPerformerOrganization}`));
if (stationNameUpdated) {
_.set(eventInformation, `${dictionary.keyword.ScheduledStationNameCodeSequence}`, _.get(workItem.dicomJson, `${dictionary.keyword.ScheduledStationNameCodeSequence}`));
}
return eventInformation;
}
}

module.exports.BaseWorkItemService = BaseWorkItemService;
40 changes: 35 additions & 5 deletions api/dicom-web/controller/UPS-RS/service/create-workItem.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const {
DicomWebStatusCodes
} = require("@error/dicom-web-service");
const { DicomJsonModel } = require("@models/DICOM/dicom-json-model");
const { BaseWorkItemService } = require("./base-workItem.service");
const { SubscribeService } = require("./subscribe.service");
const { UPS_EVENT_TYPE } = require("./workItem-event");
const { dictionary } = require("@models/DICOM/dicom-tags-dic");

class CreateWorkItemService {
class CreateWorkItemService extends BaseWorkItemService {
constructor(req, res) {
this.request = req;
this.response = res;
super(req, res);
this.requestWorkItem = /** @type {Object[]} */(this.request.body).pop();
/** @type {DicomJsonModel} */
this.requestWorkItem = new DicomJsonModel(this.requestWorkItem);
Expand Down Expand Up @@ -57,10 +60,37 @@ class CreateWorkItemService {
400
);
}
await workItem.save();
let savedWorkItem = await workItem.save();


//TODO: subscription
let workItemDicomJson = new DicomJsonModel(savedWorkItem);
let hitGlobalSubscriptions = await this.getHitGlobalSubscriptions(workItemDicomJson);
for(let hitGlobalSubscription of hitGlobalSubscriptions) {
let subscribeService = new SubscribeService(this.request, this.response);
subscribeService.upsInstanceUID = workItemDicomJson.dicomJson.upsInstanceUID;
subscribeService.deletionLock = hitGlobalSubscription.isDeletionLock;
subscribeService.subscriberAeTitle = hitGlobalSubscription.aeTitle;
await subscribeService.create();
}

let hitSubscriptions = await this.getHitSubscriptions(workItemDicomJson);

if (hitSubscriptions) {
let hitSubscriptionAeTitleArray = hitSubscriptions.map(sub => sub.aeTitle);
this.addUpsEvent(UPS_EVENT_TYPE.StateReport, workItemDicomJson.dicomJson.upsInstanceUID, this.stateReportOf(workItemDicomJson), hitSubscriptionAeTitleArray);
let assignedEventInformationArray = await this.getAssignedEventInformationArray(
workItemDicomJson,
_.get(workItemDicomJson.dicomJson, `${dictionary.keyword.ScheduledStationNameCodeSequence}`, false),
_.get(workItemDicomJson.dicomJson, `${dictionary.keyword.ScheduledHumanPerformersSequence}`, false)
);

for(let assignedEventInfo of assignedEventInformationArray) {
this.addUpsEvent(UPS_EVENT_TYPE.Assigned, workItemDicomJson.dicomJson.upsInstanceUID, assignedEventInfo, hitSubscriptionAeTitleArray);
}
}

this.triggerUpsEvents();

return workItem;
}

Expand Down

0 comments on commit c985d94

Please sign in to comment.