Skip to content

Commit

Permalink
Further abstract module management and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort committed Nov 28, 2019
1 parent e7c4166 commit 1d16d06
Show file tree
Hide file tree
Showing 29 changed files with 544 additions and 431 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export * from './indices';
export * from './log_entry_rate_indices';
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@

import * as rt from 'io-ts';

export const LOG_ANALYSIS_VALIDATION_INDICES_PATH = '/api/infra/log_analysis/validation/indices';
export const LOG_ANALYSIS_VALIDATE_INDICES_PATH =
'/api/infra/log_analysis/validation/log_entry_rate_indices';

/**
* Request types
*/
export const validationIndicesFieldSpecificationRT = rt.type({
name: rt.string,
validTypes: rt.array(rt.string),
});

export type ValidationIndicesFieldSpecification = rt.TypeOf<
typeof validationIndicesFieldSpecificationRT
>;

export const validationIndicesRequestPayloadRT = rt.type({
data: rt.type({
timestampField: rt.string,
fields: rt.array(validationIndicesFieldSpecificationRT),
indices: rt.array(rt.string),
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import * as rt from 'io-ts';

export const bucketSpan = 900000;

export const partitionField = 'event.dataset';

export const getJobIdPrefix = (spaceId: string, sourceId: string) =>
`kibana-logs-ui-${spaceId}-${sourceId}-`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,24 @@ export const jobCustomSettingsRT = rt.partial({
job_revision: rt.number,
logs_source_config: rt.partial(jobSourceConfigurationRT.props),
});

export const getMlCapabilitiesResponsePayloadRT = rt.type({
capabilities: rt.type({
canGetJobs: rt.boolean,
canCreateJob: rt.boolean,
canDeleteJob: rt.boolean,
canOpenJob: rt.boolean,
canCloseJob: rt.boolean,
canForecastJob: rt.boolean,
canGetDatafeeds: rt.boolean,
canStartStopDatafeed: rt.boolean,
canUpdateJob: rt.boolean,
canUpdateDatafeed: rt.boolean,
canPreviewDatafeed: rt.boolean,
}),
isPlatinumOrTrialLicense: rt.boolean,
mlFeatureEnabledInSpace: rt.boolean,
upgradeInProgress: rt.boolean,
});

export type GetMlCapabilitiesResponsePayload = rt.TypeOf<typeof getMlCapabilitiesResponsePayloadRT>;
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export const deleteJobsResponsePayloadRT = rt.record(
})
);

export type DeleteJobsResponsePayload = rt.TypeOf<typeof deleteJobsResponsePayloadRT>;

export const getJobDeletionTasksResponsePayloadRT = rt.type({
jobIds: rt.array(rt.string),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ import { identity } from 'fp-ts/lib/function';
import { kfetch } from 'ui/kfetch';

import {
LOG_ANALYSIS_VALIDATION_INDICES_PATH,
LOG_ANALYSIS_VALIDATE_INDICES_PATH,
ValidationIndicesFieldSpecification,
validationIndicesRequestPayloadRT,
validationIndicesResponsePayloadRT,
} from '../../../../../common/http_api';

import { throwErrors, createPlainError } from '../../../../../common/runtime_types';

export const callIndexPatternsValidate = async (timestampField: string, indices: string[]) => {
export const callValidateIndicesAPI = async (
indices: string[],
fields: ValidationIndicesFieldSpecification[]
) => {
const response = await kfetch({
method: 'POST',
pathname: LOG_ANALYSIS_VALIDATION_INDICES_PATH,
body: JSON.stringify(
validationIndicesRequestPayloadRT.encode({ data: { timestampField, indices } })
),
pathname: LOG_ANALYSIS_VALIDATE_INDICES_PATH,
body: JSON.stringify(validationIndicesRequestPayloadRT.encode({ data: { indices, fields } })),
});

return pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

export * from './log_analysis_capabilities';
export * from './log_analysis_cleanup';
export * from './log_analysis_jobs';
export * from './log_analysis_status_state';
export * from './log_analysis_module';
export * from './log_analysis_module_status';
export * from './log_analysis_module_types';
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useTrackedPromise } from '../../../utils/use_tracked_promise';
import {
getMlCapabilitiesResponsePayloadRT,
GetMlCapabilitiesResponsePayload,
} from './ml_api_types';
} from './api/ml_api_types';
import { throwErrors, createPlainError } from '../../../../common/runtime_types';

export const useLogAnalysisCapabilities = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,24 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { useMemo } from 'react';

import { getJobId } from '../../../../common/log_analysis';
import { useTrackedPromise } from '../../../utils/use_tracked_promise';
import { callDeleteJobs, callGetJobDeletionTasks, callStopDatafeeds } from './api/ml_cleanup';

export const useLogAnalysisCleanup = <JobType extends string>({
sourceId,
spaceId,
jobTypes,
}: {
sourceId: string;
spaceId: string;
jobTypes: JobType[];
}) => {
const [cleanupMLResourcesRequest, cleanupMLResources] = useTrackedPromise(
{
cancelPreviousOn: 'resolution',
createPromise: async () => {
try {
await callStopDatafeeds(spaceId, sourceId, jobTypes);
} catch (err) {
// Proceed only if datafeed has been deleted or didn't exist in the first place
if (err?.res?.status !== 404) {
throw err;
}
}

return await deleteJobs(spaceId, sourceId, jobTypes);
},
},
[spaceId, sourceId]
);

const isCleaningUp = useMemo(() => cleanupMLResourcesRequest.state === 'pending', [
cleanupMLResourcesRequest.state,
]);
export const cleanUpJobsAndDatafeeds = async <JobType extends string>(
spaceId: string,
sourceId: string,
jobTypes: JobType[]
) => {
try {
await callStopDatafeeds(spaceId, sourceId, jobTypes);
} catch (err) {
// Proceed only if datafeed has been deleted or didn't exist in the first place
if (err?.res?.status !== 404) {
throw err;
}
}

return {
cleanupMLResources,
isCleaningUp,
};
return await deleteJobs(spaceId, sourceId, jobTypes);
};

const deleteJobs = async <JobType extends string>(
Expand Down

This file was deleted.

Loading

0 comments on commit 1d16d06

Please sign in to comment.