Skip to content

Commit

Permalink
updates for the latest elasticsearch client
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Oct 26, 2021
1 parent ae1814f commit d635dae
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import { MemoryPreviewChart } from './memory_preview_chart';
import { useFieldFormatter } from '../../contexts/kibana/use_field_formatter';
import { ListingPageUrlState } from '../../../../common/types/common';
import { useToastNotificationService } from '../../services/toast_notification_service';

export type NodeItem = NodeDeploymentStatsResponse;

Expand All @@ -46,6 +47,7 @@ export const getDefaultNodesListState = (): ListingPageUrlState => ({

export const NodesList: FC = () => {
const trainedModelsApiService = useTrainedModelsApiService();
const { displayErrorToast } = useToastNotificationService();
const bytesFormatter = useFieldFormatter('bytes');
const [items, setItems] = useState<NodeItem[]>([]);
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -60,10 +62,19 @@ export const NodesList: FC = () => {
const searchQueryText = pageState.queryText ?? '';

const fetchNodesData = useCallback(async () => {
const nodesResponse = await trainedModelsApiService.getTrainedModelsNodesOverview();
setItems(nodesResponse.nodes);
setIsLoading(false);
refreshAnalyticsList$.next(REFRESH_ANALYTICS_LIST_STATE.IDLE);
try {
const nodesResponse = await trainedModelsApiService.getTrainedModelsNodesOverview();
setItems(nodesResponse.nodes);
setIsLoading(false);
refreshAnalyticsList$.next(REFRESH_ANALYTICS_LIST_STATE.IDLE);
} catch (e) {
displayErrorToast(
e,
i18n.translate('xpack.ml.trainedModels.nodesList.nodesFetchError', {
defaultMessage: 'Nodes fetch failed',
})
);
}
}, []);

const toggleDetails = (item: NodeItem) => {
Expand Down
26 changes: 8 additions & 18 deletions x-pack/plugins/ml/server/lib/ml_client/ml_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,26 +380,16 @@ export function getMlClient(
async getTrainedModelsStats(...p: Parameters<MlClient['getTrainedModelsStats']>) {
return mlClient.getTrainedModelsStats(...p);
},
// TODO update when the new elasticsearch-js client is available
async getTrainedModelsDeploymentStats(...p: Parameters<MlClient['getTrainedModelsStats']>) {
return client.asCurrentUser.transport.request({
method: 'GET',
path: `/_ml/trained_models/${p[0]?.model_id ?? '*'}/deployment/_stats`,
});
async getTrainedModelDeploymentStats(
...p: Parameters<MlClient['getTrainedModelDeploymentStats']>
) {
return mlClient.getTrainedModelDeploymentStats(...p);
},
// TODO update when the new elasticsearch-js client is available
async startTrainedModelDeployment(...p: Parameters<MlClient['deleteTrainedModel']>) {
return client.asCurrentUser.transport.request({
method: 'POST',
path: `/_ml/trained_models/${p[0].model_id}/deployment/_start`,
});
async startTrainedModelDeployment(...p: Parameters<MlClient['startTrainedModelDeployment']>) {
return mlClient.startTrainedModelDeployment(...p);
},
// TODO update when the new elasticsearch-js client is available
async stopTrainedModelDeployment(...p: Parameters<MlClient['deleteTrainedModel']>) {
return client.asCurrentUser.transport.request({
method: 'POST',
path: `/_ml/trained_models/${p[0].model_id}/deployment/_stop`,
});
async stopTrainedModelDeployment(...p: Parameters<MlClient['stopTrainedModelDeployment']>) {
return mlClient.stopTrainedModelDeployment(...p);
},
async info(...p: Parameters<MlClient['info']>) {
return mlClient.info(...p);
Expand Down
16 changes: 3 additions & 13 deletions x-pack/plugins/ml/server/lib/ml_client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,11 @@

import { ElasticsearchClient } from 'kibana/server';
import { searchProvider } from './search';
import { TrainedModelDeploymentStatsResponse } from '../../../common/types/trained_models';

type OrigMlClient = ElasticsearchClient['ml'];

export interface MlClient extends OrigMlClient {
anomalySearch: ReturnType<typeof searchProvider>['anomalySearch'];
// TODO remove when the new elasticsearch-js client is available
getTrainedModelsDeploymentStats: (options?: { model_id?: string }) => Promise<{
body: { count: number; deployment_stats: TrainedModelDeploymentStatsResponse[] };
}>;
// TODO remove when the new elasticsearch-js client is available
startTrainedModelDeployment: (options: { model_id: string }) => Promise<{
body: { acknowledge: boolean };
}>;
// TODO remove when the new elasticsearch-js client is available
stopTrainedModelDeployment: (options: { model_id: string }) => Promise<{
body: { acknowledge: boolean };
}>;
}

export type MlClientParams =
Expand Down Expand Up @@ -61,6 +48,9 @@ export type MlClientParams =
| Parameters<MlClient['getRecords']>
| Parameters<MlClient['getTrainedModels']>
| Parameters<MlClient['getTrainedModelsStats']>
| Parameters<MlClient['getTrainedModelDeploymentStats']>
| Parameters<MlClient['startTrainedModelDeployment']>
| Parameters<MlClient['stopTrainedModelDeployment']>
| Parameters<MlClient['info']>
| Parameters<MlClient['openJob']>
| Parameters<MlClient['postCalendarEvents']>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { IScopedClusterClient } from 'kibana/server';
import { sumBy, pick } from 'lodash';
import { NodesInfoNodeInfo } from '@elastic/elasticsearch/api/types';
import { NodesInfoNodeInfo } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type {
NodeDeploymentStatsResponse,
PipelineDefinition,
Expand All @@ -18,6 +18,7 @@ import {
MemoryOverviewService,
NATIVE_EXECUTABLE_CODE_OVERHEAD,
} from '../memory_overview/memory_overview_service';
import { TrainedModelDeploymentStatsResponse } from '../../../common/types/trained_models';

export type ModelService = ReturnType<typeof modelsProvider>;

Expand Down Expand Up @@ -77,15 +78,15 @@ export function modelsProvider(
throw new Error('Memory overview service is not provided');
}

const { body: deploymentStats } = await mlClient.getTrainedModelsDeploymentStats();
const { body: deploymentStats } = await mlClient.getTrainedModelDeploymentStats({
model_id: '*',
});

const {
body: { nodes: clusterNodes },
} = await client.asCurrentUser.nodes.stats();

const mlNodes = Object.entries(clusterNodes).filter(([id, node]) =>
node.roles.includes('ml')
);
const mlNodes = Object.entries(clusterNodes).filter(([, node]) => node.roles.includes('ml'));

const adMemoryReport = await memoryOverviewService.getAnomalyDetectionMemoryOverview();
const dfaMemoryReport = await memoryOverviewService.getDFAMemoryOverview();
Expand All @@ -94,7 +95,9 @@ export function modelsProvider(
([nodeId, node]) => {
const nodeFields = pick(node, NODE_FIELDS) as RequiredNodeFields;

const allocatedModels = deploymentStats.deployment_stats
const allocatedModels = (
deploymentStats.deployment_stats as TrainedModelDeploymentStatsResponse[]
)
.filter((v) => v.nodes.some((n) => Object.keys(n.node)[0] === nodeId))
.map(({ nodes, ...rest }) => {
const { node: tempNode, ...nodeRest } = nodes.find(
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/ml/server/routes/trained_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export function trainedModelsRoutes({ router, routeGuard }: RouteInitialization)
routeGuard.fullLicenseAPIGuard(async ({ mlClient, request, response }) => {
try {
const { modelId } = request.params;
const { body } = await mlClient.getTrainedModelsDeploymentStats({
const { body } = await mlClient.getTrainedModelDeploymentStats({
model_id: modelId,
});
return response.ok({
Expand Down

0 comments on commit d635dae

Please sign in to comment.