ovirt_metrics 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0972797c4ecb7eddabc08532a7890321e856027
4
+ data.tar.gz: ec1207a571b31f0a97edc69c49f99ce6cbf72910
5
+ SHA512:
6
+ metadata.gz: 57da84932314838e52e990ffe4c55ec49a8066e1f7d3f1b49b565f66a587aa384f8b7a16e327da8a583a6d74ddb5f69fc277069987103d2fb277aed018887ff4
7
+ data.tar.gz: 3a1c778df4042d75041f418391d10a9ac13faf685e251f7f5507d264a9a2da7b134b5938907144b8559cd4a94143ddce5d0b2ea67b8991fed1dae76864d132fa
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
- --format documentation
2
+ --warnings
3
+ --require spec_helper
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # OvirtMetrics
2
2
 
3
- [![Build Status](https://travis-ci.org/ManageIQ/ovirt_metrics.png?branch=master)](https://travis-ci.org/ManageIQ/ovirt_metrics)
4
- [![Code Climate](https://codeclimate.com/github/ManageIQ/ovirt_metrics.png)](https://codeclimate.com/github/ManageIQ/ovirt_metrics)
5
- [![Coverage Status](https://coveralls.io/repos/ManageIQ/ovirt_metrics/badge.png?branch=master)](https://coveralls.io/r/ManageIQ/ovirt_metrics)
3
+ [![Gem Version](https://badge.fury.io/rb/ovirt_metrics.svg)](http://badge.fury.io/rb/ovirt_metrics)
4
+ [![Build Status](https://travis-ci.org/ManageIQ/ovirt_metrics.svg?branch=master)](https://travis-ci.org/ManageIQ/ovirt_metrics)
5
+ [![Code Climate](http://img.shields.io/codeclimate/github/ManageIQ/ovirt_metrics.svg)](https://codeclimate.com/github/ManageIQ/ovirt_metrics)
6
+ [![Coverage Status](http://img.shields.io/coveralls/ManageIQ/ovirt_metrics.svg)](https://coveralls.io/r/ManageIQ/ovirt_metrics)
7
+ [![Dependency Status](https://gemnasium.com/ManageIQ/ovirt_metrics.svg)](https://gemnasium.com/ManageIQ/ovirt_metrics)
6
8
 
7
9
  OvirtMetrics is an ActiveRecord-based gem for reading the oVirt History database.
8
10
 
@@ -14,5 +14,11 @@ module OvirtMetrics
14
14
  other.primary_key = :history_id
15
15
  other.table_name = other.name.split('::').last.underscore
16
16
  end
17
+
18
+ def self.with_time_range(start_time = nil, end_time = nil)
19
+ return all if start_time.nil?
20
+ where(:history_datetime => (start_time..(end_time || Time.now.utc)))
21
+ end
22
+
17
23
  end
18
- end
24
+ end
data/lib/ovirt_metrics.rb CHANGED
@@ -9,6 +9,7 @@ Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), "models", "*.rb")))
9
9
  module OvirtMetrics
10
10
  DEFAULT_HISTORY_DATABASE_NAME = "ovirt_engine_history".freeze
11
11
  DEFAULT_HISTORY_DATABASE_NAME_3_0 = "rhevm_history".freeze
12
+ VM_NOT_RUNNING = 0
12
13
 
13
14
  def self.establish_connection(opts)
14
15
  self.connect(opts)
@@ -41,100 +42,79 @@ module OvirtMetrics
41
42
  metrics = query_vm_realtime_metrics(vm_id, start_time, end_time)
42
43
  disk_metrics = query_vm_disk_realtime_metrics(vm_id, start_time, end_time)
43
44
  nic_metrics = query_vm_nic_realtime_metrics(vm_id, start_time, end_time)
44
- hashes = vm_realtime_metrics_to_hashes(metrics, disk_metrics, nic_metrics)
45
+ vm_realtime_metrics_to_hashes(metrics, disk_metrics, nic_metrics)
45
46
  end
46
47
 
47
48
  def self.host_realtime(host_id, start_time = nil, end_time = nil)
48
49
  metrics = query_host_realtime_metrics(host_id, start_time, end_time).all
49
50
  nic_metrics = query_host_nic_realtime_metrics(host_id, start_time, end_time)
50
- hashes = host_realtime_metrics_to_hashes(metrics, nic_metrics)
51
+ host_realtime_metrics_to_hashes(metrics, nic_metrics)
51
52
  end
52
53
 
53
54
  private
54
55
 
55
56
  def self.query_host_realtime_metrics(host_id, start_time = nil, end_time = nil)
56
- query = HostSamplesHistory.where(:host_id => host_id).includes(:host_configuration)
57
- query = query.where(:history_datetime => (start_time..(end_time || Time.now.utc))) unless start_time.nil?
58
- query
57
+ HostSamplesHistory.where(:host_id => host_id).includes(:host_configuration).with_time_range(start_time, end_time)
59
58
  end
60
59
 
61
60
  def self.query_host_nic_realtime_metrics(host_id, start_time = nil, end_time = nil)
62
61
  nic_ids = HostInterfaceConfiguration.where(:host_id => host_id).collect(&:host_interface_id)
63
- query = HostInterfaceSamplesHistory.where(:host_interface_id => nic_ids)
64
- query = query.where(:history_datetime => (start_time..(end_time || Time.now.utc))) unless start_time.nil?
65
- query
62
+ HostInterfaceSamplesHistory.where(:host_interface_id => nic_ids).with_time_range(start_time, end_time)
66
63
  end
67
64
 
68
65
  def self.host_realtime_metrics_to_hashes(metrics, nic_metrics)
69
- counters_by_id = {}
70
- counter_values_by_id_and_ts = {}
71
-
72
- nic_metrics = nic_metrics.group_by { |m| m.history_datetime }
73
-
74
- metrics.each do |metric|
75
- options = {
76
- :metric => metric,
77
- :nic => nic_metrics[metric.history_datetime]
78
- }
79
-
80
- href = "/api/hosts/#{metric.host_id}"
81
- counters_by_id[href] ||= {}
82
- values = {}
83
-
84
- HOST_COLUMN_DEFINITIONS.each do |evm_col, info|
85
- counters_by_id[href][info[:ovirt_key]] ||= info[:counter]
86
- values[info[:ovirt_key]] = info[:ovirt_method].call(options)
87
- end
88
-
89
- # For (temporary) symmetry with VIM API having 20-second intervals
90
- counter_values_by_id_and_ts[href] ||= {}
91
- [0, 20, 40].each do |t|
92
- counter_values_by_id_and_ts[href][(metric.history_datetime + t).utc.iso8601] = values
93
- end
94
- end
66
+ related_metrics = { }
67
+ related_metrics[:nic] = nic_metrics.group_by { |m| m.history_datetime }
95
68
 
96
- return counters_by_id, counter_values_by_id_and_ts
69
+ metrics_to_hashes(metrics, related_metrics, HOST_COLUMN_DEFINITIONS, :host_metric_to_href)
97
70
  end
98
71
 
99
72
  def self.query_vm_realtime_metrics(vm_id, start_time = nil, end_time = nil)
100
- query = VmSamplesHistory.where(:vm_id => vm_id).includes(:host_configuration)
101
- query = query.where(:history_datetime => (start_time..(end_time || Time.now.utc))) unless start_time.nil?
102
- query
73
+ VmSamplesHistory.where(:vm_id => vm_id).includes(:host_configuration).with_time_range(start_time, end_time)
103
74
  end
104
75
 
105
76
  def self.query_vm_disk_realtime_metrics(vm_id, start_time = nil, end_time = nil)
106
77
  disk_ids = DisksVmMap.where(:vm_id => vm_id).collect(&:vm_disk_id)
107
- query = VmDiskSamplesHistory.where(:vm_disk_id => disk_ids)
108
- query = query.where(:history_datetime => (start_time..(end_time || Time.now.utc))) unless start_time.nil?
109
- query
78
+ VmDiskSamplesHistory.where(:vm_disk_id => disk_ids).with_time_range(start_time, end_time)
110
79
  end
111
80
 
112
81
  def self.query_vm_nic_realtime_metrics(vm_id, start_time = nil, end_time = nil)
113
82
  nic_ids = VmInterfaceConfiguration.where(:vm_id => vm_id).collect(&:vm_interface_id)
114
- query = VmInterfaceSamplesHistory.where(:vm_interface_id => nic_ids)
115
- query = query.where(:history_datetime => (start_time..(end_time || Time.now.utc))) unless start_time.nil?
116
- query
83
+ VmInterfaceSamplesHistory.where(:vm_interface_id => nic_ids).with_time_range(start_time, end_time)
117
84
  end
118
85
 
119
86
  def self.vm_realtime_metrics_to_hashes(metrics, disk_metrics, nic_metrics)
87
+ related_metrics = { }
88
+ related_metrics[:disk] = disk_metrics.group_by { |m| m.history_datetime }
89
+ related_metrics[:nic] = nic_metrics.group_by { |m| m.history_datetime }
90
+
91
+ metrics_to_hashes(metrics, related_metrics, VM_COLUMN_DEFINITIONS, :vm_metric_to_href)
92
+ end
93
+
94
+ def self.vm_metric_to_href(metric)
95
+ "/api/vms/#{metric.vm_id}"
96
+ end
97
+
98
+ def self.host_metric_to_href(metric)
99
+ "/api/hosts/#{metric.host_id}"
100
+ end
101
+
102
+ def self.metrics_to_hashes(metrics, related_metrics, column_definitions, href_method)
120
103
  counters_by_id = {}
121
104
  counter_values_by_id_and_ts = {}
122
105
 
123
- disk_metrics = disk_metrics.group_by { |m| m.history_datetime }
124
- nic_metrics = nic_metrics.group_by { |m| m.history_datetime }
125
-
126
106
  metrics.each do |metric|
127
- options = {
128
- :metric => metric,
129
- :disk => disk_metrics[metric.history_datetime],
130
- :nic => nic_metrics[metric.history_datetime]
131
- }
107
+ options = { :metric => metric }
108
+ related_metrics.each { |key, related_metric| options[key] = related_metric[metric.history_datetime] }
109
+
110
+ href = self.send(href_method, metric)
132
111
 
133
- href = "/api/vms/#{metric.vm_id}"
134
112
  counters_by_id[href] ||= {}
135
113
  values = {}
136
114
 
137
- VM_COLUMN_DEFINITIONS.each do |evm_col, info|
115
+ column_definitions.each do |evm_col, info|
116
+ next if column_definitions == VM_COLUMN_DEFINITIONS && options[:metric].vm_status.to_i == VM_NOT_RUNNING
117
+
138
118
  counters_by_id[href][info[:ovirt_key]] ||= info[:counter]
139
119
  values[info[:ovirt_key]] = info[:ovirt_method].call(options)
140
120
  end
@@ -1,5 +1,5 @@
1
1
  module OvirtMetrics
2
- VM_COLUMN_DEFINITIONS = {
2
+ COMMON_COLUMN_DEFINITIONS = {
3
3
  "cpu_usage_rate_average" => {
4
4
  :ovirt_key => :cpu_usage_rate_average,
5
5
  :ovirt_method => lambda { |metrics| metrics[:metric].cpu_usage_percent.to_f },
@@ -41,6 +41,12 @@ module OvirtMetrics
41
41
  :capture_interval_name => "realtime"
42
42
  }
43
43
  },
44
+ }
45
+
46
+ VM_COLUMN_DEFINITIONS = {
47
+ "cpu_usage_rate_average" => COMMON_COLUMN_DEFINITIONS["cpu_usage_rate_average"],
48
+ "cpu_usagemhz_rate_average" => COMMON_COLUMN_DEFINITIONS["cpu_usagemhz_rate_average"],
49
+ "mem_usage_absolute_average" => COMMON_COLUMN_DEFINITIONS["mem_usage_absolute_average"],
44
50
 
45
51
  "disk_usage_rate_average" => {
46
52
  :ovirt_key => :disk_usage_rate_average,
@@ -72,47 +78,9 @@ module OvirtMetrics
72
78
  }
73
79
 
74
80
  HOST_COLUMN_DEFINITIONS = {
75
- "cpu_usage_rate_average" => {
76
- :ovirt_key => :cpu_usage_rate_average,
77
- :ovirt_method => lambda { |metrics| metrics[:metric].cpu_usage_percent.to_f },
78
- :counter => {
79
- :counter_key => "cpu_usage_rate_average",
80
- :instance => "",
81
- :capture_interval => "20",
82
- :precision => 1,
83
- :rollup => "average",
84
- :unit_key => "percent",
85
- :capture_interval_name => "realtime"
86
- }
87
- },
88
-
89
- "cpu_usagemhz_rate_average" => {
90
- :ovirt_key => :cpu_usagemhz_rate_average,
91
- :ovirt_method => lambda { |metrics| metrics[:metric].cpu_usagemhz_rate_average },
92
- :counter => {
93
- :counter_key => "cpu_usagemhz_rate_average",
94
- :instance => "",
95
- :capture_interval => "20",
96
- :precision => 2,
97
- :rollup => "average",
98
- :unit_key => "megahertz",
99
- :capture_interval_name => "realtime"
100
- }
101
- },
102
-
103
- "mem_usage_absolute_average" => {
104
- :ovirt_key => :mem_usage_absolute_average,
105
- :ovirt_method => lambda { |metrics| metrics[:metric].memory_usage_percent.to_f },
106
- :counter => {
107
- :counter_key => "mem_usage_absolute_average",
108
- :instance => "",
109
- :capture_interval => "20",
110
- :precision => 1,
111
- :rollup => "average",
112
- :unit_key => "percent",
113
- :capture_interval_name => "realtime"
114
- }
115
- },
81
+ "cpu_usage_rate_average" => COMMON_COLUMN_DEFINITIONS["cpu_usage_rate_average"],
82
+ "cpu_usagemhz_rate_average" => COMMON_COLUMN_DEFINITIONS["cpu_usagemhz_rate_average"],
83
+ "mem_usage_absolute_average" => COMMON_COLUMN_DEFINITIONS["mem_usage_absolute_average"],
116
84
 
117
85
  "net_usage_rate_average" => {
118
86
  :ovirt_key => :net_usage_rate_average,
@@ -1,3 +1,3 @@
1
1
  module OvirtMetrics
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,24 +1,20 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), %w{.. spec_helper}))
2
-
3
- require 'ovirt_metrics'
4
-
5
1
  describe OvirtMetrics::HostConfiguration do
6
2
  shared_examples_for "HostConfiguration" do
7
3
  context "#speed_in_mhz" do
8
4
  it "when cpu_model is nil" do
9
- described_class.new(:cpu_model => nil).speed_in_mhz.should be_nil
5
+ expect(described_class.new(:cpu_model => nil).speed_in_mhz).to be_nil
10
6
  end
11
7
 
12
8
  it "when cpu_model is in GHz" do
13
- described_class.new(:cpu_model => "Intel(R) Xeon(R) CPU E5506 @ 2.00GHz").speed_in_mhz.should == 2048.0
9
+ expect(described_class.new(:cpu_model => "Intel(R) Xeon(R) CPU E5506 @ 2.00GHz").speed_in_mhz).to eq(2048.0)
14
10
  end
15
11
 
16
12
  it "when cpu_model is in MHz" do
17
- described_class.new(:cpu_model => "Intel(R) Xeon(R) CPU E5506 @ 2.00MHz").speed_in_mhz.should == 2.0
13
+ expect(described_class.new(:cpu_model => "Intel(R) Xeon(R) CPU E5506 @ 2.00MHz").speed_in_mhz).to eq(2.0)
18
14
  end
19
15
 
20
16
  it "when cpu_model is some other string" do
21
- described_class.new(:cpu_model => "XXX").speed_in_mhz.should be_nil
17
+ expect(described_class.new(:cpu_model => "XXX").speed_in_mhz).to be_nil
22
18
  end
23
19
  end
24
20
  end
@@ -32,4 +28,4 @@ describe OvirtMetrics::HostConfiguration do
32
28
  before(:each) { load_rhev_31 }
33
29
  it_should_behave_like "HostConfiguration"
34
30
  end
35
- end
31
+ end
@@ -1,45 +1,40 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), %w{.. spec_helper}))
2
-
3
- require 'ovirt_metrics'
4
-
5
1
  describe OvirtMetrics::HostSamplesHistory do
6
2
  shared_examples_for "HostSamplesHistory" do
7
3
  context "#cpu_usagemhz_rate_average" do
8
4
 
9
5
  it "when host_configuration is nil" do
10
6
  host_history = described_class.new(:host_configuration => nil)
11
- host_history.cpu_usagemhz_rate_average.should == 0
7
+ expect(host_history.cpu_usagemhz_rate_average).to eq(0)
12
8
  end
13
9
 
14
10
  context "when host_configuration exists" do
15
11
  before(:each) { @host_configuration = OvirtMetrics::HostConfiguration.new }
16
12
  it "and speed_in_mhz and number_of_cores is nil" do
17
13
  host_history = described_class.new(:host_configuration => @host_configuration)
18
- host_history.cpu_usagemhz_rate_average.should == 0
14
+ expect(host_history.cpu_usagemhz_rate_average).to eq(0)
19
15
  end
20
16
 
21
17
  it "and speed_in_mhz is nil and number_of_cores is numeric" do
22
18
  host_history = described_class.new(:host_configuration => @host_configuration)
23
- @host_configuration.stub(:number_of_cores => 1)
24
- host_history.cpu_usagemhz_rate_average.should == 0
19
+ allow(@host_configuration).to receive_messages(:number_of_cores => 1)
20
+ expect(host_history.cpu_usagemhz_rate_average).to eq(0)
25
21
  end
26
22
 
27
23
  it "and speed_in_mhz is numeric and number_of_cores is nil" do
28
24
  host_history = described_class.new(:host_configuration => @host_configuration)
29
- @host_configuration.stub(:speed_in_mhz => 2048.0)
30
- host_history.cpu_usagemhz_rate_average.should == 0
25
+ allow(@host_configuration).to receive_messages(:speed_in_mhz => 2048.0)
26
+ expect(host_history.cpu_usagemhz_rate_average).to eq(0)
31
27
  end
32
28
 
33
29
  it "and speed_in_mhz is numeric and number_of_cores is numeric" do
34
- host_configuration = OvirtMetrics::HostConfiguration.new
35
- @host_configuration.stub(:speed_in_mhz => 2048.0)
36
- @host_configuration.stub(:number_of_cores => 2)
30
+ allow(@host_configuration).to receive_messages(:speed_in_mhz => 2048.0)
31
+ allow(@host_configuration).to receive_messages(:number_of_cores => 2)
37
32
 
38
33
  host_history = described_class.new(
39
34
  :cpu_usage_percent => 50,
40
35
  :host_configuration => @host_configuration
41
36
  )
42
- host_history.cpu_usagemhz_rate_average.should == 2048.0
37
+ expect(host_history.cpu_usagemhz_rate_average).to eq(2048.0)
43
38
  end
44
39
  end
45
40
  end
@@ -54,4 +49,4 @@ describe OvirtMetrics::HostSamplesHistory do
54
49
  before(:each) { load_rhev_31 }
55
50
  it_should_behave_like "HostSamplesHistory"
56
51
  end
57
- end
52
+ end
@@ -1,7 +1,3 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), %w{.. spec_helper}))
2
-
3
- require 'ovirt_metrics'
4
-
5
1
  describe OvirtMetrics::VmDiskSamplesHistory do
6
2
  KILOBYTE = 1024
7
3
  MEGABYTE = KILOBYTE * 1024
@@ -9,27 +5,27 @@ describe OvirtMetrics::VmDiskSamplesHistory do
9
5
  shared_examples_for "VmDiskSamplesHistory" do
10
6
  context ".disk_usage_rate_average_in_kilobytes_per_second" do
11
7
  it "when disk_metrics array is empty" do
12
- described_class.disk_usage_rate_average_in_kilobytes_per_second([]).should == 0.0
8
+ expect(described_class.disk_usage_rate_average_in_kilobytes_per_second([])).to eq(0.0)
13
9
  end
14
10
 
15
11
  it "when disk_metrics array has one element" do
16
12
  disk_metric = double("disk_metric")
17
- disk_metric.stub(:read_rate_bytes_per_second => 2.0 * MEGABYTE, :write_rate_bytes_per_second => 1.0 * MEGABYTE)
13
+ allow(disk_metric).to receive_messages(:read_rate_bytes_per_second => 2.0 * MEGABYTE, :write_rate_bytes_per_second => 1.0 * MEGABYTE)
18
14
  expected_result = (disk_metric.read_rate_bytes_per_second + disk_metric.write_rate_bytes_per_second) / KILOBYTE
19
- described_class.disk_usage_rate_average_in_kilobytes_per_second([disk_metric]).should == expected_result
15
+ expect(described_class.disk_usage_rate_average_in_kilobytes_per_second([disk_metric])).to eq(expected_result)
20
16
  end
21
17
 
22
18
  it "when disk_metrics array has two elements" do
23
19
  disk_metric1 = double("disk_metric")
24
- disk_metric1.stub(:read_rate_bytes_per_second => 2.0 * MEGABYTE, :write_rate_bytes_per_second => 1.0 * MEGABYTE)
20
+ allow(disk_metric1).to receive_messages(:read_rate_bytes_per_second => 2.0 * MEGABYTE, :write_rate_bytes_per_second => 1.0 * MEGABYTE)
25
21
 
26
22
  disk_metric2 = double("disk_metric")
27
- disk_metric2.stub(:read_rate_bytes_per_second => 2.0 * MEGABYTE, :write_rate_bytes_per_second => 5.0 * MEGABYTE)
23
+ allow(disk_metric2).to receive_messages(:read_rate_bytes_per_second => 2.0 * MEGABYTE, :write_rate_bytes_per_second => 5.0 * MEGABYTE)
28
24
 
29
25
  sum_m1 = disk_metric1.read_rate_bytes_per_second + disk_metric1.write_rate_bytes_per_second
30
26
  sum_m2 = disk_metric2.read_rate_bytes_per_second + disk_metric2.write_rate_bytes_per_second
31
27
  expected_result = (sum_m1 + sum_m2) / KILOBYTE / 2
32
- described_class.disk_usage_rate_average_in_kilobytes_per_second([disk_metric1, disk_metric2]).should == expected_result
28
+ expect(described_class.disk_usage_rate_average_in_kilobytes_per_second([disk_metric1, disk_metric2])).to eq(expected_result)
33
29
  end
34
30
  end
35
31
  end
@@ -43,4 +39,4 @@ describe OvirtMetrics::VmDiskSamplesHistory do
43
39
  before(:each) { load_rhev_31 }
44
40
  it_should_behave_like "VmDiskSamplesHistory"
45
41
  end
46
- end
42
+ end
@@ -1,32 +1,28 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), %w{.. spec_helper}))
2
-
3
- require 'ovirt_metrics'
4
-
5
1
  describe OvirtMetrics::VmSamplesHistory do
6
2
  shared_examples_for "VmSamplesHistory" do
7
3
  context "#cpu_usagemhz_rate_average" do
8
4
  it "when host_configuration is nil" do
9
5
  vm_history = described_class.new
10
- vm_history.stub(:host_configuration => nil)
6
+ allow(vm_history).to receive_messages(:host_configuration => nil)
11
7
 
12
- vm_history.cpu_usagemhz_rate_average.should == 0
8
+ expect(vm_history.cpu_usagemhz_rate_average).to eq(0)
13
9
  end
14
10
 
15
11
  context "when host_configuration exists" do
16
12
  it "and speed_in_mhz is nil" do
17
13
  vm_history = described_class.new(:host_configuration => OvirtMetrics::HostConfiguration.new)
18
- vm_history.cpu_usagemhz_rate_average.should == 0
14
+ expect(vm_history.cpu_usagemhz_rate_average).to eq(0)
19
15
  end
20
16
 
21
17
  it "and speed_in_mhz is not nil" do
22
18
  host_configuration = OvirtMetrics::HostConfiguration.new
23
- host_configuration.stub(:speed_in_mhz => 2048.0)
19
+ allow(host_configuration).to receive_messages(:speed_in_mhz => 2048.0)
24
20
 
25
21
  vm_history = described_class.new(
26
22
  :cpu_usage_percent => 50,
27
23
  :host_configuration => host_configuration
28
24
  )
29
- vm_history.cpu_usagemhz_rate_average.should == 1024.0
25
+ expect(vm_history.cpu_usagemhz_rate_average).to eq(1024.0)
30
26
  end
31
27
  end
32
28
  end
@@ -41,4 +37,4 @@ describe OvirtMetrics::VmSamplesHistory do
41
37
  before(:each) { load_rhev_31 }
42
38
  it_should_behave_like "VmSamplesHistory"
43
39
  end
44
- end
40
+ end
@@ -1,27 +1,23 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
2
-
3
- require 'ovirt_metrics'
4
-
5
1
  describe OvirtMetrics::NicMetrics do
6
2
  context ".net_usage_rate_average_in_kilobytes_per_second" do
7
3
  it "when nic_metrics array is empty" do
8
- described_class.net_usage_rate_average_in_kilobytes_per_second([]).should == 0.0
4
+ expect(described_class.net_usage_rate_average_in_kilobytes_per_second([])).to eq(0.0)
9
5
  end
10
6
 
11
7
  it "when nic_metrics array has one element" do
12
8
  nic_metric = double("nic_metric")
13
- nic_metric.stub(:receive_rate_percent => 90, :transmit_rate_percent => 10)
9
+ allow(nic_metric).to receive_messages(:receive_rate_percent => 90, :transmit_rate_percent => 10)
14
10
  expected = (OvirtMetrics::NicMetrics::GIGABYTE_PER_SECOND / 2) / 1024
15
- described_class.net_usage_rate_average_in_kilobytes_per_second([nic_metric]).should == expected
11
+ expect(described_class.net_usage_rate_average_in_kilobytes_per_second([nic_metric])).to eq(expected)
16
12
  end
17
13
 
18
14
  it "when nic_metrics array has multiple elements" do
19
15
  nic_metric1 = double("nic_metric")
20
- nic_metric1.stub(:receive_rate_percent => 90, :transmit_rate_percent => 10)
16
+ allow(nic_metric1).to receive_messages(:receive_rate_percent => 90, :transmit_rate_percent => 10)
21
17
  nic_metric2 = double("nic_metric")
22
- nic_metric2.stub(:receive_rate_percent => 90, :transmit_rate_percent => 10)
18
+ allow(nic_metric2).to receive_messages(:receive_rate_percent => 90, :transmit_rate_percent => 10)
23
19
  expected = (OvirtMetrics::NicMetrics::GIGABYTE_PER_SECOND / 2) / 1024
24
- described_class.net_usage_rate_average_in_kilobytes_per_second([nic_metric1, nic_metric2]).should == expected
20
+ expect(described_class.net_usage_rate_average_in_kilobytes_per_second([nic_metric1, nic_metric2])).to eq(expected)
25
21
  end
26
22
  end
27
- end
23
+ end
@@ -1,6 +1,69 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
1
+ describe OvirtMetrics do
2
+ shared_examples_for "OvirtMetrics" do
3
+ context ".vm_realtime" do
4
+ it "when vm_id finds no matches" do
5
+ expect(described_class.vm_realtime(42)).to eq([{}, {}])
6
+ end
2
7
 
3
- require 'ovirt_metrics'
8
+ it "when vm_id finds 1 match" do
9
+ assert_object_with_empty_samples_data("vm")
10
+ end
11
+ end
4
12
 
5
- describe OvirtMetrics do
6
- end
13
+ context ".host_realtime" do
14
+ it "when host_id finds no matches" do
15
+ expect(described_class.host_realtime(42)).to eq([{}, {}])
16
+ end
17
+
18
+ it "when host_id finds 1 match" do
19
+ assert_object_with_empty_samples_data("host")
20
+ end
21
+ end
22
+
23
+ def assert_object_with_empty_samples_data(type)
24
+ id = 42
25
+ status = 1
26
+ history_datetime = Time.now
27
+ klass = "OvirtMetrics::#{type.capitalize}SamplesHistory".constantize
28
+ id_key = "#{type}_id".to_sym
29
+ status_key = "#{type}_status".to_sym
30
+ record_attrs = {
31
+ id_key => id,
32
+ status_key => status,
33
+ :history_datetime => history_datetime,
34
+ }
35
+ record = klass.create!(record_attrs)
36
+
37
+ href = "/api/#{type.pluralize}/#{id}"
38
+ constant = "OvirtMetrics::#{type.upcase}_COLUMN_DEFINITIONS".constantize
39
+ column_definitions = constant.each_with_object({}) do |(_name, defn), hash|
40
+ key = defn[:ovirt_key]
41
+ value = defn[:counter]
42
+ hash[key] = value
43
+ end
44
+ columns = { href => column_definitions }
45
+
46
+ rows_hash = [0, 20, 40].each_with_object({}) do |offset, hash|
47
+ value = column_definitions.keys.each_with_object({}) { |key, col_hash| col_hash[key] = 0.0 }
48
+ key = (record.history_datetime + offset).utc.iso8601.to_s
49
+ hash[key] = value
50
+ end
51
+ rows = { href => rows_hash }
52
+
53
+ method = "#{type}_realtime"
54
+ expect(described_class.send(method, id)).to eq([columns, rows])
55
+ end
56
+
57
+ end
58
+
59
+ context "RHEV 3.0" do
60
+ before(:each) { load_rhev_30 }
61
+ it_should_behave_like "OvirtMetrics"
62
+ end
63
+
64
+ context "RHEV 3.1" do
65
+ before(:each) { load_rhev_31 }
66
+ it_should_behave_like "OvirtMetrics"
67
+ end
68
+
69
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,21 +1,87 @@
1
1
  # This file was generated by the `rspec --init` command. Conventionally, all
2
2
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
- # Require this file using `require "spec_helper"` to ensure that it is only
4
- # loaded once.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, make a
10
+ # separate helper file that requires this one and then use it only in the specs
11
+ # that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
5
15
  #
6
16
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
17
  RSpec.configure do |config|
8
- config.treat_symbols_as_metadata_keys_with_true_values = true
18
+ # The settings below are suggested to provide a good initial experience
19
+ # with RSpec, but feel free to customize to your heart's content.
20
+
21
+ # These two settings work together to allow you to limit a spec run
22
+ # to individual examples or groups you care about by tagging them with
23
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
24
+ # get run.
25
+ config.filter_run :focus
9
26
  config.run_all_when_everything_filtered = true
10
27
 
28
+ # Many RSpec users commonly either run the entire suite or an individual
29
+ # file, and it's useful to allow more verbose output when running an
30
+ # individual spec file.
31
+ if config.files_to_run.one?
32
+ # Use the documentation formatter for detailed output,
33
+ # unless a formatter has already been configured
34
+ # (e.g. via a command-line flag).
35
+ config.default_formatter = 'doc'
36
+ end
37
+
38
+ # Print the 10 slowest examples and example groups at the
39
+ # end of the spec run, to help surface which specs are running
40
+ # particularly slow.
41
+ # config.profile_examples = 10
42
+
11
43
  # Run specs in random order to surface order dependencies. If you find an
12
44
  # order dependency and want to debug it, you can fix the order by providing
13
45
  # the seed, which is printed after each run.
14
46
  # --seed 1234
15
- config.order = 'random'
47
+ config.order = :random
48
+
49
+ # Seed global randomization in this process using the `--seed` CLI option.
50
+ # Setting this allows you to use `--seed` to deterministically reproduce
51
+ # test failures related to randomization by passing the same `--seed` value
52
+ # as the one that triggered the failure.
53
+ Kernel.srand config.seed
54
+
55
+ # rspec-expectations config goes here. You can use an alternate
56
+ # assertion/expectation library such as wrong or the stdlib/minitest
57
+ # assertions if you prefer.
58
+ config.expect_with :rspec do |expectations|
59
+ # Enable only the newer, non-monkey-patching expect syntax.
60
+ # For more details, see:
61
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
62
+ expectations.syntax = :expect
63
+ end
64
+
65
+ # rspec-mocks config goes here. You can use an alternate test double
66
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
67
+ config.mock_with :rspec do |mocks|
68
+ # Enable only the newer, non-monkey-patching expect syntax.
69
+ # For more details, see:
70
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ mocks.syntax = :expect
72
+
73
+ # Prevents you from mocking or stubbing a method that does not exist on
74
+ # a real object. This is generally recommended.
75
+ mocks.verify_partial_doubles = true
76
+ end
16
77
  end
17
78
 
18
79
  require 'support/active_record'
19
80
 
20
- require 'coveralls'
21
- Coveralls.wear!
81
+ begin
82
+ require 'coveralls'
83
+ Coveralls.wear!
84
+ rescue LoadError
85
+ end
86
+
87
+ require 'ovirt_metrics'
@@ -34,9 +34,20 @@ end
34
34
  def load_rhev_30
35
35
  ActiveRecord::Schema.verbose = false
36
36
  load File.join(File.dirname(__FILE__), %w{.. schemas schema_rhev30.rb})
37
+ reset_models
37
38
  end
38
39
 
39
40
  def load_rhev_31
40
41
  ActiveRecord::Schema.verbose = false
41
42
  load File.join(File.dirname(__FILE__), %w{.. schemas schema_rhev31.rb})
43
+ reset_models
44
+ end
45
+
46
+ def reset_models
47
+ OvirtMetrics.constants.each do |c|
48
+ o = OvirtMetrics.const_get(c)
49
+ next if o == OvirtMetrics::OvirtHistory
50
+ next unless o.respond_to?(:ancestors) && o.ancestors.include?(ActiveRecord::Base)
51
+ o.reset_column_information
52
+ end
42
53
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ovirt_metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
5
- prerelease:
4
+ version: 1.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Oleg Barenboim
@@ -10,104 +9,92 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2013-04-25 00:00:00.000000000 Z
12
+ date: 2015-08-12 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: bundler
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ~>
18
+ - - "~>"
21
19
  - !ruby/object:Gem::Version
22
20
  version: '1.3'
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
- - - ~>
25
+ - - "~>"
29
26
  - !ruby/object:Gem::Version
30
27
  version: '1.3'
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: rake
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
- - - ! '>='
32
+ - - ">="
37
33
  - !ruby/object:Gem::Version
38
34
  version: '0'
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
- - - ! '>='
39
+ - - ">="
45
40
  - !ruby/object:Gem::Version
46
41
  version: '0'
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: rspec
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
- - - ! '>='
46
+ - - ">="
53
47
  - !ruby/object:Gem::Version
54
- version: '0'
48
+ version: '3.0'
55
49
  type: :development
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
- - - ! '>='
53
+ - - ">="
61
54
  - !ruby/object:Gem::Version
62
- version: '0'
55
+ version: '3.0'
63
56
  - !ruby/object:Gem::Dependency
64
57
  name: sqlite3
65
58
  requirement: !ruby/object:Gem::Requirement
66
- none: false
67
59
  requirements:
68
- - - ! '>='
60
+ - - ">="
69
61
  - !ruby/object:Gem::Version
70
62
  version: '0'
71
63
  type: :development
72
64
  prerelease: false
73
65
  version_requirements: !ruby/object:Gem::Requirement
74
- none: false
75
66
  requirements:
76
- - - ! '>='
67
+ - - ">="
77
68
  - !ruby/object:Gem::Version
78
69
  version: '0'
79
70
  - !ruby/object:Gem::Dependency
80
71
  name: coveralls
81
72
  requirement: !ruby/object:Gem::Requirement
82
- none: false
83
73
  requirements:
84
- - - ! '>='
74
+ - - ">="
85
75
  - !ruby/object:Gem::Version
86
76
  version: '0'
87
77
  type: :development
88
78
  prerelease: false
89
79
  version_requirements: !ruby/object:Gem::Requirement
90
- none: false
91
80
  requirements:
92
- - - ! '>='
81
+ - - ">="
93
82
  - !ruby/object:Gem::Version
94
83
  version: '0'
95
84
  - !ruby/object:Gem::Dependency
96
85
  name: activerecord
97
86
  requirement: !ruby/object:Gem::Requirement
98
- none: false
99
87
  requirements:
100
- - - ~>
88
+ - - "~>"
101
89
  - !ruby/object:Gem::Version
102
- version: 3.2.0
90
+ version: 4.2.3
103
91
  type: :runtime
104
92
  prerelease: false
105
93
  version_requirements: !ruby/object:Gem::Requirement
106
- none: false
107
94
  requirements:
108
- - - ~>
95
+ - - "~>"
109
96
  - !ruby/object:Gem::Version
110
- version: 3.2.0
97
+ version: 4.2.3
111
98
  description: OvirtMetrics is an ActiveRecord-based gem for reading the oVirt History
112
99
  database.
113
100
  email:
@@ -117,12 +104,9 @@ executables: []
117
104
  extensions: []
118
105
  extra_rdoc_files: []
119
106
  files:
120
- - .gitignore
121
- - .rspec
122
- - Gemfile
107
+ - ".rspec"
123
108
  - LICENSE.txt
124
109
  - README.md
125
- - Rakefile
126
110
  - lib/models/calendar.rb
127
111
  - lib/models/cluster_configuration.rb
128
112
  - lib/models/datacenter_configuration.rb
@@ -168,7 +152,6 @@ files:
168
152
  - lib/ovirt_metrics/column_definitions.rb
169
153
  - lib/ovirt_metrics/nic_metrics.rb
170
154
  - lib/ovirt_metrics/version.rb
171
- - ovirt_metrics.gemspec
172
155
  - spec/models/host_configuration_spec.rb
173
156
  - spec/models/host_samples_history_spec.rb
174
157
  - spec/models/vm_disk_samples_history_spec.rb
@@ -179,37 +162,29 @@ files:
179
162
  - spec/schemas/schema_rhev31.rb
180
163
  - spec/spec_helper.rb
181
164
  - spec/support/active_record.rb
182
- - travis.yml
183
165
  homepage: http://github.com/ManageIQ/ovirt_metrics
184
166
  licenses:
185
167
  - MIT
168
+ metadata: {}
186
169
  post_install_message:
187
170
  rdoc_options: []
188
171
  require_paths:
189
172
  - lib
190
173
  required_ruby_version: !ruby/object:Gem::Requirement
191
- none: false
192
174
  requirements:
193
- - - ! '>='
175
+ - - ">="
194
176
  - !ruby/object:Gem::Version
195
- version: '0'
196
- segments:
197
- - 0
198
- hash: -568016211607175379
177
+ version: 1.9.3
199
178
  required_rubygems_version: !ruby/object:Gem::Requirement
200
- none: false
201
179
  requirements:
202
- - - ! '>='
180
+ - - ">="
203
181
  - !ruby/object:Gem::Version
204
182
  version: '0'
205
- segments:
206
- - 0
207
- hash: -568016211607175379
208
183
  requirements: []
209
184
  rubyforge_project:
210
- rubygems_version: 1.8.24
185
+ rubygems_version: 2.4.8
211
186
  signing_key:
212
- specification_version: 3
187
+ specification_version: 4
213
188
  summary: OvirtMetrics is an ActiveRecord-based gem for reading the oVirt History database.
214
189
  test_files:
215
190
  - spec/models/host_configuration_spec.rb
@@ -222,3 +197,4 @@ test_files:
222
197
  - spec/schemas/schema_rhev31.rb
223
198
  - spec/spec_helper.rb
224
199
  - spec/support/active_record.rb
200
+ - ".rspec"
data/.gitignore DELETED
@@ -1,17 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in ovirt_metrics.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
-
3
- require 'rspec/core/rake_task'
4
- RSpec::Core::RakeTask.new('spec')
5
- task :test => :spec
6
- task :default => :spec
@@ -1,28 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'ovirt_metrics/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "ovirt_metrics"
8
- spec.version = OvirtMetrics::VERSION
9
- spec.authors = ["Oleg Barenboim", "Jason Frey"]
10
- spec.email = ["chessbyte@gmail.com", "fryguy9@gmail.com"]
11
- spec.description = %q{OvirtMetrics is an ActiveRecord-based gem for reading the oVirt History database.}
12
- spec.summary = %q{OvirtMetrics is an ActiveRecord-based gem for reading the oVirt History database.}
13
- spec.homepage = "http://github.com/ManageIQ/ovirt_metrics"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
24
- spec.add_development_dependency "sqlite3"
25
- spec.add_development_dependency "coveralls"
26
-
27
- spec.add_dependency "activerecord", "~> 3.2.0"
28
- end
data/travis.yml DELETED
@@ -1,9 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - "1.8.7"
4
- - "1.9.2"
5
- - "1.9.3"
6
- - jruby-18mode # JRuby in 1.8 mode
7
- - jruby-19mode # JRuby in 1.9 mode
8
- - rbx-18mode
9
- - rbx-19mode