Skip to content

Commit

Permalink
Code Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedhamed-ahmed committed May 16, 2023
1 parent 311517d commit 09ed682
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 32 deletions.
23 changes: 23 additions & 0 deletions x-pack/plugins/infra/public/locators/discover_logs_locator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { LocatorDefinition } from '@kbn/share-plugin/public';
import type { LogsLocatorDependencies, LogsLocatorParams } from './logs_locator';

const DISCOVER_LOGS_LOCATOR_ID = 'DISCOVER_LOGS_LOCATOR';

export class DiscoverLogsLocatorDefinition implements LocatorDefinition<LogsLocatorParams> {
public readonly id = DISCOVER_LOGS_LOCATOR_ID;

constructor(protected readonly deps: LogsLocatorDependencies) {}

public readonly getLocation = async (params: LogsLocatorParams) => {
const { getLocationToDiscover } = await import('./helpers');

return getLocationToDiscover({ core: this.deps.core, ...params });
};
}
31 changes: 31 additions & 0 deletions x-pack/plugins/infra/public/locators/discover_node_logs_locator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { LocatorDefinition } from '@kbn/share-plugin/public';
import type { NodeLogsLocatorDependencies, NodeLogsLocatorParams } from './node_logs_locator';

const DISCOVER_NODE_LOGS_LOCATOR_ID = 'DISCOVER_NODE_LOGS_LOCATOR';

