Skip to content

Commit

Permalink
getUnhealthyTransformsReport
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Mar 1, 2023
1 parent 391220a commit 93dd0e7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,28 @@ import { PLUGIN, TRANSFORM_RULE_TYPE } from '../../../../common/constants';
import { transformHealthRuleParams, TransformHealthRuleParams } from './schema';
import { transformHealthServiceProvider } from './transform_health_service';

export interface BaseResponse {
export interface TransformHealth {
status: 'green' | 'unknown' | 'yellow' | 'red';
issues?: Array<{ issue: string; details?: string; count: number; first_occurrence?: string }>;
}

export interface BaseTransformAlertResponse {
transform_id: string;
description?: string;
health: TransformHealth;
}

export interface NotStartedTransformResponse extends BaseTransformAlertResponse {
transform_state: string;
node_name?: string;
}

export interface NotStartedTransformResponse extends BaseResponse {
export interface UnhealthyTransformResponse extends BaseTransformAlertResponse {
transform_state: string;
node_name?: string;
}

export interface ErrorMessagesTransformResponse extends BaseResponse {
export interface ErrorMessagesTransformResponse extends BaseTransformAlertResponse {
error_messages: Array<{ message: string; timestamp: number; node_name?: string }>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import { ElasticsearchClient } from '@kbn/core/server';
import { i18n } from '@kbn/i18n';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { keyBy, partition } from 'lodash';
import { keyBy, memoize, partition } from 'lodash';
import type { RulesClient } from '@kbn/alerting-plugin/server';
import { type TransformGetTransformStatsTransformStats } from '@elastic/elasticsearch/lib/api/types';
import { TransformHealthRuleParams } from './schema';
import {
ALL_TRANSFORMS_SELECTION,
Expand All @@ -18,10 +19,12 @@ import {
TRANSFORM_STATE,
} from '../../../../common/constants';
import { getResultTestConfig } from '../../../../common/utils/alerts';
import {
import type {
ErrorMessagesTransformResponse,
NotStartedTransformResponse,
TransformHealth,
TransformHealthAlertContext,
UnhealthyTransformResponse,
} from './register_transform_health_rule_type';
import type { TransformHealthAlertRule } from '../../../../common/types/alerting';
import { isContinuousTransform } from '../../../../common/types/transform';
Expand All @@ -41,6 +44,13 @@ type Transform = estypes.TransformGetTransformTransformSummary & {

type TransformWithAlertingRules = Transform & { alerting_rules: TransformHealthAlertRule[] };

/**
* TODO update types in the es client
*/
type TransformGetTransformStats = TransformGetTransformStatsTransformStats & {
health: TransformHealth;
};

export function transformHealthServiceProvider(
esClient: ElasticsearchClient,
rulesClient?: RulesClient
Expand Down Expand Up @@ -90,6 +100,16 @@ export function transformHealthServiceProvider(
return resultTransformIds;
};

const getTransformStats = memoize(
async (transformIds: string[]): Promise<TransformGetTransformStats[]> => {
return (
await esClient.transform.getTransformStats({
transform_id: transformIds.join(','),
})
).transforms as TransformGetTransformStats[];
}
);

return {
/**
* Returns report about not started transforms
Expand All @@ -100,18 +120,15 @@ export function transformHealthServiceProvider(
async getTransformsStateReport(
transformIds: string[]
): Promise<[NotStartedTransformResponse[], NotStartedTransformResponse[]]> {
const transformsStats = (
await esClient.transform.getTransformStats({
transform_id: transformIds.join(','),
})
).transforms;
const transformsStats = await getTransformStats(transformIds);

return partition(
transformsStats.map((t) => ({
transform_id: t.id,
description: transformsDict.get(t.id)?.description,
transform_state: t.state,
node_name: t.node?.name,
health: t.health,
})),
(t) =>
t.transform_state !== TRANSFORM_STATE.STARTED &&
Expand Down Expand Up @@ -192,6 +209,27 @@ export function transformHealthServiceProvider(
})
.filter((v) => failedTransforms.has(v.transform_id));
},
/**
* Returns report about unhealthy transforms
* @param transformIds
*/
async getUnhealthyTransformsReport(
transformIds: string[]
): Promise<UnhealthyTransformResponse[]> {
const transformsStats = await getTransformStats(transformIds);

return transformsStats
.filter((t) => t.health.status !== 'green')
.map((t) => {
return {
transform_id: t.id,
transform_state: t.state,
node_name: t.node?.name,
description: transformsDict.get(t.id)?.description,
health: t.health,
};
});
},
/**
* Returns results of the transform health checks
* @param params
Expand Down

0 comments on commit 93dd0e7

Please sign in to comment.