Skip to content

Commit

Permalink
Fixing things broken by the merge
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed Sep 27, 2024
1 parent bd1dfd2 commit 1698dd1
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

import { z } from '@kbn/zod';
import {
IngestGetPipelineResponse,
IndicesGetIndexTemplateIndexTemplateItem,
TransformGetTransformStatsTransformStats,
TransformGetTransformTransformSummary,
} from '@elastic/elasticsearch/lib/api/types';
import { BooleanFromString } from '@kbn/zod-helpers';
import { entityDefinitionSchema } from '../../schema/entity_definition';

export const findEntityDefinitionQuerySchema = z.object({
page: z.optional(z.coerce.number()),
perPage: z.optional(z.coerce.number()),
includeState: z.optional(BooleanFromString).default(false),
});

export type FindEntityDefinitionQuery = z.infer<typeof findEntityDefinitionQuerySchema>;
Expand All @@ -24,10 +26,6 @@ export const entitiyDefinitionWithStateSchema = entityDefinitionSchema.extend({
state: z.object({
installed: z.boolean(),
running: z.boolean(),
avgCheckpointDuration: z.object({
history: z.number().or(z.null()),
latest: z.number().or(z.null()),
}),
}),
stats: z.object({
entityCount: z.number(),
Expand All @@ -36,17 +34,34 @@ export const entitiyDefinitionWithStateSchema = entityDefinitionSchema.extend({
}),
});

interface IngestPipelineState {
id: string;
installed: boolean;
stats: {
count: number;
failed: number;
};
}

interface IndexTemplateState {
id: string;
installed: boolean;
template: IndicesGetIndexTemplateIndexTemplateItem;
}

interface TransformState {
id: string;
installed: boolean;
running: boolean;
summary: TransformGetTransformTransformSummary;
stats: TransformGetTransformStatsTransformStats;
}

export type EntityDefinitionWithState = z.infer<typeof entitiyDefinitionWithStateSchema> & {
resources: {
ingestPipelines: IngestGetPipelineResponse;
transforms: {
history?: TransformGetTransformTransformSummary;
latest?: TransformGetTransformTransformSummary;
stats: {
history?: TransformGetTransformStatsTransformStats;
latest?: TransformGetTransformStatsTransformStats;
};
};
ingestPipelines: IngestPipelineState[];
indexTemplates: IndexTemplateState[];
transforms: TransformState[];
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import {
ENTITY_BASE_PREFIX,
ENTITY_HISTORY,
ENTITY_LATEST,
ENTITY_SCHEMA_VERSION_V1,
EntityDefinition,
entitiesIndexPattern,
} from '@kbn/entities-schema';
import { ENTITY_HISTORY_PREFIX_V1, ENTITY_LATEST_PREFIX_V1 } from '../constants_entities';

// History
function generateHistoryId(definition: EntityDefinition) {
return `${ENTITY_HISTORY_PREFIX_V1}-${definition.id}` as const;
}

// History Backfill
export function generateHistoryBackfillTransformId(definition: EntityDefinition) {
return `${ENTITY_HISTORY_PREFIX_V1}-backfill-${definition.id}` as const;
}

export const generateHistoryTransformId = generateHistoryId;
export const generateHistoryIngestPipelineId = generateHistoryId;

export function generateHistoryIndexName(definition: EntityDefinition) {
return entitiesIndexPattern({
schemaVersion: ENTITY_SCHEMA_VERSION_V1,
dataset: ENTITY_HISTORY,
definitionId: definition.id,
});
}

export function generateHistoryIndexTemplateId(definition: EntityDefinition) {
return `${ENTITY_BASE_PREFIX}_${ENTITY_SCHEMA_VERSION_V1}_${ENTITY_HISTORY}_${definition.id}_index_template` as const;
}

// Latest
function generateLatestId(definition: EntityDefinition) {
return `${ENTITY_LATEST_PREFIX_V1}-${definition.id}` as const;
}

export const generateLatestTransformId = generateLatestId;
export const generateLatestIngestPipelineId = generateLatestId;

export function generateLatestIndexName(definition: EntityDefinition) {
return entitiesIndexPattern({
schemaVersion: ENTITY_SCHEMA_VERSION_V1,
dataset: ENTITY_LATEST,
definitionId: definition.id,
});
}

export const generateLatestIndexTemplateId = (definition: EntityDefinition) =>
`${ENTITY_BASE_PREFIX}_${ENTITY_SCHEMA_VERSION_V1}_${ENTITY_LATEST}_${definition.id}_index_template` as const;
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ import { i18n } from '@kbn/i18n';
import numeral from '@elastic/numeral';
import { EuiStat } from '@elastic/eui';
import { DefinitionStatProps } from './types';
import { generateHistoryTransformId } from '../../../common/helpers/generate_component_id';

export function HistoryCheckpointDurationStat({
definition,
textAlign,
titleSize,
}: DefinitionStatProps) {
const transformState = definition.resources.transforms.find(
(doc) => doc.id === generateHistoryTransformId(definition)
);
const value =
transformState != null
? numeral(transformState.stats.stats.exponential_avg_checkpoint_duration_ms).format('0,0') +
'ms'
: 'N/A';

return (
<EuiStat
titleSize={titleSize}
title={`${
numeral(definition.state.avgCheckpointDuration.history).format('0,0') + 'ms' || 'N/A'
}`}
title={value}
textAlign={textAlign}
description={i18n.translate(
'xpack.entityManager.definitionStat.historyCheckpointDuration.label',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ import { i18n } from '@kbn/i18n';
import numeral from '@elastic/numeral';
import { EuiStat } from '@elastic/eui';
import { DefinitionStatProps } from './types';
import { generateLatestTransformId } from '../../../common/helpers/generate_component_id';

export function LatestCheckpointDurationStat({
definition,
textAlign,
titleSize,
}: DefinitionStatProps) {
const transformState = definition.resources.transforms.find(
(doc) => doc.id === generateLatestTransformId(definition)
);
const value =
transformState != null
? numeral(transformState.stats.stats.exponential_avg_checkpoint_duration_ms).format('0,0') +
'ms'
: 'N/A';
return (
<EuiStat
titleSize={titleSize}
title={`${
numeral(definition.state.avgCheckpointDuration.latest).format('0,0') + 'ms' || 'N/A'
}`}
title={value}
textAlign={textAlign}
description={i18n.translate(
'xpack.entityManager.definitionStat.latestCheckpointDuration.label',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export function useFetchEntityDefinitions() {
try {
const response = await http.get<EntityDefintionResponse>('/internal/entities/definition', {
signal,
query: {
includeState: true,
},
});
return response.definitions;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ function Listing({ definition }: ListingProps) {
} = useKibana().services;
const entityDetailUrl = basePath.prepend(paths.entitieDetail(definition.id));

const checkpointDuration = definition.resources.transforms.reduce(
(acc, transformState) => {
if (transformState.stats.stats.exponential_avg_checkpoint_duration_ms > acc.duration) {
return {
duration: transformState.stats.stats.exponential_avg_checkpoint_duration_ms,
id: transformState.id,
};
}
return acc;
},
{ duration: 0, id: '' }
);

return (
<EuiPanel hasBorder>
<EuiFlexGroup alignItems="center">
Expand Down Expand Up @@ -55,16 +68,12 @@ function Listing({ definition }: ListingProps) {
<EuiFlexItem grow={0}>
<EuiStat
titleSize="s"
title={`${
numeral(definition.state.avgCheckpointDuration.history).format('0,0') + 'ms' || 'N/A'
} | ${
numeral(definition.state.avgCheckpointDuration.latest).format('0,0') + 'ms' || 'N/A'
}`}
title={`${numeral(checkpointDuration.duration).format('0,0') + 'ms' || 'N/A'}`}
textAlign="right"
description={i18n.translate(
'xpack.entityManager.listing.historyCheckpointDuration.label',
{
defaultMessage: 'Avg. Checkpoint Duration',
defaultMessage: 'Checkpoint Duration',
}
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import numeral from '@elastic/numeral';
import { TransformHealthBadge } from './transform_health_badge';
import { TransformStatusBadge } from './transform_status_badge';
import { TransformContent } from './transform_content';
import {
generateHistoryTransformId,
generateLatestTransformId,
} from '../../../../common/helpers/generate_component_id';

interface TransformProps {
type: 'history' | 'latest';
Expand All @@ -28,9 +32,16 @@ const HISTORY_TITLE = i18n.translate('xpack.entityManager.transfrom.historyTitle
});

export function Transform({ definition, type }: TransformProps) {
const title = type === 'history' ? HISTORY_TITLE : LATEST_TITLE;
const transform = definition.resources.transforms[type];
const stats = definition.resources.transforms.stats[type];
const isHistory = type === 'history';
const title = isHistory ? HISTORY_TITLE : LATEST_TITLE;

const id = isHistory
? generateHistoryTransformId(definition)
: generateLatestTransformId(definition);

const transformState = definition.resources.transforms.find((doc) => doc.id === id);
const transform = transformState?.summary;
const stats = transformState?.stats;

if (!transform || !stats) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import {
generateLatestIndexTemplateId,
} from './helpers/generate_component_id';
import { BUILT_IN_ID_PREFIX } from './built_in';
import { EntityDefinitionState, EntityDefinitionWithState } from './types';
import { EntityDefinitionWithState } from './types';
import { isBackfillEnabled } from './helpers/is_backfill_enabled';
import { getEntityDefinitionStats } from './get_entity_definition_stats';

export async function findEntityDefinitions({
soClient,
Expand Down Expand Up @@ -59,8 +60,14 @@ export async function findEntityDefinitions({

return Promise.all(
response.saved_objects.map(async ({ attributes }) => {
const state = await getEntityDefinitionState(esClient, attributes);
return { ...attributes, state };
const { state, resources } = await getEntityDefinitionState(esClient, attributes);
const stats = await getEntityDefinitionStats(esClient, attributes);
return {
...attributes,
state,
resources,
stats,
};
})
);
}
Expand Down Expand Up @@ -90,7 +97,7 @@ export async function findEntityDefinitionById({
async function getEntityDefinitionState(
esClient: ElasticsearchClient,
definition: EntityDefinition
): Promise<EntityDefinitionState> {
) {
const [ingestPipelines, transforms, indexTemplates] = await Promise.all([
getIngestPipelineState({ definition, esClient }),
getTransformState({ definition, esClient }),
Expand All @@ -104,9 +111,8 @@ async function getEntityDefinitionState(
const running = transforms.every((transform) => transform.running);

return {
installed,
running,
components: { transforms, ingestPipelines, indexTemplates },
state: { installed, running },
resources: { transforms, ingestPipelines, indexTemplates },
};
}

Expand All @@ -127,16 +133,23 @@ async function getTransformState({
transformIds.map((id) => esClient.transform.getTransformStats({ transform_id: id }))
).then((results) => results.map(({ transforms }) => transforms).flat());

const transformSummaries = await Promise.all(
transformIds.map((id) => esClient.transform.getTransform({ transform_id: id }))
).then((results) => results.map(({ transforms }) => transforms).flat());

return transformIds.map((id) => {
const summary = transformSummaries.find((transform) => transform.id === id);
const stats = transformStats.find((transform) => transform.id === id);
if (!stats) {

if (!stats || !summary) {
return { id, installed: false, running: false };
}

return {
id,
installed: true,
running: stats.state === 'started' || stats.state === 'indexing',
summary,
stats,
};
});
Expand Down Expand Up @@ -209,7 +222,7 @@ async function getIndexTemplatesState({
return {
id,
installed: true,
stats: template.index_template,
template: template.index_template,
};
});
}
Loading

0 comments on commit 1698dd1

Please sign in to comment.