Skip to content

Commit

Permalink
[Backport 2.x] Optimize searches for integration data (opensearch-pro…
Browse files Browse the repository at this point in the history
…ject#1406)

(cherry picked from commit 0cd79bf)

Signed-off-by: Simeon Widdis <sawiddis@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 045306e commit 2d41847
Showing 1 changed file with 60 additions and 18 deletions.
78 changes: 60 additions & 18 deletions server/adaptors/integrations/repository/index_data_adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,79 @@ export class IndexDataAdaptor implements CatalogDataAdaptor {
this.client = client;
}

private async asJsonAdaptor(): Promise<JsonCatalogDataAdaptor> {
const results = await this.client.find({ type: 'integration-template' });
const filteredIntegrations: SerializedIntegration[] = results.saved_objects
.map((obj) => obj.attributes as SerializedIntegration)
.filter((obj) => this.directory === undefined || this.directory === obj.name);
return new JsonCatalogDataAdaptor(filteredIntegrations);
}

async findIntegrationVersions(dirname?: string | undefined): Promise<Result<string[], Error>> {
const adaptor = await this.asJsonAdaptor();
const integrationVersions = await this.client.find({
type: 'integration-template',
fields: ['name', 'version'],
search: dirname ? `"${dirname}"` : undefined,
searchFields: ['name'],
});
const adaptor = new JsonCatalogDataAdaptor(
integrationVersions.saved_objects.map((obj) => obj.attributes as SerializedIntegration)
);
return await adaptor.findIntegrationVersions(dirname);
}

async readFile(filename: string, type?: IntegrationPart): Promise<Result<object[] | object>> {
const adaptor = await this.asJsonAdaptor();
return await adaptor.readFile(filename, type);
// Duplicates a lot of logic from the Json Adaptor version since we need to parse config
// for an efficient network query anyways
if (type !== undefined) {
return {
ok: false,
error: new Error('JSON adaptor does not support subtypes (isConfigLocalized: true)'),
};
}

const filenameParts = filename.match(/([\w]+)-(\d+(\.\d+)*)\.json/);
if (!filenameParts) {
return { ok: false, error: new Error(`Invalid Config filename: ${filename}`) };
}

const integrations = await this.client.find({
type: 'integration-template',
search: `"${filenameParts[1]}" + "${filenameParts[2]}"`,
searchFields: ['name', 'version'],
});
if (integrations.total === 0) {
return { ok: false, error: new Error('Config file not found: ' + filename) };
}

return { ok: true, value: integrations.saved_objects[0].attributes as object };
}

async readFileRaw(filename: string, type?: IntegrationPart): Promise<Result<Buffer>> {
const adaptor = await this.asJsonAdaptor();
return await adaptor.readFileRaw(filename, type);
async readFileRaw(_filename: string, _type?: IntegrationPart): Promise<Result<Buffer>> {
return {
ok: false,
error: new Error('JSON adaptor does not support raw files (isConfigLocalized: true)'),
};
}

async findIntegrations(dirname: string = '.'): Promise<Result<string[]>> {
const adaptor = await this.asJsonAdaptor();
return await adaptor.findIntegrations(dirname);
const dir = dirname !== '.' ? dirname : this.directory;
const integrations = await this.client.find({
type: 'integration-template',
fields: ['name'],
search: dir ? `"${dir}"` : undefined,
searchFields: ['name'],
});
const names = integrations.saved_objects.map(
(obj) => (obj.attributes as SerializedIntegration).name
);
return {
ok: true,
value: [...new Set(names)],
};
}

async getDirectoryType(dirname?: string): Promise<'integration' | 'repository' | 'unknown'> {
const adaptor = await this.asJsonAdaptor();
return await adaptor.getDirectoryType(dirname);
const names = await this.findIntegrations(dirname ?? '.');
if (!names.value || names.value.length === 0) {
return 'unknown';
}
if (names.value.length === 1) {
return 'integration';
}
return 'repository';
}

join(filename: string): IndexDataAdaptor {
Expand Down

0 comments on commit 2d41847

Please sign in to comment.