Skip to content

Commit

Permalink
Code Refactoring and fixing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedhamed-ahmed committed May 15, 2023
1 parent 6cde016 commit 5d99d46
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 85 deletions.
29 changes: 16 additions & 13 deletions x-pack/plugins/infra/public/locators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface LocationToDiscoverParams {
logView?: LogViewReference;
}

export const parseSearchString = ({
export const createSearchString = ({
time,
timeRange,
filter = '',
Expand Down Expand Up @@ -88,6 +88,8 @@ export const getLocationToDiscover = async ({
state.matches('checkingStatusFailed')
);

service.stop();

if ('resolvedLogView' in doneState.context) {
discoverLocation = await constructDiscoverLocation(
discover,
Expand All @@ -98,8 +100,6 @@ export const getLocationToDiscover = async ({
discoverLocation = await constructDiscoverLocation(discover, discoverParams);
}

service.stop();

if (!discoverLocation) {
throw new Error('Discover location not found');
}
Expand All @@ -112,16 +112,19 @@ const constructDiscoverLocation = async (
discoverParams: DiscoverAppLocatorParams,
resolvedLogView?: ResolvedLogView
) => {
const locationParams = !resolvedLogView
? discoverParams
: {
...discoverParams,
columns: parseColumns(resolvedLogView.columns),
dataViewId: resolvedLogView.dataViewReference.toSpec().id,
dataViewSpec: resolvedLogView.dataViewReference.toSpec(),
};

return await discover.locator?.getLocation(locationParams);
if (!resolvedLogView) {
return await discover.locator?.getLocation(discoverParams);
}

const columns = parseColumns(resolvedLogView.columns);
const dataViewSpec = resolvedLogView.dataViewReference.toSpec();

return await discover.locator?.getLocation({
...discoverParams,
columns,
dataViewId: dataViewSpec.id,
dataViewSpec,
});
};

const parseColumns = (columns: ResolvedLogView['columns']) => {
Expand Down
16 changes: 10 additions & 6 deletions x-pack/plugins/infra/public/locators/locators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import { coreMock } from '@kbn/core/public/mocks';
import { findInventoryFields } from '../../common/inventory_models';
import moment from 'moment';
import { DEFAULT_LOG_VIEW } from '../observability_logs/log_view_state';
import type { LogViewReference } from '../../common/log_views';

const setupLogsLocator = async (appTarget: string = LOGS_APP_TARGET) => {
const deps: LogsLocatorDependencies = {
core: coreMock.createSetup(),
appTarget,
config: { appTarget },
};
const logsLocator = new LogsLocatorDefinition(deps);
const nodeLogsLocator = new NodeLogsLocatorDefinition(deps);
Expand Down Expand Up @@ -167,7 +168,7 @@ describe('Infra Locators', () => {
nodeId,
nodeType,
time,
logView: DEFAULT_LOG_VIEW,
logView: { ...DEFAULT_LOG_VIEW, logViewId: 'test' },
};
const { nodeLogsLocator } = await setupLogsLocator();
const { path } = await nodeLogsLocator.getLocation(params);
Expand Down Expand Up @@ -217,15 +218,18 @@ describe('Infra Locators', () => {
*/

export const constructUrlSearchString = (params: Partial<NodeLogsLocatorParams>) => {
const { time = 1550671089404 } = params;
const { time = 1550671089404, logView } = params;

return `/stream?logView=${constructLogView()}&logPosition=${constructLogPosition(
return `/stream?logView=${constructLogView(logView)}&logPosition=${constructLogPosition(
time
)}&logFilter=${constructLogFilter(params)}`;
};

const constructLogView = () => {
return `(logViewId:default,type:log-view-reference)`;
const constructLogView = (logView?: LogViewReference) => {
const logViewId =
logView && 'logViewId' in logView ? logView.logViewId : DEFAULT_LOG_VIEW.logViewId;

return `(logViewId:${logViewId},type:log-view-reference)`;
};

const constructLogPosition = (time: number = 1550671089404) => {
Expand Down
12 changes: 8 additions & 4 deletions x-pack/plugins/infra/public/locators/logs_locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ export interface LogsLocatorParams extends SerializableRecord {

export type LogsLocator = LocatorPublic<LogsLocatorParams>;

interface LocatorConfig {
appTarget: string;
}

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

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

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

if (this.deps.appTarget === DISCOVER_APP_TARGET) {
if (this.deps.config.appTarget === DISCOVER_APP_TARGET) {
return await getLocationToDiscover({ core: this.deps.core, ...params });
}

const searchString = parseSearchString(params);
const searchString = createSearchString(params);

return {
app: 'logs',
Expand Down
14 changes: 5 additions & 9 deletions x-pack/plugins/infra/public/locators/node_logs_locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import { LocatorDefinition, LocatorPublic } from '@kbn/share-plugin/public';
import type { InventoryItemType } from '../../common/inventory_models/types';
import type { InfraClientCoreSetup } from '../types';
import type { LogsLocatorParams } from './logs_locator';
import type { LogsLocatorDependencies, LogsLocatorParams } from './logs_locator';
import { DISCOVER_APP_TARGET } from '../../common/constants';

const NODE_LOGS_LOCATOR_ID = 'NODE_LOGS_LOCATOR';
Expand All @@ -20,29 +19,26 @@ export interface NodeLogsLocatorParams extends LogsLocatorParams {

export type NodeLogsLocator = LocatorPublic<NodeLogsLocatorParams>;

export interface NodeLogsLocatorDependencies {
core: InfraClientCoreSetup;
appTarget: string;
}
export type NodeLogsLocatorDependencies = LogsLocatorDependencies;

export class NodeLogsLocatorDefinition implements LocatorDefinition<NodeLogsLocatorParams> {
public readonly id = NODE_LOGS_LOCATOR_ID;

constructor(protected readonly deps: NodeLogsLocatorDependencies) {}

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

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

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

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

return {
app: 'logs',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const decodePositionQueryValueFromUrl = (queryValueFromUrl: unknown) => {

// Used by linkTo components
export const replaceLogPositionInQueryString = (time?: number) =>
Number.isNaN(time)
Number.isNaN(time) || time == null
? (value: string) => value
: replaceStateKeyInQueryString<PositionStateInUrl>(defaultPositionStateKey, {
position: {
Expand Down
16 changes: 11 additions & 5 deletions x-pack/plugins/infra/public/pages/link_to/redirect_to_logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { useEffect } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { getFilterFromLocation, getTimeFromLocation } from './query_params';
import { useKibanaContextForPlugin } from '../../hooks/use_kibana';
Expand All @@ -21,11 +22,16 @@ export const RedirectToLogs = () => {
const filter = getFilterFromLocation(location);
const time = getTimeFromLocation(location);

locators.logsLocator.navigate({
time,
filter,
logView: { ...DEFAULT_LOG_VIEW, logViewId: logViewId || DEFAULT_LOG_VIEW.logViewId },
});
useEffect(() => {
locators.logsLocator.navigate(
{
time,
filter,
logView: { ...DEFAULT_LOG_VIEW, logViewId: logViewId || DEFAULT_LOG_VIEW.logViewId },
},
{ replace: true }
);
}, [filter, locators.logsLocator, logViewId, time]);

return null;
};
51 changes: 16 additions & 35 deletions x-pack/plugins/infra/public/pages/link_to/redirect_to_node_logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { LinkDescriptor } from '@kbn/observability-plugin/public';
import React from 'react';
import { useEffect } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import useMount from 'react-use/lib/useMount';
import { InventoryItemType } from '../../../common/inventory_models/types';
import { LoadingPage } from '../../components/loading_page';
import { useKibanaContextForPlugin } from '../../hooks/use_kibana';
import { useLogView } from '../../hooks/use_log_view';
import { DEFAULT_LOG_VIEW_ID } from '../../observability_logs/log_view_state';
import { getFilterFromLocation, getTimeFromLocation } from './query_params';

Expand All @@ -29,40 +25,25 @@ export const RedirectToNodeLogs = ({
},
location,
}: RedirectToNodeLogsType) => {
const { services } = useKibanaContextForPlugin();
const { isLoading, load } = useLogView({
initialLogViewReference: { type: 'log-view-reference', logViewId },
logViews: services.logViews.client,
});

useMount(() => {
load();
});

if (isLoading) {
return (
<LoadingPage
data-test-subj={`nodeLoadingPage-${nodeType}`}
message={i18n.translate('xpack.infra.redirectToNodeLogs.loadingNodeLogsMessage', {
defaultMessage: 'Loading {nodeType} logs',
values: {
nodeType,
},
})}
/>
);
}
const {
services: { locators },
} = useKibanaContextForPlugin();

const filter = getFilterFromLocation(location);
const time = getTimeFromLocation(location);

services.locators.nodeLogsLocator.navigate({
nodeId,
nodeType,
time,
filter,
logView: { type: 'log-view-reference', logViewId },
});
useEffect(() => {
locators.nodeLogsLocator.navigate(
{
nodeId,
nodeType,
time,
filter,
logView: { type: 'log-view-reference', logViewId },
},
{ replace: true }
);
}, [filter, locators.nodeLogsLocator, logViewId, nodeId, nodeType, time]);

return null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ export const LogsLinkToStream = ({ startTime, endTime, query }: LogsLinkToStream
return (
<RedirectAppLinks coreStart={services}>
<EuiButtonEmpty
onClick={() =>
locators.logsLocator?.navigate({
time: endTime,
timeRange: {
startTime,
endTime,
},
filter: query,
})
}
href={locators.logsLocator?.getRedirectUrl({
time: endTime,
timeRange: {
startTime,
endTime,
},
filter: query,
})}
data-test-subj="hostsView-logs-link-to-stream-button"
iconType="popout"
flush="both"
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/infra/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ export class Plugin implements InfraClientPluginClass {

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

this.locators = {
Expand Down

0 comments on commit 5d99d46

Please sign in to comment.