Skip to content

Commit

Permalink
Merge pull request #3033 from sap-contributions/avoid-unnecessary-que…
Browse files Browse the repository at this point in the history
…ries

Use foreign keys more to avoid DB queries
  • Loading branch information
philippthun authored Oct 31, 2022
2 parents ae88895 + ec5bf62 commit 6edaf8d
Show file tree
Hide file tree
Showing 44 changed files with 110 additions and 110 deletions.
2 changes: 1 addition & 1 deletion app/access/route_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def create?(route, params=nil)
end

def read?(route)
context.queryer.can_read_route?(route.space.guid)
context.queryer.can_read_route?(route.space_id)
end

def read_for_update?(route, params=nil)
Expand Down
6 changes: 3 additions & 3 deletions app/actions/droplet_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def create(app, message, user_audit_info)
droplet,
user_audit_info,
app.name,
app.space.guid,
app.space_guid,
app.organization.guid
)

Expand Down Expand Up @@ -81,8 +81,8 @@ def create_kpack_droplet(build)

def droplet_from_build(build)
DropletModel.new(
app_guid: build.app.guid,
package_guid: build.package.guid,
app_guid: build.app_guid,
package_guid: build.package_guid,
state: DropletModel::STAGING_STATE,
build: build,
)
Expand Down
2 changes: 1 addition & 1 deletion app/actions/manifest_route_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def find_or_create_valid_route(app, manifest_route, user_audit_info)
domain: existing_domain,
manifest_triggered: true,
)
elsif route.space.guid != app.space.guid
elsif route.space.guid != app.space_guid
raise InvalidRoute.new('Routes cannot be mapped to destinations in different spaces')
end

