Skip to content

Commit

Permalink
Merge pull request #5995 from h-kataria/fixed_cloud_tenant_dashboard_…
Browse files Browse the repository at this point in the history
…view

Fixed correct setting of record to get counts of relationships

(cherry picked from commit 1492f6a)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1731735
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1487178
  • Loading branch information
himdel authored and simaishi committed Aug 16, 2019
1 parent e5b48f9 commit 482aa40
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
8 changes: 4 additions & 4 deletions app/controllers/cloud_tenant_dashboard_controller.rb
Expand Up @@ -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
Expand Down
73 changes: 66 additions & 7 deletions 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
Expand Down Expand Up @@ -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
21 changes: 21 additions & 0 deletions app/services/dashboard_service.rb
Expand Up @@ -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

0 comments on commit 482aa40

Please sign in to comment.