Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AnnotationAPI.js « annotation « api « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 148b1d3c4b429dcc852755c45c53aed6b84928e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*****************************************************************************
 * Open MCT, Copyright (c) 2014-2022, United States Government
 * as represented by the Administrator of the National Aeronautics and Space
 * Administration. All rights reserved.
 *
 * Open MCT is licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 * Open MCT includes source code licensed under additional open source
 * licenses. See the Open Source Licenses file (LICENSES.md) included with
 * this source code distribution or the Licensing information page available
 * at runtime from the About dialog for additional information.
 *****************************************************************************/

import { v4 as uuid } from 'uuid';
import EventEmitter from 'EventEmitter';
import _ from 'lodash';

/**
 * @readonly
 * @enum {String} AnnotationType
 * @property {String} NOTEBOOK The notebook annotation type
 * @property {String} GEOSPATIAL The geospatial annotation type
 * @property {String} PIXEL_SPATIAL The pixel-spatial annotation type
 * @property {String} TEMPORAL The temporal annotation type
 * @property {String} PLOT_SPATIAL The plot-spatial annotation type
 */
const ANNOTATION_TYPES = Object.freeze({
    NOTEBOOK: 'NOTEBOOK',
    GEOSPATIAL: 'GEOSPATIAL',
    PIXEL_SPATIAL: 'PIXEL_SPATIAL',
    TEMPORAL: 'TEMPORAL',
    PLOT_SPATIAL: 'PLOT_SPATIAL'
});

const ANNOTATION_TYPE = 'annotation';

const ANNOTATION_LAST_CREATED = 'annotationLastCreated';

/**
 * @typedef {Object} Tag
 * @property {String} key a unique identifier for the tag
 * @property {String} backgroundColor eg. "#cc0000"
 * @property {String} foregroundColor eg. "#ffffff"
 */

export default class AnnotationAPI extends EventEmitter {

    /**
     * @param {OpenMCT} openmct
     */
    constructor(openmct) {
        super();
        this.openmct = openmct;
        this.availableTags = {};

        this.ANNOTATION_TYPES = ANNOTATION_TYPES;
        this.ANNOTATION_TYPE = ANNOTATION_TYPE;
        this.ANNOTATION_LAST_CREATED = ANNOTATION_LAST_CREATED;

        this.openmct.types.addType(ANNOTATION_TYPE, {
            name: 'Annotation',
            description: 'A user created note or comment about time ranges, pixel space, and geospatial features.',
            creatable: false,
            cssClass: 'icon-notebook',
            initialize: function (domainObject) {
                domainObject.targets = domainObject.targets || {};
                domainObject._deleted = domainObject._deleted || false;
                domainObject.originalContextPath = domainObject.originalContextPath || '';
                domainObject.tags = domainObject.tags || [];
                domainObject.contentText = domainObject.contentText || '';
                domainObject.annotationType = domainObject.annotationType || 'plotspatial';
            }
        });
    }

    /**
    * Create the a generic annotation
    * @typedef {Object} CreateAnnotationOptions
    * @property {String} name a name for the new parameter
    * @property {import('../objects/ObjectAPI').DomainObject} domainObject the domain object to create
    * @property {ANNOTATION_TYPES} annotationType the type of annotation to create
    * @property {Tag[]} tags
    * @property {String} contentText
    * @property {import('../objects/ObjectAPI').Identifier[]} targets
    */
    /**
    * @method create
    * @param {CreateAnnotationOptions} options
    * @returns {Promise<import('../objects/ObjectAPI').DomainObject>} a promise which will resolve when the domain object
    *          has been created, or be rejected if it cannot be saved
    */
    async create({name, domainObject, annotationType, tags, contentText, targets}) {
        if (!Object.keys(this.ANNOTATION_TYPES).includes(annotationType)) {
            throw new Error(`Unknown annotation type: ${annotationType}`);
        }

        if (!Object.keys(targets).length) {
            throw new Error(`At least one target is required to create an annotation`);
        }

        const domainObjectKeyString = this.openmct.objects.makeKeyString(domainObject.identifier);
        const originalPathObjects = await this.openmct.objects.getOriginalPath(domainObjectKeyString);
        const originalContextPath = this.openmct.objects.getRelativePath(originalPathObjects);
        const namespace = domainObject.identifier.namespace;
        const type = 'annotation';
        const typeDefinition = this.openmct.types.get(type);
        const definition = typeDefinition.definition;

        const createdObject = {
            name,
            type,
            identifier: {
                key: uuid(),
                namespace
            },
            tags,
            _deleted: false,
            annotationType,
            contentText,
            originalContextPath
        };

        if (definition.initialize) {
            definition.initialize(createdObject);
        }

        createdObject.targets = targets;
        createdObject.originalContextPath = originalContextPath;

        const success = await this.openmct.objects.save(createdObject);
        if (success) {
            this.emit('annotationCreated', createdObject);
            this.#updateAnnotationModified(domainObject);

            return createdObject;
        } else {
            throw new Error('Failed to create object');
        }
    }

