mirror of
https://github.com/nasa/openmct.git
synced 2025-05-31 06:31:04 +00:00
Fix CouchDB search duplicates - Mct4667 (#4688)
* added separate search provider * add unit test Co-authored-by: John Hill <john.c.hill@nasa.gov>
This commit is contained in:
parent
e14b7cd0e2
commit
556f762d20
@ -298,18 +298,12 @@ class CouchObjectProvider {
|
||||
return Array.from(new Set(array));
|
||||
}
|
||||
|
||||
search(query, abortSignal) {
|
||||
const filter = {
|
||||
"selector": {
|
||||
"model": {
|
||||
"name": {
|
||||
"$regex": `(?i)${query}`
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return this.getObjectsByFilter(filter, abortSignal);
|
||||
search() {
|
||||
// Dummy search function. It has to appear to support search,
|
||||
// otherwise the in-memory indexer will index all of its objects,
|
||||
// but actually search results will be provided by a separate search provider
|
||||
// see CoucheSearchProvider.js
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
|
||||
async getObjectsByFilter(filter, abortSignal) {
|
||||
|
49
src/plugins/persistence/couch/CouchSearchProvider.js
Normal file
49
src/plugins/persistence/couch/CouchSearchProvider.js
Normal file
@ -0,0 +1,49 @@
|
||||
/*****************************************************************************
|
||||
* Open MCT, Copyright (c) 2014-2021, 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.
|
||||
*****************************************************************************/
|
||||
|
||||
// This provider exists because due to legacy reasons, we need to install
|
||||
// two plugins for two namespaces for CouchDB: one for "mct", and one for "".
|
||||
// Because of this, we need to separate out the search provider from the object
|
||||
// provider so we don't return two results for each found object.
|
||||
// If the above namespace is ever resolved, we can fold this search provider
|
||||
// back into the object provider.
|
||||
|
||||
class CouchSearchProvider {
|
||||
constructor(couchObjectProvider) {
|
||||
this.couchObjectProvider = couchObjectProvider;
|
||||
}
|
||||
|
||||
search(query, abortSignal) {
|
||||
const filter = {
|
||||
"selector": {
|
||||
"model": {
|
||||
"name": {
|
||||
"$regex": `(?i)${query}`
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return this.couchObjectProvider.getObjectsByFilter(filter, abortSignal);
|
||||
}
|
||||
}
|
||||
export default CouchSearchProvider;
|
@ -21,8 +21,10 @@
|
||||
*****************************************************************************/
|
||||
|
||||
import CouchObjectProvider from './CouchObjectProvider';
|
||||
import CouchSearchProvider from './CouchSearchProvider';
|
||||
const NAMESPACE = '';
|
||||
const LEGACY_SPACE = 'mct';
|
||||
const COUCH_SEARCH_ONLY_NAMESPACE = `COUCH_SEARCH_${Date.now()}`;
|
||||
|
||||
export default function CouchPlugin(options) {
|
||||
return function install(openmct) {
|
||||
@ -32,5 +34,6 @@ export default function CouchPlugin(options) {
|
||||
// Installing the same provider under both namespaces means that it can respond to object gets for both namespaces.
|
||||
openmct.objects.addProvider(LEGACY_SPACE, install.couchProvider);
|
||||
openmct.objects.addProvider(NAMESPACE, install.couchProvider);
|
||||
openmct.objects.addProvider(COUCH_SEARCH_ONLY_NAMESPACE, new CouchSearchProvider(install.couchProvider));
|
||||
};
|
||||
}
|
||||
|
@ -262,7 +262,9 @@ describe('the plugin', () => {
|
||||
await Promise.all(openmct.objects.search('test'));
|
||||
const requestUrl = fetch.calls.mostRecent().args[0];
|
||||
|
||||
expect(fetch).toHaveBeenCalled();
|
||||
// we only want one call to fetch, not 2!
|
||||
// see https://github.com/nasa/openmct/issues/4667
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
expect(requestUrl.endsWith('_find')).toBeTrue();
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user