Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Role model/dataset #3068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/controllers/v3/roles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def show
decorators << IncludeRoleOrganizationDecorator if IncludeRoleOrganizationDecorator.match?(message.include)
decorators << IncludeRoleSpaceDecorator if IncludeRoleSpaceDecorator.match?(message.include)

role = readable_roles.first(guid: hashed_params[:guid])
role = readable_roles.where(guid: hashed_params[:guid]).first
resource_not_found!(:role) unless role

render status: :ok, json: Presenters::V3::RolePresenter.new(role, decorators: decorators)
end

def destroy
role = readable_roles.first(guid: hashed_params[:guid])
role = readable_roles.where(guid: hashed_params[:guid]).first
resource_not_found!(:role) unless role

if role.for_space?
Expand Down Expand Up @@ -147,7 +147,11 @@ def readable_roles
if permission_queryer.can_read_globally?
Role
else
Role.where(space_id: visible_space_ids).or(organization_id: visible_org_ids)
# This rather complex filter should be applied to the overall Role dataset and is thus using the (virtual row)
# block syntax.
readable_by_space = { space_id: visible_space_ids }
readable_by_org = { organization_id: visible_org_ids }
Role.where { Sequel.|(readable_by_space, readable_by_org) }
end
end

Expand Down
8 changes: 4 additions & 4 deletions app/fetchers/base_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ def advanced_filtering(message, dataset, klass)
values.map do |operator, given_timestamp|
if operator == RelationalOperators::LESS_THAN_COMPARATOR
normalized_timestamp = Time.parse(given_timestamp).utc
dataset = dataset.where { Sequel.qualify(klass.table_name, filter) < normalized_timestamp }
dataset = dataset.where(Sequel.qualify(klass.table_name, filter) < normalized_timestamp)
elsif operator == RelationalOperators::LESS_THAN_OR_EQUAL_COMPARATOR
normalized_timestamp = (Time.parse(given_timestamp).utc + 0.999999).utc
dataset = dataset.where { Sequel.qualify(klass.table_name, filter) <= normalized_timestamp }
dataset = dataset.where(Sequel.qualify(klass.table_name, filter) <= normalized_timestamp)
elsif operator == RelationalOperators::GREATER_THAN_COMPARATOR
normalized_timestamp = (Time.parse(given_timestamp).utc + 0.999999).utc
dataset = dataset.where { Sequel.qualify(klass.table_name, filter) > normalized_timestamp }
dataset = dataset.where(Sequel.qualify(klass.table_name, filter) > normalized_timestamp)
elsif operator == RelationalOperators::GREATER_THAN_OR_EQUAL_COMPARATOR
normalized_timestamp = Time.parse(given_timestamp).utc
dataset = dataset.where { Sequel.qualify(klass.table_name, filter) >= normalized_timestamp }
dataset = dataset.where(Sequel.qualify(klass.table_name, filter) >= normalized_timestamp)
end
end
else
Expand Down
4 changes: 2 additions & 2 deletions app/fetchers/role_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
module VCAP::CloudController
class RoleListFetcher < BaseListFetcher
class << self
def fetch(message, readable_users_dataset, eager_loaded_associations: [])
filter(message, readable_users_dataset).eager(eager_loaded_associations)
def fetch(message, readable_roles, eager_loaded_associations: [])
filter(message, readable_roles).eager(eager_loaded_associations)
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/runtime/pollable_job_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def resource_exists?
Sequel::Model(ActiveSupport::Inflector.pluralize(resource_type).to_sym)
end

!!model.find(guid: resource_guid)
!!model.where(guid: resource_guid).first
end

def self.find_by_delayed_job(delayed_job)
Expand Down
Loading