ovirt_metrics 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46aa30051329934e20b2d398bea92e479b9c83d5
4
- data.tar.gz: e76da24f38bf64960e153e6bdb3bbb799ed46532
3
+ metadata.gz: b8f6eb0cee5ba8ae32e0f2fa316048a258204db1
4
+ data.tar.gz: 16b8c4b956bdbf7016483bb4dc225dde3779a6c7
5
5
  SHA512:
6
- metadata.gz: 02aae111c03834c1df61d379c8c272a197ed21e46c349a66fc68261e5ce0467ef636e4c64099a6b730b57338192b89048a583518ff09ddfad681487a5b150780
7
- data.tar.gz: b6622dbb636612cb2650ea10dfc0793e8c2efcb989e2f5557b4a2d7c0e317171adcc4d42acdeda6bb228f219072b35e05d3273cd6534ca4377f963472ac2c400
6
+ metadata.gz: ebde982b37176d5d7374792f54508747feda496cc2b4a4166548c6117c3fae3fab73a1a1fc434c39a16ceb727f665c7f85fd7d535acf6474927766137395520e
7
+ data.tar.gz: b215b596b463b6c07b7230ada719f7f07c1c642ac28c807e7d5c93f0a266393dc17c6614a7306e8b84b227c64d0278c61a1864f588e4baf484378ca5ab5b8164
data/lib/ovirt_metrics.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "active_record"
2
2
  require "ovirt_metrics/version"
3
+ require "ovirt_metrics/configurator"
3
4
  require "ovirt_metrics/column_definitions"
4
5
  require "ovirt_metrics/nic_metrics"
5
6
 
@@ -12,6 +13,12 @@ module OvirtMetrics
12
13
  DEFAULT_HISTORY_DATABASE_NAME_3_0 = "rhevm_history".freeze
13
14
  VM_NOT_RUNNING = 0
14
15
 
16
+ def self.config
17
+ @config ||= Configurator.new
18
+ yield @config if block_given?
19
+ @config
20
+ end
21
+
15
22
  def self.establish_connection(opts)
16
23
  self.connect(opts)
17
24
  end
@@ -38,8 +45,15 @@ module OvirtMetrics
38
45
  OvirtHistory.establish_connection(opts)
39
46
  end
40
47
 
48
+ # Note: This method is expected to raise PGErrors and other exceptions
41
49
  def self.connected?
42
- OvirtHistory.connection.active?
50
+ OvirtHistory.connection.active? && OvirtHistory.connected?
51
+ rescue ActiveRecord::ConnectionNotEstablished => e
52
+ if e.message =~ /No connection pool.+found/
53
+ false
54
+ else
55
+ raise
56
+ end
43
57
  end
44
58
 
45
59
  def self.disconnect
@@ -59,7 +73,14 @@ module OvirtMetrics
59
73
  host_realtime_metrics_to_hashes(metrics, nic_metrics)
60
74
  end
61
75
 
62
- private
76
+ def self.warn(message)
77
+ unless config.suppress_warnings
78
+ puts "#{message} To hide this warning, use OvirtMetrics.config.suppress_warnings = true"
79
+ end
80
+ end
81
+
82
+ # The methods below this line are PRIVATE not meant to be used out of the scope
83
+ # of this class. TODO refactor to force privacy or remove this line.
63
84
 
64
85
  def self.query_host_realtime_metrics(host_id, start_time = nil, end_time = nil)
65
86
  HostSamplesHistory.where(:host_id => host_id).includes(:host_configuration).with_time_range(start_time, end_time)
@@ -82,10 +103,14 @@ module OvirtMetrics
82
103
  end
83
104
 
84
105
  def self.query_vm_disk_realtime_metrics(vm_id, start_time = nil, end_time = nil)
85
- disk_ids = DisksVmMap.where(:vm_id => vm_id).collect(&:vm_disk_id)
106
+ disk_ids = vms_disk_ids_for(vm_id)
86
107
  VmDiskSamplesHistory.where(:vm_disk_id => disk_ids).with_time_range(start_time, end_time)