export class DiscoverNodeLogsLocatorDefinition implements LocatorDefinition<NodeLogsLocatorParams> {
public readonly id = DISCOVER_NODE_LOGS_LOCATOR_ID;

constructor(protected readonly deps: NodeLogsLocatorDependencies) {}

public readonly getLocation = async (params: NodeLogsLocatorParams) => {
const { createNodeLogsQuery, getLocationToDiscover } = await import('./helpers');

const { timeRange, logView } = params;
const query = createNodeLogsQuery(params);

return getLocationToDiscover({
core: this.deps.core,
timeRange,
filter: query,
logView,
});
};
}
11 changes: 11 additions & 0 deletions x-pack/plugins/infra/public/locators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { waitFor } from 'xstate/lib/waitFor';
import { flowRight } from 'lodash';
import type { DiscoverAppLocatorParams } from '@kbn/discover-plugin/common';
import type { DiscoverStart } from '@kbn/discover-plugin/public';
import { findInventoryFields } from '../../common/inventory_models';
import { MESSAGE_FIELD, TIMESTAMP_FIELD } from '../../common/constants';
import {
createLogViewStateMachine,
Expand All @@ -26,6 +27,7 @@ import type {
LogViewReference,
ResolvedLogView,
} from '../../common/log_views';
import type { NodeLogsLocatorParams } from './node_logs_locator';

interface LocationToDiscoverParams {
core: InfraClientCoreSetup;
Expand All @@ -34,6 +36,15 @@ interface LocationToDiscoverParams {
logView?: LogViewReference;
}

export const createNodeLogsQuery = (params: NodeLogsLocatorParams) => {
const { nodeType, nodeId, filter } = params;

const nodeFilter = `${findInventoryFields(nodeType).id}: ${nodeId}`;
const query = filter ? `(${nodeFilter}) and (${filter})` : nodeFilter;

return query;
};

export const createSearchString = ({
time,
timeRange,
Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/infra/public/locators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
* 2.0.
*/

import type { DiscoverLogsLocatorDefinition } from './discover_logs_locator';
import type { DiscoverNodeLogsLocatorDefinition } from './discover_node_logs_locator';
import type { LogsLocator } from './logs_locator';
import type { NodeLogsLocator } from './node_logs_locator';

export * from './discover_logs_locator';
export * from './discover_node_logs_locator';
export * from './logs_locator';
export * from './node_logs_locator';

export interface InfraLocators {
logsLocator: LogsLocator;
nodeLogsLocator: NodeLogsLocator;
logsLocator: LogsLocator | DiscoverLogsLocatorDefinition;
nodeLogsLocator: NodeLogsLocator | DiscoverNodeLogsLocatorDefinition;
}
12 changes: 1 addition & 11 deletions x-pack/plugins/infra/public/locators/logs_locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { SerializableRecord } from '@kbn/utility-types';
import type { LogViewReference } from '../../common/log_views';
import type { TimeRange } from '../../common/time';
import type { InfraClientCoreSetup } from '../types';
import { DISCOVER_APP_TARGET } from '../../common/constants';

const LOGS_LOCATOR_ID = 'LOGS_LOCATOR';

Expand All @@ -27,13 +26,8 @@ export interface LogsLocatorParams extends SerializableRecord {

export type LogsLocator = LocatorPublic<LogsLocatorParams>;

interface LocatorConfig {
appTarget: string;
}

export interface LogsLocatorDependencies {
core: InfraClientCoreSetup;
config: LocatorConfig;
}

export class LogsLocatorDefinition implements LocatorDefinition<LogsLocatorParams> {
Expand All @@ -42,11 +36,7 @@ export class LogsLocatorDefinition implements LocatorDefinition<LogsLocatorParam
constructor(protected readonly deps: LogsLocatorDependencies) {}

public readonly getLocation = async (params: LogsLocatorParams) => {
const { createSearchString, getLocationToDiscover } = await import('./helpers');

if (this.deps.config.appTarget === DISCOVER_APP_TARGET) {
return await getLocationToDiscover({ core: this.deps.core, ...params });
}
const { createSearchString } = await import('./helpers');

const searchString = createSearchString(params);

Expand Down
12 changes: 2 additions & 10 deletions x-pack/plugins/infra/public/locators/node_logs_locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { LocatorDefinition, LocatorPublic } from '@kbn/share-plugin/public';
import type { InventoryItemType } from '../../common/inventory_models/types';
import type { LogsLocatorDependencies, LogsLocatorParams } from './logs_locator';
import { DISCOVER_APP_TARGET } from '../../common/constants';

const NODE_LOGS_LOCATOR_ID = 'NODE_LOGS_LOCATOR';

Expand All @@ -27,16 +26,9 @@ export class NodeLogsLocatorDefinition implements LocatorDefinition<NodeLogsLoca
constructor(protected readonly deps: NodeLogsLocatorDependencies) {}

public readonly getLocation = async (params: NodeLogsLocatorParams) => {
const { createSearchString, getLocationToDiscover } = await import('./helpers');
const { findInventoryFields } = await import('../../common/inventory_models');
const { createNodeLogsQuery, createSearchString } = await import('./helpers');

const { nodeType, nodeId, filter, timeRange, logView } = params;
const nodeFilter = `${findInventoryFields(nodeType).id}: ${nodeId}`;
const query = filter ? `(${nodeFilter}) and (${filter})` : nodeFilter;

if (this.deps.config.appTarget === DISCOVER_APP_TARGET) {
return await getLocationToDiscover({ core: this.deps.core, timeRange, filter, logView });
}
const query = createNodeLogsQuery(params);

const searchString = createSearchString({ ...params, filter: query });

Expand Down
30 changes: 21 additions & 9 deletions x-pack/plugins/infra/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ import { createLazyHostMetricsTable } from './components/infrastructure_node_met
import { createLazyPodMetricsTable } from './components/infrastructure_node_metrics_tables/pod/create_lazy_pod_metrics_table';
import { LOG_STREAM_EMBEDDABLE } from './components/log_stream/log_stream_embeddable';
import { LogStreamEmbeddableFactoryDefinition } from './components/log_stream/log_stream_embeddable_factory';
import { InfraLocators, LogsLocatorDefinition, NodeLogsLocatorDefinition } from './locators';
import {
DiscoverLogsLocatorDefinition,
DiscoverNodeLogsLocatorDefinition,
InfraLocators,
LogsLocatorDefinition,
NodeLogsLocatorDefinition,
} from './locators';
import { createMetricsFetchData, createMetricsHasData } from './metrics_overview_fetchers';
import { registerFeatures } from './register_feature';
import { InventoryViewsService } from './services/inventory_views';
Expand Down Expand Up @@ -153,7 +159,21 @@ export class Plugin implements InfraClientPluginClass {
new LogStreamEmbeddableFactoryDefinition(core.getStartServices)
);

// Register Locators
let logsLocator = pluginsSetup.share.url.locators.create(new LogsLocatorDefinition({ core }));
let nodeLogsLocator = pluginsSetup.share.url.locators.create(
new NodeLogsLocatorDefinition({ core })
);

if (this.appTarget === DISCOVER_APP_TARGET) {
// Register Locators
logsLocator = pluginsSetup.share.url.locators.create(
new DiscoverLogsLocatorDefinition({ core })
);
nodeLogsLocator = pluginsSetup.share.url.locators.create(
new DiscoverNodeLogsLocatorDefinition({ core })
);

core.application.register({
id: 'logs-to-discover',
title: '',
Expand Down Expand Up @@ -296,14 +316,6 @@ export class Plugin implements InfraClientPluginClass {
// Setup telemetry events
this.telemetry.setup({ analytics: core.analytics });

// Register Locators
const logsLocator = pluginsSetup.share.url.locators.create(
new LogsLocatorDefinition({ config: { appTarget: this.appTarget }, core })
);
const nodeLogsLocator = pluginsSetup.share.url.locators.create(
new NodeLogsLocatorDefinition({ config: { appTarget: this.appTarget }, core })
);

this.locators = {
logsLocator,
nodeLogsLocator,
Expand Down

0 comments on commit 09ed682

Please sign in to comment.