forked from ManageIQ/manageiq
/
chargeback_container_image.rb
108 lines (94 loc) · 4.29 KB
/
chargeback_container_image.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class ChargebackContainerImage < Chargeback
set_columns_hash(
:start_date => :datetime,
:end_date => :datetime,
:interval_name => :string,
:display_range => :string,
:chargeback_rates => :string,
:project_name => :string,
:image_name => :string,
:tag_name => :string,
:project_uid => :string,
:provider_name => :string,
:provider_uid => :string,
:archived => :string,
:cpu_cores_used_cost => :float,
:cpu_cores_used_metric => :float,
:fixed_compute_metric => :integer,
:fixed_compute_1_cost => :float,
:fixed_compute_2_cost => :float,
:fixed_2_cost => :float,
:fixed_cost => :float,
:memory_used_cost => :float,
:memory_used_metric => :float,
:net_io_used_cost => :float,
:net_io_used_metric => :float,
:total_cost => :float,
:entity => :binary
)
def self.build_results_for_report_ChargebackContainerImage(options)
# Options: a hash transformable to Chargeback::ReportOptions
# Find Project by id or get all projects
provider_id = options[:provider_id]
id = options[:entity_id]
raise "must provide option :entity_id and provider_id" if id.nil? && provider_id.nil?
@containers = if provider_id == "all"
Container.all
elsif id == "all"
Container.where('ems_id = ? or old_ems_id = ?', provider_id, provider_id)
else
Container.joins(:container_group).where('container_groups.container_project_id = ? or container_groups.old_container_project_id = ?', id, id)
end
@containers = @containers.includes(:container_project, :old_container_project, :container_image)
return [[]] if @containers.empty?
@data_index = {}
@containers.each do |c|
@data_index.store_path(:container_project, :by_container_id, c.id, c.container_project || c.old_container_project)
@data_index.store_path(:container_image, :by_container_id, c.id, c.container_image)
end
build_results_for_report_chargeback(options)
end
def self.default_key(metric_rollup_record, ts_key)
project = @data_index.fetch_path(:container_project, :by_container_id, metric_rollup_record.resource_id)
image = @data_index.fetch_path(:container_image, :by_container_id, metric_rollup_record.resource_id)
@options[:groupby] == 'project' ? "#{project.id}_#{ts_key}" : "#{project.id}_#{image.id}_#{ts_key}"
end
def self.image(consumption)
@data_index.fetch_path(:container_image, :by_container_id, consumption.resource_id)
end
def self.project(consumption)
@data_index.fetch_path(:container_project, :by_container_id, consumption.resource_id)
end
def self.where_clause(records, _options)
records.where(:resource_type => Container.name, :resource_id => @containers.pluck(:id))
end
def self.report_static_cols
%w(project_name image_name)
end
def self.report_col_options
{
"cpu_cores_used_cost" => {:grouping => [:total]},
"cpu_cores_used_metric" => {:grouping => [:total]},
"fixed_compute_metric" => {:grouping => [:total]},
"fixed_compute_1_cost" => {:grouping => [:total]},
"fixed_compute_2_cost" => {:grouping => [:total]},
"fixed_cost" => {:grouping => [:total]},
"memory_used_cost" => {:grouping => [:total]},
"memory_used_metric" => {:grouping => [:total]},
"net_io_used_cost" => {:grouping => [:total]},
"net_io_used_metric" => {:grouping => [:total]},
"total_cost" => {:grouping => [:total]}
}
end
private
def init_extra_fields(consumption)
self.project_name = self.class.project(consumption).name
# until image archiving is implemented
self.image_name = self.class.image(consumption).try(:full_name) || _('Deleted')
self.project_uid = self.class.project(consumption).ems_ref
self.provider_name = consumption.parent_ems.try(:name)
self.provider_uid = consumption.parent_ems.try(:guid)
self.archived = self.class.project(consumption).archived? ? _('Yes') : _('No')
self.entity = self.class.image(consumption)
end
end # class ChargebackContainerImage