diff --git a/app/controllers/cloud_tenant_dashboard_controller.rb b/app/controllers/cloud_tenant_dashboard_controller.rb index eb3efac2515..92c0c8f366a 100644 --- a/app/controllers/cloud_tenant_dashboard_controller.rb +++ b/app/controllers/cloud_tenant_dashboard_controller.rb @@ -32,19 +32,19 @@ def aggregate_status_data private def collect_data - CloudTenantDashboardService.new(@tenant, self, EmsCloud).all_data + CloudTenantDashboardService.new(@tenant, self, CloudTenant).all_data end def recent_instances - CloudTenantDashboardService.new(@tenant, self, EmsCloud).recent_instances_data + CloudTenantDashboardService.new(@tenant, self, CloudTenant).recent_instances_data end def recent_images - CloudTenantDashboardService.new(@tenant, self, EmsCloud).recent_images_data + CloudTenantDashboardService.new(@tenant, self, CloudTenant).recent_images_data end def aggregate_status - CloudTenantDashboardService.new(@tenant, self, EmsCloud).aggregate_status_data + CloudTenantDashboardService.new(@tenant, self, CloudTenant).aggregate_status_data end def get_session_data diff --git a/app/services/cloud_tenant_dashboard_service.rb b/app/services/cloud_tenant_dashboard_service.rb index 4168a6e6b6e..b7920dd1e2b 100644 --- a/app/services/cloud_tenant_dashboard_service.rb +++ b/app/services/cloud_tenant_dashboard_service.rb @@ -1,11 +1,9 @@ -class CloudTenantDashboardService < EmsCloudDashboardService +class CloudTenantDashboardService < DashboardService include Mixins::CheckedIdMixin def initialize(tenant, controller, klass) - @ems_id = tenant.ext_management_system.id - @ems = find_record_with_rbac(klass, @ems_id) if @ems_id.present? - @tenant_id = tenant.id - @conroller = controller + @record_id = tenant.id + @record = tenant end def attributes_data @@ -47,7 +45,68 @@ def attributes_data format_data('cloud_tenant', attributes, attr_icon, attr_url, attr_hsh) end - def get_url(resource, _ems_id, attr_url) - "/#{resource}/show/#{@tenant_id}?display=#{attr_url}" + def get_url(resource, _record_id, attr_url) + "/#{resource}/show/#{@record_id}?display=#{attr_url}" + end + + def get_icon(tenant) + fileicon = tenant.ext_management_system.decorate.try(:fileicon) + fileicon ? ActionController::Base.helpers.image_path(fileicon) : nil + end + + def recent_images_data + recent_resources(MiqTemplate, _('Recent Images'), _('Images'), @record.miq_templates) + end + + def recent_instances_data + recent_resources(ManageIQ::Providers::CloudManager::Vm, _('Recent Instances'), _('Instances'), @record.vms) + end + + def aggregate_status_data + { + :aggStatus => aggregate_status + }.compact + end + + def aggregate_status + { + :status => status_data, + :attrData => attributes_data, + } + end + + def recent_resources(model, title, label, relation) + all_resources = recent_records(model, relation) + config = { + :title => title, + :label => label + } + + data = if all_resources.blank? + { + :dataAvailable => false, + :config => config + } + else + { + :dataAvailable => true, + :xData => all_resources.keys, + :yData => all_resources.values.map, + :config => config + } + end + { + :recentResources => data + }.compact + end + + def recent_records(model, relation) + db_table = model.arel_table + to_char_args = [db_table[:created_on], Arel::Nodes::SqlLiteral.new("'YYYY-MM-DD'")] + group_by_sql = Arel::Nodes::NamedFunction.new("to_char", to_char_args) + + relation.where(db_table[:created_on].gt(30.days.ago.utc)) + .group(group_by_sql.to_sql) + .count end end diff --git a/app/services/dashboard_service.rb b/app/services/dashboard_service.rb index 6ada1188530..03df4842246 100644 --- a/app/services/dashboard_service.rb +++ b/app/services/dashboard_service.rb @@ -54,4 +54,25 @@ def get_url_to_entity(controller, entity, ems_id = nil, ems = nil) end end end + + def format_data(resource, attributes, attr_icon, attr_url, attr_hsh) + attr_data = [] + attributes.each do |attr| + attr_data.push( + :id => "#{attr_hsh[attr]}_#{@record_id}", + :iconClass => attr_icon[attr], + :title => attr_hsh[attr], + :count => @record.send(attr).count, + :href => get_url(resource, @record_id, attr_url[attr]) + ) + end + attr_data + end + + def status_data + { + :iconImage => get_icon(@record), + :largeIcon => true, + } + end end