-
Notifications
You must be signed in to change notification settings - Fork 6
/
dump.ts
340 lines (310 loc) · 10.7 KB
/
dump.ts
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
import {CLICommand} from '@coveo/cli-commons/command/cliCommand';
import {Flags, CliUx} from '@oclif/core';
import {readJSONSync, writeFileSync, writeJSONSync} from 'fs-extra';
import {Parser} from 'json2csv';
import {SingleBar} from 'cli-progress';
import PlatformClient from '@coveo/platform-client';
import {dirSync} from 'tmp';
import {AuthenticatedClient} from '@coveo/cli-commons/platform/authenticatedClient';
import {
Preconditions,
IsAuthenticated,
} from '@coveo/cli-commons/preconditions/index';
import {Config} from '@coveo/cli-commons/config/config';
import {Trackable} from '@coveo/cli-commons/preconditions/trackable';
import {without} from '../../../lib/utils/list';
import {join} from 'path';
import dedent from 'ts-dedent';
import {formatOrgId} from '@coveo/cli-commons/utils/ux';
import {Example} from '@oclif/core/lib/interfaces';
type ResponseExceededMaximumSizeError = {message: string; type: string};
interface RawResult {
rowid: string;
[key: string]: unknown;
}
interface SearchResult {
raw: RawResult;
}
interface SearchResponse {
indexToken: string;
results: SearchResult[];
totalCount: number;
}
interface FetchParameters {
client: PlatformClient;
organizationId: string;
sources: string[];
additionalFilter: string;
fieldsToExclude: string[] | undefined;
pipeline: string | undefined;
}
export default class Dump extends CLICommand {
private static readonly DefaultNumberOfResultPerQuery = 1000;
private static mandatoryFields = ['rowid', 'sysrowid'];
public static description = dedent`
Dump the content of one or more sources in CSV format.
Note: DictionnaryFields/Values are experimentally supported. In case of failure, you should exclude them using the \`-x\` flag.
`;
public static examples: Example[] = [
{
command:
'coveo org:search:dump --source "My Web Source" "My Sitemap Source"',
description:
'Get content indexed into the "My Web Source" and "My Sitemap Source" sources.',
},
{
command:
'coveo org:search:dump --source Products --pipeline Search --additionalFilter "@cat_categories==(Shorts, Jackets)"',
description:
'Get all the products coming from the "Search" pipeline that are either in the "Shorts" or "Jackets" category.',
},
{
command:
'coveo org:search:dump --source Products --pipeline Search --additionalFilter "@cat_categories==Shorts AND @color==Black"',
description:
'Get all the products coming from the "Search" pipeline that are in the "Shorts" category and have the color "Black".',
},
{
command:
'coveo org:search:dump --fieldsToExclude ec_description ec_summary',
description:
'Get all the documents without the fields "ec_description" and "ec_summary" in them.',
},
];
public static flags = {
source: Flags.string({
char: 's',
description:
'The names (not the identifiers) of the sources from which to get content.',
helpValue: 'mySourceName',
required: true,
multiple: true,
}),
pipeline: Flags.string({
char: 'p',
description:
'The name of the query pipeline through which to get content. If not specified, the default query pipeline is used. See <https://docs.coveo.com/en/180>',
}),
fieldsToExclude: Flags.string({
char: 'x',
description:
'The fields to exclude from the data dump. If not specified, all fields are included.',
multiple: true,
}),
destination: Flags.string({
char: 'd',
description:
"The folder in which to create the CSV files. The data dump will fail if the folder doesn't exist.",
default: '.',
}),
name: Flags.string({
char: 'n',
description:
'The base name to use when creating a new CSV file. If more than one file is created, the CLI will append `_2`, `_3`, etc. to each new file name after the first one.',
default: 'indexdump',
}),
additionalFilter: Flags.string({
char: 'f',
description:
'The additional search filter to apply while getting the content. See <https://docs.coveo.com/en/1552>.',
default: '',
}),
chunkSize: Flags.integer({
char: 'c',
description: 'The maximum number of results to dump into each CSV file.',
default: 10000,
}),
};
private aggregatedResults: RawResult[] = [];
private aggregatedFieldsWithDupes: string[] = [];
private temporaryDumpDirectory = dirSync();
private numberOfResultPerQuery = Dump.DefaultNumberOfResultPerQuery;
private internalDumpFileIdx = 0;
private progressBar?: SingleBar;
private static readonly ResponseExceededMaximumSizeType =
'ResponseExceededMaximumSizeException';
private get dumpFileIndex() {
return this.internalDumpFileIdx++;
}
@Trackable({eventName: 'source content dump'})
@Preconditions(IsAuthenticated())
public async run() {
const {flags} = await this.parse(Dump);
const client = await new AuthenticatedClient().getClient();
const organizationId = new Config(this.config.configDir).get().organization;
const fieldsToExclude =
flags.fieldsToExclude &&
without(flags.fieldsToExclude, Dump.mandatoryFields);
const hasResults = await this.fetchResults({
client,
organizationId,
sources: flags.source,
pipeline: flags.pipeline,
additionalFilter: flags.additionalFilter,
fieldsToExclude,
});
if (hasResults) {
await this.convertRawChunksToCSVs();
} else {
this.log(
'No results found. Ensure that the specified sources, query pipeline, and additional filter are valid.'
);
}
}
public catch(err?: Error & {exitCode?: number}) {
this.progressBar?.stop();
return super.catch(err);
}
private async convertRawChunksToCSVs() {
const {flags} = await this.parse(Dump);
const fields = this.getFieldsToExport(flags.fieldsToExclude!);
for (
let currentDumpFileIndex = this.internalDumpFileIdx - 1;
currentDumpFileIndex >= 0;
currentDumpFileIndex--
) {
const data = readJSONSync(
this.getDumpFilePathFromIndex(currentDumpFileIndex)
);
const parser = new Parser({fields});
writeFileSync(
join(
flags.destination,
`${flags.name}${
this.internalDumpFileIdx === 1 ? '' : currentDumpFileIndex
}.csv`
),
parser.parse(data)
);
}
}
private getFieldsToExport(fieldsToExclude: string[]) {
const fieldSet = new Set(this.aggregatedFieldsWithDupes);
for (const fieldToExclude of fieldsToExclude ?? []) {
fieldSet.delete(fieldToExclude);
}
return Array.from(fieldSet);
}
private getFilter(params: FetchParameters, rowId = '') {
const sourcesFilter = `(${params.sources
.map((source) => `( @source=="${source}" )`)
.join(' OR ')})`;
return `${sourcesFilter} ${params.additionalFilter} ${
rowId !== '' ? `( @rowid>${rowId} )` : ''
}`;
}
private async fetchResults(
params: FetchParameters,
indexToken = '',
rowId = ''
): Promise<boolean> {
this.progressBar = this.getProgressBar();
this.log(
`Fetching all results from organization ${formatOrgId(
params.organizationId
)} from source(s) ${params.sources.join(',')}...`
);
if (params.additionalFilter) {
this.log(`Applying additional filter ${params.additionalFilter}`);
}
this.progressBar.start(Dump.DefaultNumberOfResultPerQuery, 0);
let lastResultsLength;
do {
const isFirstQuery = indexToken === '';
const response = await this.doQuery(params, rowId, indexToken);
if (isFirstQuery) {
this.progressBar.setTotal(response.totalCount);
}
this.progressBar.increment(response.results.length);
lastResultsLength = response.results.length;
await this.aggregateResults(response.results.map((result) => result.raw));
if (lastResultsLength < this.numberOfResultPerQuery) {
break;
}
indexToken = response.indexToken;
rowId = response.results[lastResultsLength - 1].raw.rowid;
} while (lastResultsLength >= this.numberOfResultPerQuery);
this.progressBar.stop();
return this.progressBar.getTotal() > 0;
}
private async doQuery(
params: FetchParameters,
rowId?: string,
indexToken?: string
): Promise<SearchResponse> {
while (this.numberOfResultPerQuery !== 0) {
try {
const results = (await params.client.search.query({
aq: this.getFilter(params, rowId),
debug: true,
organizationId: params.organizationId,
numberOfResults: this.numberOfResultPerQuery,
sortCriteria: '@rowid ascending',
...(params.fieldsToExclude && {
fieldsToExclude: params.fieldsToExclude,
}),
...(params.pipeline && {pipeline: params.pipeline}),
...(indexToken !== '' && {indexToken: indexToken}),
})) as unknown as SearchResponse;
return results;
} catch (error) {
if (this.isResponseExceededMaximumSizeError(error)) {
this.numberOfResultPerQuery = Math.floor(
this.numberOfResultPerQuery / 2
);
} else {
throw error;
}
}
}
this.error(
dedent`
Response exceeds the maximum size of 31457280 bytes.
Cannot query a single result, please exclude some/more fields
`
);
}
private isResponseExceededMaximumSizeError(
error: unknown
): error is ResponseExceededMaximumSizeError {
return (
(error as ResponseExceededMaximumSizeError)?.type ===
Dump.ResponseExceededMaximumSizeType
);
}
private getProgressBar() {
return CliUx.ux.progress({
format: 'Progress | {bar} | ETA: {eta}s | {value}/{total} results',
}) as SingleBar;
}
private async aggregateResults(results: RawResult[]) {
const maxAggregatedResults = (await this.parse(Dump)).flags.chunkSize;
while (results.length > 0) {
const amountOfNewResultsToExtract =
maxAggregatedResults - this.aggregatedResults.length;
this.aggregatedResults.push(
...results.splice(
0,
Math.min(amountOfNewResultsToExtract, results.length)
)
);
this.dumpAggregatedResults();
}
}
private getDumpFilePathFromIndex(index: number) {
return join(this.temporaryDumpDirectory.name, `dump${index}`);
}
private dumpAggregatedResults() {
this.extractFieldsFromAggregatedResults();
writeJSONSync(
this.getDumpFilePathFromIndex(this.dumpFileIndex),
this.aggregatedResults
);
this.aggregatedResults = [];
}
private extractFieldsFromAggregatedResults() {
this.aggregatedFieldsWithDupes.push(
...this.aggregatedResults.flatMap(Object.keys)
);
}
}