<commit_before>import { Meteor } from 'meteor/meteor';
import { $ } from 'meteor/jquery';

import { OHIF } from 'meteor/ohif:core';

class MeasurementHandlers {

    static onAdded(e, instance, eventData) {
        const measurementData = eventData.measurementData;

        const config = OHIF.measurements.MeasurementApi.getConfiguration();
        const toolTypes = config.measurementTools.map(tool => tool.cornerstoneToolType);

        const index = toolTypes.indexOf(eventData.toolType);
        if (index === -1) {
            return;
        }

        const measurementToolConfiguration = config.measurementTools[index];
        const measurementApi = instance.data.measurementApi;
        const Collection = measurementApi[measurementToolConfiguration.id];

        // Get the Cornerstone imageId
        const enabledElement = cornerstone.getEnabledElement(eventData.element);
        const imageId = enabledElement.image.imageId;

        // Get studyInstanceUid & patientId
        const study = cornerstoneTools.metaData.get('study', imageId);
        const studyInstanceUid = study.studyInstanceUid;
        const patientId = study.patientId;

        // Get seriesInstanceUid
        const series = cornerstoneTools.metaData.get('series', imageId);
        const seriesInstanceUid = series.seriesInstanceUid;

        // Get sopInstanceUid
        const sopInstance = cornerstoneTools.metaData.get('instance', imageId);
        const sopInstanceUid = sopInstance.sopInstanceUid;
        const frameIndex = sopInstance.frame || 0;

        OHIF.log.info('CornerstoneToolsMeasurementAdded');

        let measurement = $.extend({
            userId: Meteor.userId(),
            patientId: patientId,
            studyInstanceUid: studyInstanceUid,
            seriesInstanceUid: seriesInstanceUid,
            sopInstanceUid: sopInstanceUid,
            frameIndex: frameIndex,
            imageId: imageId // TODO: In the future we should consider removing this
        }, measurementData);

        const timepointApi = instance.data.timepointApi;
        if (timepointApi) {
            const timepoint = timepointApi.study(studyInstanceUid)[0];
            if (timepoint) {
                const timepointId = timepoint.timepointId;
                measurement.timepointId = timepointId;
                measurement.measurementNumber = OHIF.measurements.MeasurementManager.getNewMeasurementNumber(timepointId, Collection, timepointApi);

                // TODO: Fix this
                measurement.measurementNumberAbsolute = measurement.measurementNumber;
            }
        } else {
            const numCurrentMeasurementsInStudy = Collection.find({
                studyInstanceUid: study.studyInstanceUid
            }).count();
            measurement.measurementNumber = numCurrentMeasurementsInStudy + 1;
        }

        // Clean the measurement according to the Schema
        measurementToolConfiguration.schema.clean(measurement);

        // Insert the new measurement into the collection
        measurementData.id = Collection.insert(measurement);
    }

    static onModified(e, instance, eventData) {
        const measurementData = eventData.measurementData;

        const config = OHIF.measurements.MeasurementApi.getConfiguration();
        const toolTypes = config.measurementTools.map(tool => tool.cornerstoneToolType);

        const index = toolTypes.indexOf(eventData.toolType);
        if (index === -1) {
            return;
        }

        const measurementToolConfiguration = config.measurementTools[index];
        const measurementApi = instance.data.measurementApi;
        const Collection = measurementApi[measurementToolConfiguration.id];

        OHIF.log.info('CornerstoneToolsMeasurementModified');

        let measurement = Collection.findOne(measurementData.id);

        Object.keys(measurementData).forEach(key => {
            measurement[key] = measurementData[key];
        });

        const measurementId = measurement._id;
        delete measurement._id;

        // Clean the measurement according to the Schema
        measurementToolConfiguration.schema.clean(measurement);

        // Insert the new measurement into the collection
        Collection.update(measurementId, {
            $set: measurement
        });
    }

    static onRemoved(e, instance, eventData) {
        const measurementData = eventData.measurementData;

        const config = OHIF.measurements.MeasurementApi.getConfiguration();
        const toolTypes = config.measurementTools.map(tool => tool.cornerstoneToolType);

        const index = toolTypes.indexOf(measurementData.toolType);
        if (index === -1) {
            return;
        }

        OHIF.log.info('CornerstoneToolsMeasurementRemoved');

        const measurementToolConfiguration = config.measurementTools[index];
        const measurementApi = instance.data.measurementApi;
        const Collection = measurementApi[measurementToolConfiguration.id];

        Collection.remove(measurementData.id);
    }

}

OHIF.measurements.MeasurementHandlers = MeasurementHandlers;<commit_msg>LT-275: Fix tooldata / measurement collection syncing, but there are weird performance issues<commit_after>import { Meteor } from 'meteor/meteor';
import { $ } from 'meteor/jquery';

