Skip to content

Commit

Permalink
Remove raw rows aces#1454 (aces#1458)
Browse files Browse the repository at this point in the history
* Code cleanup: replace RawData with plucking stage 1 aces#1454
  • Loading branch information
MontrealSergiy authored Jan 14, 2025
1 parent 9e62a91 commit aec2730
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 45 deletions.
6 changes: 3 additions & 3 deletions Bourreau/lib/bourreau_system_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ def self.a076_ensure_task_archived_status_is_consistent #:nodoc:

# CASE 5
tasks_archived_as_file = local_tasks.archived_as_file
valid_tasks_ids = tasks_archived_as_file.joins(:workdir_archive).raw_first_column("cbrain_tasks.id").compact
all_tasks_ids = tasks_archived_as_file.raw_first_column("cbrain_tasks.id")
valid_tasks_ids = tasks_archived_as_file.joins(:workdir_archive).pluck("cbrain_tasks.id").compact
all_tasks_ids = tasks_archived_as_file.pluck("cbrain_tasks.id")
case5_tasks = CbrainTask.find(all_tasks_ids - valid_tasks_ids)
case5_count = case5_tasks.size
puts "C> \t- Processing #{case5_count} CbrainTasks that seem to be archived but that haven't workdir_archive." if case5_count > 0
Expand Down Expand Up @@ -388,7 +388,7 @@ def self.a090_check_for_spurious_task_workdirs #:nodoc:
uids2path[idstring.to_i] = path.strip.sub(/\A\.\//,"") # 12345 => "01/23/45"
end

all_task_ids = CbrainTask.where({}).raw_first_column(:id)
all_task_ids = CbrainTask.where({}).ids
spurious_ids = uids2path.keys - all_task_ids

if spurious_ids.empty?
Expand Down
4 changes: 2 additions & 2 deletions BrainPortal/app/controllers/bourreaux_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ def cache_disk_usage
user_ids = params[:user_ids] || nil

available_users = current_user.available_users
user_ids = user_ids ? available_users.where(:id => user_ids).raw_first_column(:id) :
available_users.raw_first_column(:id)
user_ids = user_ids ? available_users.where(:id => user_ids).ids :
available_users.ids

raise "Bad params" if bourreau_id.blank? || user_ids.blank?
bourreau = Bourreau.find(bourreau_id.to_i)
Expand Down
4 changes: 2 additions & 2 deletions BrainPortal/app/controllers/data_providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ def disk_usage
user_ids = params[:user_ids] || nil

available_users = current_user.available_users
user_ids = user_ids ? available_users.where(:id => user_ids).raw_first_column(:id) :
available_users.raw_first_column(:id)
user_ids = user_ids ? available_users.where(:id => user_ids).ids :
available_users.ids

raise "Bad params" if dataprovider_id.blank? || user_ids.blank?
dataprovider = DataProvider.find(dataprovider_id.to_i)
Expand Down
4 changes: 2 additions & 2 deletions BrainPortal/app/controllers/neurohub_portal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class NeurohubPortalController < NeurohubApplicationController
# Main welcome/dashboard page
def welcome #:nodoc:
@username = current_user.login
bourreau_ids = Bourreau.find_all_accessible_by_user(current_user).raw_first_column("remote_resources.id")
user_ids = current_user.available_users.raw_first_column(:id)
bourreau_ids = Bourreau.find_all_accessible_by_user(current_user).ids
user_ids = current_user.available_users.ids
@tasks = CbrainTask.real_tasks.not_archived.where(:user_id => user_ids, :bourreau_id => bourreau_ids).order( "updated_at DESC" ).limit(5).all
@files = Userfile.find_all_accessible_by_user(current_user).where(:hidden => false).order( "updated_at DESC" ).limit(5).all
@dashboard_messages = Message
Expand Down
10 changes: 5 additions & 5 deletions BrainPortal/app/controllers/noc_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def gather_info(since_when) #:nodoc:
@active_users = CbrainSession.session_model
.where([ "updated_at > ?", since_when ])
.where(:active => true)
.raw_first_column(:user_id)
.pluck(:user_id)
.compact.uniq.size
@active_tasks = CbrainTask.active.count
@data_transfer = SyncStatus
Expand Down Expand Up @@ -239,9 +239,9 @@ def gather_info(since_when) #:nodoc:
@active_tasks = rand(fake)
@data_transfer = rand(fake.gigabytes)
@cpu_time = rand(fake * 3600)
@dp_space_delta_P = DataProvider.where({}).raw_first_column(:id).shuffle[0..rand(5)]
@dp_space_delta_P = DataProvider.ids.shuffle[0..rand(5)]
.map { |dp| [ dp, rand(fake.gigabytes) ] }.to_h
@dp_space_delta_M = DataProvider.where({}).raw_first_column(:id).shuffle[0..rand(5)]
@dp_space_delta_M = DataProvider.ids.shuffle[0..rand(5)]
.map { |dp| [ dp, - rand(fake.gigabytes) ] }.to_h
end

Expand Down Expand Up @@ -293,14 +293,14 @@ def gather_info(since_when) #:nodoc:
# Add entries with 0 for DPs that happen to be offline, so we see still them in red.
DataProvider.where(:online => false)
.where(["updated_at > ?", offline_resource_limit.ago])
.raw_first_column(:id)
.ids
.each do |dpid|
# @updated_files[dpid] = 0 unless @updated_files[dpid].present?
@dp_space_delta_P[dpid] = 0 unless @dp_space_delta_P[dpid].present?
end

#if fake
# @updated_files = DataProvider.where({}).raw_first_column(:id).shuffle[0..rand(5)]
# @updated_files = DataProvider.where({}).ids.shuffle[0..rand(5)]
# .map { |dp| [ dp, rand(fake.gigabytes) ] }.to_h
#end

Expand Down
4 changes: 2 additions & 2 deletions BrainPortal/app/controllers/portal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def welcome #:nodoc:
end
end

bourreau_ids = Bourreau.find_all_accessible_by_user(current_user).raw_first_column("remote_resources.id")
user_ids = current_user.available_users.raw_first_column(:id)
bourreau_ids = Bourreau.find_all_accessible_by_user(current_user).pluck("remote_resources.id")
user_ids = current_user.available_users.ids
@tasks = CbrainTask.real_tasks.not_archived.where(:user_id => user_ids, :bourreau_id => bourreau_ids).order( "updated_at DESC" ).limit(10).all
@files = Userfile.find_all_accessible_by_user(current_user).where(:hidden => false).order( "userfiles.updated_at DESC" ).limit(10).all
end
Expand Down
8 changes: 4 additions & 4 deletions BrainPortal/app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def update #:nodoc:
new_site_attr = site_params

original_user_ids = @site.user_ids
original_manager_ids = @site.managers.raw_first_column(&:id)
original_manager_ids = @site.managers.ids
original_group_ids = @site.group_ids

commit_name = extract_params_key([ :update_users, :update_groups ])
Expand All @@ -111,9 +111,9 @@ def update #:nodoc:
respond_to do |format|
if @site.update_attributes_with_logging(new_site_attr, current_user)
@site.reload
@site.addlog_object_list_updated("Users", User, original_user_ids, @site.user_ids, current_user, :login)
@site.addlog_object_list_updated("Managers", User, original_manager_ids, @site.managers.raw_first_column(&:id), current_user, :login)
@site.addlog_object_list_updated("Groups", Group, original_group_ids, @site.group_ids, current_user)
@site.addlog_object_list_updated("Users", User, original_user_ids, @site.user_ids, current_user, :login)
@site.addlog_object_list_updated("Managers", User, original_manager_ids, @site.managers.ids, current_user, :login)
@site.addlog_object_list_updated("Groups", Group, original_group_ids, @site.group_ids, current_user)
flash[:notice] = 'Site was successfully updated.'
format.html { redirect_to(@site) }
format.xml { head :ok }
Expand Down
2 changes: 1 addition & 1 deletion BrainPortal/app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def update_multiple
task_ids = Array(params[:tasklist] || [])
batch_ids = Array(params[:batch_ids] || [])
batch_ids << nil if batch_ids.delete('nil')
task_ids += filtered_scope(CbrainTask.where(:batch_id => batch_ids)).select('cbrain_tasks.id').raw_first_column
task_ids += filtered_scope(CbrainTask.where(:batch_id => batch_ids)).pluck('cbrain_tasks.id')
task_ids = task_ids.map(&:to_i).uniq

commit_name = extract_params_key([ :update_user_id, :update_group_id, :update_results_data_provider_id, :update_tool_config_id ])
Expand Down
2 changes: 1 addition & 1 deletion BrainPortal/app/controllers/tool_configs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def base_scope #:nodoc:
unless current_user.has_role?(:admin_user)
bourreau_ids = Bourreau.all.select { |b| b.can_be_accessed_by?(current_user) }.map(&:id)
tool_ids = Tool.all.select { |t| t.can_be_accessed_by?(current_user) }.map(&:id)
group_ids = current_user.groups.raw_first_column(:id)
group_ids = current_user.groups.ids
scope = scope.where(
:bourreau_id => bourreau_ids,
:tool_id => tool_ids,
Expand Down
10 changes: 5 additions & 5 deletions BrainPortal/app/controllers/userfiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def index #:nodoc:
@tag_filters = @base_scope
.joins(:tags)
.group('tags.name')
.raw_rows(['tags.name', 'tags.id', 'COUNT(tags.name)'])
.pluck('tags.name', 'tags.id', 'COUNT(tags.name)')
.map do |name, id, count|
{
:value => id,
Expand All @@ -116,13 +116,13 @@ def index #:nodoc:

# Special case; only userfile IDs are required (API request)
if params[:ids_only] && (api_request? || jQuery_request?)
@userfiles = @view_scope.raw_first_column('userfiles.id')
@userfiles = @view_scope.pluck('userfiles.id')

# Tree sort
elsif @scope.custom[:tree_sort]
# Sort using just IDs and parent IDs then paginate, giving the final
# userfiles list in tuple (see +tree_sort_by_pairs+) form.
tuples = tree_sort_by_pairs(@view_scope.raw_rows(['userfiles.id', 'userfiles.parent_id']))
tuples = tree_sort_by_pairs(@view_scope.pluck('userfiles.id', 'userfiles.parent_id'))
tuples = @scope.pagination.apply(tuples) unless api_request?

# Keep just ID and depth/level; there is no need for the parent ID,
Expand Down Expand Up @@ -770,7 +770,7 @@ def update_multiple #:nodoc:

# Pre-spawn checks; tags, project and owner
if changes.has_key?(:tags)
available_tags = current_user.available_tags.raw_first_column(:id)
available_tags = current_user.available_tags.ids
flash[:error] += "You do not have access to all tags you want to update.\n" unless
(changes[:tags] - available_tags).blank?
changes[:tags] &= available_tags
Expand Down Expand Up @@ -821,7 +821,7 @@ def update_multiple #:nodoc:

# R/W access check
failed["you don't have access"] = Userfile
.where(:id => file_ids - userfiles.raw_first_column(:id))
.where(:id => file_ids - userfiles.ids)
.select([:id, :name, :type])
.all.to_a

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def paths_to_delete(paths)
# yet we couldn't find any files on disk.
rr_id = self.remote_resource_id
supposedly_in_cache = SyncStatus.where( :remote_resource_id => rr_id, :status => [ 'InSync', 'CacheNewer' ] )
supposedly_in_cache_uids = supposedly_in_cache.raw_first_column(:userfile_id)
supposedly_in_cache_uids = supposedly_in_cache.pluck(:userfile_id)
not_in_cache_uids = supposedly_in_cache_uids - uids_seen_in_cache
supposedly_in_cache.where( :userfile_id => not_in_cache_uids ).destroy_all if not_in_cache_uids.present?

Expand Down
2 changes: 1 addition & 1 deletion BrainPortal/app/models/cluster_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2687,7 +2687,7 @@ def submit_task_from_string(json_filename,json_string)
# Sets tool config among tool configs accessible by user of current task
new_tool = new_task.tool
accessible_tool_configs = ToolConfig.find_all_accessible_by_user(self.user).where(:tool_id => new_tool.id, :bourreau_id => self.bourreau_id)
new_tcid = accessible_tool_configs.limit(1).raw_first_column(:id)[0] if new_tcid.blank?
new_tcid = accessible_tool_configs.limit(1).ids[0] if new_tcid.blank?
if new_tcid.blank? || ! accessible_tool_configs.where(:id => new_tcid).exists?
cb_error "Cannot find an acceptable tool config ID for this user, tool, and bourreau."
end
Expand Down
4 changes: 2 additions & 2 deletions BrainPortal/app/models/tool_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def self.find_all_accessible_by_user(user) #:nodoc:
ToolConfig.specific_versions
else
gids = user.group_ids
bids = Bourreau.find_all_accessible_by_user(user).raw_first_column("remote_resources.id")
tids = Tool.find_all_accessible_by_user(user).raw_first_column("tools.id")
bids = Bourreau.find_all_accessible_by_user(user).ids
tids = Tool.find_all_accessible_by_user(user).ids
ToolConfig.specific_versions.where(:group_id => gids, :bourreau_id => bids, :tool_id => tids)
end
end
Expand Down
11 changes: 5 additions & 6 deletions BrainPortal/app/models/userfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,18 @@ class Userfile < ApplicationRecord
scope :has_no_parent, -> { where(parent_id: nil) }

scope :has_no_child, lambda { |ignored|
parents_ids = Userfile.where("parent_id IS NOT NULL").raw_first_column(:parent_id).uniq
parents_ids = Userfile.where("parent_id IS NOT NULL").pluck(:parent_id).uniq
parents_ids.blank? ? where({}) : where("userfiles.id NOT IN (?)", parents_ids)
}

scope :parent_name_like, lambda { |n|
matching_parents_ids = Userfile.where("name like ?", "%#{n.strip}%").raw_first_column(:id).uniq
matching_parents_ids = Userfile.where("name like ?", "%#{n.strip}%").ids.uniq
where(:parent_id => matching_parents_ids)
}

scope :child_name_like, lambda { |n|
matching_children_ids = Userfile.where("name like ?", "%#{n.strip}%").where("parent_id IS NOT NULL").raw_first_column(:id).uniq
matching_parents_ids = Userfile.where(:id => matching_children_ids).raw_first_column(:parent_id).uniq
matching_children_ids = Userfile.where("name like ?", "%#{n.strip}%").where("parent_id IS NOT NULL").ids.uniq
matching_parents_ids = Userfile.where(:id => matching_children_ids).pluck(:parent_id).uniq
where(:id => matching_parents_ids)
}

Expand Down Expand Up @@ -300,8 +300,7 @@ def set_tags_for_user(user, tags)
tags = [tags] unless tags.is_a? Array

non_user_tags = self.tags
.where(["tags.user_id <> ? AND tags.group_id NOT IN (?)", user.id, user.group_ids])
.raw_first_column(:id)
.where(["tags.user_id <> ? AND tags.group_id NOT IN (?)", user.id, user.group_ids]).ids
new_tag_set = tags + non_user_tags

self.tag_ids = new_tag_set
Expand Down
4 changes: 2 additions & 2 deletions BrainPortal/app/models/vault_local_data_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def impl_provider_report #:nodoc:
issues = []
base_path = Pathname.new(remote_dir)
users = User.where({})
user_dirs = users.raw_rows(:login).flatten
user_dirs = users.pluck(:login)

# Look for files outside user directories
Dir.foreach(base_path).reject { |f| f.start_with?('.') || user_dirs.include?(f) }.each do |out|
Expand All @@ -104,7 +104,7 @@ def impl_provider_report #:nodoc:
# Look for unregistered files in user directories
users.each do |user|
next unless File.directory?(base_path + user.login)
registered = self.userfiles.where(:user_id => user).raw_rows(:name).flatten
registered = self.userfiles.where(:user_id => user).pluck(:name)
Dir.foreach(base_path + user.login).reject { |f| f.start_with?('.') || registered.include?(f) }.each do |unreg|
issues << {
:type => :vault_unregistered,
Expand Down
4 changes: 2 additions & 2 deletions BrainPortal/app/models/vault_ssh_data_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def impl_provider_report #:nodoc:
issues = []
base_path = Pathname.new(remote_dir)
users = User.where({})
user_dirs = users.raw_rows(:login).flatten
user_dirs = users.pluck(:login)

# Look for files outside user directories
self.remote_dir_entries(remote_dir,nil).map(&:name).reject { |f| user_dirs.include? f }.each do |out|
Expand All @@ -84,7 +84,7 @@ def impl_provider_report #:nodoc:

users.each do |user|
remote_files = self.remote_dir_entries((base_path + user.login).to_s,nil).map(&:name) rescue []
registered = self.userfiles.where(:user_id => user).raw_rows(:id, :name)
registered = self.userfiles.where(:user_id => user).pluck(:id, :name)

# Make sure all registered files exist
registered.reject { |i,n| remote_files.include? n }.each do |id,name|
Expand Down
6 changes: 3 additions & 3 deletions BrainPortal/lib/cbrain_system_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def self.a045_ensure_syncstatus_is_clean #:nodoc:
puts "C> Cleaning up old SyncStatus objects..."
#-----------------------------------------------------------------------------

rr_ids = RemoteResource.where({}).raw_first_column(:id)
rr_ids = RemoteResource.ids
bad_ss = SyncStatus.where([ "remote_resource_id NOT IN (?)", rr_ids ])
ss_deleted = bad_ss.count
if ss_deleted > 0
Expand All @@ -181,8 +181,8 @@ def self.a045_ensure_syncstatus_is_clean #:nodoc:
else
puts "C> \t- No SyncStatus objects are associated with obsolete resources."
end
ss_uids = SyncStatus.where({}).raw_first_column(:userfile_id) || []
uids = Userfile.where({}).raw_first_column(:id) || []
ss_uids = SyncStatus.pluck(:userfile_id) || []
uids = Userfile.ids || []
bad_ids = (ss_uids - uids).uniq
if bad_ids.size > 0
SyncStatus.where(:userfile_id => nil).destroy_all rescue true
Expand Down
2 changes: 1 addition & 1 deletion BrainPortal/spec/controllers/userfiles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ class << file; attr_reader :tempfile; end

it "should update tags when requested" do
tag = create(:tag, :userfiles => [admin_userfile], :user => admin)
allow(admin).to receive_message_chain(:available_tags, :raw_first_column).and_return([tag.id])
allow(admin).to receive_message_chain(:available_tags, :ids).and_return([tag.id])
post :update_multiple, params: {file_ids: [user_userfile.id], tags: [tag.id]}
expect(user_userfile.tags).to match_array([tag])
end
Expand Down

0 comments on commit aec2730

Please sign in to comment.