forked from ManageIQ/manageiq-ui-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathems_infra_dashboard_service.rb
218 lines (195 loc) · 7.3 KB
/
ems_infra_dashboard_service.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class EmsInfraDashboardService
include UiServiceMixin
CPU_USAGE_PRECISION = 2 # 2 decimal points
def initialize(ems_id, controller)
@ems_id = ems_id
@ems = EmsInfra.find(@ems_id) unless @ems_id.blank?
@controller = controller
end
def all_data
{
:providers_link => get_url_to_entity(:ems_infra),
:status => status,
:providers => providers,
:heatmaps => heatmaps,
:recentHosts => recentHosts,
:recentVms => recentVms,
:ems_utilization => ems_utilization,
}.compact
end
def status
status_hsh = {
:ems_clusters => {
:title => openstack? ? _('Deplyoment Roles') : _('Cluster'),
:count => @ems.present? ? @ems.ems_clusters.count : EmsCluster.count,
:errorCount => 0,
:warningCount => 0,
:href => get_url_to_entity(:ems_cluster)
},
:hosts => {
:title => openstack? ? _('Nodes') : _('Hosts'),
:count => @ems.present? ? @ems.hosts.count : Host.where.not(:ext_management_system => nil).count,
:errorCount => 0,
:warningCount => 0,
:href => get_url_to_entity(:host)
},
:vms => {
:count => @ems.present? ? @ems.vms.count : VmInfra.where.not(:ext_management_system => nil).count,
:errorCount => 0,
:warningCount => 0,
:href => get_url_to_entity(:vm)
},
:miq_templates => {
:count => @ems.present? ?
@ems.miq_templates.count : MiqTemplate.where.not(:ext_management_system => nil).count,
:errorCount => 0,
:warningCount => 0,
:href => get_url_to_entity(:miq_template)
}
}
unless openstack?
status_hsh[:datastores] = {
:count => @ems.present? ? @ems.storages.count : Storage.count,
:errorCount => 0,
:warningCount => 0,
:href => get_url_to_entity(:storage)
}
end
status_hsh
end
def providers
provider_classes_to_ui_types = ManageIQ::Providers::InfraManager.subclasses.each_with_object({}) { |subclass, h|
name = subclass.name.split('::')[2]
h[subclass.name] = name.to_sym
}
providers = @ems.present? ? {@ems.type => 1} : ManageIQ::Providers::InfraManager.group(:type).count
result = {}
providers.each do |provider, count|
ui_type = provider_classes_to_ui_types[provider]
(result[ui_type] ||= build_provider_status(ui_type))[:count] += count
end
result.values
end
def build_provider_status(provider_type)
{
:count => 0,
:iconImage => icons[provider_type][:icon]
}
end
def get_url_to_entity(entity)
if @ems.present?
@controller.polymorphic_url(@ems, :display => entity.to_s.pluralize)
else
@controller.url_for_only_path(:action => 'show_list',
:controller => entity)
end
end
def heatmaps
# Get latest hourly rollup for each node.
cluster_ids = @ems.ems_clusters if @ems.present?
metrics = MetricRollup.latest_rollups(EmsCluster.name, cluster_ids)
metrics = metrics.where('timestamp > ?', 1.day.ago.utc).includes(:resource)
metrics = metrics.includes(:resource => [:ext_management_system]) unless @ems.present?
cluster_cpu_usage = []
cluster_memory_usage = []
metrics.each do |m|
next if m.resource.nil? # Metrics are purged asynchronously and might be missing their node
provider_name = @ems.present? ? @ems.name : m.resource.ext_management_system.name
cluster_cpu_usage << {
:id => m.resource.id,
:node => m.resource.name,
:provider => provider_name,
:unit => "Cores",
:total => m.derived_vm_numvcpus.present? ? m.derived_vm_numvcpus.round : nil,
:percent => m.cpu_usage_rate_average.present? ?
(m.cpu_usage_rate_average / 100.0).round(CPU_USAGE_PRECISION) : nil # pf accepts fractions 90% = 0.90
}
cluster_memory_usage << {
:id => m.resource.id,
:node => m.resource.name,
:provider => m.resource.ext_management_system.name,
:unit => "GB",
:total => m.derived_memory_available.present? ? (m.derived_memory_available / 1024).round : nil,
:percent => m.mem_usage_absolute_average.present? ?
(m.mem_usage_absolute_average / 100.0).round(CPU_USAGE_PRECISION) : nil # pf accepts fractions 90% = 0.90
}
end
{
:clusterCpuUsage => cluster_cpu_usage.presence,
:clusterMemoryUsage => cluster_memory_usage.presence,
:title => openstack? ? _('Deplyoment Roles Utilization') : _('Cluster Utilization')
}
end
def recentHosts
# Get recent hosts
all_hosts = recentRecords(Host)
{
:xData => all_hosts.keys,
:yData => all_hosts.values.map,
:title => openstack? ? _('Recent Nodes') : _('Recent Hosts'),
:label => openstack? ? _('Nodes') : _('Hosts'),
}
end
def recentVms
# Get recent VMs
all_vms = recentRecords(VmOrTemplate)
{
:xData => all_vms.keys,
:yData => all_vms.values.map
}
end
def recentRecords(model)
all_records = Hash.new(0)
records = model.where('created_on > ? and ems_id = ?', 30.days.ago.utc, @ems.id)
records = records.includes(:resource => [:ext_management_system]) unless @ems.present?
records.sort_by { |r| r.created_on }.uniq.each do |r|
date = r.created_on.strftime("%Y-%m-%d")
all_records[date] += model.where('created_on = ?', r.created_on).count
end
all_records
end
def ems_utilization
used_cpu = Hash.new(0)
used_mem = Hash.new(0)
total_cpu = Hash.new(0)
total_mem = Hash.new(0)
daily_provider_metrics.each do |metric|
date = metric.timestamp.strftime("%Y-%m-%d")
used_cpu[date] += metric.v_derived_cpu_total_cores_used if metric.v_derived_cpu_total_cores_used.present?
used_mem[date] += metric.derived_memory_used if metric.derived_memory_used.present?
total_cpu[date] += metric.derived_vm_numvcpus if metric.derived_vm_numvcpus.present?
total_mem[date] += metric.derived_memory_available if metric.derived_memory_available.present?
end
if used_cpu.any?
{
:cpu => {
:used => used_cpu.values.last.round,
:total => total_cpu.values.last.round,
:xData => used_cpu.keys,
:yData => used_cpu.values.map(&:round)
},
:mem => {
:used => (used_mem.values.last / 1024.0).round,
:total => (total_mem.values.last / 1024.0).round,
:xData => used_mem.keys,
:yData => used_mem.values.map { |m| (m / 1024.0).round }
}
}
else
{
:cpu => nil,
:mem => nil
}
end
end
def daily_provider_metrics
current_user = @controller.current_user
tp = TimeProfile.profile_for_user_tz(current_user.id, current_user.get_timezone) || TimeProfile.default_time_profile
@daily_metrics ||= Metric::Helper.find_for_interval_name('daily', tp)
.where(:resource => (@ems || ManageIQ::Providers::InfraManager.all))
.where('timestamp > ?', 30000.days.ago.utc).order('timestamp')
end
def openstack?
@ems.kind_of?(ManageIQ::Providers::Openstack::InfraManager)
end
end