Skip to content

Commit 74b04a8

Browse files
authored
Merge pull request #1086 from travis-ci/ce-tweak-jobs-query
Speed up jobs queries/endpoint This updates API to use the optimized database functions added in travis-ci/travis-migrations#222.
2 parents e9e850b + ed0a812 commit 74b04a8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

lib/travis/api/v3/queries/jobs.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ def find(build)
1313
end
1414

1515
def filter(relation)
16-
relation = relation.where(state: ACTIVE_STATES) if bool(active)
17-
relation = relation.where(state: list(state)) if state
1816
relation = for_owner(relation) if created_by
1917

2018
relation = relation.includes(:build)
@@ -36,8 +34,7 @@ def for_owner(relation)
3634

3735
def for_user(user)
3836
set_custom_timeout(host_timeout)
39-
fragment = "SELECT repository_id FROM permissions where user_id = #{user.id}"
40-
jobs = V3::Models::Job.where("EXISTS (#{fragment}) AND jobs.repository_id IN (#{fragment})")
37+
jobs = V3::Models::Job.where("jobs.id in (select id from most_recent_job_ids_for_user_repositories_by_states(#{user.id}, ?))", states)
4138

4239
sort filter(jobs)
4340
end
@@ -48,5 +45,15 @@ def stats_by_queue(queue)
4845
.count
4946
Models::JobsStats.new(stats, queue)
5047
end
48+
49+
private
50+
51+
def states
52+
s = []
53+
s << ACTIVE_STATES if bool(active)
54+
s << list(state) if state
55+
return '' if s.empty?
56+
s.flatten.uniq.join(',')
57+
end
5158
end
5259
end

0 commit comments

Comments
 (0)