Skip to content

Commit

Permalink
[api] Reduce complexity of get_latest_updated
Browse files Browse the repository at this point in the history
This moves the two queries into seperate methods.
  • Loading branch information
bgeuken committed Oct 5, 2017
1 parent 3c2e02b commit 140e200
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/api/lib/statistics_calculations.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
class StatisticsCalculations
def self.get_latest_updated(limit = 10, timelimit = Time.at(0), prj_filter = ".*", pkg_filter = ".*")
packages = Package.includes(:project).where(updated_at: timelimit..Time.now).
where('packages.name REGEXP ? AND projects.name REGEXP ?', pkg_filter, prj_filter).
references(:project).order("updated_at DESC").limit(limit).
pluck(:name, "projects.name as project", :updated_at).
map { |name, project, at| [at, :package, name, project] }
list = packages(limit, timelimit, prj_filter, pkg_filter) + projects(limit, timelimit, prj_filter)

projects = Project.where(updated_at: timelimit..Time.now).
where('name REGEXP ?', prj_filter).
order("updated_at DESC").limit(limit).
pluck(:name, :updated_at).
map { |name, at| [at, name, :project] }

list = (packages + projects).sort { |a, b| b[0] <=> a[0] }
list.sort! { |a, b| b[0] <=> a[0] }

limit ? list.first(limit) : list
end

def self.packages(limit, timelimit, prj_filter, pkg_filter)
Package.includes(:project).where(updated_at: timelimit..Time.now).
where('packages.name REGEXP ? AND projects.name REGEXP ?', pkg_filter, prj_filter).
references(:project).
order("updated_at DESC").
limit(limit).
pluck(:name, "projects.name as project", :updated_at).
map { |name, project, at| [at, :package, name, project] }
end
private_class_method :packages

def self.projects(limit, timelimit, prj_filter)
Project.where(updated_at: timelimit..Time.now).
where('name REGEXP ?', prj_filter).
order("updated_at DESC").
limit(limit).
pluck(:name, :updated_at).
map { |name, at| [at, name, :project] }
end
private_class_method :projects
end

0 comments on commit 140e200

Please sign in to comment.