forked from cloudfoundry/cloud_controller_ng
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance /v3/organizations/:guid/usage_summary (cloudfoundry#3513)
* Enhance /v3/organizations/:guid/usage_summary It would be nice to get org usage info for (nearly) all resources that are restricted by an org quota plan, beside memory in mb and started instances, which are already implemented. In the course of the change running_and_pending_tasks_count was refactored from nested selects to joins. Joins are generally more efficient than nested selects, especially when dealing with large datasets. Co-authored-by: Philipp Thun <[email protected]>
- Loading branch information
1 parent
7935829
commit 772033d
Showing
7 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module VCAP::CloudController | ||
class OrganizationQuotaUsage | ||
def initialize(organization) | ||
@organization = organization | ||
end | ||
|
||
def routes | ||
OrganizationRoutes.new(@organization).count | ||
end | ||
|
||
def service_instances | ||
@organization.managed_service_instances_dataset.count | ||
end | ||
|
||
def private_domains | ||
@organization.owned_private_domains_dataset.count | ||
end | ||
|
||
def service_keys | ||
VCAP::CloudController::ServiceKey.dataset.join(:service_instances, id: :service_instance_id). | ||
join(:spaces, id: :space_id). | ||
where(spaces__organization_id: @organization.id). | ||
count | ||
end | ||
|
||
def reserved_route_ports | ||
OrganizationReservedRoutePorts.new(@organization).count | ||
end | ||
|
||
def app_tasks | ||
@organization.running_and_pending_tasks_count | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
spec/unit/lib/cloud_controller/organization_quota_usage_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require 'spec_helper' | ||
|
||
module VCAP::CloudController | ||
RSpec.describe OrganizationQuotaUsage do | ||
let(:org) { Organization.make } | ||
let(:space1) { Space.make(organization: org) } | ||
let(:space2) { Space.make } | ||
let(:space3) { Space.make(organization: org) } | ||
|
||
subject(:org_usage) { OrganizationQuotaUsage.new(org) } | ||
|
||
describe '#routes' do | ||
before do | ||
[space1, space1, space2, space3].each { |s| Route.make(space: s) } | ||
end | ||
|
||
it 'returns the number of routes in all spaces under the org' do | ||
expect(org_usage.routes).to eq(3) | ||
end | ||
end | ||
|
||
describe '#service_instances' do | ||
before do | ||
[space1, space1, space2, space3].each { |s| ManagedServiceInstance.make(space: s) } | ||
UserProvidedServiceInstance.make(space: space1) | ||
end | ||
|
||
it 'returns the number of service instances in all spaces under the org' do | ||
expect(org_usage.service_instances).to eq(3) | ||
end | ||
end | ||
|
||
describe '#private_domains' do | ||
before do | ||
[space1, space1, space2, space3].each { |s| Domain.make(owning_organization: s.organization) } | ||
end | ||
|
||
it 'returns the number of private domains in all spaces under the org' do | ||
expect(org_usage.private_domains).to eq(3) | ||
end | ||
end | ||
|
||
describe '#service_keys' do | ||
before do | ||
[space1, space1, space2, space3].each { |s| ServiceKey.make(service_instance: ServiceInstance.make(space: s)) } | ||
end | ||
|
||
it 'returns the number of service keys in all spaces under the org' do | ||
expect(org_usage.service_keys).to eq(3) | ||
end | ||
end | ||
|
||
describe '#reserved_route_ports' do | ||
before do | ||
reservable_ports = [1234, 2, 2345, 3] | ||
router_group = instance_double(RoutingApi::RouterGroup, type: 'tcp', reservable_ports: reservable_ports) | ||
routing_api_client = instance_double(RoutingApi::Client, router_group: router_group, enabled?: true) | ||
allow(CloudController::DependencyLocator.instance).to receive(:routing_api_client).and_return(routing_api_client) | ||
domain = SharedDomain.make(router_group_guid: 'some-router-group') | ||
[space1, space1, space2, space3].each_with_index { |s, i| Route.make(space: s, domain: domain, host: '', port: reservable_ports[i]) } | ||
end | ||
|
||
it 'returns the number of reserved route ports in all spaces under the org' do | ||
expect(org_usage.reserved_route_ports).to eq(3) | ||
end | ||
end | ||
|
||
describe '#app_tasks' do | ||
before do | ||
[space1, space1, space2, space3].each { |s| TaskModel.make(app: AppModel.make(space: s), state: TaskModel::RUNNING_STATE) } | ||
TaskModel.make(app: AppModel.make(space: space1), state: TaskModel::PENDING_STATE) | ||
TaskModel.make(app: AppModel.make(space: space1), state: TaskModel::CANCELING_STATE) | ||
end | ||
|
||
it 'returns the number of app tasks in all spaces under the org' do | ||
expect(org_usage.app_tasks).to eq(4) | ||
end | ||
end | ||
end | ||
end |