Expand Down
2 changes: 1 addition & 1 deletion app/actions/service_credential_binding_app_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def precursor(service_instance, app: nil, volume_mount_services_enabled: false,

def validate_service_instance!(app, service_instance, volume_mount_services_enabled)
app_is_required! unless app.present?
space_mismatch! unless all_space_guids(service_instance).include? app.space.guid
space_mismatch! unless all_space_guids(service_instance).include? app.space_guid
if service_instance.managed_instance?
service_not_bindable! unless service_instance.service_plan.bindable?
volume_mount_not_enabled! if service_instance.volume_service? && !volume_mount_services_enabled
Expand Down
2 changes: 1 addition & 1 deletion app/actions/sidecar_synchronize_from_app_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def synchronize(app)
TelemetryLogger.v3_emit(
'create-sidecar',
{
'app-id' => sidecar.app.guid,
'app-id' => sidecar.app_guid,
},
{
'api-version' => 'v3',
Expand Down
2 changes: 1 addition & 1 deletion app/actions/sidecar_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def validate_memory_allocation!(message, sidecar)
end

processes = ProcessModel.where(
app_guid: sidecar.app.guid,
app_guid: sidecar.app_guid,
type: process_types,
)
policy = SidecarMemoryLessThanProcessMemoryPolicy.new(processes, memory)
Expand Down
2 changes: 1 addition & 1 deletion app/actions/v2/route_mapping_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def validate_web_port!
end

def validate_space!
raise SpaceMismatch.new(INVALID_SPACE_MESSAGE) unless app.space.guid == route.space.guid
raise SpaceMismatch.new(INVALID_SPACE_MESSAGE) unless app.space_guid == route.space.guid
end

def app_event_repository
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/runtime/app_bits_upload_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def upload(guid)

raise CloudController::Errors::ApiError.new_from_details('UnprocessableEntity', 'cannot upload bits to a docker app') if process.docker?

relationships = { app: { data: { guid: process.app.guid } } }
relationships = { app: { data: { guid: process.app_guid } } }
create_message = PackageCreateMessage.new({ type: 'bits', relationships: relationships })
package = PackageCreate.create_without_event(create_message)

Expand Down Expand Up @@ -66,7 +66,7 @@ def upload(guid)
TelemetryLogger.v2_emit(
'upload-package',
{
'app-id' => process.app.guid,
'app-id' => process.app_guid,
'user-id' => current_user.guid,
})
result
Expand All @@ -84,7 +84,7 @@ def copy_app_bits(dest_app_guid)
dest_process = find_guid_and_validate_access(:upload, dest_app_guid)

copier = PackageCopy.new
copier.copy_without_event(dest_process.app.guid, src_process.latest_package)
copier.copy_without_event(dest_process.app_guid, src_process.latest_package)

@app_event_repository.record_src_copy_bits(dest_process.app, src_process.app, UserAuditInfo.from_context(SecurityContext))
@app_event_repository.record_dest_copy_bits(dest_process.app, src_process.app, UserAuditInfo.from_context(SecurityContext))
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/runtime/apps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def delete(guid)
TelemetryLogger.v2_emit(
'delete-app',
{
'app-id' => process.app.guid,
'app-id' => process.app_guid,
'user-id' => current_user.guid,
}
)
Expand Down Expand Up @@ -300,7 +300,7 @@ def update(guid)
TelemetryLogger.v2_emit(
'update-app',
{
'app-id' => process.app.guid,
'app-id' => process.app_guid,
'user-id' => current_user.guid,
}
)
Expand All @@ -309,7 +309,7 @@ def update(guid)
TelemetryLogger.v2_emit(
'scale-app',
{
'app-id' => process.app.guid,
'app-id' => process.app_guid,
'user-id' => current_user.guid,
},
{
Expand All @@ -326,7 +326,7 @@ def update(guid)
TelemetryLogger.v2_emit(
'start-app',
{
'app-id' => process.app.guid,
'app-id' => process.app_guid,
'user-id' => current_user.guid,
}
)
Expand All @@ -335,7 +335,7 @@ def update(guid)
TelemetryLogger.v2_emit(
'stop-app',
{
'app-id' => process.app.guid,
'app-id' => process.app_guid,
'user-id' => current_user.guid,
}
)
Expand Down Expand Up @@ -366,7 +366,7 @@ def create
TelemetryLogger.v2_emit(
'create-app',
{
'app-id' => process.app.guid,
'app-id' => process.app_guid,
'user-id' => current_user.guid,
}
)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/runtime/restages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def restage(guid)
TelemetryLogger.v2_emit(
'restage-app',
{
'app-id' => process.app.guid,
'app-id' => process.app_guid,
'user-id' => current_user.guid,
}, {
'lifecycle' => process.app.lifecycle_type,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/v3/builds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create
TelemetryLogger.v3_emit(
'create-build',
{
'app-id' => package.app.guid,
'app-id' => package.app_guid,
'build-id' => build.guid,
'user-id' => current_user.guid,
},
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/v3/processes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def scale
TelemetryLogger.v3_emit(
'scale-app',
{
'app-id' => @process.app.guid,
'app-id' => @process.app_guid,
'user-id' => current_user.guid
},
{
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/v3/routes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def update_destination
unprocessable!(message.errors.full_messages) unless message.valid?

route = Route.find(guid: hashed_params[:guid])
route_not_found! unless route && permission_queryer.can_read_route?(route.space.guid)
route_not_found! unless route && permission_queryer.can_read_route?(route.space_id)
unauthorized! unless permission_queryer.can_manage_apps_in_active_space?(route.space_id)
suspended! unless permission_queryer.is_space_active?(route.space_id)

Expand All @@ -246,7 +246,7 @@ def update_destination
def route
@route || begin
@route = Route.find(guid: hashed_params[:guid])
route_not_found! unless @route && permission_queryer.can_read_route?(@route.space.guid)
route_not_found! unless @route && permission_queryer.can_read_route?(@route.space_id)
@route
end
end
Expand All @@ -266,7 +266,7 @@ def destroy_destination
route = Route.find(guid: hashed_params[:guid])
route_not_found! unless route

route_not_found! unless permission_queryer.can_read_route?(route.space.guid)
route_not_found! unless permission_queryer.can_read_route?(route.space_id)
unauthorized! unless permission_queryer.can_manage_apps_in_active_space?(route.space_id)
suspended! unless permission_queryer.is_space_active?(route.space_id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def log_telemetry(binding)
{
'service-id' => binding.service_instance.managed_instance? ? binding.service_instance.service_plan.service.guid : 'user-provided',
'service-instance-id' => binding.service_instance.guid,
'app-id' => binding.app.guid,
'app-id' => binding.app_guid,
'user-id' => user_audit_info.user_guid,
}
)
Expand Down
2 changes: 1 addition & 1 deletion app/fetchers/droplet_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def filter(message, app, space_guids, dataset)
dataset = dataset.extension(:null_dataset)
return dataset.nullify unless app.droplet

dataset = dataset.where(guid: app.droplet.guid)
dataset = dataset.where(guid: app.droplet_guid)
end

if message.requested?(:app_guids)
Expand Down
2 changes: 1 addition & 1 deletion app/models/runtime/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def validate
validates_unique [:organization_id, :name]
validates_format SPACE_NAME_REGEX, :name

if space_quota_definition && space_quota_definition.organization.guid != organization.guid
if space_quota_definition && space_quota_definition.organization_id != organization.id
errors.add(:space_quota_definition, :invalid_organization)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/services/service_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def self.plan_ids_for_visible_service_instances(user)
plan_ids = []
user.spaces.each do |space|
space.service_instances.select(&:managed_instance?).each do |service_instance|
plan_ids << service_instance.service_plan.id
plan_ids << service_instance.service_plan_id
end
end
plan_ids.uniq
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/v3/build_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def error
def build_links
{
self: { href: url_builder.build_url(path: "/v3/builds/#{build.guid}") },
app: { href: url_builder.build_url(path: "/v3/apps/#{build.app.guid}") }
app: { href: url_builder.build_url(path: "/v3/apps/#{build.app_guid}") }
}.tap do |links|
links[:droplet] = { href: url_builder.build_url(path: "/v3/droplets/#{build.droplet.guid}") } if droplet
end
Expand Down
4 changes: 2 additions & 2 deletions app/presenters/v3/deployment_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def to_hash
relationships: {
app: {
data: {
guid: deployment.app.guid
guid: deployment.app_guid
}
}
},
Expand Down Expand Up @@ -66,7 +66,7 @@ def build_links
href: url_builder.build_url(path: "/v3/deployments/#{deployment.guid}")
},
app: {
href: url_builder.build_url(path: "/v3/apps/#{deployment.app.guid}")
href: url_builder.build_url(path: "/v3/apps/#{deployment.app_guid}")
},
}.tap do |links|
if deployment.cancelable?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def build_links
{
self: { href: url_builder.build_url(path: "/v3/revisions/#{revision.guid}/environment_variables") },
revision: { href: url_builder.build_url(path: "/v3/revisions/#{revision.guid}") },
app: { href: url_builder.build_url(path: "/v3/apps/#{revision.app.guid}") }
app: { href: url_builder.build_url(path: "/v3/apps/#{revision.app_guid}") }
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/v3/shared_spaces_usage_summary_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def service_instance
def build_usage_summary
count = Hash.new(0).tap do |h|
service_instance.service_bindings.each do |binding|
h[binding.app.space.guid] += 1
h[binding.app.space_guid] += 1
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/presenters/v3/task_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def task
def build_links
{
self: { href: url_builder.build_url(path: "/v3/tasks/#{task.guid}") },
app: { href: url_builder.build_url(path: "/v3/apps/#{task.app.guid}") },
app: { href: url_builder.build_url(path: "/v3/apps/#{task.app_guid}") },
cancel: { href: url_builder.build_url(path: "/v3/tasks/#{task.guid}/actions/cancel"), method: 'POST' },
droplet: { href: url_builder.build_url(path: "/v3/droplets/#{task.droplet_guid}") },
}
Expand Down
6 changes: 3 additions & 3 deletions app/repositories/app_usage_event_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_from_process(process, state_name=nil)
space_name: process.space.name,
buildpack_guid: process.detected_buildpack_guid,
buildpack_name: buildpack_name_for_app(process),
parent_app_guid: process.app.guid,
parent_app_guid: process.app_guid,
parent_app_name: process.app.name,
process_type: process.type
)
Expand All @@ -48,7 +48,7 @@ def create_from_task(task, state)
space_name: task.space.name,
buildpack_guid: nil,
buildpack_name: nil,
parent_app_guid: task.app.guid,
parent_app_guid: task.app_guid,
parent_app_name: task.app.name,
process_type: nil,
task_guid: task.guid,
Expand All @@ -67,7 +67,7 @@ def create_from_build(build, state)
org_guid: build.space.organization.guid,
space_guid: build.space.guid,
space_name: build.space.name,
parent_app_guid: build.app.guid,
parent_app_guid: build.app_guid,
parent_app_name: build.app.name,
package_guid: build.package_guid,
app_guid: '',
Expand Down
2 changes: 1 addition & 1 deletion app/repositories/build_event_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.record_build_create(build, user_audit_info, v3_app_name, space_guid, or

metadata = {
build_guid: build.guid,
package_guid: build.package.guid,
package_guid: build.package_guid,
}

Event.create(
Expand Down
2 changes: 1 addition & 1 deletion app/repositories/droplet_event_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.record_create_by_staging(droplet, user_audit_info, v3_app_name, space_g

metadata = {
droplet_guid: droplet.guid,
package_guid: droplet.package.guid
package_guid: droplet.package_guid
}

Event.create(
Expand Down
8 changes: 4 additions & 4 deletions app/repositories/package_event_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ def self.record_app_package_copy(package, user_audit_info, source_package_guid)
end

def self.record_app_package_upload(package, user_audit_info)
VCAP::AppLogEmitter.emit(package.app.guid, "Uploading app package for app with guid #{package.app.guid}")
VCAP::AppLogEmitter.emit(package.app_guid, "Uploading app package for app with guid #{package.app_guid}")
metadata = { package_guid: package.guid }
type = 'audit.app.package.upload'

create_event(package, type, user_audit_info, metadata)
end

def self.record_app_upload_bits(package, user_audit_info)
VCAP::AppLogEmitter.emit(package.app.guid, "Uploading bits for app with guid #{package.app.guid}")
VCAP::AppLogEmitter.emit(package.app_guid, "Uploading bits for app with guid #{package.app_guid}")
metadata = { package_guid: package.guid }
type = 'audit.app.upload-bits'

create_event(package, type, user_audit_info, metadata)
end

def self.record_app_package_delete(package, user_audit_info)
VCAP::AppLogEmitter.emit(package.app.guid, "Deleting app package for app with guid #{package.app.guid}")
VCAP::AppLogEmitter.emit(package.app_guid, "Deleting app package for app with guid #{package.app_guid}")
metadata = { package_guid: package.guid }
type = 'audit.app.package.delete'

create_event(package, type, user_audit_info, metadata)
end

def self.record_app_package_download(package, user_audit_info)
VCAP::AppLogEmitter.emit(package.app.guid, "Downloading app package for app with guid #{package.app.guid}")
VCAP::AppLogEmitter.emit(package.app_guid, "Downloading app package for app with guid #{package.app_guid}")
metadata = { package_guid: package.guid }
type = 'audit.app.package.download'

Expand Down
Loading

0 comments on commit 6edaf8d

Please sign in to comment.