Skip to content

Commit

Permalink
Feature/unittest fix 2.5 (#525)
Browse files Browse the repository at this point in the history
* Feature/common 2.5 (#506)

* feat: split to common change

Signed-off-by: suzhou <[email protected]>

* feat: update

Signed-off-by: suzhou <[email protected]>

Signed-off-by: suzhou <[email protected]>

* feat: update

Signed-off-by: suzhou <[email protected]>

* feat: update

Signed-off-by: suzhou <[email protected]>

* feat: update

Signed-off-by: suzhou <[email protected]>

* feat: update

Signed-off-by: suzhou <[email protected]>

Signed-off-by: suzhou <[email protected]>
  • Loading branch information
SuZhou-Joe authored Jan 3, 2023
1 parent b253ce4 commit 14646b7
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const DefinePolicy = ({ jsonString, onChange, onAutoIndent, hasJSONError }: Defi
</div>
<EuiSpacer size="m" />
<DarkModeConsumer>
{isDarkMode => (
{(isDarkMode) => (
<EuiCodeEditor
mode="json"
theme={isDarkMode ? "sense-dark" : "github"}
Expand Down
10 changes: 10 additions & 0 deletions public/pages/CreatePolicy/containers/CreatePolicy/CreatePolicy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface CreatePolicyState {

export default class CreatePolicy extends Component<CreatePolicyProps, CreatePolicyState> {
static contextType = CoreServicesContext;
_isMount: boolean;
constructor(props: CreatePolicyProps) {
super(props);

Expand All @@ -47,6 +48,8 @@ export default class CreatePolicy extends Component<CreatePolicyProps, CreatePol
isSubmitting: false,
hasSubmitted: false,
};

this._isMount = true;
}

componentDidMount = async (): Promise<void> => {
Expand All @@ -71,6 +74,10 @@ export default class CreatePolicy extends Component<CreatePolicyProps, CreatePol
}
};

componentWillUnmount() {
this._isMount = false;
}

getPolicyToEdit = async (policyId: string): Promise<void> => {
try {
const { policyService } = this.props;
Expand Down Expand Up @@ -169,6 +176,9 @@ export default class CreatePolicy extends Component<CreatePolicyProps, CreatePol
console.error(err);
}

if (!this._isMount) {
return;
}
this.setState({ isSubmitting: false });
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export default class AdvancedAggregation extends Component<AdvancedAggregationPr
align: "left",
render: (aggregationMethod, item) => (
<EuiForm>
<EuiFormRow compressed={true}>
<EuiFormRow>
<EuiSelect
compressed={true}
value={aggregationMethod}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ export default class MetricsCalculation extends Component<MetricsCalculationProp
truncateText: true,
render: (all: boolean, item: MetricItem) => (
<EuiForm>
<EuiFormRow compressed={true}>
<EuiFormRow>
<EuiCheckbox
compressed
id={`all-${item.source_field.label}`}
checked={all}
onChange={(e) => this.setChecked(e, "all", item)}
Expand All @@ -277,8 +278,9 @@ export default class MetricsCalculation extends Component<MetricsCalculationProp
align: "center",
render: (min: boolean, item: MetricItem) => (
<EuiForm>
<EuiFormRow compressed={true}>
<EuiFormRow>
<EuiCheckbox
compressed
id={`min-${item.source_field.label}`}
checked={min}
onChange={(e) => this.setChecked(e, "min", item)}
Expand All @@ -294,8 +296,9 @@ export default class MetricsCalculation extends Component<MetricsCalculationProp
align: "center",
render: (max: boolean, item: MetricItem) => (
<EuiForm>
<EuiFormRow compressed={true}>
<EuiFormRow>
<EuiCheckbox
compressed
id={`max-${item.source_field.label}`}
checked={max}
onChange={(e) => this.setChecked(e, "max", item)}
Expand All @@ -311,8 +314,9 @@ export default class MetricsCalculation extends Component<MetricsCalculationProp
align: "center",
render: (sum: boolean, item: MetricItem) => (
<EuiForm>
<EuiFormRow compressed={true}>
<EuiFormRow>
<EuiCheckbox
compressed
id={`sum-${item.source_field.label}`}
checked={sum}
onChange={(e) => this.setChecked(e, "sum", item)}
Expand All @@ -328,8 +332,9 @@ export default class MetricsCalculation extends Component<MetricsCalculationProp
align: "center",
render: (avg: boolean, item: MetricItem) => (
<EuiForm>
<EuiFormRow compressed={true}>
<EuiFormRow>
<EuiCheckbox
compressed
id={`avg-${item.source_field.label}`}
checked={avg}
onChange={(e) => this.setChecked(e, "avg", item)}
Expand All @@ -345,8 +350,9 @@ export default class MetricsCalculation extends Component<MetricsCalculationProp
align: "center",
render: (value_count: boolean, item: MetricItem) => (
<EuiForm>
<EuiFormRow compressed={true}>
<EuiFormRow>
<EuiCheckbox
compressed
id={`value_count-${item.source_field.label}`}
checked={value_count}
onChange={(e) => this.setChecked(e, "value_count", item)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/

import React, { Component, Fragment } from "react";
import { EuiSpacer, EuiFormRow, EuiComboBox, EuiCallOut, EuiText, EuiLink } from "@elastic/eui";
import { EuiSpacer, EuiFormRow, EuiCallOut, EuiText, EuiLink } from "@elastic/eui";
import { EuiComboBoxOptionOption } from "@elastic/eui/src/components/combo_box/types";
import _ from "lodash";
import EuiComboBox from "../../../../components/BetterComboBox";
import { ContentPanel } from "../../../../components/ContentPanel";
import { IndexItem } from "../../../../../models/interfaces";
import IndexService from "../../../../services/IndexService";
Expand Down Expand Up @@ -34,6 +35,7 @@ export const ROLLUP_RESULTS_HELP_TEXT_LINK = "https://opensearch.org/docs/latest

export default class RollupIndices extends Component<RollupIndicesProps, RollupIndicesState> {
static contextType = CoreServicesContext;
_isMount: boolean;
constructor(props: RollupIndicesProps) {
super(props);
this.state = {
Expand All @@ -42,14 +44,22 @@ export default class RollupIndices extends Component<RollupIndicesProps, RollupI
targetIndexOptions: [],
};

this._isMount = true;
this.onIndexSearchChange = _.debounce(this.onIndexSearchChange, 500, { leading: true });
}

async componentDidMount(): Promise<void> {
await this.onIndexSearchChange("");
}

componentWillUnmount(): void {
this._isMount = false;
}

onIndexSearchChange = async (searchValue: string): Promise<void> => {
if (!this._isMount) {
return;
}
const { indexService } = this.props;
this.setState({ isLoading: true, indexOptions: [] });
try {
Expand All @@ -59,7 +69,9 @@ export default class RollupIndices extends Component<RollupIndicesProps, RollupI
const options = searchValue.trim() ? [{ label: wildcardOption(searchValue) }] : [];
const dataStreams = dataStreamsAndIndicesNamesResponse.response.dataStreams.map((label) => ({ label }));
const indices = dataStreamsAndIndicesNamesResponse.response.indices.map((label) => ({ label }));
this.setState({ indexOptions: options.concat(dataStreams, indices), targetIndexOptions: indices });
if (this._isMount) {
this.setState({ indexOptions: options.concat(dataStreams, indices), targetIndexOptions: indices });
}
} else {
if (dataStreamsAndIndicesNamesResponse.error.startsWith("[index_not_found_exception]")) {
this.context.notifications.toasts.addDanger("No index available");
Expand All @@ -71,7 +83,9 @@ export default class RollupIndices extends Component<RollupIndicesProps, RollupI
this.context.notifications.toasts.addDanger(err.message);
}

this.setState({ isLoading: false });
if (this._isMount) {
this.setState({ isLoading: false });
}
};

onCreateOption = (searchValue: string, flattenedOptions: { label: string; value?: IndexItem }[]): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import React, { ChangeEvent, Component, Fragment } from "react";
import {
EuiSpacer,
EuiFormRow,
EuiComboBox,
EuiSelect,
EuiFlexGroup,
EuiFlexItem,
Expand All @@ -21,6 +20,7 @@ import {
EuiText,
} from "@elastic/eui";
import moment from "moment-timezone";
import EuiComboBox from "../../../../components/BetterComboBox";
import { RollupService } from "../../../../services";
import { FieldItem } from "../../../../../models/interfaces";
import { CalendarTimeunitOptions, FixedTimeunitOptions } from "../../../../utils/constants";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ interface CreateRollupFormState {

export default class CreateRollupForm extends Component<CreateRollupFormProps, CreateRollupFormState> {
static contextType = CoreServicesContext;
_isMount: boolean;

constructor(props: CreateRollupFormProps) {
super(props);
Expand Down Expand Up @@ -131,12 +132,17 @@ export default class CreateRollupForm extends Component<CreateRollupFormProps, C
};
this._next = this._next.bind(this);
this._prev = this._prev.bind(this);
this._isMount = true;
}

componentDidMount = async (): Promise<void> => {
this.context.chrome.setBreadcrumbs([BREADCRUMBS.INDEX_MANAGEMENT, BREADCRUMBS.ROLLUPS, BREADCRUMBS.CREATE_ROLLUP]);
};

componentWillUnmount() {
this._isMount = false;
}

getMappings = async (srcIndex: string): Promise<void> => {
if (!srcIndex.length) return;
try {
Expand Down Expand Up @@ -481,7 +487,9 @@ export default class CreateRollupForm extends Component<CreateRollupFormProps, C
console.error(err);
}

this.setState({ isSubmitting: false });
if (this._isMount) {
this.setState({ isSubmitting: false });
}
};

onCancel = (): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ const setOfSteps = (step: number) => {
return [
{
title: "Set up indices",
children: null,
children: <></>,
},
{
title: "Define transforms",
children: null,
children: <></>,
status: step < 2 ? "disabled" : null,
},
{
title: "Specify schedule",
children: null,
children: <></>,
status: step < 3 ? "disabled" : null,
},
{
title: "Review and create",
children: null,
children: <></>,
status: step < 4 ? "disabled" : null,
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import React, { Component, Fragment } from "react";
import {
EuiSpacer,
EuiFormRow,
EuiComboBox,
EuiCallOut,
EuiPopover,
EuiFlexGroup,
Expand All @@ -20,6 +19,7 @@ import {
EuiLink,
} from "@elastic/eui";
import _ from "lodash";
import EuiComboBox from "../../../../components/BetterComboBox";
import { ContentPanel } from "../../../../components/ContentPanel";
import IndexFilterPopover from "../IndexFilterPopover";
import { FieldItem, IndexItem } from "../../../../../models/interfaces";
Expand Down Expand Up @@ -53,6 +53,7 @@ interface TransformIndicesState {

export default class TransformIndices extends Component<TransformIndicesProps, TransformIndicesState> {
static contextType = CoreServicesContext;
_isMount: boolean;
constructor(props: TransformIndicesProps) {
super(props);
this.state = {
Expand All @@ -63,19 +64,30 @@ export default class TransformIndices extends Component<TransformIndicesProps, T
selectFieldValue: "",
};

this._isMount = true;
this.onIndexSearchChange = _.debounce(this.onIndexSearchChange, 500, { leading: true });
}

async componentDidMount(): Promise<void> {
await this.onIndexSearchChange("");
}

componentWillUnmount(): void {
this._isMount = false;
}

// TODO: created shared method with rollup indices to reduce duplicate code.
onIndexSearchChange = async (searchValue: string): Promise<void> => {
if (!this._isMount) {
return;
}
const { indexService } = this.props;
this.setState({ isLoading: true, indexOptions: [] });
try {
const dataStreamsAndIndicesNamesResponse = await indexService.getDataStreamsAndIndicesNames(searchValue);
if (!this._isMount) {
return;
}
if (dataStreamsAndIndicesNamesResponse.ok) {
// Adding wildcard to search value
const options = searchValue.trim() ? [{ label: wildcardOption(searchValue) }] : [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ interface CreateTransformFormState {

export default class CreateTransformForm extends Component<CreateTransformFormProps, CreateTransformFormState> {
static contextType = CoreServicesContext;
_isMount: boolean;

constructor(props: CreateTransformFormProps) {
super(props);
Expand Down Expand Up @@ -129,12 +130,17 @@ export default class CreateTransformForm extends Component<CreateTransformFormPr
};
this._next = this._next.bind(this);
this._prev = this._prev.bind(this);
this._isMount = true;
}

componentDidMount = async (): Promise<void> => {
this.context.chrome.setBreadcrumbs([BREADCRUMBS.INDEX_MANAGEMENT, BREADCRUMBS.TRANSFORMS, BREADCRUMBS.CREATE_TRANSFORM]);
};

componentWillUnmount() {
this._isMount = false;
}

getMappings = async (srcIndex: string): Promise<void> => {
if (!srcIndex.length) return;
try {
Expand Down Expand Up @@ -485,6 +491,9 @@ export default class CreateTransformForm extends Component<CreateTransformFormPr
console.error(err);
}

if (!this._isMount) {
return;
}
this.setState({ isSubmitting: false });
};

Expand Down
2 changes: 1 addition & 1 deletion public/pages/Rollups/containers/Rollups/Rollups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export default class Rollups extends Component<RollupsProps, RollupsState> {
pageIndex: page,
pageSize: size,
pageSizeOptions: DEFAULT_PAGE_SIZE_OPTIONS,
totalItemCount: totalRollups,
totalItemCount: totalRollups || 0,
};

const sorting: EuiTableSortingType<DocumentRollup> = {
Expand Down
8 changes: 4 additions & 4 deletions public/pages/VisualCreatePolicy/components/States/State.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ const State = ({ state, isInitialState, idx, onClickEditState, onClickDeleteStat
<EuiText>No actions. Edit state to add actions.</EuiText>
) : (
<EuiFlexGroup wrap>
{state.actions.map((action) => (
<EuiFlexItem grow={false} key={makeId()}>
{state.actions.map((action, index) => (
<EuiFlexItem grow={false} key={`${makeId()}-${index}`}>
<EuiPanel>{actionRepoSingleton.getUIActionFromData(action).content()}</EuiPanel>
</EuiFlexItem>
))}
Expand All @@ -131,8 +131,8 @@ const State = ({ state, isInitialState, idx, onClickEditState, onClickDeleteStat
<EuiText>No transitions. Edit state to add transitions.</EuiText>
) : (
<EuiFlexGroup wrap>
{state.transitions.map((transition) => (
<EuiFlexItem grow={false} key={makeId()}>
{state.transitions.map((transition, index) => (
<EuiFlexItem grow={false} key={`${makeId()}-${index}`}>
<EuiPanel>
<TransitionContent transition={transition} />
</EuiPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const States = ({ onOpenFlyout, policy, onClickEditState, onClickDeleteState, on
<div style={{ padding: "0px 10px" }}>
{!isReadOnly && (
<>
<EuiFormRow compressed style={{ maxWidth: "300px", padding: "15px" }} isInvalid={false} error={null}>
<EuiFormRow style={{ maxWidth: "300px", padding: "15px" }} isInvalid={false} error={null}>
<EuiSelect
compressed
prepend="Initial state"
Expand Down
Loading

0 comments on commit 14646b7

Please sign in to comment.