Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed correct setting of record to get counts of relationships #5995

Merged
merged 2 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/controllers/cloud_tenant_dashboard_controller.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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