/
vim_performance_analysis.rb
682 lines (594 loc) · 26.9 KB
/
vim_performance_analysis.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
module VimPerformanceAnalysis
class Planning
include Vmdb::Logging
attr_accessor :options, :vm
attr_reader :compute, :storage
def initialize(vm, options = {})
# Options
# :userid => User ID of requesting user for RBAC
# :targets => Defines explicit target Cluster or Host and Datastore - mutually exclusive with :target_tags
# :compute => Target cluster/host
# :storage => Target storage
# :target_tags => Defines tags that will be used to select targets - mutually exclusive with :targets
# :compute_filter => MiqSearch instance of id
# :compute_type => :EmsCluster || :Host - Select host or cluster as target
# :compute_tags => Array of arrays of management tags used for selecting targets. inner array elements are 'And'ed, outter arrays are 'OR'ed
# Example: [["/managed/department/accounting", "/managed/department/automotive"], ["/managed/environment/prod"], ["/managed/function/desktop"]]
# :storage_filter => MiqSearch instance of id
# :storage_tags => Same as above for storage selection only
#
# :range => Trend calculation options
# :days => Number of days back from daily_date
# :end_date => Ending date
#
# :vm_options =>
# :cpu =>
# :mode => :perf_trend (:perf_trend - Trend of perf data | :perf_latest - Perf data last row in range| :current - directly from object)
# :metric => :max_cpu_usage_rate_average
# :memory =>
# :mode => :perf_trend (:perf_trend | :perf_latest | :current)
# :metric => :max_mem_usage_absolute_average
# :storage =>
# :mode => :current (:perf_trend | :perf_latest | :current)
# :metric => :used_disk_storage | :allocated_disk_storage
#
# :target_options => Absence of one of the sub-hashes implies that key (:cpu, :memory, :storage) should not be used in calculation.
# :cpu =>
# :mode => :perf_trend (:perf_trend | :perf_latest | :current)
# :metric => :max_cpu_usage_rate_average
# :limit_col => :derived_cpu_available
# :limit_pct => 90
# :memory =>
# :mode => :perf_trend (:perf_trend | :perf_latest | :current)
# :metric => :max_mem_usage_absolute_average
# :limit_col => :derived_memory_available
# :limit_pct => 90
# :storage =>
# :mode => :current (:perf_trend | :perf_latest | :current)
# :metric => :v_used_space
# :limit_col => :total_space
# :limit_pct => 80
@vm = vm
@options = options
get_targets
end
def get_targets
if @options[:targets]
@compute = @options[:targets][:compute].to_miq_a
@storage = @options[:targets][:storage].to_miq_a
elsif @options[:target_tags]
topts = @options[:target_tags]
includes = topts[:compute_type].to_sym == :Host ? {:hardware => {}, :vms => {:hardware => {}}} : nil
search_options = {
:class => topts[:compute_type].to_s,
:results_format => :objects,
:include_for_find => includes,
:userid => @options[:userid],
:miq_group_id => @options[:miq_group_id],
}
if topts[:compute_filter]
search = if topts[:compute_filter].kind_of?(MiqSearch)
topts[:compute_filter]
else
MiqSearch.find(topts[:compute_filter])
end
search_options[:filter] = search.filter
elsif topts[:compute_tags]
search_options[:tag_filters] = {"managed" => topts[:compute_tags]}
end
@compute, _attrs = Rbac.search(search_options)
MiqPreloader.preload(@compute, :storages)
stores = @compute.collect { |c| storages_for_compute_target(c) }.flatten.uniq
filter_options = {:class => Storage, :userid => @options[:userid], :miq_group_id => @options[:miq_group_id]}
if topts[:storage_filter]
search = if topts[:storage_filter].kind_of?(MiqSearch)
topts[:storage_filter]
else
MiqSearch.find(topts[:storage_filter])
end
filter_options[:filter] = search.filter
elsif topts[:storage_tags]
filter_options[:tag_filters] = {"managed" => topts[:storage_tags]}
end
@storage = Rbac.filtered(stores, filter_options)
end
return @compute, @storage
end
def storages_for_compute_target(target)
return target.storages if target.kind_of?(Host)
if target.kind_of?(EmsCluster)
return target.hosts.collect(&:storages).flatten.compact
else
raise "unable to get storages for #{target.class}"
end
end
# This method answers C&U Planning question 2
def vm_how_many_more_can_fit(options = {})
options ||= @options
vm_needs = get_vm_needs
result = []
return result, vm_needs if @compute.blank?
if @compute.first.kind_of?(EmsCluster)
# For clusters, to allow for fragmentation in calculations, calculate using child hosts and aggregate at the end.
@hosts_to_cluster = {}
MiqPreloader.preload(@compute, :hosts => [:hardware, {:vms => :hardware}])
compute_hosts = @compute.collect do |c|
c.hosts.each { |h| @hosts_to_cluster[h.id] = c }
c.hosts
end.flatten
else
MiqPreloader.preload(@compute, [:hardware, {:vms => :hardware}])
compute_hosts = @compute
end
# Set :only_cols for target daily requested cols for better performance
options[:ext_options][:only_cols] = [:cpu, :memory, :storage].collect { |t| [options[:target_options].fetch_path(t, :metric), options[:target_options].fetch_path(t, :limit_col)] }.flatten.compact
compute_hosts.each do |c|
hash = {:target => c}
count_hash = {}
need_compute_perf = VimPerformanceAnalysis.needs_perf_data?(options[:target_options])
start_time, end_time = get_time_range(options[:range])
compute_perf = VimPerformanceAnalysis.get_daily_perf(c, start_time, end_time, options[:ext_options]) if need_compute_perf
unless need_compute_perf && compute_perf.blank?
ts = compute_perf.last.timestamp if compute_perf
[:cpu, :vcpus, :memory].each do |type|
next if vm_needs[type].nil? || options[:target_options][type].nil?
if type == :vcpus && vm_needs[type] > c.total_vcpus
count_hash[type] = {:total => 0}
next
end
avail, usage = compute_offers(compute_perf, ts, options[:target_options][type], type, c)
count_hash[type] = {:total => can_fit(avail, usage, vm_needs[type])}
end
unless vm_needs[:storage].nil? || options[:target_options][:storage].nil?
details = []
total = 0
storages_for_compute_target(c).each do |s|
avail, usage = storage_offers(compute_perf, ts, options[:target_options][:storage], s)
fits = can_fit(avail, usage, vm_needs[:storage])
details << {s.id => fits}
total += fits unless fits.nil?
end
count_hash[:storage] = {:total => total, :details => details}
end
end
total = nil
count_hash.each_value do |v|
next if v[:total].nil?
total = v[:total] if total.nil? || total > v[:total]
end
count_hash[:total] = {:total => total}
hash[:count] = count_hash
result << hash
end
result = how_many_more_can_fit_host_to_cluster_results(result) if @compute.first.kind_of?(EmsCluster)
# Returns an array of hashes
# [{:target => Host/Cluster Object, :count => {:total => {:total => Overall number of instances of provided VM that will fit in the target},
# :cpu => {:total => Count based on CPU},
# :memory => {:total => Count based on memory},
# :storage => {:total => Count based on storage, :details => [{<storage.id> => Count for this storage}, ...],
# ...]
return result, vm_needs
end
def how_many_more_can_fit_host_to_cluster_results(result)
nh = {}
result.each do |v|
cluster = @hosts_to_cluster[v[:target].id]
nh[cluster.id] ||= {:target => cluster}
[:cpu, :vcpus, :memory, :storage, :total].each do |type|
next if v[:count][type].nil?
nh[cluster.id][:count] ||= {}
nh[cluster.id][:count][type] ||= {:total => 0}
chash = nh[cluster.id][:count][type]
hhash = v[:count][type]
unless type == :storage
chash[:total] += hhash[:total] unless hhash[:total].nil?
else
# build up array of storage details unique by storage Id
chash[:details] ||= []
hhash[:details].each do |h|
id, val = h.to_a.flatten
next if chash[:details].find { |i| i.keys.first == id }
chash[:details] << h
end
end
end
end
result = []
nh.each do |_cid, v|
chash = v[:count][:storage]
# Calculate storage total based on merged counts.
chash[:total] = chash[:details].inject(0) do|t, h|
k, val = h.to_a.flatten
t += val
end if v[:count].key?(:storage)
#
chash = v[:count]
[:cpu, :vcpus, :memory, :storage].each do |type|
next unless chash.key?(type)
chash[:total][:total] = chash[type][:total] if chash[type][:total] < chash[:total][:total]
end
result << v
end
result
end
VM_CONSUMES_METRIC_DEFAULT = {
:cpu => {
:used => {:metric => :max_cpu_usagemhz_rate_average, :mode => :perf_trend},
:reserved => {:metric => :cpu_reserve, :mode => :current},
:allocated => nil,
:manual => {:value => nil, :mode => :manual}
},
:vcpus => {
:used => {:metric => :num_cpu, :mode => :current},
:reserved => {:metric => :num_cpu, :mode => :current},
:allocated => {:metric => :num_cpu, :mode => :current},
:manual => {:value => nil, :mode => :manual}
},
:memory => {
:used => {:metric => :max_derived_memory_used, :mode => :perf_trend},
:reserved => {:metric => :memory_reserve, :mode => :current},
:allocated => {:metric => :ram_size, :mode => :current},
:manual => {:value => nil, :mode => :manual}
},
:storage => {
:used => {:metric => :used_disk_storage, :mode => :current},
:reserved => {:metric => :provisioned_storage, :mode => :current},
:allocated => {:metric => :allocated_disk_storage, :mode => :current},
:manual => {:value => nil, :mode => :manual}
}
}
##########################################################
# :vm_options =>
# :cpu =>
# :mode => :perf_trend (:perf_trend - Trend of perf data | :perf_latest - Perf data last row in range| :current - directly from object)
# :metric => :max_cpu_usage_rate_average
# :memory =>
# :mode => :perf_trend (:perf_trend | :perf_latest | :current)
# :metric => :max_mem_usage_absolute_average
# :storage =>
# :mode => :current (:perf_trend | :perf_latest | :current)
# :metric => :used_disk_storage | :allocated_disk_storage
##########################################################
def get_vm_needs
options = @options
vm_perf = nil
if VimPerformanceAnalysis.needs_perf_data?(options[:vm_options])
# Get VM performance data
start_time, end_time = get_time_range(options[:range])
# Set :only_cols for VM daily requested cols for better performance
options[:ext_options] ||= {}
options[:ext_options][:only_cols] = [:cpu, :vcpus, :memory, :storage].collect { |t| options[:vm_options][t][:metric] if options[:vm_options][t] }.compact
vm_perf = VimPerformanceAnalysis.get_daily_perf(@vm, start_time, end_time, options[:ext_options])
end
@vm_needs = {}
vm_ts = vm_perf.last.timestamp unless vm_perf.blank?
[:cpu, :vcpus, :memory, :storage].each do |type|
@vm_needs[type] = vm_consumes(vm_perf, vm_ts, options[:vm_options][type], type)
end
@vm_needs
end
def vm_consumes(perf, ts, options, type, vm = @vm)
return nil if options.nil?
options[:metric] ||= VM_CONSUMES_METRIC_DEFAULT[type][:used][:metric]
options[:mode] ||= VM_CONSUMES_METRIC_DEFAULT[type][:used][:metric]
return options[:value] if options[:mode] == :manual
measure_object(vm, options[:mode], options[:metric], perf, ts, type)
end
COMPUTE_OFFERS_MODE_DEFAULT = {
:cpu => :perf_trend,
:memory => :perf_trend,
:storage => :current
}
COMPUTE_OFFERS_METRIC_DEFAULT = {
:cpu => :max_cpu_usagemhz_rate_average,
:memory => :max_derived_memory_used,
:storage => :v_used_space
}
COMPUTE_OFFERS_RESERVE_METRIC_DEFAULT = {
:cpu => :total_vm_cpu_reserve,
:memory => :total_vm_memory_reserve,
}
COMPUTE_OFFERS_LIMIT_COL_DEFAULT = {
:cpu => :derived_cpu_available,
:memory => :derived_memory_available,
:storage => :total_space
}
COMPUTE_OFFERS_LIMIT_PCT_DEFAULT = {
:cpu => 90,
:memory => 90,
:storage => 80
}
##########################################################
# :target_options => Absence of one of the sub-hashes implies that key (:cpu, :memory, :storage) should not be used in calculation.
# :cpu =>
# :mode => :perf_trend (:perf_trend | :perf_latest | :current)
# :metric => :max_cpu_usage_rate_average
# :limit_col => :derived_cpu_available
# :limit_pct => 90
# :vcpus =>
# :mode => :current (:perf_trend | :perf_latest | :current)
# :metric => Not applicable
# :limit_col => Not applicable
# :limit_ratio => 20
# :memory =>
# :mode => :perf_trend (:perf_trend | :perf_latest | :current)
# :metric => :max_mem_usage_absolute_average
# :limit_col => :derived_memory_available
# :limit_pct => 90
# :storage =>
# :mode => :current (:perf_trend | :perf_latest | :current)
# :metric => :v_used_space
# :limit_col => :total_space
# :limit_pct => 80
##########################################################
def offers(perf, ts, options, type, target)
return nil if options.nil?
options[:mode] ||= COMPUTE_OFFERS_MODE_DEFAULT[type]
options[:metric] ||= COMPUTE_OFFERS_METRIC_DEFAULT[type]
options[:reserve] ||= COMPUTE_OFFERS_RESERVE_METRIC_DEFAULT[type]
options[:limit_col] ||= COMPUTE_OFFERS_LIMIT_COL_DEFAULT[type]
options[:limit_pct] ||= COMPUTE_OFFERS_LIMIT_PCT_DEFAULT[type]
if type == :vcpus
# Example:
# cores = 5
# total_vcpus = 20
# limit_ratio = 10
# vcpus_per_core = (total_vcpus / cores) => 20 / 5 = 4
# avail = (limit_ratio - vcpus_per_core) * cores => (10 - 4) * 5 = 30
usage = 0
reserve = 0
avail = (options[:limit_ratio] - target.vcpus_per_core) * target.total_vcpus
else
usage = measure_object(target, options[:mode], options[:metric], perf, ts, type) || 0
reserve = measure_object(target, :current, options[:reserve], perf, ts, type) || 0
avail = measure_object(target, options[:mode], options[:limit_col], perf, ts, type) || 0
avail = (avail * (options[:limit_pct] / 100.0)) unless avail.nil? || options[:limit_pct].blank?
end
usage = (usage > reserve) ? usage : reserve # Take the greater of usage or total reserve of child VMs
[avail, usage]
end
def compute_offers(perf, ts, options, type, target)
offers(perf, ts, options, type, target)
end
def storage_offers(perf, ts, options, storage)
offers(perf, ts, options, :storage, storage)
end
def can_fit(avail, usage, need)
return nil if avail.nil? || usage.nil? || need.nil?
return 0 unless avail > usage && need > 0
fits = (avail - usage) / need
fits.truncate
end
def measure_object(obj, mode, col, perf, ts, type)
return 0 if col.nil?
case mode
when :current
obj.send(col)
when :perf_trend
VimPerformanceAnalysis.calc_trend_value_at_timestamp(perf, col, ts)
else
raise "Unsupported Mode (#{mode}) for #{obj.class} #{type} options"
end
end
def vm_recommended_targets
# This method answers C&U Planning question 3
# Returns a hash with 2 keys - :recommendations and :errors
# :recommendations => [ Array of hashes each containing a recomended pair of Host or Cluster and Datastore and the number of VMs that fit
# :cluster => Recommended target cluster - mutually exclusive with :host
# :host => Recommended target host - mutually exclusive with :cluster
# :datastore => Recommended target datastore
# :vm_count => Count of instances of VM that fit ]
# :errors => Array of user friendly error messages if any errors are encountered
# Return mocked up result for now
hash = {:datastore => Storage.first, :vm_count => 42}
if options[:target_tags][:type] == :cluster
hash[:cluster] = EmsCluster.first
else
hash[:host] = Host.first
end
{:recomendations => [hash], :errors => nil}
end
def get_time_range(range)
##########################################################
# :range => Trend calculation options
# :days => Number of days back from daily_date
# :end_date => Ending date
##########################################################
range[:days] ||= 20
range[:end_date] ||= Time.now
start_time = (range[:end_date].utc - range[:days].days)
end_time = Time.now.utc
return start_time, end_time
end
end # class Planning
# Helper methods
def self.needs_perf_data?(options)
options.each { |_k, v| return true if v && v[:mode] == :perf_trend }
false
end
def self.find_perf_for_time_period(obj, interval_name, options = {})
# Options
# :days => Number of days back from end_date. Used only if start_date not passed
# :start_date => Starting date
# :end_date => Ending date
# :conditions => ActiveRecord find conditions
options[:end_date] ||= Time.now.utc
start_time = (options[:start_date] || (options[:end_date].utc - options[:days].days)).utc
end_time = options[:end_date].utc
rel = if interval_name == "daily"
VimPerformanceDaily.find_entries(options[:ext_options])
else
klass, _meth = Metric::Helper.class_and_association_for_interval_name(interval_name)
klass.where(:capture_interval_name => interval_name)
end
rel = rel.where(options[:conditions]) if options[:conditions]
rel
.where("timestamp > ? and timestamp <= ?", start_time.utc, end_time.utc)
.where(:resource_type => obj.class.base_class.name, :resource_id => obj.id)
.order("timestamp")
.select(options[:select])
.to_a
end
def self.find_child_perf_for_time_period(obj, interval_name, options = {})
# Options
# :days => Number of days back from end_date. Used only if start_date not passed
# :start_date => Starting date
# :end_date => Ending date
# :conditions => ActiveRecord find conditions
start_time = (options[:start_date] || (options[:end_date].utc - options[:days].days)).utc
end_time = options[:end_date].utc
klass, = Metric::Helper.class_and_association_for_interval_name(interval_name)
user_cond = nil
user_cond = klass.send(:sanitize_sql_for_conditions, options[:conditions]) if options[:conditions]
cond = klass.send(:sanitize_sql_for_conditions, ["(timestamp > ? AND timestamp <= ?)", start_time.utc, end_time.utc])
cond += klass.send(:sanitize_sql_for_conditions, [" AND capture_interval_name = ?", interval_name]) unless interval_name == "daily"
cond = "(#{user_cond}) AND (#{cond})" if user_cond
if obj.kind_of?(MiqEnterprise) || obj.kind_of?(MiqRegion)
cond1 = klass.send(:sanitize_sql_for_conditions, :resource_type => "Storage", :resource_id => obj.storage_ids)
cond2 = klass.send(:sanitize_sql_for_conditions, :resource_type => "ExtManagementSystem", :resource_id => obj.ext_management_system_ids)
cond += " AND ((#{cond1}) OR (#{cond2}))"
else
parent_col = case obj
when Host then :parent_host_id
when EmsCluster then :parent_ems_cluster_id
when Storage then :parent_storage_id
when ExtManagementSystem then :parent_ems_id
else raise "unknown object type: #{obj.class}"
end
cond += " AND #{parent_col} = ?"
cond += " AND resource_type in ('Host', 'EmsCluster')" if obj.kind_of?(ExtManagementSystem)
cond = [cond, obj.id]
end
# puts "find_child_perf_for_time_period: cond: #{cond.inspect}"
if interval_name == "daily"
VimPerformanceDaily.find(:all, :conditions => cond, :ext_options => options[:ext_options], :select => options[:select])
else
klass.where(cond).select(options[:select]).to_a
end
end
def self.child_tags_over_time_period(obj, interval_name, options = {})
# Options
# :days => Number of days back from daily_date
# :end_date => Ending date
# Returns a hash:
# "Host/environment/prod" => "Host: Environment: Production",
# "Host/environment/dev" => "Host: Environment: Development"
classifications = Classification.hash_all_by_type_and_name
find_child_perf_for_time_period(obj, interval_name, options.merge(:conditions => "resource_type != 'VmOrTemplate' AND tag_names IS NOT NULL", :select => "resource_type, tag_names")).inject({}) do |h, p|
p.tag_names.split("|").each do |t|
next if t.starts_with?("power_state")
tag = "#{p.resource_type}/#{t}"
next if h.key?(tag)
c, e = t.split("/")
cat = classifications.fetch_path(c, :category)
cat_desc = cat.nil? ? c.titleize : cat.description
ent = cat.nil? ? nil : classifications.fetch_path(c, :entry, e)
ent_desc = ent.nil? ? e.titleize : ent.description
h[tag] = "#{ui_lookup(:model => p.resource_type)}: #{cat_desc}: #{ent_desc}"
end
h
end
end
def self.group_perf_by_timestamp(obj, perfs, cols = nil)
cols ||= Metric::Rollup::ROLLUP_COLS
result = {}
counts = {}
perf_klass = nil
perfs.each do |p|
perf_klass ||= p.class
key = p.timestamp
result[key] ||= {}
counts[key] ||= {}
result[key][:timestamp] = key
result[key][:capture_interval_name] = p.capture_interval_name
result[key][:capture_interval] = p.capture_interval
result[key][:resource_type] = obj.class.base_class.name
result[key][:resource_id] = obj.id
result[key][:resource_name] = obj.name
cols.each do |col|
c = col.to_sym
next unless p.respond_to?(c) && p.send(c).kind_of?(Float)
result[key][c] ||= 0
counts[key][c] ||= 0
Metric::Aggregation.aggregate_for_column(c, nil, result[key], counts[key], p.send(c), :average)
end
end
result.each do |_k, h|
h[:min_max] = h.keys.find_all { |k| k.to_s.starts_with?("min") || k.to_s.starts_with?("max") }.inject({}) do |mm, k|
val = h.delete(k)
mm[k] = val unless val.nil?
mm
end
end
result.inject([]) do |recs, k|
ts, v = k
cols.each do |c|
next unless v[c].kind_of?(Float)
Metric::Aggregation.process_for_column(c, nil, v, counts[k], true, :average)
end
recs.push(perf_klass.new(v))
recs
end
end
def self.calc_slope_from_data(recs, x_attr, y_attr)
recs.sort! { |a, b| a.send(x_attr) <=> b.send(x_attr) } if recs.first.respond_to?(x_attr)
y_array, x_array = recs.inject([]) do |arr, r|
arr[0] ||= []; arr[1] ||= []
next(arr) unless r.respond_to?(x_attr) && r.respond_to?(y_attr)
if r.respond_to?(:inside_time_profile) && r.inside_time_profile == false
_log.debug("Class: [#{r.class}], [#{r.resource_type} - #{r.resource_id}], Timestamp: [#{r.timestamp}] is outside of time profile")
next(arr)
end
arr[0] << r.send(y_attr).to_f
# arr[1] << r.send(x_attr).to_i # Calculate normal way by using the integer value of the timestamp
adj_x_attr = "time_profile_adjusted_#{x_attr}"
if r.respond_to?(adj_x_attr)
r.send("#{adj_x_attr}=", (recs.first.send(x_attr).to_i + arr[1].length.days.to_i))
arr[1] << r.send(adj_x_attr).to_i # Caculate by using the number of days out from the first timestamp
else
arr[1] << r.send(x_attr).to_i
end
arr
end
begin
slope_arr = MiqStats.slope(x_array.to_miq_a, y_array.to_miq_a)
rescue ZeroDivisionError
slope_arr = []
rescue => err
_log.warn("#{err.message}, calculating slope")
slope_arr = []
end
slope_arr
end
def self.get_daily_perf(obj, start_time, end_time, options)
cond = ["resource_type = ? and resource_id = ? and (timestamp > ? and timestamp <= ?)", obj.class.base_class.name, obj.id, start_time.utc, end_time.utc]
results = VimPerformanceDaily.find(:all, :conditions => cond, :order => "timestamp", :ext_options => options)
# apply time profile to returned records if one was specified
results.each { |rec| rec.apply_time_profile(options[:time_profile]) if rec.respond_to?(:apply_time_profile) } unless options[:time_profile].nil?
results
end
def self.calc_trend_value_at_timestamp(recs, attr, timestamp)
slope, yint = calc_slope_from_data(recs, :timestamp, attr)
return nil if slope.nil?
begin
return MiqStats.solve_for_y(timestamp.to_f, slope, yint)
rescue RangeError
return nil
rescue => err
_log.warn("#{err.message}, calculating trend value")
return nil
end
end
def self.calc_timestamp_at_trend_value(recs, attr, value)
slope, yint = calc_slope_from_data(recs, :timestamp, attr)
return nil if slope.nil?
begin
return Time.at(MiqStats.solve_for_x(value.to_f, slope, yint)).utc
rescue RangeError
return nil
rescue => err
_log.warn("#{err.message}, calculating timestamp at trend value")
return nil
end
end
end # module VimPerformanceAnalysis