-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] Reduce complexity of get_latest_updated
This moves the two queries into seperate methods.
- Loading branch information
Showing
1 changed file
with
23 additions
and
12 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
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 |