/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { ChangeEvent } from "react"; import { EuiFormRow, EuiCodeEditor, EuiFieldText, EuiSpacer, EuiRadioGroup, EuiCallOut } from "@elastic/eui"; import { ShrinkAction, UIAction } from "../../../../../models/interfaces"; import { makeId } from "../../../../utils/helpers"; import { ActionType } from "../../utils/constants"; import { DarkModeConsumer } from "../../../../components/DarkMode"; import EuiFormCustomLabel from "../EuiFormCustomLabel"; const radios = [ { id: "no", label: "No", }, { id: "yes", label: "Yes", }, ]; export default class ShrinkUIAction implements UIAction { id: string; action: ShrinkAction; type = ActionType.Shrink; constructor(action: ShrinkAction, id: string = makeId(), useJsons: boolean = false) { this.action = { ...action }; this.id = id; // When the jsons are edited by the user, don't overwrite the json. When cloned elsewhere, take the actual object as source of truth if (!useJsons) { this.action.target_index_name_template_json = JSON.stringify(this.action.shrink.target_index_name_template, null, 4); this.action.aliases_json = JSON.stringify(this.action.shrink.aliases, null, 4); this.action.force_unsafe_input = this.action.shrink.force_unsafe ? "yes" : "no"; } } content = () => `Shrink`; clone = (action: ShrinkAction = this.action) => new ShrinkUIAction(action, this.id); cloneUsingString = (action: ShrinkAction) => new ShrinkUIAction(action, this.id, true); isValid = () => { return this.isValidNumShards() && this.isValidAliasesJson() && this.isValidIndexNameTemplateJson(); }; isValidNumShards = () => { const shrink = this.action.shrink; const numSet = [shrink.num_new_shards != null, shrink.percentage_of_source_shards != null, shrink.max_shard_size != null].filter( (it) => it ).length; return numSet == 1; }; isValidAliasesJson = () => { if (this.action.aliases_json && this.action.aliases_json?.length > 0) { try { JSON.parse(this.getAliasesJsonString(this.action) as string); return true; } catch (err) { return false; } } return true; }; getAliasesJsonString = (action: ShrinkAction) => { const shrink = action.shrink; return action.hasOwnProperty("aliases_json") ? action.aliases_json : JSON.stringify(shrink.aliases, null, 4); }; isValidIndexNameTemplateJson = () => { if (this.action.target_index_name_template_json && this.action.target_index_name_template_json?.length > 0) { try { JSON.parse(this.getIndexNameTemplateJsonString(this.action) as string); return true; } catch (err) { return false; } } return true; }; getIndexNameTemplateJsonString = (action: ShrinkAction) => { if (action.hasOwnProperty("target_index_name_template_json")) { return action.target_index_name_template_json; } else { return JSON.stringify(action.shrink.target_index_name_template, null, 4); } }; render = (action: UIAction, onChangeAction: (action: UIAction) => void) => { const shrink = action.action.shrink; return ( <> ) => { const numNewShards = e.target.value; const shrinkObject = { ...action.action }; if (numNewShards) shrinkObject.shrink.num_new_shards = numNewShards; else delete shrinkObject.shrink.num_new_shards; onChangeAction(this.cloneUsingString(shrinkObject)); }} data-test-subj="action-render-shrink-num-new-shards" /> ) => { const maxShardSize = e.target.value; const shrinkObject = { ...action.action }; if (maxShardSize) shrinkObject.shrink.max_shard_size = maxShardSize; else delete shrinkObject.shrink.max_shard_size; onChangeAction(this.cloneUsingString(shrinkObject)); }} data-test-subj="action-render-shrink-max-shard-size" /> ) => { const percentageOfSourceShards = e.target.value; const shrinkObject = { ...action.action }; if (percentageOfSourceShards) shrinkObject.shrink.percentage_of_source_shards = percentageOfSourceShards; else delete shrinkObject.shrink.percentage_of_source_shards; onChangeAction(this.cloneUsingString(shrinkObject)); }} data-test-subj="action-render-shrink-percentage-of-source-shards" /> {(isDarkMode) => ( { const indexNameTemplateJSON = str; const shrinkObject = { ...action.action }; if (indexNameTemplateJSON) shrinkObject.target_index_name_template_json = indexNameTemplateJSON; else delete shrinkObject.target_index_name_template_json; onChangeAction(this.cloneUsingString(shrinkObject)); }} setOptions={{ fontSize: "14px" }} aria-label="Code Editor" height="100px" /> )} { const forceUnsafe = id === "yes"; const shrinkObject = { ...action.action }; if (forceUnsafe) { shrinkObject.shrink.force_unsafe = forceUnsafe; shrinkObject.force_unsafe_input = id; } else { shrinkObject.shrink.force_unsafe = false; shrinkObject.force_unsafe_input = "no"; } onChangeAction(this.cloneUsingString(shrinkObject)); }} name="forceUnsafe" /> {(isDarkMode) => ( { const aliasesJSON = str; const shrinkObject: ShrinkAction = { ...action.action }; // const shrink = { ...action.action.shrink }; if (aliasesJSON) shrinkObject.aliases_json = aliasesJSON; else delete shrinkObject.aliases_json; onChangeAction(this.cloneUsingString(shrinkObject)); }} setOptions={{ fontSize: "14px" }} aria-label="Code Editor" height="100px" /> )} ); }; toAction = () => { // Use the spread operator to copy the action const shrink = { ...this.action }; if (shrink.aliases_json != null) { shrink.shrink.aliases = JSON.parse(shrink.aliases_json as string); } if (shrink.target_index_name_template_json != null) { shrink.shrink.target_index_name_template = JSON.parse(shrink.target_index_name_template_json as string); } delete shrink.aliases_json; delete shrink.force_unsafe_input; delete shrink.target_index_name_template_json; return shrink; }; }