2016-04-09 05:10:05 +08:00
|
|
|
import _ from 'underscore'
|
2016-04-11 06:58:23 +08:00
|
|
|
import {
|
|
|
|
Actions,
|
|
|
|
NylasAPI,
|
|
|
|
Thread,
|
|
|
|
DatabaseStore,
|
2016-11-11 01:08:50 +08:00
|
|
|
ComponentRegistry,
|
2016-04-11 06:58:23 +08:00
|
|
|
FocusedContentStore,
|
|
|
|
MutableQuerySubscription,
|
2017-03-01 06:25:59 +08:00
|
|
|
SearchQueryParser,
|
2016-04-11 06:58:23 +08:00
|
|
|
} from 'nylas-exports'
|
|
|
|
import SearchActions from './search-actions'
|
2016-04-09 05:10:05 +08:00
|
|
|
|
2016-07-27 17:56:55 +08:00
|
|
|
const {LongConnectionStatus} = NylasAPI
|
|
|
|
|
2016-04-09 05:10:05 +08:00
|
|
|
class SearchQuerySubscription extends MutableQuerySubscription {
|
|
|
|
|
|
|
|
constructor(searchQuery, accountIds) {
|
2016-04-20 02:32:33 +08:00
|
|
|
super(null, {emitResultSet: true})
|
2016-04-09 05:10:05 +08:00
|
|
|
this._searchQuery = searchQuery
|
|
|
|
this._accountIds = accountIds
|
|
|
|
|
|
|
|
this.resetData()
|
|
|
|
|
|
|
|
this._connections = []
|
|
|
|
this._unsubscribers = [
|
2017-02-23 03:30:20 +08:00
|
|
|
FocusedContentStore.listen(this.onFocusedContentChanged),
|
2016-04-09 05:10:05 +08:00
|
|
|
]
|
2016-11-11 01:08:50 +08:00
|
|
|
this._extDisposables = []
|
|
|
|
|
2016-04-09 05:10:05 +08:00
|
|
|
_.defer(() => this.performSearch())
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:58:23 +08:00
|
|
|
replaceRange = () => {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2016-04-09 05:10:05 +08:00
|
|
|
resetData() {
|
|
|
|
this._searchStartedAt = null
|
2017-02-23 03:30:20 +08:00
|
|
|
this._localResultsReceivedAt = null
|
|
|
|
this._remoteResultsReceivedAt = null
|
|
|
|
this._remoteResultsCount = 0
|
|
|
|
this._localResultsCount = 0
|
2016-04-09 05:10:05 +08:00
|
|
|
this._firstThreadSelectedAt = null
|
|
|
|
this._lastFocusedThread = null
|
|
|
|
this._focusedThreadCount = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
performSearch() {
|
|
|
|
this._searchStartedAt = Date.now()
|
|
|
|
|
|
|
|
this.performLocalSearch()
|
|
|
|
this.performRemoteSearch()
|
2016-11-11 01:08:50 +08:00
|
|
|
this.performExtensionSearch()
|
2016-04-09 05:10:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
performLocalSearch() {
|
2016-11-05 01:47:20 +08:00
|
|
|
let dbQuery = DatabaseStore.findAll(Thread).distinct()
|
2016-04-09 05:10:05 +08:00
|
|
|
if (this._accountIds.length === 1) {
|
|
|
|
dbQuery = dbQuery.where({accountId: this._accountIds[0]})
|
|
|
|
}
|
2017-01-10 02:50:54 +08:00
|
|
|
try {
|
2017-03-01 06:25:59 +08:00
|
|
|
const parsedQuery = SearchQueryParser.parse(this._searchQuery);
|
2017-01-10 02:50:54 +08:00
|
|
|
console.info('Successfully parsed and codegened search query', parsedQuery);
|
|
|
|
dbQuery = dbQuery.structuredSearch(parsedQuery);
|
|
|
|
} catch (e) {
|
|
|
|
console.info('Failed to parse local search query, falling back to generic query', e);
|
|
|
|
dbQuery = dbQuery.search(this._searchQuery);
|
|
|
|
}
|
2016-10-25 05:18:19 +08:00
|
|
|
dbQuery = dbQuery
|
2017-01-10 02:50:54 +08:00
|
|
|
.order(Thread.attributes.lastMessageReceivedTimestamp.descending())
|
|
|
|
.limit(100)
|
|
|
|
|
|
|
|
console.info('dbQuery.sql() =', dbQuery.sql());
|
2016-10-25 05:18:19 +08:00
|
|
|
|
2016-04-09 05:10:05 +08:00
|
|
|
dbQuery.then((results) => {
|
2017-02-23 03:30:20 +08:00
|
|
|
if (!this._localResultsReceivedAt) {
|
|
|
|
this._localResultsReceivedAt = Date.now()
|
|
|
|
}
|
|
|
|
this._localResultsCount += results.length
|
2017-03-07 02:43:55 +08:00
|
|
|
// Even if we don't have any results now we might sync additional messages
|
|
|
|
// from the provider which will cause new results to appear later.
|
|
|
|
this.replaceQuery(dbQuery)
|
2016-04-09 05:10:05 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-11 01:08:50 +08:00
|
|
|
_addThreadIdsToSearch(ids = []) {
|
|
|
|
const currentResults = this._set && this._set.ids().length > 0;
|
|
|
|
let searchIds = ids;
|
|
|
|
if (currentResults) {
|
|
|
|
const currentResultIds = this._set.ids()
|
|
|
|
searchIds = _.uniq(currentResultIds.concat(ids))
|
|
|
|
}
|
|
|
|
const dbQuery = (
|
|
|
|
DatabaseStore.findAll(Thread)
|
|
|
|
.where({id: searchIds})
|
|
|
|
.order(Thread.attributes.lastMessageReceivedTimestamp.descending())
|
|
|
|
)
|
|
|
|
this.replaceQuery(dbQuery)
|
|
|
|
}
|
|
|
|
|
2016-04-09 05:10:05 +08:00
|
|
|
performRemoteSearch() {
|
|
|
|
const accountsSearched = new Set()
|
|
|
|
let resultIds = []
|
|
|
|
|
2016-04-11 06:58:23 +08:00
|
|
|
const allAccountsSearched = () => accountsSearched.size === this._accountIds.length
|
2016-04-09 05:10:05 +08:00
|
|
|
const resultsReturned = () => {
|
|
|
|
// Don't emit a "result" until we have at least one thread to display.
|
|
|
|
// Otherwise it will show "No Results Found"
|
2016-04-11 06:58:23 +08:00
|
|
|
if (resultIds.length > 0 || allAccountsSearched()) {
|
2016-11-11 01:08:50 +08:00
|
|
|
this._addThreadIdsToSearch(resultIds)
|
2016-04-09 05:10:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._connections = this._accountIds.map((accountId) => {
|
|
|
|
return NylasAPI.startLongConnection({
|
|
|
|
accountId,
|
|
|
|
path: `/threads/search/streaming?q=${encodeURIComponent(this._searchQuery)}`,
|
|
|
|
onResults: (results) => {
|
2017-02-23 03:30:20 +08:00
|
|
|
if (!this._remoteResultsReceivedAt) {
|
|
|
|
this._remoteResultsReceivedAt = Date.now()
|
2016-04-09 05:10:05 +08:00
|
|
|
}
|
|
|
|
const threads = results[0]
|
|
|
|
resultIds = resultIds.concat(_.pluck(threads, 'id'))
|
2017-02-23 03:30:20 +08:00
|
|
|
this._remoteResultsCount += resultIds.length
|
2016-04-09 05:10:05 +08:00
|
|
|
resultsReturned()
|
|
|
|
},
|
2016-07-27 17:56:55 +08:00
|
|
|
onStatusChanged: (status) => {
|
|
|
|
const hasClosed = [
|
|
|
|
LongConnectionStatus.Closed,
|
|
|
|
LongConnectionStatus.Ended,
|
|
|
|
].includes(status)
|
|
|
|
|
|
|
|
if (hasClosed) {
|
2016-04-09 05:10:05 +08:00
|
|
|
accountsSearched.add(accountId)
|
2016-04-11 06:58:23 +08:00
|
|
|
if (allAccountsSearched()) {
|
|
|
|
SearchActions.searchCompleted()
|
|
|
|
}
|
2016-04-09 05:10:05 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-11 01:08:50 +08:00
|
|
|
performExtensionSearch() {
|
|
|
|
const searchExtensions = ComponentRegistry.findComponentsMatching({
|
|
|
|
role: "SearchBarResults",
|
|
|
|
})
|
|
|
|
|
|
|
|
this._extDisposables = searchExtensions.map((ext) => {
|
|
|
|
return ext.observeThreadIdsForQuery(this._searchQuery)
|
|
|
|
.subscribe((ids = []) => {
|
|
|
|
const allIds = _.compact(_.flatten(ids))
|
|
|
|
if (allIds.length === 0) return;
|
|
|
|
this._addThreadIdsToSearch(allIds)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-23 03:30:20 +08:00
|
|
|
// We want to keep track of how many threads from the search results were
|
|
|
|
// focused
|
|
|
|
onFocusedContentChanged = () => {
|
2016-04-11 06:58:23 +08:00
|
|
|
const thread = FocusedContentStore.focused('thread')
|
2016-04-09 05:10:05 +08:00
|
|
|
const shouldRecordChange = (
|
|
|
|
thread &&
|
|
|
|
(this._lastFocusedThread || {}).id !== thread.id
|
|
|
|
)
|
|
|
|
if (shouldRecordChange) {
|
|
|
|
if (this._focusedThreadCount === 0) {
|
|
|
|
this._firstThreadSelectedAt = Date.now()
|
|
|
|
}
|
|
|
|
this._focusedThreadCount += 1
|
|
|
|
this._lastFocusedThread = thread
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reportSearchMetrics() {
|
|
|
|
if (!this._searchStartedAt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-23 03:30:20 +08:00
|
|
|
let timeToLocalResultsMs = null
|
|
|
|
let timeToFirstRemoteResultsMs = null;
|
|
|
|
let timeToFirstThreadSelectedMs = null;
|
|
|
|
const timeInsideSearchMs = Date.now() - this._searchStartedAt
|
|
|
|
const numThreadsSelected = this._focusedThreadCount
|
|
|
|
const numLocalResults = this._localResultsCount
|
|
|
|
const numRemoteResults = this._remoteResultsCount
|
2016-04-09 05:10:05 +08:00
|
|
|
|
|
|
|
if (this._firstThreadSelectedAt) {
|
2017-02-23 03:30:20 +08:00
|
|
|
timeToFirstThreadSelectedMs = this._firstThreadSelectedAt - this._searchStartedAt
|
2016-04-09 05:10:05 +08:00
|
|
|
}
|
2017-02-23 03:30:20 +08:00
|
|
|
if (this._localResultsReceivedAt) {
|
|
|
|
timeToLocalResultsMs = this._localResultsReceivedAt - this._searchStartedAt
|
2016-04-09 05:10:05 +08:00
|
|
|
}
|
2017-02-23 03:30:20 +08:00
|
|
|
if (this._remoteResultsReceivedAt) {
|
|
|
|
timeToFirstRemoteResultsMs = this._remoteResultsReceivedAt - this._searchStartedAt
|
2016-04-09 05:10:05 +08:00
|
|
|
}
|
2017-02-23 03:30:20 +08:00
|
|
|
|
|
|
|
Actions.recordPerfMetric({
|
|
|
|
action: 'search-performed',
|
|
|
|
actionTimeMs: timeToLocalResultsMs,
|
|
|
|
numLocalResults,
|
|
|
|
numRemoteResults,
|
|
|
|
numThreadsSelected,
|
2017-02-24 05:15:31 +08:00
|
|
|
clippedData: [
|
|
|
|
{key: 'timeToLocalResultsMs', val: timeToLocalResultsMs},
|
|
|
|
{key: 'timeToFirstThreadSelectedMs', val: timeToFirstThreadSelectedMs},
|
|
|
|
{key: 'timeInsideSearchMs', val: timeInsideSearchMs, maxValue: 60 * 1000},
|
|
|
|
{key: 'timeToFirstRemoteResultsMs', val: timeToFirstRemoteResultsMs, maxValue: 10 * 1000},
|
|
|
|
],
|
2017-02-23 03:30:20 +08:00
|
|
|
})
|
2016-04-09 05:10:05 +08:00
|
|
|
this.resetData()
|
|
|
|
}
|
|
|
|
|
2017-02-23 03:30:20 +08:00
|
|
|
// This function is called when the user leaves the SearchPerspective
|
2016-04-20 02:32:33 +08:00
|
|
|
onLastCallbackRemoved() {
|
|
|
|
this.reportSearchMetrics();
|
2016-04-09 05:10:05 +08:00
|
|
|
this._connections.forEach((conn) => conn.end())
|
|
|
|
this._unsubscribers.forEach((unsub) => unsub())
|
2016-11-11 01:08:50 +08:00
|
|
|
this._extDisposables.forEach((disposable) => disposable.dispose())
|
2016-04-09 05:10:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:58:23 +08:00
|
|
|
export default SearchQuerySubscription
|