4242 db_get_keys , db_insert_report , db_get_recent_results2 , db_patch_approval2 , db_get_report ,
4343 db_ensure_schema , db_get_apikeys , db_update_apikey , db_filter_apikeys , db_clear_delegates ,
4444 db_find_subjects , db_insert_result2 , db_get_relevant_results2 , db_add_delegate , db_get_group ,
45- db_get_relevant_compliance_results , db_insert_compliance_result ,
45+ db_get_relevant_compliance_results , db_insert_compliance_result , db_insert_event ,
4646)
4747
4848
@@ -495,19 +495,19 @@ async def post_report(
495495 result = rdata ['result' ]
496496 approval = 1 == result # pre-approve good result
497497 db_insert_result2 (cur , checked_at , subject , scopeuuid , version , check , result , approval , reportid )
498- conn .commit ()
499498
500- checked_at = datetime .now ()
501- for approved_only in (False , True ):
502- with conn .cursor () as cur :
499+ checked_at = datetime .now ()
500+ # add new compliance result if existing compliance result is not newer than `threshold`
501+ threshold = checked_at - timedelta (hours = 12 )
502+ for approved_only in (False , True ):
503503 # fetch latest compliance results before new report
504504 rows = db_get_relevant_compliance_results (cur , approved_only = approved_only )
505505 results0 = defaultdict (lambda : defaultdict (dict ))
506506 for row in rows :
507- subj , scope_uuid , version , result , _ , _ = row
507+ subj , scope_uuid , version , result , _ , ch_at = row
508508 if subj not in reported_subjects :
509509 continue
510- results0 [subj ][scope_uuid ][version ] = result
510+ results0 [subj ][scope_uuid ][version ] = ( result , ch_at )
511511 # compute latest compliance results after new report
512512 rows2 = db_get_relevant_results2 (cur , approved_only = approved_only )
513513 results = convert_result_rows_to_dict2 (rows2 , get_scopes ())
@@ -517,11 +517,15 @@ async def post_report(
517517 continue
518518 for scope_uuid , scope_results in subj_results .items ():
519519 for version , version_results in scope_results ['versions' ].items ():
520- result = results0 [subj ][scope_uuid ].get (version , 0 )
520+ result , ch_at = results0 [subj ][scope_uuid ].get (version , ( 0 , threshold ) )
521521 new_result = version_results ['result' ]
522- db_insert_compliance_result (cur , checked_at , subj , scope_uuid , version , new_result , approval )
522+ if new_result != result or ch_at <= threshold :
523+ print (ch_at , threshold )
524+ db_insert_compliance_result (cur , checked_at , subj , scope_uuid , version , new_result , approved_only )
523525 if new_result != result :
526+ db_insert_event (cur , checked_at , subj , scope_uuid , version , result , new_result , approved_only )
524527 print (f"{ subj } { scope_uuid } { version } : { result } -> { new_result } " )
528+ conn .commit ()
525529
526530
527531def convert_result_rows_to_dict2 (
0 commit comments