    #updateAnnotationModified(domainObject) {
        this.openmct.objects.mutate(domainObject, this.ANNOTATION_LAST_CREATED, Date.now());
    }

    /**
    * @method defineTag
    * @param {String} key a unique identifier for the tag
    * @param {Tag} tagsDefinition the definition of the tag to add
    */
    defineTag(tagKey, tagsDefinition) {
        this.availableTags[tagKey] = tagsDefinition;
    }

    /**
    * @method isAnnotation
    * @param {import('../objects/ObjectAPI').DomainObject} domainObject domainObject the domainObject in question
    * @returns {Boolean} Returns true if the domain object is an annotation
    */
    isAnnotation(domainObject) {
        return domainObject && (domainObject.type === ANNOTATION_TYPE);
    }

    /**
    * @method getAvailableTags
    * @returns {Tag[]} Returns an array of the available tags that have been loaded
    */
    getAvailableTags() {
        if (this.availableTags) {
            const rearrangedToArray = Object.keys(this.availableTags).map(tagKey => {
                return {
                    id: tagKey,
                    ...this.availableTags[tagKey]
                };
            });

            return rearrangedToArray;
        } else {
            return [];
        }
    }

    /**
    * @method getAnnotations
    * @param {String} query - The keystring of the domain object to search for annotations for
    * @returns {import('../objects/ObjectAPI').DomainObject[]} Returns an array of domain objects that match the search query
    */
    async getAnnotations(query) {
        const searchResults = (await Promise.all(this.openmct.objects.search(query, null, this.openmct.objects.SEARCH_TYPES.ANNOTATIONS))).flat();

        return searchResults;
    }

    /**
    * @method addSingleAnnotationTag
    * @param {import('../objects/ObjectAPI').DomainObject=} existingAnnotation - An optional annotation to add the tag to. If not specified, we will create an annotation.
    * @param {import('../objects/ObjectAPI').DomainObject} targetDomainObject - The domain object the annotation will point to.
    * @param {Object=} targetSpecificDetails - Optional object to add to the target object. E.g., for notebooks this would be an entryID
    * @param {AnnotationType} annotationType - The type of annotation this is for.
    * @returns {import('../objects/ObjectAPI').DomainObject[]} Returns the annotation that was either created or passed as an existingAnnotation
    */
    async addSingleAnnotationTag(existingAnnotation, targetDomainObject, targetSpecificDetails, annotationType, tag) {
        if (!existingAnnotation) {
            const targets = {};
            const targetKeyString = this.openmct.objects.makeKeyString(targetDomainObject.identifier);
            targets[targetKeyString] = targetSpecificDetails;
            const contentText = `${annotationType} tag`;
            const annotationCreationArguments = {
                name: contentText,
                domainObject: targetDomainObject,
                annotationType,
                tags: [tag],
                contentText,
                targets
            };
            const newAnnotation = await this.create(annotationCreationArguments);

            return newAnnotation;
        } else {
            if (!existingAnnotation.tags.includes(tag)) {
                throw new Error(`Existing annotation did not contain tag ${tag}`);
            }

            if (existingAnnotation._deleted) {
                this.unDeleteAnnotation(existingAnnotation);
            }

            return existingAnnotation;
        }
    }

    /**
    * @method deleteAnnotations
    * @param {import('../objects/ObjectAPI').DomainObject[]} existingAnnotation - An array of annotations to delete (set _deleted to true)
    */
    deleteAnnotations(annotations) {
        if (!annotations) {
            throw new Error('Asked to delete null annotations! 🙅‍♂️');
        }

        annotations.forEach(annotation => {
            if (!annotation._deleted) {
                this.openmct.objects.mutate(annotation, '_deleted', true);
            }
        });
    }

    /**
    * @method deleteAnnotations
    * @param {import('../objects/ObjectAPI').DomainObject} existingAnnotation - An annotations to undelete (set _deleted to false)
    */
    unDeleteAnnotation(annotation) {
        if (!annotation) {
            throw new Error('Asked to undelete null annotation! 🙅‍♂️');
        }

        this.openmct.objects.mutate(annotation, '_deleted', false);
    }

    #getMatchingTags(query) {
        if (!query) {
            return [];
        }

        const matchingTags = Object.keys(this.availableTags).filter(tagKey => {
            if (this.availableTags[tagKey] && this.availableTags[tagKey].label) {
                return this.availableTags[tagKey].label.toLowerCase().includes(query.toLowerCase());
            }

            return false;
        });

        return matchingTags;
    }

    #addTagMetaInformationToResults(results, matchingTagKeys) {
        const tagsAddedToResults = results.map(result => {
            const fullTagModels = result.tags.map(tagKey => {
                const tagModel = this.availableTags[tagKey];
                tagModel.tagID = tagKey;

                return tagModel;
            });

            return {
                fullTagModels,
                matchingTagKeys,
                ...result
            };
        });

        return tagsAddedToResults;
    }

    async #addTargetModelsToResults(results) {
        const modelAddedToResults = await Promise.all(results.map(async result => {
            const targetModels = await Promise.all(Object.keys(result.targets).map(async (targetID) => {
                const targetModel = await this.openmct.objects.get(targetID);
                const targetKeyString = this.openmct.objects.makeKeyString(targetModel.identifier);
                const originalPathObjects = await this.openmct.objects.getOriginalPath(targetKeyString);

                return {
                    originalPath: originalPathObjects,
                    ...targetModel
                };
            }));

            return {
                targetModels,
                ...result
            };
        }));

        return modelAddedToResults;
    }

    #combineSameTargets(results) {
        const combinedResults = [];
        results.forEach(currentAnnotation => {
            const existingAnnotation = combinedResults.find((annotationToFind) => {
                return _.isEqual(currentAnnotation.targets, annotationToFind.targets);
            });
            if (!existingAnnotation) {
                combinedResults.push(currentAnnotation);
            } else {
                existingAnnotation.tags.push(...currentAnnotation.tags);
            }
        });

        return combinedResults;
    }

    /**
    * @method searchForTags
    * @param {String} query A query to match against tags. E.g., "dr" will match the tags "drilling" and "driving"
    * @param {Object} [abortController] An optional abort method to stop the query
    * @returns {Promise} returns a model of matching tags with their target domain objects attached
    */
    async searchForTags(query, abortController) {
        const matchingTagKeys = this.#getMatchingTags(query);
        const searchResults = (await Promise.all(this.openmct.objects.search(matchingTagKeys, abortController, this.openmct.objects.SEARCH_TYPES.TAGS))).flat();
        const filteredDeletedResults = searchResults.filter((result) => {
            return !(result._deleted);
        });
        const combinedSameTargets = this.#combineSameTargets(filteredDeletedResults);
        const appliedTagSearchResults = this.#addTagMetaInformationToResults(combinedSameTargets, matchingTagKeys);
        const appliedTargetsModels = await this.#addTargetModelsToResults(appliedTagSearchResults);
        const resultsWithValidPath = appliedTargetsModels.filter(result => {
            return this.openmct.objects.isReachable(result.targetModels?.[0]?.originalPath);
        });

        return resultsWithValidPath;
    }
}