87
108
  end
88
109
 
110
+ def self.vms_disk_ids_for(vm_id)
111
+ VmDeviceHistory.where(:vm_id => vm_id).disks.attached.pluck('DISTINCT device_id')
112
+ end
113
+
89
114
  def self.query_vm_nic_realtime_metrics(vm_id, start_time = nil, end_time = nil)
90
115
  nic_ids = VmInterfaceConfiguration.where(:vm_id => vm_id).collect(&:vm_interface_id)
91
116
  VmInterfaceSamplesHistory.where(:vm_interface_id => nic_ids).with_time_range(start_time, end_time)
@@ -0,0 +1,24 @@
1
+ module OvirtMetrics
2
+ class Configurator
3
+ def initialize
4
+ self.suppress_warnings = false
5
+ self.connection_specification_name = 'ovirt_metrics' if ActiveRecord::VERSION::MAJOR >= 5
6
+ end
7
+
8
+ attr_accessor :suppress_warnings
9
+
10
+ attr_reader :connection_specification_name
11
+
12
+ def connection_specification_name=(value)
13
+ if ActiveRecord::VERSION::MAJOR < 5
14
+ OvirtMetrics.warn "WARNING: ovirt_metric's " \
15
+ "connection_specification_name option is only available with " \
16
+ "Active Record 5 or newer. The main application pool " \
17
+ "('primary') will be used by default until you connect to a " \
18
+ "separate Ovirt database."
19
+ else
20
+ @connection_specification_name = value
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,8 +1,17 @@
1
1
  module OvirtMetrics
2
2
  class OvirtHistory < ActiveRecord::Base
3
+ attr_writer :connection_specification_name if ActiveRecord::VERSION::MAJOR < 5
4
+
3
5
  self.abstract_class = true
4
6
  self.pluralize_table_names = false
5
7
 
8
+ def self.connection_specification_name
9
+ if !defined?(@connection_specification_name) || @connection_specification_name.nil?
10
+ return self == OvirtHistory ? OvirtMetrics.config.connection_specification_name : superclass.connection_specification_name
11
+ end
12
+ @connection_specification_name
13
+ end
14
+
6
15
  def self.with_time_range(start_time = nil, end_time = nil)
7
16
  return all if start_time.nil?
8
17
  where(:history_datetime => (start_time..(end_time || Time.now.utc)))
@@ -0,0 +1,10 @@
1
+ module OvirtMetrics
2
+ class VmDeviceHistory < OvirtHistory
3
+ # have to rename the inheritance_column since this table has a column
4
+ # called "type"
5
+ self.inheritance_column = :_type_disabled
6
+
7
+ scope :disks, -> { where(:type => "disk") }
8
+ scope :attached, -> { where(:delete_date => nil) }
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module OvirtMetrics
2
- VERSION = "1.3.1"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -1,6 +1,36 @@
1
1
  describe OvirtMetrics do
2
2
  shared_examples_for "OvirtMetrics" do |multiplication_required|
3
3
  let(:multiplication_required) { multiplication_required }
