Skip to content

Commit

Permalink
Merge branch 'main' into ent-search-explicit-app-inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored May 3, 2023
2 parents 94843ea + f615466 commit c7f4ffb
Show file tree
Hide file tree
Showing 88 changed files with 1,971 additions and 425 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('migration from 7.7.2-xpack with 100k objects', () => {
await esServer.stop();
}

await new Promise((resolve) => setTimeout(resolve, 10000));
await new Promise((resolve) => setTimeout(resolve, 20000));
};

beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import React from 'react';

import { EuiSuperDatePicker } from '@elastic/eui';

import { useUrlState } from '@kbn/ml-url-state';
import type { UI_SETTINGS } from '@kbn/data-plugin/common';

import { useDatePickerContext } from '../hooks/use_date_picker_context';
import { mlTimefilterRefresh$ } from '../services/timefilter_refresh_service';

import { DatePickerWrapper } from './date_picker_wrapper';
import { useRefreshIntervalUpdates } from '../..';

jest.mock('@elastic/eui', () => {
const EuiButtonMock = jest.fn(() => {
Expand Down Expand Up @@ -51,7 +51,12 @@ jest.mock('@kbn/ml-url-state', () => {
});

jest.mock('../hooks/use_timefilter', () => ({
useRefreshIntervalUpdates: jest.fn(),
useRefreshIntervalUpdates: jest.fn(() => {
return {
pause: false,
};
}),

useTimefilter: () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { of } = require('rxjs');
Expand Down Expand Up @@ -152,7 +157,7 @@ describe('<DatePickerWrapper />', () => {

test('should set interval to default of 5s when pause is disabled and refresh interval is 0', () => {
// arrange
(useUrlState as jest.Mock).mockReturnValue([{ refreshInterval: { pause: false, value: 0 } }]);
(useRefreshIntervalUpdates as jest.Mock).mockReturnValue({ pause: false, value: 0 });

const displayWarningSpy = jest.fn(() => {});

Expand All @@ -171,7 +176,7 @@ describe('<DatePickerWrapper />', () => {

test('should show a warning when configured interval is too short', () => {
// arrange
(useUrlState as jest.Mock).mockReturnValue([{ refreshInterval: { pause: false, value: 10 } }]);
(useRefreshIntervalUpdates as jest.Mock).mockReturnValue({ pause: false, value: 10 });

const displayWarningSpy = jest.fn(() => {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const DatePickerWrapper: FC<DatePickerWrapperProps> = (props) => {
const time = useTimeRangeUpdates();

useEffect(
function syncTimRangeFromUrlState() {
function syncTimeRangeFromUrlState() {
if (globalState?.time !== undefined) {
timefilter.setTime({
from: globalState.time.from,
Expand Down Expand Up @@ -162,11 +162,7 @@ export const DatePickerWrapper: FC<DatePickerWrapperProps> = (props) => {
timefilter.isTimeRangeSelectorEnabled()
);

const refreshInterval = useMemo(
(): RefreshInterval => globalState?.refreshInterval ?? timeFilterRefreshInterval,
// eslint-disable-next-line react-hooks/exhaustive-deps
[JSON.stringify(globalState?.refreshInterval), timeFilterRefreshInterval]
);
const refreshInterval = timeFilterRefreshInterval;

useEffect(
function warnAboutShortRefreshInterval() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { type FC, useMemo, useState } from 'react';
import React, { type FC, useMemo, useState, useEffect, useRef } from 'react';
import {
EuiBadge,
EuiDescriptionList,
Expand All @@ -21,7 +21,9 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { SelectedChangePoint } from './change_point_detection_context';
import { useTimefilter } from '@kbn/ml-date-picker';
import { type RefreshInterval } from '@kbn/data-plugin/common';
import { type SelectedChangePoint } from './change_point_detection_context';
import { ChartComponent } from './chart_component';

const CHARTS_PER_PAGE = 6;
Expand All @@ -31,6 +33,28 @@ interface ChartsGridProps {
}

export const ChartsGrid: FC<ChartsGridProps> = ({ changePoints: changePointsDict }) => {
const timefilter = useTimefilter();

const initialRefreshSetting = useRef<RefreshInterval>();

useEffect(
function pauseRefreshOnMount() {
initialRefreshSetting.current = timefilter.getRefreshInterval();

timefilter.setRefreshInterval({
...initialRefreshSetting.current,
pause: true,
});
return () => {
if (initialRefreshSetting.current) {
// reset initial settings
timefilter.setRefreshInterval(initialRefreshSetting.current);
}
};
},
[timefilter]
);

const changePoints = useMemo(() => {
return Object.values(changePointsDict).flat();
}, [changePointsDict]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { FilterStateStore } from '@kbn/es-query';
import moment from 'moment';
import { FilterStateStore, type TimeRange } from '@kbn/es-query';
import { type TypedLensByValueInput } from '@kbn/lens-plugin/public';
import { useTimeRangeUpdates } from '@kbn/ml-date-picker';
import { useMemo } from 'react';
Expand Down Expand Up @@ -33,6 +34,17 @@ export const useCommonChartProps = ({
const { dataView } = useDataSource();
const { bucketInterval, resultQuery, resultFilters } = useChangePointDetectionContext();

/**
* In order to correctly render annotations for change points at the edges,
* we need to adjust time bound based on the change point timestamp.
*/
const chartTimeRange = useMemo<TimeRange>(() => {
return {
from: moment.min(moment(timeRange.from), moment(annotation.timestamp)).toISOString(),
to: moment.max(moment(timeRange.to), moment(annotation.timestamp)).toISOString(),
};
}, [timeRange, annotation.timestamp]);

const filters = useMemo(() => {
return [
...resultFilters,
Expand Down Expand Up @@ -214,7 +226,7 @@ export const useCommonChartProps = ({
]);

return {
timeRange,
timeRange: chartTimeRange,
filters,
query: resultQuery,
attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ describe('useFindMaintenanceWindows', () => {
expect(mockAddDanger).toBeCalledWith('Unable to load maintenance windows.')
);
});

it('should not try to find maintenance windows if not enabled', async () => {
renderHook(() => useFindMaintenanceWindows({ enabled: false }), {
wrapper: appMockRenderer.AppWrapper,
});

await waitFor(() => expect(findMaintenanceWindows).toHaveBeenCalledTimes(0));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { useQuery } from '@tanstack/react-query';
import { useKibana } from '../utils/kibana_react';
import { findMaintenanceWindows } from '../services/maintenance_windows_api/find';

export const useFindMaintenanceWindows = () => {
interface UseFindMaintenanceWindowsProps {
enabled?: boolean;
}

export const useFindMaintenanceWindows = (props?: UseFindMaintenanceWindowsProps) => {
const { enabled = true } = props || {};

const {
http,
notifications: { toasts },
Expand All @@ -32,6 +38,7 @@ export const useFindMaintenanceWindows = () => {

const {
isLoading,
isFetching,
data = [],
refetch,
} = useQuery({
Expand All @@ -41,11 +48,12 @@ export const useFindMaintenanceWindows = () => {
refetchOnWindowFocus: false,
retry: false,
cacheTime: 0,
enabled,
});

return {
maintenanceWindows: data,
isLoading,
isLoading: enabled && (isLoading || isFetching),
refetch,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const LicensePrompt = React.memo(() => {
>
{i18n.UPGRADE_SUBSCRIPTION}
</EuiButtonEmpty>
,
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton
Expand All @@ -58,7 +57,6 @@ export const LicensePrompt = React.memo(() => {
>
{i18n.START_TRIAL}
</EuiButton>
,
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ export const MaintenanceWindowsPage = React.memo(() => {
docLinks,
} = useKibana().services;
const { isAtLeastPlatinum } = useLicense();
const hasLicense = isAtLeastPlatinum();

const { navigateToCreateMaintenanceWindow } = useCreateMaintenanceWindowNavigation();

const { isLoading, maintenanceWindows, refetch } = useFindMaintenanceWindows();
const { isLoading, maintenanceWindows, refetch } = useFindMaintenanceWindows({
enabled: hasLicense,
});

useBreadcrumbs(AlertingDeepLinkId.maintenanceWindows);

Expand All @@ -56,7 +59,6 @@ export const MaintenanceWindowsPage = React.memo(() => {
maintenanceWindows.length === 0 &&
showWindowMaintenance &&
writeWindowMaintenance;
const hasLicense = isAtLeastPlatinum();

const readOnly = showWindowMaintenance && !writeWindowMaintenance;

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/common/api/cases/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as rt from 'io-ts';

import { NumberFromString } from '../saved_object';
import { UserRt } from '../user';
import { CommentResponseRt } from './comment';
import { CommentRt } from './comment';
import { CasesStatusResponseRt, CaseStatusRt } from './status';
import { CaseConnectorRt } from '../connectors/connector';
import { CaseAssigneesRt } from './assignee';
Expand Down Expand Up @@ -258,7 +258,7 @@ export const CaseRt = rt.intersection([
version: rt.string,
}),
rt.partial({
comments: rt.array(CommentResponseRt),
comments: rt.array(CommentRt),
}),
]);

Expand Down
18 changes: 8 additions & 10 deletions x-pack/plugins/cases/common/api/cases/comment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export const CommentRequestRt = rt.union([
PersistableStateAttachmentRt,
]);

export const CommentResponseRt = rt.intersection([
export const CommentRt = rt.intersection([
CommentAttributesRt,
rt.type({
id: rt.string,
Expand Down Expand Up @@ -233,8 +233,6 @@ export const CommentResponseTypePersistableStateRt = rt.intersection([
}),
]);

export const AllCommentsResponseRT = rt.array(CommentResponseRt);

export const CommentPatchRequestRt = rt.intersection([
/**
* Partial updates are not allowed.
Expand Down Expand Up @@ -266,14 +264,14 @@ export const CommentPatchAttributesRt = rt.intersection([
rt.partial(CommentAttributesBasicRt.props),
]);

export const CommentsResponseRt = rt.type({
comments: rt.array(CommentResponseRt),
export const CommentsFindResponseRt = rt.type({
comments: rt.array(CommentRt),
page: rt.number,
per_page: rt.number,
total: rt.number,
});

export const AllCommentsResponseRt = rt.array(CommentResponseRt);
export const CommentsRt = rt.array(CommentRt);

export const FindQueryParamsRt = rt.partial({
...SavedObjectFindOptionsRt.props,
Expand All @@ -286,7 +284,7 @@ export const BulkGetAttachmentsRequestRt = rt.type({
});

export const BulkGetAttachmentsResponseRt = rt.type({
attachments: AllCommentsResponseRt,
attachments: CommentsRt,
errors: rt.array(
rt.type({
error: rt.string,
Expand Down Expand Up @@ -314,7 +312,7 @@ export type CommentAttributesNoSO = rt.TypeOf<typeof CommentAttributesNoSORt>;
export type CommentAttributesWithoutRefs = rt.TypeOf<typeof CommentAttributesWithoutRefsRt>;
export type CommentRequest = rt.TypeOf<typeof CommentRequestRt>;
export type BulkCreateCommentRequest = rt.TypeOf<typeof BulkCreateCommentRequestRt>;
export type CommentResponse = rt.TypeOf<typeof CommentResponseRt>;
export type Comment = rt.TypeOf<typeof CommentRt>;
export type CommentResponseUserType = rt.TypeOf<typeof CommentResponseTypeUserRt>;
export type CommentResponseAlertsType = rt.TypeOf<typeof CommentResponseTypeAlertsRt>;
export type CommentResponseTypePersistableState = rt.TypeOf<
Expand All @@ -324,8 +322,8 @@ export type CommentResponseExternalReferenceType = rt.TypeOf<
typeof CommentResponseTypeExternalReferenceRt
>;
export type CommentResponseActionsType = rt.TypeOf<typeof CommentResponseTypeActionsRt>;
export type AllCommentsResponse = rt.TypeOf<typeof AllCommentsResponseRt>;
export type CommentsResponse = rt.TypeOf<typeof CommentsResponseRt>;
export type Comments = rt.TypeOf<typeof CommentsRt>;
export type CommentsFindResponse = rt.TypeOf<typeof CommentsFindResponseRt>;
export type CommentPatchRequest = rt.TypeOf<typeof CommentPatchRequestRt>;
export type CommentPatchAttributes = rt.TypeOf<typeof CommentPatchAttributesRt>;
export type CommentRequestUserType = rt.TypeOf<typeof ContextTypeUserRt>;
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/cases/common/ui/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
ActionConnector,
UserAction,
SingleCaseMetricsResponse,
CommentResponse,
Comment,
Case as CaseSnakeCase,
UserActionFindResponse,
FindTypeField as UserActionFindTypeField,
Expand Down Expand Up @@ -83,7 +83,7 @@ export type CaseViewRefreshPropInterface = null | {
refreshCase: () => Promise<void>;
};

export type Comment = SnakeToCamelCase<CommentResponse>;
export type CommentUI = SnakeToCamelCase<Comment>;
export type AlertComment = SnakeToCamelCase<CommentResponseAlertsType>;
export type ExternalReferenceComment = SnakeToCamelCase<CommentResponseExternalReferenceType>;
export type PersistableComment = SnakeToCamelCase<CommentResponseTypePersistableState>;
Expand All @@ -92,7 +92,7 @@ export type FindCaseUserActions = Omit<SnakeToCamelCase<UserActionFindResponse>,
userActions: UserActionUI[];
};
export type CaseUserActionsStats = SnakeToCamelCase<CaseUserActionStatsResponse>;
export type CaseUI = Omit<SnakeToCamelCase<CaseSnakeCase>, 'comments'> & { comments: Comment[] };
export type CaseUI = Omit<SnakeToCamelCase<CaseSnakeCase>, 'comments'> & { comments: CommentUI[] };
export type CasesUI = Omit<SnakeToCamelCase<CasesFindResponse>, 'cases'> & { cases: CaseUI[] };
export type CasesStatus = SnakeToCamelCase<CasesStatusResponse>;
export type CasesMetrics = SnakeToCamelCase<CasesMetricsResponse>;
Expand Down
Loading

0 comments on commit c7f4ffb

Please sign in to comment.