From 8d0069e303ba8e755bf140ea1b654df02b060660 Mon Sep 17 00:00:00 2001 From: anu-jose-ibm Date: Tue, 16 May 2023 17:35:41 +0530 Subject: [PATCH] C and U collection form coversion staging changes reordered form added lint error coorection lint error correction --- .../miq_ae_customization_controller.rb | 6 +- app/controllers/ops_controller/diagnostics.rb | 82 +++++++++++-------- .../c-and-u-collections-form/index.jsx | 57 +++++++++++++ .../c-and-u-collections-form/schema.js | 39 +++++++++ .../packs/component-definitions-common.js | 2 + .../c-and-u-collections-form.spec.js | 23 ++++++ .../ops/_diagnostics_cu_repair_tab.html.erb | 2 + .../ops/_diagnostics_cu_repair_tab.html.haml | 41 ---------- 8 files changed, 175 insertions(+), 77 deletions(-) create mode 100644 app/javascript/components/c-and-u-collections-form/index.jsx create mode 100644 app/javascript/components/c-and-u-collections-form/schema.js create mode 100644 app/javascript/spec/c-and-u-collections-form/c-and-u-collections-form.spec.js create mode 100644 app/views/ops/_diagnostics_cu_repair_tab.html.erb delete mode 100644 app/views/ops/_diagnostics_cu_repair_tab.html.haml diff --git a/app/controllers/miq_ae_customization_controller.rb b/app/controllers/miq_ae_customization_controller.rb index b4924c5e8396..743414d8ead3 100644 --- a/app/controllers/miq_ae_customization_controller.rb +++ b/app/controllers/miq_ae_customization_controller.rb @@ -185,7 +185,7 @@ def x_show # Dialog show selected from catalog explorer def show - assert_privileges('dialog_accord') # feature like miq_ae_customization_service_dialog_show is missing + assert_privileges('dialog_accord') # feature like miq_ae_customization_service_dialog_show is missing nodes = params[:id].split("-") record = Dialog.find_by(:id =>nodes.last) self.x_active_accord = "dialogs" @@ -330,8 +330,8 @@ def no_items_selected?(field_name) end def rebuild_toolbars(presenter) - unless @in_a_form - c_tb = build_toolbar(center_toolbar_filename) if center_toolbar_filename + if !@in_a_form && center_toolbar_filename + c_tb = build_toolbar(center_toolbar_filename) end presenter.set_visibility(c_tb.present?, :toolbar) diff --git a/app/controllers/ops_controller/diagnostics.rb b/app/controllers/ops_controller/diagnostics.rb index a5bde2d713f1..8d64fd1db5b4 100644 --- a/app/controllers/ops_controller/diagnostics.rb +++ b/app/controllers/ops_controller/diagnostics.rb @@ -11,10 +11,10 @@ def diagnostics_tree_select @record = MiqServer.find(id) when "role" @record = ServerRole.find(id) - @rec_status = @record.assigned_server_roles.find_by(:active => true) ? "active" : "stopped" if @record.class == ServerRole + @rec_status = @record.assigned_server_roles.find_by(:active => true) ? "active" : "stopped" if @record.instance_of?(ServerRole) when "asr" @record = AssignedServerRole.find(id) - @rec_status = @record.assigned_server_roles.find_by(:active => true) ? "active" : "stopped" if @record.class == ServerRole + @rec_status = @record.assigned_server_roles.find_by(:active => true) ? "active" : "stopped" if @record.instance_of?(ServerRole) end @sb[:diag_selected_model] = @record.class.to_s @sb[:diag_selected_id] = @record.id @@ -100,7 +100,7 @@ def log_depot_edit build_supported_depots_for_select log_protocol = params[:log_protocol] - protocols = FileDepot.supported_depots.map { |k, _v| [k, k.constantize] }.to_h + protocols = FileDepot.supported_depots.transform_values { |v| some_value } raise _('Invalid or unsupported file depot type.') unless protocols.key?(log_protocol) depot = @record.log_file_depot.instance_of?(protocols[log_protocol]) ? @record.log_file_depot : @record.build_log_file_depot(:type => log_protocol) @@ -132,7 +132,7 @@ def log_depot_edit begin log_protocol = params[:log_protocol] - protocols = FileDepot.supported_depots.map { |k, _v| [k, k.constantize] }.to_h + protocols = FileDepot.supported_depots.to_h { |k, _v| [k, k.constantize] } raise _("Unsupported log depot protocol: %{protocol}") % {:protocol => log_protocol} unless protocols.key?(log_protocol) protocols[log_protocol].validate_settings(settings) @@ -239,6 +239,27 @@ def cu_repair_field_changed def cu_repair assert_privileges("ops_diagnostics") + if params[:end_date].to_time < params[:start_date].to_time + render :json => {:status => :error, :message => _("End Date cannot be earlier than Start Date")} + else + # converting string to time, and then converting into user selected timezone + from = "#{params[:start_date]} #{params[:start_hour]}:#{params[:start_min]}:00".to_time.in_time_zone(params[:timezone]) + to = "#{params[:end_date]} #{params[:end_hour]}:#{params[:end_min]}:00".to_time.in_time_zone(params[:timezone]) + selected_zone = Zone.find(x_node.split('-').last) + begin + Metric::Capture.perf_capture_gap_queue(from, to, selected_zone) + rescue => bang + # Push msg and error flag + render :json => {:status => :error, :message => _("Error during 'C & U Gap Collection': %{message}") % {:message => bang.message}} + else + render :json => {:status => :success, :message => _("C & U Gap Collection successfully initiated")} + end + end + end + + def cu_repair_old + assert_privileges("ops_diagnostics") + return unless load_edit("curepair_edit__new", "replace_cell__explorer") if @edit[:new][:end_date].to_time < @edit[:new][:start_date].to_time @@ -285,22 +306,20 @@ def build_log_depot_json(log_depot) prefix, uri = log_depot[:uri].to_s.split('://') klass = @record.log_file_depot.try(:class) - log_depot_json = {:depot_name => log_depot[:name], - :uri => uri, - :uri_prefix => prefix, - :log_userid => log_depot.authentication_userid, - :log_protocol => klass.to_s} - log_depot_json + {:depot_name => log_depot[:name], + :uri => uri, + :uri_prefix => prefix, + :log_userid => log_depot.authentication_userid, + :log_protocol => klass.to_s} end def build_empty_log_depot_json - log_depot_json = {:depot_name => '', - :uri => '', - :uri_prefix => '', - :log_userid => '', - :log_password => '', - :log_protocol => ''} - log_depot_json + {:depot_name => '', + :uri => '', + :uri_prefix => '', + :log_userid => '', + :log_password => '', + :log_protocol => ''} end # to delete orphaned records for user that was delete from db @@ -519,7 +538,7 @@ def delete_server def process_server_deletion(server) server.destroy rescue => bang - add_flash(_("Server \"%{name}\": Error during 'destroy': ") % {:name => server.name} << bang.message, + add_flash((_("Server \"%{name}\": Error during 'destroy': ") % {:name => server.name}) << bang.message, :error) else AuditEvent.success( @@ -681,27 +700,28 @@ def diagnostics_set_form_vars elsif active_node && active_node.split('-').first == "svr" @selected_server ||= MiqServer.find(@sb[:selected_server_id]) # Reread the server record if @sb[:selected_server_id] == my_server.id - if @sb[:active_tab] == "diagnostics_evm_log" + case @sb[:active_tab] + when "diagnostics_evm_log" @log = fetch_local_log("evm") add_flash(_("Logs for this %{product} Server are not available for viewing") % {:product => Vmdb::Appliance.PRODUCT_NAME}, :warning) if @log.blank? @msg_title = _("ManageIQ") @refresh_action = "refresh_log" @download_action = "fetch_log" - elsif @sb[:active_tab] == "diagnostics_audit_log" + when "diagnostics_audit_log" @log = fetch_local_log("audit") add_flash(_("Logs for this %{product} Server are not available for viewing") % {:product => Vmdb::Appliance.PRODUCT_NAME}, :warning) if @log.blank? @msg_title = _("Audit") @refresh_action = "refresh_audit_log" @download_action = "fetch_audit_log" - elsif @sb[:active_tab] == "diagnostics_production_log" + when "diagnostics_production_log" @log = fetch_local_log(Rails.env) add_flash(_("Logs for this %{product} Server are not available for viewing") % {:product => Vmdb::Appliance.PRODUCT_NAME}, :warning) if @log.blank? @msg_title = @sb[:rails_log] @refresh_action = "refresh_production_log" @download_action = "fetch_production_log" - elsif @sb[:active_tab] == "diagnostics_summary" + when "diagnostics_summary" @selected_server = MiqServer.find(@sb[:selected_server_id]) - elsif @sb[:active_tab] == "diagnostics_workers" + when "diagnostics_workers" pm_get_workers @record = @selected_server else @@ -749,7 +769,7 @@ def build_server_tree(parent) end if @sb[:diag_selected_id] @record = @sb[:diag_selected_model].constantize.find(@sb[:diag_selected_id]) # Set the current record - @rec_status = @record.assigned_server_roles.find_by(:active => true) ? "active" : "stopped" if @record.class == ServerRole + @rec_status = @record.assigned_server_roles.find_by(:active => true) ? "active" : "stopped" if @record.instance_of?(ServerRole) end end @@ -758,25 +778,21 @@ def diagnostics_get_info @in_a_form = false node = x_node.downcase.split("-").first case node - when "root" - @sb[:diag_tree_type] ||= "roles" - @sb[:diag_selected_id] = nil - diagnostics_set_form_vars - when "z" + when "root", "z" @sb[:diag_tree_type] ||= "roles" @sb[:diag_selected_id] = nil - diagnostics_set_form_vars when "svr" @record = @selected_server = MiqServer.find(x_node.downcase.split("-").last) @sb[:selected_server_id] = @selected_server.id - diagnostics_set_form_vars end + + diagnostics_set_form_vars end def build_supported_depots_for_select not_supported_depots = %w[FileDepotS3 FileDepotSwift] - supported_depots = FileDepot.supported_depots.reject { |model, _desc| not_supported_depots.include?(model) } - @uri_prefixes = supported_depots.keys.map { |model| [model, model.constantize.uri_prefix] }.to_h + supported_depots = FileDepot.supported_depots.except(*not_supported_depots) + @uri_prefixes = supported_depots.keys.index_with { |model| model.constantize.uri_prefix } @supported_depots_for_select = {'' => _('')}.merge(supported_depots) end diff --git a/app/javascript/components/c-and-u-collections-form/index.jsx b/app/javascript/components/c-and-u-collections-form/index.jsx new file mode 100644 index 000000000000..7a863bdb757a --- /dev/null +++ b/app/javascript/components/c-and-u-collections-form/index.jsx @@ -0,0 +1,57 @@ +import React, {useState} from 'react'; +import MiqFormRenderer from '@@ddf'; +import createSchema from './schema'; +import NotificationMessage from '../notification-message'; + +// import handleFailure from '../../helpers/handle-failure'; + +const DiagnosticsCURepairForm = ({ timezones }) => { + + const [data, setData] = useState({ + response: undefined + }); + + /** Function to append 0 to beginning of a number if it is a single digit. */ + const padStart = (number) => String(number).padStart(2, '0'); + + /** This function format the date. */ + const formatDate = (date) => { + const newDate = new Date(date[0]); + return `${padStart(newDate.getMonth()+1)}/${padStart(newDate.getDate())}/${newDate.getFullYear()}`; + } + + const onSubmit = (values) => { + const data = { + timezone: values.timezone, + start_date: formatDate(values.startDate), + end_date: formatDate(values.endDate), + } + console.log(data); + http.post(`/ops/cu_repair?button=submit`, data, { skipErrors: [400, 500] }) + .then(({status, message}) => { + ['startDate', 'endDate'].forEach((item) => document.getElementById(item).value = ""); + setData({ + ...data, + response: { status, message } + }); + }); + }; + + const renderMessage = ({status, message}) => + + return ( + <> + { data.response && renderMessage(data.response) } + + { + `${__('Note')} : ${__('Gap Collection is only available for VMware vSphere Infrastructures')}` + } + + ) +}; + + +export default DiagnosticsCURepairForm; diff --git a/app/javascript/components/c-and-u-collections-form/schema.js b/app/javascript/components/c-and-u-collections-form/schema.js new file mode 100644 index 000000000000..3dbd16663cee --- /dev/null +++ b/app/javascript/components/c-and-u-collections-form/schema.js @@ -0,0 +1,39 @@ +import { componentTypes, validatorTypes } from '@@ddf'; + +const createSchema = (timezones) => ({ + title: __('Collection Options'), + fields: [ + { + component: componentTypes.SELECT, + id: 'timezone', + name: 'timezone', + label: __('Timezone'), + initialValue: 'UTC', + validate: [{ type: validatorTypes.REQUIRED }], + isRequired: true, + includeEmpty: true, + options: timezones.map((timezone) => ({ + label: timezone[0], + value: timezone[1], + })), + }, + { + component: 'date-picker', + id: 'startDate', + name: 'startDate', + label: __('Start Date'), + validate: [{ type: validatorTypes.REQUIRED }], + isRequired: true, + }, + { + component: 'date-picker', + id: 'endDate', + name: 'endDate', + label: __('End Date'), + validate: [{ type: validatorTypes.REQUIRED }], + isRequired: true, + } + ], +}); + +export default createSchema; diff --git a/app/javascript/packs/component-definitions-common.js b/app/javascript/packs/component-definitions-common.js index 8ea69b9092e4..b97e3c6f4d8a 100644 --- a/app/javascript/packs/component-definitions-common.js +++ b/app/javascript/packs/component-definitions-common.js @@ -161,6 +161,7 @@ import StorageServiceForm from '../components/storage-service-form'; import VolumeMappingForm from '../components/volume-mapping-form'; import ZoneForm from '../components/zone-form'; import AnsiblePlayBookEditCatalogForm from '../components/ansible-playbook-edit-catalog-form'; +import DiagnosticsCURepairForm from '../components/c-and-u-collections-form'; /** * Add component definitions to this file. @@ -331,3 +332,4 @@ ManageIQ.component.addReact('VolumeMappingForm', VolumeMappingForm); ManageIQ.component.addReact('VmCommonRenameForm', VmCommonRenameForm); ManageIQ.component.addReact('ZoneForm', ZoneForm); ManageIQ.component.addReact('AnsiblePlayBookEditCatalogForm', AnsiblePlayBookEditCatalogForm); +ManageIQ.component.addReact('DiagnosticsCURepairForm', DiagnosticsCURepairForm); diff --git a/app/javascript/spec/c-and-u-collections-form/c-and-u-collections-form.spec.js b/app/javascript/spec/c-and-u-collections-form/c-and-u-collections-form.spec.js new file mode 100644 index 000000000000..d8f408243b77 --- /dev/null +++ b/app/javascript/spec/c-and-u-collections-form/c-and-u-collections-form.spec.js @@ -0,0 +1,23 @@ +import React from 'react'; +import toJson from 'enzyme-to-json'; +import fetchMock from 'fetch-mock'; +import { act } from 'react-dom/test-utils'; + +import { mount } from '../helpers/mountForm'; +import DiagnosticsCURepairForm from '../../components/c-and-u-collections-form'; + +describe('DiagnosticsCURepairForm Component', () => { + afterEach(() => { + fetchMock.reset(); + fetchMock.restore(); + }); + it('Should render a new DiagnosticsCURepair form', async () => { + let wrapper; + await act(async () => { + wrapper = mount(); + }); + wrapper.update(); + expect(toJson(wrapper)).toMatchSnapshot(); + }); + +}); diff --git a/app/views/ops/_diagnostics_cu_repair_tab.html.erb b/app/views/ops/_diagnostics_cu_repair_tab.html.erb new file mode 100644 index 000000000000..024cb96dab16 --- /dev/null +++ b/app/views/ops/_diagnostics_cu_repair_tab.html.erb @@ -0,0 +1,2 @@ +<%= react('DiagnosticsCURepairForm', {:timezones => ViewHelper::ALL_TIMEZONES}) =%> + diff --git a/app/views/ops/_diagnostics_cu_repair_tab.html.haml b/app/views/ops/_diagnostics_cu_repair_tab.html.haml deleted file mode 100644 index 57a36f3a7361..000000000000 --- a/app/views/ops/_diagnostics_cu_repair_tab.html.haml +++ /dev/null @@ -1,41 +0,0 @@ -- if @sb[:active_tab] == "diagnostics_cu_repair" - - url = url_for_only_path(:action => 'cu_repair_field_changed') - - url_json = {:url => url}.to_json - -# Create from/to date JS vars to limit calendar starting from - :javascript - ManageIQ.calendar.calDateFrom = null; - ManageIQ.calendar.calDateTo = new Date(); - = render :partial => "layouts/flash_msg" - - %h3= _("Collection Options") - .form-horizontal - .form-group - %label.col-md-2.control-label - = _("Timezone") - .col-md-8 - = select_tag('cu_repair_tz', | - options_for_select(ViewHelper::ALL_TIMEZONES, @edit[:new][:timezone]), | - :class => "selectpicker") | - :javascript - miqInitSelectPicker(); - miqSelectPickerEvent('cu_repair_tz', "#{url}") - .form-group - %label.col-md-2.control-label - = _("Start Date") - .col-md-8 - = datepicker_input_tag("miq_date_1", | - @edit[:new][:start_date], | - :readonly => true, | - "data-date-orientation" => "bottom", | - "data-miq_observe_date" => url_json) | - .form-group - %label.col-md-2.control-label - = _("End Date") - .col-md-8 - = datepicker_input_tag("miq_date_2", | - @edit[:new][:end_date], | - :readonly => true, | - "data-date-orientation" => "bottom", | - "data-miq_observe_date" => url_json) | - - .note= _("Note: Gap Collection is only available for VMware vSphere Infrastructures")