Skip to content

Commit

Permalink
[Entity Analytics] Remove usage of KibanaServices singleton for routes (
Browse files Browse the repository at this point in the history
  • Loading branch information
hop-dev authored Dec 14, 2023
1 parent 72f2eff commit 126f45c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 88 deletions.
152 changes: 76 additions & 76 deletions x-pack/plugins/security_solution/public/entity_analytics/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ASSET_CRITICALITY_PRIVILEGES_URL,
} from '../../../common/constants';

import { KibanaServices } from '../../common/lib/kibana';
import type {
CalculateScoresResponse,
EnableRiskEngineResponse,
Expand All @@ -25,89 +24,90 @@ import type {
} from '../../../server/lib/entity_analytics/types';
import type { RiskScorePreviewRequestSchema } from '../../../common/entity_analytics/risk_engine/risk_score_preview/request_schema';
import type { EntityAnalyticsPrivileges } from '../../../common/api/entity_analytics/common';
import { useKibana } from '../../common/lib/kibana/kibana_react';

/**
* Fetches preview risks scores
*/
export const fetchRiskScorePreview = async ({
signal,
params,
}: {
signal?: AbortSignal;
params: RiskScorePreviewRequestSchema;
}): Promise<CalculateScoresResponse> => {
return KibanaServices.get().http.fetch<CalculateScoresResponse>(RISK_SCORE_PREVIEW_URL, {
version: '1',
method: 'POST',
body: JSON.stringify(params),
signal,
});
};
export const useEntityAnalyticsRoutes = () => {
const http = useKibana().services.http;

/**
* Fetches risks engine status
*/
export const fetchRiskEngineStatus = async ({
signal,
}: {
signal?: AbortSignal;
}): Promise<GetRiskEngineStatusResponse> => {
return KibanaServices.get().http.fetch<GetRiskEngineStatusResponse>(RISK_ENGINE_STATUS_URL, {
version: '1',
method: 'GET',
/**
* Fetches preview risks scores
*/
const fetchRiskScorePreview = ({
signal,
});
};
params,
}: {
signal?: AbortSignal;
params: RiskScorePreviewRequestSchema;
}) =>
http.fetch<CalculateScoresResponse>(RISK_SCORE_PREVIEW_URL, {
version: '1',
method: 'POST',
body: JSON.stringify(params),
signal,
});

/**
* Init risk score engine
*/
export const initRiskEngine = async (): Promise<InitRiskEngineResponse> => {
return KibanaServices.get().http.fetch<InitRiskEngineResponse>(RISK_ENGINE_INIT_URL, {
version: '1',
method: 'POST',
});
};
/**
* Fetches risks engine status
*/
const fetchRiskEngineStatus = ({ signal }: { signal?: AbortSignal }) =>
http.fetch<GetRiskEngineStatusResponse>(RISK_ENGINE_STATUS_URL, {
version: '1',
method: 'GET',
signal,
});

/**
* Enable risk score engine
*/
export const enableRiskEngine = async (): Promise<EnableRiskEngineResponse> => {
return KibanaServices.get().http.fetch<EnableRiskEngineResponse>(RISK_ENGINE_ENABLE_URL, {
version: '1',
method: 'POST',
});
};
/**
* Init risk score engine
*/
const initRiskEngine = () =>
http.fetch<InitRiskEngineResponse>(RISK_ENGINE_INIT_URL, {
version: '1',
method: 'POST',
});

/**
* Disable risk score engine
*/
export const disableRiskEngine = async (): Promise<DisableRiskEngineResponse> => {
return KibanaServices.get().http.fetch<DisableRiskEngineResponse>(RISK_ENGINE_DISABLE_URL, {
version: '1',
method: 'POST',
});
};
/**
* Enable risk score engine
*/
const enableRiskEngine = () =>
http.fetch<EnableRiskEngineResponse>(RISK_ENGINE_ENABLE_URL, {
version: '1',
method: 'POST',
});

/**
* Get risk engine privileges
*/
export const fetchRiskEnginePrivileges = async (): Promise<EntityAnalyticsPrivileges> => {
return KibanaServices.get().http.fetch<EntityAnalyticsPrivileges>(RISK_ENGINE_PRIVILEGES_URL, {
version: '1',
method: 'GET',
});
};
/**
* Disable risk score engine
*/
const disableRiskEngine = () =>
http.fetch<DisableRiskEngineResponse>(RISK_ENGINE_DISABLE_URL, {
version: '1',
method: 'POST',
});

/**
* Get asset criticality privileges
*/
export const fetchAssetCriticalityPrivileges = async (): Promise<EntityAnalyticsPrivileges> => {
return KibanaServices.get().http.fetch<EntityAnalyticsPrivileges>(
ASSET_CRITICALITY_PRIVILEGES_URL,
{
/**
* Get risk engine privileges
*/
const fetchRiskEnginePrivileges = () =>
http.fetch<EntityAnalyticsPrivileges>(RISK_ENGINE_PRIVILEGES_URL, {
version: '1',
method: 'GET',
});

/**
* Get asset criticality privileges
*/
const fetchAssetCriticalityPrivileges = () =>
http.fetch<EntityAnalyticsPrivileges>(ASSET_CRITICALITY_PRIVILEGES_URL, {
version: '1',
method: 'GET',
}
);
});

return {
fetchRiskScorePreview,
fetchRiskEngineStatus,
initRiskEngine,
enableRiskEngine,
disableRiskEngine,
fetchRiskEnginePrivileges,
fetchAssetCriticalityPrivileges,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* 2.0.
*/
import { useQuery } from '@tanstack/react-query';
import { fetchAssetCriticalityPrivileges } from '../api';
import { useEntityAnalyticsRoutes } from '../api';

export const useAssetCriticalityPrivileges = () => {
const { fetchAssetCriticalityPrivileges } = useEntityAnalyticsRoutes();
return useQuery(['GET', 'FETCH_ASSET_CRITICALITY_PRIVILEGES'], fetchAssetCriticalityPrivileges);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import type { UseMutationOptions } from '@tanstack/react-query';
import { useMutation } from '@tanstack/react-query';
import { disableRiskEngine } from '../api';
import { useEntityAnalyticsRoutes } from '../api';
import { useInvalidateRiskEngineStatusQuery } from './use_risk_engine_status';
import type {
EnableRiskEngineResponse,
Expand All @@ -17,6 +17,7 @@ export const DISABLE_RISK_ENGINE_MUTATION_KEY = ['POST', 'DISABLE_RISK_ENGINE'];

export const useDisableRiskEngineMutation = (options?: UseMutationOptions<{}>) => {
const invalidateRiskEngineStatusQuery = useInvalidateRiskEngineStatusQuery();
const { disableRiskEngine } = useEntityAnalyticsRoutes();

return useMutation<EnableRiskEngineResponse, EnableDisableRiskEngineErrorResponse>(
() => disableRiskEngine(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import type { UseMutationOptions } from '@tanstack/react-query';
import { useMutation } from '@tanstack/react-query';
import { enableRiskEngine } from '../api';
import { useEntityAnalyticsRoutes } from '../api';
import { useInvalidateRiskEngineStatusQuery } from './use_risk_engine_status';
import type {
EnableRiskEngineResponse,
Expand All @@ -16,9 +16,9 @@ export const ENABLE_RISK_ENGINE_MUTATION_KEY = ['POST', 'ENABLE_RISK_ENGINE'];

export const useEnableRiskEngineMutation = (options?: UseMutationOptions<{}>) => {
const invalidateRiskEngineStatusQuery = useInvalidateRiskEngineStatusQuery();

const { enableRiskEngine } = useEntityAnalyticsRoutes();
return useMutation<EnableRiskEngineResponse, EnableDisableRiskEngineErrorResponse>(
() => enableRiskEngine(),
enableRiskEngine,
{
...options,
mutationKey: ENABLE_RISK_ENGINE_MUTATION_KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import type { UseMutationOptions } from '@tanstack/react-query';
import { useMutation } from '@tanstack/react-query';
import { initRiskEngine } from '../api';
import { useEntityAnalyticsRoutes } from '../api';
import { useInvalidateRiskEngineStatusQuery } from './use_risk_engine_status';
import type {
InitRiskEngineResponse,
Expand All @@ -17,7 +17,7 @@ export const INIT_RISK_ENGINE_STATUS_KEY = ['POST', 'INIT_RISK_ENGINE'];

export const useInitRiskEngineMutation = (options?: UseMutationOptions<{}>) => {
const invalidateRiskEngineStatusQuery = useInvalidateRiskEngineStatusQuery();

const { initRiskEngine } = useEntityAnalyticsRoutes();
return useMutation<InitRiskEngineResponse, InitRiskEngineError>(() => initRiskEngine(), {
...options,
mutationKey: INIT_RISK_ENGINE_STATUS_KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
*/
import { useQuery } from '@tanstack/react-query';
import dateMath from '@kbn/datemath';
import { fetchRiskScorePreview } from '../api';
import { useEntityAnalyticsRoutes } from '../api';
import type { RiskScorePreviewRequestSchema } from '../../../../common/entity_analytics/risk_engine/risk_score_preview/request_schema';

export const useRiskScorePreview = ({
data_view_id: dataViewId,
range,
filter,
}: RiskScorePreviewRequestSchema) => {
const { fetchRiskScorePreview } = useEntityAnalyticsRoutes();

return useQuery(['POST', 'FETCH_PREVIEW_RISK_SCORE', range, filter], async ({ signal }) => {
const params: RiskScorePreviewRequestSchema = { data_view_id: dataViewId };

if (range) {
const startTime = dateMath.parse(range.start)?.utc().toISOString();
const endTime = dateMath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* 2.0.
*/
import { useQuery } from '@tanstack/react-query';
import { fetchRiskEnginePrivileges } from '../api';
import { useEntityAnalyticsRoutes } from '../api';

export const useRiskEnginePrivileges = () => {
const { fetchRiskEnginePrivileges } = useEntityAnalyticsRoutes();
return useQuery(['GET', 'FETCH_RISK_ENGINE_PRIVILEGES'], fetchRiskEnginePrivileges);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useCallback } from 'react';
import { fetchRiskEngineStatus } from '../api';
import { useEntityAnalyticsRoutes } from '../api';
import { RiskEngineStatus } from '../../../../common/entity_analytics/risk_engine/types';
import { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features';
const FETCH_RISK_ENGINE_STATUS = ['GET', 'FETCH_RISK_ENGINE_STATUS'];
Expand All @@ -29,7 +29,7 @@ export const useIsNewRiskScoreModuleInstalled = () => {

export const useRiskEngineStatus = () => {
const isNewRiskScoreModuleAvailable = useIsExperimentalFeatureEnabled('riskScoringRoutesEnabled');

const { fetchRiskEngineStatus } = useEntityAnalyticsRoutes();
return useQuery(FETCH_RISK_ENGINE_STATUS, async ({ signal }) => {
if (!isNewRiskScoreModuleAvailable) {
return {
Expand Down

0 comments on commit 126f45c

Please sign in to comment.