Skip to content

Commit f7faa87

Browse files
committed
wip: uncomplete type refactoring
1 parent bd55dd3 commit f7faa87

File tree

4 files changed

+40
-37
lines changed

4 files changed

+40
-37
lines changed

x-pack/platform/packages/shared/kbn-slo-schema/src/rest_specs/routes/find_definition.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ const findSloDefinitionsParamsSchema = t.partial({
2121

2222
const healthMetadataSchema = t.partial({
2323
health: t.type({
24-
overall: transformHealthSchema,
25-
rollup: healthStatusSchema,
26-
summary: healthStatusSchema,
24+
overall: healthStatusSchema,
25+
rollup: transformHealthSchema,
26+
summary: transformHealthSchema,
2727
}),
2828
});
2929

x-pack/platform/packages/shared/kbn-slo-schema/src/schema/health.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,12 @@ import * as t from 'io-ts';
1414
* If types need to diverge, they should be split into separate files.
1515
*/
1616

17-
const transformHealthSchema = t.union([
18-
t.literal('healthy'),
19-
t.literal('unhealthy'),
20-
t.literal('missing'),
21-
]);
17+
const healthStatusSchema = t.union([t.literal('healthy'), t.literal('unhealthy')]);
2218

23-
const healthStatusSchema = t.intersection([
24-
t.partial({
25-
transformState: t.union([t.literal('stopped'), t.literal('started')]),
26-
}),
27-
t.type({
28-
status: transformHealthSchema,
29-
}),
30-
]);
19+
const transformHealthSchema = t.type({
20+
status: t.union([t.literal('healthy'), t.literal('unhealthy'), t.literal('missing')]),
21+
state: t.union([t.literal('stopped'), t.literal('started'), t.literal('unavailable')]),
22+
});
3123

3224
const stateSchema = t.union([
3325
t.literal('no_data'),

x-pack/solutions/observability/plugins/slo/server/domain/services/compute_health.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface Item {
2424
instanceId?: string;
2525
revision: number;
2626
name: string;
27+
enabled: boolean;
2728
}
2829

2930
interface Dependencies {
@@ -37,9 +38,9 @@ export interface SLOHealth {
3738
sloName: string;
3839
state: State;
3940
health: {
40-
overall: TransformHealth;
41-
rollup: HealthStatus;
42-
summary: HealthStatus;
41+
overall: HealthStatus;
42+
rollup: TransformHealth;
43+
summary: TransformHealth;
4344
};
4445
}
4546

@@ -49,11 +50,11 @@ const STALE_THRESHOLD_MINUTES = 2 * 24 * 60;
4950
export async function computeHealth(list: Item[], deps: Dependencies): Promise<SLOHealth[]> {
5051
const [summaryDocsById, transformStatsById] = await Promise.all([
5152
getSummaryDocsById(list, deps),
52-
getTransformStats(list, deps),
53+
getTransformStatsById(list, deps),
5354
]);
5455

5556
return list.map((item) => {
56-
const health = computeTransformHealth(transformStatsById, item);
57+
const health = computeTransformsHealth(transformStatsById, item);
5758
const state = computeTransformState(summaryDocsById, item);
5859

5960
return {
@@ -92,7 +93,7 @@ async function getSummaryDocsById(list: Item[], deps: Dependencies) {
9293
return summaryDocsById;
9394
}
9495

95-
async function getTransformStats(
96+
async function getTransformStatsById(
9697
list: Item[],
9798
deps: Dependencies
9899
): Promise<Dictionary<TransformGetTransformStatsTransformStats>> {
@@ -144,38 +145,47 @@ function computeTransformState(
144145

145146
function getTransformHealth(
146147
transformStat?: TransformGetTransformStatsTransformStats
147-
): HealthStatus {
148+
): TransformHealth {
148149
if (!transformStat) {
149150
return {
150151
status: 'missing',
152+
state: 'unavailable',
151153
};
152154
}
155+
156+
const transformStatus = transformStat.health?.status?.toLowerCase();
153157
const transformState = transformStat.state?.toLowerCase();
154-
return transformStat.health?.status?.toLowerCase() === 'green'
155-
? {
156-
status: 'healthy',
157-
transformState: transformState as HealthStatus['transformState'],
158-
}
159-
: {
160-
status: 'unhealthy',
161-
transformState: transformState as HealthStatus['transformState'],
162-
};
158+
159+
return {
160+
status: transformStatus === 'green' ? 'healthy' : 'unhealthy',
161+
state:
162+
transformState === 'started'
163+
? 'started'
164+
: transformState === 'stopped'
165+
? 'stopped'
166+
: 'unavailable',
167+
};
163168
}
164169

165-
function computeTransformHealth(
170+
function computeTransformsHealth(
166171
transformStatsById: Dictionary<TransformGetTransformStatsTransformStats>,
167172
item: Item
168-
): { overall: 'healthy' | 'unhealthy'; rollup: HealthStatus; summary: HealthStatus } {
173+
): { overall: HealthStatus; rollup: TransformHealth; summary: TransformHealth } {
169174
const rollup = getTransformHealth(transformStatsById[getSLOTransformId(item.id, item.revision)]);
170175
const summary = getTransformHealth(
171176
transformStatsById[getSLOSummaryTransformId(item.id, item.revision)]
172177
);
173178

174-
const overall: 'healthy' | 'unhealthy' =
179+
const rollupStateMatchesSloEnabled =
180+
(item.enabled && rollup.state === 'started') || (!item.enabled && rollup.state === 'stopped');
181+
const summaryStateMatchesSloEnabled =
182+
(item.enabled && rollup.state === 'started') || (!item.enabled && rollup.state === 'stopped');
183+
184+
const overall: HealthStatus =
175185
rollup.status === 'healthy' &&
176-
rollup.transformState === 'started' &&
186+
rollupStateMatchesSloEnabled &&
177187
summary.status === 'healthy' &&
178-
summary.transformState === 'started'
188+
summaryStateMatchesSloEnabled
179189
? 'healthy'
180190
: 'unhealthy';
181191

x-pack/solutions/observability/plugins/slo/server/services/get_slo_health.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class GetSLOHealth {
3030
instanceId: item.sloInstanceId,
3131
revision: definitionById[item.sloId].revision,
3232
name: definitionById[item.sloId].name,
33+
enabled: definitionById[item.sloId].enabled,
3334
}));
3435

3536
const results = await computeHealth(list, { scopedClusterClient: this.scopedClusterClient });

0 commit comments

Comments
 (0)