import { OHIF } from 'meteor/ohif:core';

class MeasurementHandlers {

    static onAdded(e, instance, eventData) {
        const measurementData = eventData.measurementData;

        const config = OHIF.measurements.MeasurementApi.getConfiguration();
        const toolTypes = config.measurementTools.map(tool => tool.cornerstoneToolType);

        const index = toolTypes.indexOf(eventData.toolType);
        if (index === -1) {
            return;
        }

        const measurementToolConfiguration = config.measurementTools[index];
        const measurementApi = instance.data.measurementApi;
        const Collection = measurementApi[measurementToolConfiguration.id];

        // Get the Cornerstone imageId
        const enabledElement = cornerstone.getEnabledElement(eventData.element);
        const imageId = enabledElement.image.imageId;

        // Get studyInstanceUid & patientId
        const study = cornerstoneTools.metaData.get('study', imageId);
        const studyInstanceUid = study.studyInstanceUid;
        const patientId = study.patientId;

        // Get seriesInstanceUid
        const series = cornerstoneTools.metaData.get('series', imageId);
        const seriesInstanceUid = series.seriesInstanceUid;

        // Get sopInstanceUid
        const sopInstance = cornerstoneTools.metaData.get('instance', imageId);
        const sopInstanceUid = sopInstance.sopInstanceUid;
        const frameIndex = sopInstance.frame || 0;

        OHIF.log.info('CornerstoneToolsMeasurementAdded');

        let measurement = $.extend({
            userId: Meteor.userId(),
            patientId: patientId,
            studyInstanceUid: studyInstanceUid,
            seriesInstanceUid: seriesInstanceUid,
            sopInstanceUid: sopInstanceUid,
            frameIndex: frameIndex,
            imageId: imageId // TODO: In the future we should consider removing this
        }, measurementData);

        const timepointApi = instance.data.timepointApi;
        if (timepointApi) {
            const timepoint = timepointApi.study(studyInstanceUid)[0];
            if (timepoint) {
                const timepointId = timepoint.timepointId;
                measurement.timepointId = timepointId;
                measurement.measurementNumber = OHIF.measurements.MeasurementManager.getNewMeasurementNumber(timepointId, Collection, timepointApi);

                // TODO: Fix this
                measurement.measurementNumberAbsolute = measurement.measurementNumber;
            }
        } else {
            const numCurrentMeasurementsInStudy = Collection.find({
                studyInstanceUid: study.studyInstanceUid
            }).count();
            measurement.measurementNumber = numCurrentMeasurementsInStudy + 1;
        }

        // Clean the measurement according to the Schema
        measurementToolConfiguration.schema.clean(measurement);

        // Insert the new measurement into the collection
        measurementData._id = Collection.insert(measurement);
    }

    static onModified(e, instance, eventData) {
        const measurementData = eventData.measurementData;

        const config = OHIF.measurements.MeasurementApi.getConfiguration();
        const toolTypes = config.measurementTools.map(tool => tool.cornerstoneToolType);

        const index = toolTypes.indexOf(eventData.toolType);
        if (index === -1) {
            return;
        }

        const measurementToolConfiguration = config.measurementTools[index];
        const measurementApi = instance.data.measurementApi;
        const Collection = measurementApi[measurementToolConfiguration.id];

        OHIF.log.info('CornerstoneToolsMeasurementModified');

        let measurement = Collection.findOne(measurementData._id);

        Object.keys(measurementData).forEach(key => {
            measurement[key] = measurementData[key];
        });

        const measurementId = measurement._id;
        delete measurement._id;

        // Clean the measurement according to the Schema
        measurementToolConfiguration.schema.clean(measurement);

        // Insert the new measurement into the collection
        Collection.update(measurementId, {
            $set: measurement
        });
    }

    static onRemoved(e, instance, eventData) {
        const measurementData = eventData.measurementData;

        const config = OHIF.measurements.MeasurementApi.getConfiguration();
        const toolTypes = config.measurementTools.map(tool => tool.cornerstoneToolType);

        const index = toolTypes.indexOf(measurementData.toolType);
        if (index === -1) {
            return;
        }

        OHIF.log.info('CornerstoneToolsMeasurementRemoved');

        const measurementToolConfiguration = config.measurementTools[index];
        const measurementApi = instance.data.measurementApi;
        const Collection = measurementApi[measurementToolConfiguration.id];

        Collection.remove(measurementData._id);
    }

}

OHIF.measurements.MeasurementHandlers = MeasurementHandlers;
{'fraction_non_alphanumeric': 0.08363248277869409, 'fraction_numerical': 0.0014553216260793636, 'mean_word_length': 2.749727173517643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4668064', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2506, 'n_words': 532}