@@ -238,9 +238,9 @@ def update_branch(cursor, build_id, build_status, commit_id, build_branch):
238
238
LIMIT 1""" ,
239
239
(build_id , build_branch , commit_id ),
240
240
)
241
- ( is_most_recent_build_id ,) = cursor .fetchone ()
242
- if is_most_recent_build_id :
243
- is_current_build_for_branch = True
241
+ if row : = cursor .fetchone ():
242
+ if row [ 0 ] :
243
+ is_current_build_for_branch = True
244
244
245
245
if is_current_build_for_branch :
246
246
# Find the latest branch (push) record corresponding to the
@@ -512,7 +512,7 @@ def ingest_webhook(conn, event_type, event):
512
512
"""select 1
513
513
from build
514
514
where build_id = %s
515
- for key share """ ,
515
+ for update """ ,
516
516
(build_id ,),
517
517
)
518
518
if not cursor .fetchone ():
@@ -674,15 +674,16 @@ def poll_stale_build(conn, build_id):
674
674
675
675
# check if we already have this task, and what its status is
676
676
cursor .execute (
677
- """SELECT status
677
+ """SELECT status, status != %s
678
678
FROM task
679
- WHERE task_id = %s""" ,
680
- (task_id ,),
679
+ WHERE task_id = %s
680
+ FOR UPDATE""" ,
681
+ (task_status , task_id ),
681
682
)
682
683
if row := cursor .fetchone ():
683
684
# process change, if it is different
684
- (old_task_status ,) = row
685
- if old_task_status != task_status :
685
+ (old_task_status , change ) = row
686
+ if change :
686
687
process_new_task_status (
687
688
cursor , task_id , old_task_status , task_status , "poll" , task_sent
688
689
)
@@ -813,12 +814,12 @@ def poll_stale_builds(conn):
813
814
status,
814
815
avg_elapsed + stddev_elapsed * 2 as elapsed_p95
815
816
from build_status_statistics
816
- where branch_name = 'master' or branch_name like 'REL_%'),
817
+ where branch_name = 'master' or branch_name like 'REL_%% '),
817
818
run as (select build_id,
818
819
status,
819
820
branch_name,
820
821
case
821
- when branch_name = 'master' or branch_name like 'REL_%'
822
+ when branch_name = 'master' or branch_name like 'REL_%% '
822
823
then branch_name
823
824
else 'master'
824
825
end as reference_branch,
@@ -831,7 +832,8 @@ def poll_stale_builds(conn):
831
832
run.status,
832
833
extract(epoch from ref.elapsed_p95),
833
834
extract(epoch from run.elapsed)
834
- from run left join ref on (run.reference_branch = ref.branch_name)
835
+ from run
836
+ left join ref on ((run.reference_branch, run.status) = (ref.branch_name, ref.status))
835
837
where run.elapsed > COALESCE(elapsed_p95, interval '30 minutes')""" )
836
838
for (
837
839
build_id ,
@@ -861,8 +863,66 @@ def poll_stale_builds(conn):
861
863
cfbot_work_queue .insert_work_queue (cursor , "poll-stale-build" , build_id )
862
864
863
865
866
+ def refresh_task_status_statistics (conn ):
867
+ cursor = conn .cursor ()
868
+ cursor .execute (
869
+ """delete from task_status_history where sent < now() - interval '30 days'"""
870
+ )
871
+ cursor .execute ("""delete from task_status_statistics""" )
872
+ # XXX waiting for more data before removing hard coded 'master' from here...
873
+ cursor .execute ("""insert into task_status_statistics
874
+ (branch_name, task_name, status, avg_elapsed, stddev_elapsed, n)
875
+ with elapsed as (select coalesce('master', build.branch_name) as branch_name,
876
+ task.task_name,
877
+ h.status,
878
+ lead(h.sent) over(partition by h.task_id order by h.sent) - h.sent as elapsed
879
+ from build
880
+ join task using (build_id)
881
+ join task_status_history h using (task_id)
882
+ where task.status = 'COMPLETED'
883
+ --- and (build.branch_name = 'master' or build.branch_name like 'REL_%%')
884
+ )
885
+ select branch_name,
886
+ task_name,
887
+ status,
888
+ avg(elapsed),
889
+ coalesce(interval '1 second' * stddev(extract(epoch from elapsed)), interval '0 seconds') as stddev,
890
+ count(elapsed) as n
891
+ from elapsed
892
+ where elapsed is not null
893
+ group by 1, 2, 3
894
+ --- having count(*) > 1""" )
895
+
896
+
897
+ def refresh_build_status_statistics (conn ):
898
+ cursor = conn .cursor ()
899
+ cursor .execute (
900
+ """delete from build_status_history where received < now() - interval '30 days'"""
901
+ )
902
+ cursor .execute ("""delete from build_status_statistics""" )
903
+ cursor .execute ("""insert into build_status_statistics
904
+ (branch_name, status, avg_elapsed, stddev_elapsed, n)
905
+ with elapsed as (select coalesce('master', build.branch_name) as branch_name,
906
+ h.status,
907
+ lead(received) over (partition by h.build_id order by received) - received as elapsed
908
+ from build_status_history h
909
+ join build using (build_id)
910
+ where build.status = 'COMPLETED'
911
+ --- and (build.branch_name = 'master' or build.branch_name like 'REL_%%')
912
+ )
913
+ select branch_name,
914
+ status,
915
+ avg(elapsed),
916
+ coalesce(interval '1 second' * stddev(extract(epoch from elapsed)), interval '0 seconds') as stddev,
917
+ count(elapsed) as n
918
+ from elapsed
919
+ where elapsed is not null
920
+ group by 1, 2
921
+ --- having count(*) > 1""" )
922
+
923
+
864
924
if __name__ == "__main__" :
865
925
with cfbot_util .db () as conn :
866
- cursor = conn . cursor ( )
867
- poll_stale_build (conn , "4879753326362624" )
926
+ refresh_task_status_statistics ( conn )
927
+ refresh_build_status_statistics (conn )
868
928
conn .commit ()
0 commit comments