-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathCatalogSearchProvider.ts
More file actions
214 lines (192 loc) · 6.4 KB
/
CatalogSearchProvider.ts
File metadata and controls
214 lines (192 loc) · 6.4 KB
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
import { autorun, makeObservable, runInAction } from "mobx";
import {
Category,
SearchAction
} from "../../Core/AnalyticEvents/analyticEvents";
import { TerriaErrorSeverity } from "../../Core/TerriaError";
import GroupMixin from "../../ModelMixins/GroupMixin";
import ReferenceMixin from "../../ModelMixins/ReferenceMixin";
import CatalogSearchProviderMixin from "../../ModelMixins/SearchProviders/CatalogSearchProviderMixin";
import CatalogSearchProviderTraits from "../../Traits/SearchProviders/CatalogSearchProviderTraits";
import CommonStrata from "../Definition/CommonStrata";
import CreateModel from "../Definition/CreateModel";
import { BaseModel } from "../Definition/Model";
import Terria from "../Terria";
import SearchProviderResult from "./SearchProviderResults";
import SearchResult from "./SearchResult";
type UniqueIdString = string;
type ResultMap = Map<UniqueIdString, boolean>;
export function loadAndSearchCatalogRecursively(
models: BaseModel[],
searchTextLowercase: string,
searchResults: SearchProviderResult,
resultMap: ResultMap,
iteration: number = 0
): Promise<void> {
// checkTerriaAgainstResults(terria, searchResults)
// don't go further than 10 deep, but also if we have references that never
// resolve to a target, might overflow
if (iteration > 10) {
return Promise.resolve();
}
// add some public interface for terria's `models`?
const referencesAndGroupsToLoad: any[] = models.filter((model: any) => {
if (resultMap.get(model.uniqueId) === undefined) {
const modelToSave = model.target || model;
// Use a flattened string of definition data later,
// without only checking name/id/descriptions?
// saveModelToJson(modelToSave, {
// includeStrata: [CommonStrata.definition]
// });
autorun((reaction) => {
const searchString = `${modelToSave.name} ${modelToSave.uniqueId} ${modelToSave.description}`;
const matchesString =
searchString.toLowerCase().indexOf(searchTextLowercase) !== -1;
resultMap.set(model.uniqueId, matchesString);
if (matchesString) {
runInAction(() => {
searchResults.results.push(
new SearchResult({
name: modelToSave.name,
catalogItem: modelToSave
})
);
});
}
reaction.dispose();
});
}
if (ReferenceMixin.isMixedInto(model) || GroupMixin.isMixedInto(model)) {
return true;
}
// Could also check for loadMembers() here, but will be even slower
// (relies on external non-magda services to be performant)
return false;
});
// If we have no members to load
if (referencesAndGroupsToLoad.length === 0) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
autorun((reaction) => {
Promise.all(
referencesAndGroupsToLoad.map(async (model) => {
if (ReferenceMixin.isMixedInto(model)) {
// TODO: could handle errors better here
(await model.loadReference()).throwIfError();
}
// TODO: investigate performant route for calling loadMembers on additional groupmixins
// else if (GroupMixin.isMixedInto(model)) {
// return model.loadMembers();
// }
})
)
.then(() => {
// Then call this function again to see if new child references were loaded in
resolve(
loadAndSearchCatalogRecursively(
models,
searchTextLowercase,
searchResults,
resultMap,
iteration + 1
)
);
})
.catch((error) => {
reject(error);
});
reaction.dispose();
});
});
}
export default class CatalogSearchProvider extends CatalogSearchProviderMixin(
CreateModel(CatalogSearchProviderTraits)
) {
static readonly type = "catalog-search-provider";
debounceTime = 300;
constructor(id: string | undefined, terria: Terria) {
super(id, terria);
makeObservable(this);
this.setTrait(
CommonStrata.defaults,
"minCharacters",
terria.searchBarModel.minCharacters
);
}
get type() {
return CatalogSearchProvider.type;
}
protected logEvent(searchText: string) {
this.terria.analytics?.logEvent(
Category.search,
SearchAction.catalog,
searchText
);
}
protected async doSearch(
searchText: string,
searchResults: SearchProviderResult
): Promise<void> {
runInAction(() => (searchResults.isSearching = true));
searchResults.results.length = 0;
searchResults.message = undefined;
if (searchText === undefined || /^\s*$/.test(searchText)) {
runInAction(() => (searchResults.isSearching = false));
return Promise.resolve();
}
// Load catalogIndex if needed
if (this.terria.catalogIndex && !this.terria.catalogIndex.loadPromise) {
try {
await this.terria.catalogIndex.load();
} catch (e) {
this.terria.raiseErrorToUser(
e,
"Failed to load catalog index. Searching may be slow/inaccurate"
);
}
}
const resultMap: ResultMap = new Map();
try {
if (this.terria.catalogIndex?.searchIndex) {
const results = await this.terria.catalogIndex.search(searchText);
runInAction(() => (searchResults.results = results));
} else {
await loadAndSearchCatalogRecursively(
this.terria.modelValues,
searchText.toLowerCase(),
searchResults,
resultMap
);
}
runInAction(() => {
searchResults.isSearching = false;
});
if (searchResults.isCanceled) {
// A new search has superseded this one, so ignore the result.
return;
}
runInAction(() => {
this.terria.catalogReferencesLoaded = true;
});
if (searchResults.results.length === 0) {
searchResults.message = {
content: "translate#viewModels.searchNoCatalogueItem"
};
}
} catch (e) {
console.error(e);
this.terria.raiseErrorToUser(e, {
message: "An error occurred while searching",
severity: TerriaErrorSeverity.Warning
});
if (searchResults.isCanceled) {
// A new search has superseded this one, so ignore the result.
return;
}
searchResults.message = {
content: "translate#viewModels.searchErrorOccurred"
};
}
}
}