Skip to content

Commit

Permalink
Fix container groups\images statistics for all entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Ari Zellner committed Aug 24, 2017
1 parent 6d5cb11 commit 1613f75
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 0 additions & 1 deletion app/models/container_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class ContainerProject < ApplicationRecord
has_many :container_replicators
has_many :container_services
has_many :containers, :through => :container_groups
has_many :containers, :through => :container_groups
has_many :container_images, -> { distinct }, :through => :container_groups
has_many :container_nodes, -> { distinct }, :through => :container_groups
has_many :container_quotas
Expand Down
21 changes: 12 additions & 9 deletions app/models/metric/statistic.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module Metric::Statistic
def self.calculate_stat_columns(obj, timestamp)
return {} unless obj.respond_to?(:container_groups)
return {} unless obj.respond_to?(:container_images)

capture_interval = Metric::Helper.get_time_interval(obj, timestamp)
container_groups = ContainerGroup.where(:ems_id => obj.id).or(ContainerGroup.where(:old_ems_id => obj.id))
stats = {}

if obj.respond_to?(:all_container_groups)
container_groups = obj.all_container_groups # Get disconnected entities as well
stats[:stat_container_group_create_rate] = container_groups.where(:ems_created_on => capture_interval).count
stats[:stat_container_group_delete_rate] = container_groups.where(:deleted_on => capture_interval).count
end

if obj.respond_to?(:all_container_images)
stats[:stat_container_image_registration_rate] = obj.all_container_images.where(:registered_on => capture_interval).count
end

{
:stat_container_group_create_rate => container_groups.where(:ems_created_on => capture_interval).count,
:stat_container_group_delete_rate => container_groups.where(:deleted_on => capture_interval).count,
:stat_container_image_registration_rate => obj.container_images.where(:registered_on => capture_interval).count
}
stats
end
end
19 changes: 19 additions & 0 deletions spec/models/metric/statistic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
:zone => Zone.first)
end

let(:project) do
FactoryGirl.create(:container_project,
:name => "project")
end

hour = Time.parse(Metric::Helper.nearest_hourly_timestamp(Time.now)).utc

let(:c1) { FactoryGirl.create(:container_group, :ems_created_on => hour - 10.minutes) }
Expand Down Expand Up @@ -42,5 +47,19 @@

expect(derived_columns[:stat_container_image_registration_rate]).to eq(2)
end

it "count created container groups in a project" do
project.container_groups << [c1, c2, c3, c4, c5, c6, c7, c8]
derived_columns = described_class.calculate_stat_columns(project, hour)

expect(derived_columns[:stat_container_group_create_rate]).to eq(2)
end

it "count deleted container groups in a project" do
project.container_groups << [c1, c2, c3, c4, c5, c6, c7, c8]
derived_columns = described_class.calculate_stat_columns(project, hour)

expect(derived_columns[:stat_container_group_delete_rate]).to eq(2)
end
end
end

0 comments on commit 1613f75

Please sign in to comment.