Skip to content

Commit

Permalink
clean up code.
Browse files Browse the repository at this point in the history
  • Loading branch information
pjhampton committed Nov 18, 2021
1 parent 2c4aad4 commit 7b53cce
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
1 change: 0 additions & 1 deletion x-pack/plugins/security_solution/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ export class Plugin implements ISecuritySolutionPlugin {
});

initUsageCollectors({
core,
ml: plugins.ml,
usageCollection: plugins.usageCollection,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import { initialMlJobsUsage, updateMlJobsUsage } from './detection_ml_helpers';

describe('Security Machine Learning usage metrics', () => {
describe('Security ML Usage Metrics', () => {
describe('Updates metrics with job information', () => {
it('Should update ML total for elastic rules', async () => {
it('Should update ML total for elastic jobs', async () => {
const initialUsage = initialMlJobsUsage;
const isElastic = true;
const isEnabled = true;
Expand All @@ -30,7 +30,7 @@ describe('Security Machine Learning usage metrics', () => {
);
});

it('Should update ML total for custom rules', async () => {
it('Should update ML total for custom jobs', async () => {
const initialUsage = initialMlJobsUsage;
const isElastic = false;
const isEnabled = true;
Expand All @@ -51,7 +51,7 @@ describe('Security Machine Learning usage metrics', () => {
);
});

it('Should update ML total for both elastic and custom rules', async () => {
it('Should update ML total for both elastic and custom jobs', async () => {
const initialUsage = initialMlJobsUsage;

let updatedUsage = updateMlJobsUsage({ isElastic: true, isEnabled: true }, initialUsage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import { isJobStarted } from '../../../common/machine_learning/helpers';
import { isSecurityJob } from '../../../common/machine_learning/is_security_job';
import { DetectionsMetric, MlJobMetric, MlJobsUsage, MlJobUsage } from './types';

/**
* Default ml job usage count
*/
export const initialMlJobsUsage: MlJobsUsage = {
custom: {
enabled: 0,
Expand Down Expand Up @@ -66,17 +63,17 @@ export const updateMlJobsUsage = (jobMetric: DetectionsMetric, usage: MlJobsUsag

export const getMlJobMetrics = async (
ml: MlPluginSetup | undefined,
savedObjectClient: SavedObjectsClientContract
soClient: SavedObjectsClientContract
): Promise<MlJobUsage> => {
let jobsUsage: MlJobsUsage = initialMlJobsUsage;

if (ml) {
try {
const fakeRequest = { headers: {} } as KibanaRequest;

const modules = await ml.modulesProvider(fakeRequest, savedObjectClient).listModules();
const modules = await ml.modulesProvider(fakeRequest, soClient).listModules();
const moduleJobs = modules.flatMap((module) => module.jobs);
const jobs = await ml.jobServiceProvider(fakeRequest, savedObjectClient).jobsSummary();
const jobs = await ml.jobServiceProvider(fakeRequest, soClient).jobsSummary();

jobsUsage = jobs.filter(isSecurityJob).reduce((usage, job) => {
const isElastic = moduleJobs.some((moduleJob) => moduleJob.id === job.id);
Expand All @@ -87,18 +84,16 @@ export const getMlJobMetrics = async (

const jobsType = 'security';
const securityJobStats = await ml
.anomalyDetectorsProvider(fakeRequest, savedObjectClient)
.anomalyDetectorsProvider(fakeRequest, soClient)
.jobStats(jobsType);

const jobDetails = await ml
.anomalyDetectorsProvider(fakeRequest, savedObjectClient)
.jobs(jobsType);
const jobDetails = await ml.anomalyDetectorsProvider(fakeRequest, soClient).jobs(jobsType);

const jobDetailsCache = new Map<string, Job>();
jobDetails.jobs.forEach((detail) => jobDetailsCache.set(detail.job_id, detail));

const datafeedStats = await ml
.anomalyDetectorsProvider(fakeRequest, savedObjectClient)
.anomalyDetectorsProvider(fakeRequest, soClient)
.datafeedStats();

const datafeedStatsCache = new Map<string, DatafeedStats>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@

import { SavedObjectsClientContract } from 'kibana/server';
import { MlPluginSetup } from '../../../../ml/server';
import { getMlJobMetrics, initialMlJobsUsage } from './detection_ml_helpers';
import { DetectionMetrics } from './types';
import { getMlJobMetrics, initialMlJobsUsage } from './detection_ml_helpers';

export const fetchDetectionsMetrics = async (
soClient: SavedObjectsClientContract,
mlClient: MlPluginSetup | undefined
): Promise<DetectionMetrics> => {
const [mlJobMetrics] = await Promise.allSettled([getMlJobMetrics(mlClient, soClient)]);
const mlJobMetrics = await getMlJobMetrics(mlClient, soClient);

return {
ml_jobs:
mlJobMetrics.status === 'fulfilled'
? mlJobMetrics.value
: { ml_job_metrics: [], ml_job_usage: initialMlJobsUsage },
ml_jobs: mlJobMetrics ? mlJobMetrics : { ml_job_metrics: [], ml_job_usage: initialMlJobsUsage },
};
};

0 comments on commit 7b53cce

Please sign in to comment.