@@ -85,27 +85,19 @@ defmodule Lightning.Runs.Query do
85
85
join: w in assoc ( wo , :workflow ) ,
86
86
join: p in assoc ( w , :project )
87
87
)
88
- |> windows ( [ r , _wo , w , p ] ,
89
- row_number: [
90
- partition_by:
91
- fragment (
92
- "CASE WHEN ? IS NOT NULL THEN ? ELSE ? END" ,
93
- w . concurrency ,
94
- w . id ,
95
- p . id
96
- ) ,
97
- order_by: [ asc: r . inserted_at ]
98
- ]
99
- )
100
88
|> select ( [ r , _wo , w , p ] , % {
101
89
id: r . id ,
102
90
state: r . state ,
103
91
# need to check what performance implications are of using row_number
104
92
# does the subsequent query's limit clause get applied to the row_number
105
93
# calculated here?
106
- row_number: row_number ( ) |> over ( :row_number ) ,
94
+ w_row_number:
95
+ row_number ( ) |> over ( partition_by: w . id , order_by: r . inserted_at ) ,
96
+ p_row_number:
97
+ row_number ( ) |> over ( partition_by: p . id , order_by: r . inserted_at ) ,
107
98
project_id: w . project_id ,
108
- concurrency: coalesce ( w . concurrency , p . concurrency ) ,
99
+ w_concurrency: coalesce ( w . concurrency , p . concurrency ) ,
100
+ p_concurrency: coalesce ( p . concurrency , 100 ) ,
109
101
inserted_at: r . inserted_at
110
102
} )
111
103
end
@@ -136,7 +128,9 @@ defmodule Lightning.Runs.Query do
136
128
|> where (
137
129
[ r , ipw ] ,
138
130
r . state == :available and
139
- ( is_nil ( ipw . concurrency ) or ipw . row_number <= ipw . concurrency )
131
+ ( is_nil ( ipw . w_concurrency ) or
132
+ ( ipw . w_row_number <= ipw . w_concurrency and
133
+ ipw . p_row_number <= ipw . p_concurrency ) )
140
134
)
141
135
|> order_by ( [ r ] , asc: r . inserted_at )
142
136
end
0 commit comments