Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { createCasesAnalyticsIndex, scheduleCasesAnalyticsSyncTask } from './cases_index';
import { createCommentsAnalyticsIndex, scheduleCommentsAnalyticsSyncTask } from './comments_index';
import { createActivityAnalyticsIndex, scheduleActivityAnalyticsSyncTask } from './activity_index';
import type { ConfigType } from '../config';

export const createCasesAnalyticsIndexes = ({
esClient,
Expand Down Expand Up @@ -69,13 +70,15 @@ export const registerCasesAnalyticsIndexesTasks = ({
taskManager,
logger,
core,
analyticsConfig,
}: {
taskManager: TaskManagerSetupContract;
logger: Logger;
core: CoreSetup<CasesServerStartDependencies>;
analyticsConfig: ConfigType['analytics'];
}) => {
registerCAIBackfillTask({ taskManager, logger, core });
registerCAISynchronizationTask({ taskManager, logger, core });
registerCAIBackfillTask({ taskManager, logger, core, analyticsConfig });
registerCAISynchronizationTask({ taskManager, logger, core, analyticsConfig });
};

export const scheduleCasesAnalyticsSyncTasks = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,36 @@
import type { Logger } from '@kbn/logging';
import type { ElasticsearchClient } from '@kbn/core/server';
import type { RunContext } from '@kbn/task-manager-plugin/server';
import type { ConfigType } from '../../../config';
import { BackfillTaskRunner } from './backfill_task_runner';

interface CaseAnalyticsIndexBackfillTaskFactoryParams {
logger: Logger;
getESClient: () => Promise<ElasticsearchClient>;
analyticsConfig: ConfigType['analytics'];
}

export class CaseAnalyticsIndexBackfillTaskFactory {
private readonly logger: Logger;
private readonly getESClient: () => Promise<ElasticsearchClient>;
private readonly analyticsConfig: ConfigType['analytics'];

constructor({ logger, getESClient }: CaseAnalyticsIndexBackfillTaskFactoryParams) {
constructor({
logger,
getESClient,
analyticsConfig,
}: CaseAnalyticsIndexBackfillTaskFactoryParams) {
this.logger = logger;
this.getESClient = getESClient;
this.analyticsConfig = analyticsConfig;
}

public create(context: RunContext) {
return new BackfillTaskRunner({
taskInstance: context.taskInstance,
logger: this.logger,
getESClient: this.getESClient,
analyticsConfig: this.analyticsConfig,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('BackfillTaskRunner', () => {

let taskRunner: BackfillTaskRunner;

const analyticsConfig = {
index: {
enabled: true,
},
};

beforeEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -61,6 +67,7 @@ describe('BackfillTaskRunner', () => {
logger,
getESClient,
taskInstance,
analyticsConfig,
});

const result = await taskRunner.run();
Expand Down Expand Up @@ -101,6 +108,7 @@ describe('BackfillTaskRunner', () => {
logger,
getESClient,
taskInstance,
analyticsConfig,
});

try {
Expand Down Expand Up @@ -132,6 +140,7 @@ describe('BackfillTaskRunner', () => {
logger,
getESClient,
taskInstance,
analyticsConfig,
});

try {
Expand All @@ -146,4 +155,29 @@ describe('BackfillTaskRunner', () => {
);
});
});

describe('Analytics index disabled', () => {
const analyticsConfigDisabled = {
index: {
enabled: false,
},
};

it('does not call the reindex API if analytics is disabled', async () => {
const esClient = elasticsearchServiceMock.createElasticsearchClient();
const getESClient = async () => esClient;

taskRunner = new BackfillTaskRunner({
logger,
getESClient,
taskInstance,
analyticsConfig: analyticsConfigDisabled,
});

await taskRunner.run();

expect(esClient.cluster.health).not.toBeCalled();
expect(esClient.reindex).not.toBeCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import type {
IndicesGetMappingResponse,
QueryDslQueryContainer,
} from '@elastic/elasticsearch/lib/api/types';
import type { ConfigType } from '../../../config';
import { isRetryableEsClientError } from '../../utils';

interface BackfillTaskRunnerFactoryConstructorParams {
taskInstance: ConcreteTaskInstance;
getESClient: () => Promise<ElasticsearchClient>;
logger: Logger;
analyticsConfig: ConfigType['analytics'];
}

export class BackfillTaskRunner implements CancellableTask {
Expand All @@ -34,16 +36,28 @@ export class BackfillTaskRunner implements CancellableTask {
private readonly getESClient: () => Promise<ElasticsearchClient>;
private readonly logger: Logger;
private readonly errorSource = TaskErrorSource.FRAMEWORK;

constructor({ taskInstance, getESClient, logger }: BackfillTaskRunnerFactoryConstructorParams) {
private readonly analyticsConfig: ConfigType['analytics'];

constructor({
taskInstance,
getESClient,
logger,
analyticsConfig,
}: BackfillTaskRunnerFactoryConstructorParams) {
this.sourceIndex = taskInstance.params.sourceIndex;
this.destIndex = taskInstance.params.destIndex;
this.sourceQuery = taskInstance.params.sourceQuery;
this.getESClient = getESClient;
this.logger = logger;
this.analyticsConfig = analyticsConfig;
}

public async run() {
if (!this.analyticsConfig.index.enabled) {
this.logDebug('Analytics index is disabled, skipping backfill task.');
return;
}

const esClient = await this.getESClient();
try {
await this.waitForDestIndex(esClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
} from '@kbn/task-manager-plugin/server';
import type { CoreSetup, ElasticsearchClient } from '@kbn/core/server';
import type { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import type { ConfigType } from '../../../config';
import { ANALYTICS_BACKFILL_TASK_TYPE } from '../../../../common/constants';
import type { CasesServerStartDependencies } from '../../../types';
import { CaseAnalyticsIndexBackfillTaskFactory } from './backfill_task_factory';
Expand All @@ -22,10 +23,12 @@ export function registerCAIBackfillTask({
taskManager,
logger,
core,
analyticsConfig,
}: {
taskManager: TaskManagerSetupContract;
logger: Logger;
core: CoreSetup<CasesServerStartDependencies>;
analyticsConfig: ConfigType['analytics'];
}) {
const getESClient = async (): Promise<ElasticsearchClient> => {
const [{ elasticsearch }] = await core.getStartServices();
Expand All @@ -37,7 +40,11 @@ export function registerCAIBackfillTask({
title: 'Backfill cases analytics indexes.',
maxAttempts: 3,
createTaskRunner: (context: RunContext) => {
return new CaseAnalyticsIndexBackfillTaskFactory({ getESClient, logger }).create(context);
return new CaseAnalyticsIndexBackfillTaskFactory({
getESClient,
logger,
analyticsConfig,
}).create(context);
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
TaskManagerStartContract,
} from '@kbn/task-manager-plugin/server';
import type { CoreSetup, ElasticsearchClient } from '@kbn/core/server';
import type { ConfigType } from '../../../config';
import { ANALYTICS_SYNCHRONIZATION_TASK_TYPE } from '../../../../common/constants';
import type { CasesServerStartDependencies } from '../../../types';
import { AnalyticsIndexSynchronizationTaskFactory } from './synchronization_task_factory';
Expand All @@ -23,10 +24,12 @@ export function registerCAISynchronizationTask({
taskManager,
logger,
core,
analyticsConfig,
}: {
taskManager: TaskManagerSetupContract;
logger: Logger;
core: CoreSetup<CasesServerStartDependencies>;
analyticsConfig: ConfigType['analytics'];
}) {
const getESClient = async (): Promise<ElasticsearchClient> => {
const [{ elasticsearch }] = await core.getStartServices();
Expand All @@ -37,9 +40,11 @@ export function registerCAISynchronizationTask({
[ANALYTICS_SYNCHRONIZATION_TASK_TYPE]: {
title: 'Synchronization for the cases analytics index',
createTaskRunner: (context: RunContext) => {
return new AnalyticsIndexSynchronizationTaskFactory({ getESClient, logger }).create(
context
);
return new AnalyticsIndexSynchronizationTaskFactory({
getESClient,
logger,
analyticsConfig,
}).create(context);
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,36 @@
import type { Logger } from '@kbn/logging';
import type { ElasticsearchClient } from '@kbn/core/server';
import type { RunContext } from '@kbn/task-manager-plugin/server';
import type { ConfigType } from '../../../config';
import { SynchronizationTaskRunner } from './synchronization_task_runner';

interface AnalyticsIndexSynchronizationTaskFactoryParams {
logger: Logger;
getESClient: () => Promise<ElasticsearchClient>;
analyticsConfig: ConfigType['analytics'];
}

export class AnalyticsIndexSynchronizationTaskFactory {
private readonly logger: Logger;
private readonly getESClient: () => Promise<ElasticsearchClient>;
private readonly analyticsConfig: ConfigType['analytics'];

constructor({ logger, getESClient }: AnalyticsIndexSynchronizationTaskFactoryParams) {
constructor({
logger,
getESClient,
analyticsConfig,
}: AnalyticsIndexSynchronizationTaskFactoryParams) {
this.logger = logger;
this.getESClient = getESClient;
this.analyticsConfig = analyticsConfig;
}

public create(context: RunContext) {
return new SynchronizationTaskRunner({
taskInstance: context.taskInstance,
logger: this.logger,
getESClient: this.getESClient,
analyticsConfig: this.analyticsConfig,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ describe('SynchronizationTaskRunner', () => {

let taskRunner: SynchronizationTaskRunner;

const analyticsConfig = {
index: {
enabled: true,
},
};

beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers().setSystemTime(newAttemptTime);
Expand Down Expand Up @@ -87,6 +93,7 @@ describe('SynchronizationTaskRunner', () => {
logger,
getESClient,
taskInstance,
analyticsConfig,
});

const result = await taskRunner.run();
Expand Down Expand Up @@ -176,6 +183,7 @@ describe('SynchronizationTaskRunner', () => {
...taskInstance,
state: {},
},
analyticsConfig,
});

const result = await taskRunner.run();
Expand Down Expand Up @@ -250,6 +258,7 @@ describe('SynchronizationTaskRunner', () => {
logger,
getESClient,
taskInstance,
analyticsConfig,
});

const result = await taskRunner.run();
Expand All @@ -273,6 +282,7 @@ describe('SynchronizationTaskRunner', () => {
logger,
getESClient,
taskInstance,
analyticsConfig,
});

const result = await taskRunner.run();
Expand Down Expand Up @@ -347,6 +357,7 @@ describe('SynchronizationTaskRunner', () => {
logger,
getESClient,
taskInstance,
analyticsConfig,
});

try {
Expand All @@ -370,6 +381,7 @@ describe('SynchronizationTaskRunner', () => {
logger,
getESClient,
taskInstance,
analyticsConfig,
});

try {
Expand All @@ -384,4 +396,29 @@ describe('SynchronizationTaskRunner', () => {
);
});
});

describe('Analytics index disabled', () => {
const analyticsConfigDisabled = {
index: {
enabled: false,
},
};

it('does not call the reindex API if analytics is disabled', async () => {
const getESClient = async () => esClient;

taskRunner = new SynchronizationTaskRunner({
logger,
getESClient,
taskInstance,
analyticsConfig: analyticsConfigDisabled,
});

await taskRunner.run();

expect(esClient.tasks.get).not.toBeCalled();
expect(esClient.cluster.health).not.toBeCalled();
expect(esClient.reindex).not.toBeCalled();
});
});
});
Loading