2015-07-16 17:35:26 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web 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 Web 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
/**
|
2015-07-17 18:24:33 +00:00
|
|
|
* Module defining GenericSearchProvider. Created by shale on 07/16/2015.
|
2015-07-16 17:35:26 +00:00
|
|
|
*/
|
|
|
|
define(
|
|
|
|
[],
|
|
|
|
function () {
|
|
|
|
"use strict";
|
2015-07-16 18:31:11 +00:00
|
|
|
|
2015-07-20 17:58:59 +00:00
|
|
|
var DEFAULT_MAX_RESULTS = 100,
|
|
|
|
DEFAULT_TIMEOUT = 1000,
|
|
|
|
stopTime;
|
2015-07-16 18:31:11 +00:00
|
|
|
|
2015-07-16 17:35:26 +00:00
|
|
|
/**
|
2015-07-20 17:58:59 +00:00
|
|
|
* A search service which searches through domain objects in
|
|
|
|
* the filetree without using external search implementations.
|
2015-07-16 17:35:26 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2015-07-29 22:07:13 +00:00
|
|
|
* @param $q
|
2015-07-20 17:58:59 +00:00
|
|
|
* @param {ObjectService} objectService the service from which
|
|
|
|
* domain objects can be gotten.
|
|
|
|
* @param {WorkerService} workerService the service which allows
|
|
|
|
* more easy creation of web workers.
|
2015-07-28 18:33:29 +00:00
|
|
|
* @param {roots[]} roots an array of all the root domain objects.
|
2015-07-16 17:35:26 +00:00
|
|
|
*/
|
2015-07-29 22:07:13 +00:00
|
|
|
function GenericSearchProvider($q, objectService, workerService, roots) {
|
2015-07-20 17:58:59 +00:00
|
|
|
var worker = workerService.run('genericSearchWorker'),
|
2015-07-29 22:07:13 +00:00
|
|
|
pendingQueries = {};
|
|
|
|
// pendingQueries is a dictionary with the key value pairs st
|
|
|
|
// the key is the timestamp and the value is the promise
|
|
|
|
|
2015-07-29 20:17:50 +00:00
|
|
|
// Tell the web worker to add a domain object's model to its list of items.
|
2015-07-21 16:58:19 +00:00
|
|
|
function indexItem(domainObject) {
|
2015-07-29 17:59:58 +00:00
|
|
|
var message;
|
|
|
|
|
|
|
|
// undefined check
|
|
|
|
if (domainObject && domainObject.getModel) {
|
|
|
|
// Using model instead of whole domain object because
|
|
|
|
// it's a JSON object.
|
|
|
|
message = {
|
|
|
|
request: 'index',
|
|
|
|
model: domainObject.getModel(),
|
|
|
|
id: domainObject.getId()
|
|
|
|
};
|
|
|
|
worker.postMessage(message);
|
|
|
|
}
|
2015-07-21 16:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the worker to search for items it has that match this searchInput.
|
|
|
|
// Takes the searchInput, as well as a max number of results (will return
|
2015-07-28 18:33:29 +00:00
|
|
|
// less than that if there are fewer matches).
|
2015-07-30 23:35:26 +00:00
|
|
|
function workerSearch(searchInput, maxResults, timestamp, timeout) {
|
2015-07-21 16:58:19 +00:00
|
|
|
var message = {
|
2015-07-28 17:15:05 +00:00
|
|
|
request: 'search',
|
2015-07-21 16:58:19 +00:00
|
|
|
input: searchInput,
|
2015-07-28 17:15:05 +00:00
|
|
|
maxNumber: maxResults,
|
2015-07-30 23:35:26 +00:00
|
|
|
timestamp: timestamp,
|
|
|
|
timeout: timeout
|
2015-07-21 16:58:19 +00:00
|
|
|
};
|
|
|
|
worker.postMessage(message);
|
|
|
|
}
|
|
|
|
|
2015-07-20 17:58:59 +00:00
|
|
|
function handleResponse(event) {
|
2015-07-29 20:17:50 +00:00
|
|
|
var ids = [],
|
2015-07-28 17:15:05 +00:00
|
|
|
id;
|
|
|
|
|
2015-07-29 20:17:50 +00:00
|
|
|
// If we have the results from a search
|
2015-07-21 18:14:10 +00:00
|
|
|
if (event.data.request === 'search') {
|
2015-07-21 19:34:08 +00:00
|
|
|
// Convert the ids given from the web worker into domain objects
|
2015-07-28 17:15:05 +00:00
|
|
|
for (id in event.data.results) {
|
2015-07-21 21:55:44 +00:00
|
|
|
ids.push(id);
|
2015-07-21 19:34:08 +00:00
|
|
|
}
|
|
|
|
objectService.getObjects(ids).then(function (objects) {
|
2015-07-30 20:54:56 +00:00
|
|
|
var searchResults = [],
|
2015-07-29 22:22:46 +00:00
|
|
|
id;
|
2015-07-28 18:33:29 +00:00
|
|
|
|
2015-07-29 20:17:50 +00:00
|
|
|
// Reset and repopulate the latest results
|
2015-07-28 17:15:05 +00:00
|
|
|
for (id in objects) {
|
2015-07-30 20:54:56 +00:00
|
|
|
searchResults.push({
|
2015-07-21 19:44:14 +00:00
|
|
|
object: objects[id],
|
2015-07-21 21:55:44 +00:00
|
|
|
id: id,
|
2015-07-30 20:19:19 +00:00
|
|
|
score: event.data.results[id]
|
2015-07-21 19:44:14 +00:00
|
|
|
});
|
2015-07-21 19:34:08 +00:00
|
|
|
}
|
2015-07-29 22:07:13 +00:00
|
|
|
|
|
|
|
// Resove the promise corresponding to this
|
2015-07-30 20:54:56 +00:00
|
|
|
pendingQueries[event.data.timestamp].resolve(
|
|
|
|
{
|
|
|
|
hits: searchResults,
|
2015-07-30 23:35:26 +00:00
|
|
|
total: event.data.total,
|
|
|
|
timedOut: event.data.timedOut
|
2015-07-30 20:54:56 +00:00
|
|
|
}
|
|
|
|
);
|
2015-07-21 19:34:08 +00:00
|
|
|
});
|
2015-07-21 18:14:10 +00:00
|
|
|
}
|
2015-07-20 17:58:59 +00:00
|
|
|
}
|
2015-07-16 17:35:26 +00:00
|
|
|
|
2015-07-28 17:15:05 +00:00
|
|
|
worker.onmessage = handleResponse;
|
|
|
|
|
2015-07-16 17:35:26 +00:00
|
|
|
// Recursive helper function for getItems()
|
|
|
|
function itemsHelper(children, i) {
|
2015-07-29 17:59:58 +00:00
|
|
|
// Index the current node
|
|
|
|
indexItem(children[i]);
|
|
|
|
|
|
|
|
if (i >= children.length) {
|
2015-07-16 17:35:26 +00:00
|
|
|
// Done!
|
|
|
|
return children;
|
|
|
|
} else if (children[i].hasCapability('composition')) {
|
|
|
|
// This child has children
|
|
|
|
return children[i].getCapability('composition').invoke().then(function (grandchildren) {
|
|
|
|
// Add grandchildren to the end of the list
|
|
|
|
// They will also be checked for composition
|
|
|
|
return itemsHelper(children.concat(grandchildren), i + 1);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// This child is a leaf
|
|
|
|
return itemsHelper(children, i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the filetree into a list
|
2015-07-20 18:21:55 +00:00
|
|
|
function getItems(timeout) {
|
2015-07-28 17:15:05 +00:00
|
|
|
var rootIds = [],
|
|
|
|
i;
|
|
|
|
for (i = 0; i < roots.length; i += 1) {
|
2015-07-22 23:21:34 +00:00
|
|
|
rootIds.push(roots[i].id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aquire root objects
|
2015-07-29 17:59:58 +00:00
|
|
|
objectService.getObjects(rootIds).then(function (objectsById) {
|
2015-07-28 17:15:05 +00:00
|
|
|
var objects = [],
|
|
|
|
id;
|
2015-07-20 17:58:59 +00:00
|
|
|
|
2015-07-29 17:59:58 +00:00
|
|
|
// Get each of the objects in objectsById
|
2015-07-28 17:15:05 +00:00
|
|
|
for (id in objectsById) {
|
2015-07-22 23:21:34 +00:00
|
|
|
objects.push(objectsById[id]);
|
|
|
|
}
|
|
|
|
|
2015-07-29 17:59:58 +00:00
|
|
|
// Index all of the roots' descendents
|
|
|
|
itemsHelper(objects, 0);
|
2015-07-16 17:35:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-29 22:07:13 +00:00
|
|
|
|
2015-07-21 22:31:23 +00:00
|
|
|
// For documentation, see query below.
|
2015-07-30 23:35:26 +00:00
|
|
|
function query(input, timestamp, maxResults, timeout) {
|
2015-07-23 23:10:03 +00:00
|
|
|
var terms = [],
|
2015-07-16 17:35:26 +00:00
|
|
|
searchResults = [],
|
2015-07-29 22:07:13 +00:00
|
|
|
defer = $q.defer();
|
|
|
|
|
|
|
|
pendingQueries[timestamp] = defer;
|
2015-07-16 17:35:26 +00:00
|
|
|
|
|
|
|
// Check to see if the user provided a maximum
|
2015-07-30 20:19:19 +00:00
|
|
|
// number of results to display
|
2015-07-16 17:35:26 +00:00
|
|
|
if (!maxResults) {
|
|
|
|
// Else, we provide a default value.
|
|
|
|
maxResults = DEFAULT_MAX_RESULTS;
|
|
|
|
}
|
2015-07-30 23:35:26 +00:00
|
|
|
// Similarly, check if timeout was provided
|
|
|
|
if (!timeout) {
|
|
|
|
timeout = DEFAULT_TIMEOUT;
|
|
|
|
}
|
2015-07-16 17:35:26 +00:00
|
|
|
|
2015-07-29 17:26:24 +00:00
|
|
|
// Instead, assume that the items have already been indexed, and
|
2015-07-30 20:19:19 +00:00
|
|
|
// just send the query to the worker.
|
2015-07-30 23:35:26 +00:00
|
|
|
workerSearch(input, maxResults, timestamp, timeout);
|
2015-07-29 22:07:13 +00:00
|
|
|
|
|
|
|
return defer.promise;
|
2015-07-16 17:35:26 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 17:26:24 +00:00
|
|
|
// Index the tree's contents once at the beginning
|
2015-07-29 20:17:50 +00:00
|
|
|
getItems();
|
2015-07-29 17:26:24 +00:00
|
|
|
|
2015-07-16 17:35:26 +00:00
|
|
|
return {
|
2015-07-21 22:31:23 +00:00
|
|
|
/**
|
|
|
|
* Searches through the filetree for domain objects which match
|
|
|
|
* the search term. This function is to be used as a fallback
|
2015-07-30 20:19:19 +00:00
|
|
|
* in the case where other search services are not avaliable.
|
2015-07-30 20:54:56 +00:00
|
|
|
* Returns a promise for a result object that has the format
|
2015-07-30 23:35:26 +00:00
|
|
|
* {hits: searchResult[], total: number, timedOut: boolean}
|
2015-07-30 20:54:56 +00:00
|
|
|
* where a searchResult has the format
|
|
|
|
* {id: domainObject ID, object: domainObject, score: number}
|
|
|
|
*
|
2015-07-21 22:31:23 +00:00
|
|
|
* Notes:
|
|
|
|
* * The order of the results is not guarenteed.
|
|
|
|
* * A domain object qualifies as a match for a search input if
|
|
|
|
* the object's name property contains any of the search terms
|
|
|
|
* (which are generated by splitting the input at spaces).
|
|
|
|
* * Scores are higher for matches that have more than one of
|
|
|
|
* the terms as substrings.
|
|
|
|
*
|
2015-07-23 23:10:03 +00:00
|
|
|
* @param input The text input that is the query.
|
2015-07-30 20:19:19 +00:00
|
|
|
* @param timestamp The time at which this function was called.
|
|
|
|
* This timestamp is used as a unique identifier for this
|
|
|
|
* query and the corresponding results.
|
|
|
|
* @param maxResults (optional) The maximum number of results
|
|
|
|
* that this function should return.
|
|
|
|
* @param timeout (optional) The time after which the search should
|
|
|
|
* stop calculations and return partial results.
|
2015-07-21 22:31:23 +00:00
|
|
|
*/
|
2015-07-30 20:19:19 +00:00
|
|
|
query: query
|
2015-07-29 22:22:46 +00:00
|
|
|
|
2015-07-16 17:35:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-17 18:24:33 +00:00
|
|
|
return GenericSearchProvider;
|
2015-07-16 17:35:26 +00:00
|
|
|
}
|
|
|
|
);
|