4
+ describe ".vms_disk_ids_for" do
5
+ before(:each) do
6
+ @vm1_id = 1
7
+ @device_id1 = "device1"
8
+ @device_id2 = "device2"
9
+ generic_params = {
10
+ :vm_id => @vm1_id,
11
+ :device_id => @device_id1,
12
+ :type => "disk",
13
+ :address => "address",
14
+ :create_date => 1.week.ago
15
+ }
16
+ OvirtMetrics::VmDeviceHistory.create(generic_params)
17
+ OvirtMetrics::VmDeviceHistory.create(generic_params.merge(:address => "duplicate_with_same_device_id"))
18
+ OvirtMetrics::VmDeviceHistory.create(generic_params.merge(:device_id => @device_id2))
19
+ OvirtMetrics::VmDeviceHistory.create(generic_params.merge(:vm_id => 2, :device_id => "disk_from_other_vm"))
20
+ OvirtMetrics::VmDeviceHistory.create(generic_params.merge(:type => "nic",
21
+ :device_id => "device_of_non_disk_type"))
22
+ OvirtMetrics::VmDeviceHistory.create(generic_params.merge(:device_id => "device_that_was_deleted",
23
+ :delete_date => 2.days.ago))
24
+ end
25
+
26
+ subject { described_class.vms_disk_ids_for(@vm1_id) }
27
+
28
+ it { is_expected.to match_array([@device_id1, @device_id2]) }
29
+ it { is_expected.not_to include("disk_from_other_vm") }
30
+ it { is_expected.not_to include("device_of_non_disk_type") }
31
+ it { is_expected.not_to include("device_that_was_deleted") }
32
+ end
33
+
4
34
  context ".vm_realtime" do
5
35
  it "when vm_id finds no matches" do
6
36
  expect(described_class.vm_realtime(42)).to eq([{}, {}])
@@ -57,11 +87,6 @@ describe OvirtMetrics do
57
87
 
58
88
  end
59
89
 
60
- context "RHEV 3.0" do
61
- before(:each) { load_rhev_30 }
62
- it_should_behave_like "OvirtMetrics", true
63
- end
64
-
65
90
  context "RHEV 3.1" do
66
91
  before(:each) { load_rhev_31 }
67
92
  it_should_behave_like "OvirtMetrics", true
data/spec/spec_helper.rb CHANGED
@@ -77,6 +77,7 @@ RSpec.configure do |config|
77
77
  end
78
78
 
79
79
  require 'support/active_record'
80
+ require 'ovirt_metrics'
80
81
 
81
82
  begin
82
83
  require 'coveralls'
@@ -84,4 +85,11 @@ begin
84
85
  rescue LoadError
85
86
  end
86
87
 
87
- require 'ovirt_metrics'
88
+ OvirtMetrics.config do |c|
89
+ # Necessary because you currently cannot specify the
90
+ # connection name for ActiveRecord::Schema, which we use
91
+ # in tests to reset db schema for different RHEV versions.
92
+ c.connection_specification_name = 'primary' if ActiveRecord::VERSION::MAJOR >= 5
93
+ end
94
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
95
+
@@ -1,7 +1,5 @@
1
1
  require 'active_record'
2
2
 
3
- ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
4
-
5
3
  module ActiveModel::Validations
6
4
  # Extension to enhance `should have` on AR Model instances. Calls
7
5
  # model.valid? in order to prepare the object's errors object.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ovirt_metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Barenboim
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-02 00:00:00.000000000 Z
12
+ date: 2016-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -176,6 +176,7 @@ files:
176
176
  - lib/active_record/connection_adapters/ovirt_legacy_postgresql_adapter.rb
177
177
  - lib/ovirt_metrics.rb
178
178
  - lib/ovirt_metrics/column_definitions.rb
179
+ - lib/ovirt_metrics/configurator.rb
179
180
  - lib/ovirt_metrics/models/calendar.rb
180
181
  - lib/ovirt_metrics/models/cluster_configuration.rb
181
182
  - lib/ovirt_metrics/models/datacenter_configuration.rb
@@ -204,6 +205,7 @@ files:
204
205
  - lib/ovirt_metrics/models/tag_relations_history.rb
205
206
  - lib/ovirt_metrics/models/vm_configuration.rb
206
207
  - lib/ovirt_metrics/models/vm_daily_history.rb
208
+ - lib/ovirt_metrics/models/vm_device_history.rb
207
209
  - lib/ovirt_metrics/models/vm_disk_configuration.rb
208
210
  - lib/ovirt_metrics/models/vm_disk_daily_history.rb
209
211
  - lib/ovirt_metrics/models/vm_disk_hourly_history.rb