@@ -532,12 +532,26 @@ def decrement_qos_rule_running(
532
532
533
533
def get_users_queue_from_processing_time (
534
534
session : sa .orm .Session ,
535
- ) -> list [tuple [str , int ]]:
535
+ interval_stop : datetime .datetime ,
536
+ interval : datetime .timedelta = datetime .timedelta (hours = 24 ),
537
+ ) -> list [tuple [str , float ]]:
536
538
"""Build the queue of the users from the processing time."""
537
- statement = sa .text ("select user_uid, sum(EXTRACT(EPOCH FROM (coalesce(finished_at, now()) -" \
538
- " coalesce(started_at, coalesce(finished_at, now()))))::integer) as cost" \
539
- " from system_requests where finished_at > (now() - interval '24h') " \
540
- " or status='running' or status='accepted' group by user_uid order by cost" )
539
+ interval_start = interval_stop - interval
540
+ request_processing_time = sa .sql .func .least (
541
+ SystemRequest .finished_at , interval_stop
542
+ ) - sa .sql .func .greatest (SystemRequest .started_at , interval_start )
543
+ user_cumulative_processing_time = sa .sql .func .sum (request_processing_time )
544
+ user_cost = sa .sql .func .extract ("epoch" , user_cumulative_processing_time )
545
+ interval_clause = sa .sql .and_ (
546
+ SystemRequest .finished_at >= interval_start ,
547
+ SystemRequest .finished_at < interval_stop ,
548
+ )
549
+ where_clause = sa .sql .or_ (
550
+ interval_clause , SystemRequest .status in ["running" , "accepted" ]
551
+ )
552
+
553
+ statement = sa .sql .select (SystemRequest .user_uid , user_cost ).where (where_clause )
554
+
541
555
return session .execute (statement ).all ()
542
556
543
557
0 commit comments