Skip to content

Commit

Permalink
Make single call to fetch registry package information instead of doi…
Browse files Browse the repository at this point in the history
…ng it per stream
  • Loading branch information
jen-huang committed Jun 25, 2020
1 parent 67f7c89 commit b9f0ddb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ paths:

jest.mock('./epm/packages/assets', () => {
return {
getAssetsDataForPackageKey: mockedGetAssetsData,
getAssetsData: mockedGetAssetsData,
};
});

jest.mock('./epm/registry', () => {
return {
fetchInfo: () => ({}),
};
});

Expand Down
31 changes: 23 additions & 8 deletions x-pack/plugins/ingest_manager/server/services/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import {
PackageInfo,
} from '../../common';
import { DATASOURCE_SAVED_OBJECT_TYPE } from '../constants';
import { NewDatasource, Datasource, ListWithKuery, DatasourceSOAttributes } from '../types';
import {
NewDatasource,
Datasource,
ListWithKuery,
DatasourceSOAttributes,
RegistryPackage,
} from '../types';
import { agentConfigService } from './agent_config';
import { getPackageInfo, getInstallation } from './epm/packages';
import { outputService } from './output';
import { getAssetsDataForPackageKey } from './epm/packages/assets';
import * as Registry from './epm/registry';
import { getPackageInfo, getInstallation } from './epm/packages';
import { getAssetsData } from './epm/packages/assets';
import { createStream } from './epm/agent/agent';

const SAVED_OBJECT_TYPE = DATASOURCE_SAVED_OBJECT_TYPE;
Expand Down Expand Up @@ -252,22 +259,30 @@ class DatasourceService {
pkgInfo: PackageInfo,
inputs: DatasourceInput[]
): Promise<DatasourceInput[]> {
const inputsPromises = inputs.map((input) => _assignPackageStreamToInput(pkgInfo, input));
const registryPkgInfo = await Registry.fetchInfo(pkgInfo.name, pkgInfo.version);
const inputsPromises = inputs.map((input) =>
_assignPackageStreamToInput(registryPkgInfo, pkgInfo, input)
);

return Promise.all(inputsPromises);
}
}

async function _assignPackageStreamToInput(pkgInfo: PackageInfo, input: DatasourceInput) {
async function _assignPackageStreamToInput(
registryPkgInfo: RegistryPackage,
pkgInfo: PackageInfo,
input: DatasourceInput
) {
const streamsPromises = input.streams.map((stream) =>
_assignPackageStreamToStream(pkgInfo, input, stream)
_assignPackageStreamToStream(registryPkgInfo, pkgInfo, input, stream)
);

const streams = await Promise.all(streamsPromises);
return { ...input, streams };
}

async function _assignPackageStreamToStream(
registryPkgInfo: RegistryPackage,
pkgInfo: PackageInfo,
input: DatasourceInput,
stream: DatasourceInputStream
Expand Down Expand Up @@ -299,8 +314,8 @@ async function _assignPackageStreamToStream(
throw new Error(`Stream template path not found for dataset ${datasetPath}`);
}

const [pkgStream] = await getAssetsDataForPackageKey(
{ pkgName: pkgInfo.name, pkgVersion: pkgInfo.version },
const [pkgStream] = await getAssetsData(
registryPkgInfo,
(path: string) => path.endsWith(streamFromPkg.template_path),
datasetPath
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,3 @@ export async function getAssetsData(

return entries;
}

export async function getAssetsDataForPackageKey(
{ pkgName, pkgVersion }: { pkgName: string; pkgVersion: string },
filter = (path: string): boolean => true,
datasetName?: string
): Promise<Registry.ArchiveEntry[]> {
const registryPkgInfo = await Registry.fetchInfo(pkgName, pkgVersion);

return getAssetsData(registryPkgInfo, filter, datasetName);
}

0 comments on commit b9f0ddb

Please sign in to comment.