From ce00ad68fac2200792a8c5922e9941e7b7c17452 Mon Sep 17 00:00:00 2001 From: mattf Date: Tue, 6 May 2025 03:01:29 +0000 Subject: [PATCH] Added --POST options for skipping over recordings for leads with active calls, and using logs for statuss to AST_CRON_audio_1 move VDonly script git-svn-id: svn://192.168.202.10@3922 3d104415-ff17-0410-8863-d5cf3c621b8a --- .../trunk/bin/AST_CRON_audio_1_move_VDonly.pl | 147 +++++++++++++++++- 1 file changed, 144 insertions(+), 3 deletions(-) diff --git a/agc_2-X/trunk/bin/AST_CRON_audio_1_move_VDonly.pl b/agc_2-X/trunk/bin/AST_CRON_audio_1_move_VDonly.pl index 4bf3b288..481af738 100644 --- a/agc_2-X/trunk/bin/AST_CRON_audio_1_move_VDonly.pl +++ b/agc_2-X/trunk/bin/AST_CRON_audio_1_move_VDonly.pl @@ -25,7 +25,7 @@ # This program assumes that recordings are saved by Asterisk as .wav # should be easy to change this code if you use .gsm instead # -# Copyright (C) 2023 Matt Florell LICENSE: AGPLv2 +# Copyright (C) 2025 Matt Florell LICENSE: AGPLv2 # # # 80302-1958 - First Build @@ -35,9 +35,13 @@ # 160731-2103 - Added --POST options to change filename with variable lookups # 190311-0105 - Added code to check for agent-muted recordings # 231019-2202 - Changed sleep time between directory scans from 5 to 15 seconds +# 250430-0850 - Added --POST options for skipping over recordings for leads with active calls, and using logs for statuss # $HTTPS=0; +$status_post_logs=0; +$delay_post_live=0; +$now_epoch = int(time()); ### begin parsing run-time options ### if (length($ARGV[0])>1) @@ -68,6 +72,8 @@ if (length($ARGV[0])>1) print " If multiple campaigns or ingroups, use --- delimiting, i.e.: TESTCAMP---TEST_IN2\n"; print " For all calls, use ----ALL----\n"; print " [--CLEAR-POST-NO-MATCH] = clear POST filename variables if no match is found\n"; + print " [--STATUS-POST-LOGS] = use the call logs for POST filename status variable, if found\n"; + print " [--DELAY-POST-LIVE-CALLS] = delay processing of recordings using POST filename variables if any live calls/agents for lead\n"; print "\n"; exit; } @@ -127,6 +133,16 @@ if (length($ARGV[0])>1) $POST=0; if ($q < 1) {print "\n----- POST disabled, no campaigns set -----\n\n";} } + if ($args =~ /--STATUS-POST-LOGS/i) + { + $status_post_logs=1; + if ($q < 1) {print "\n----- STATUS POST LOGS SET: $status_post_logs -----\n\n";} + } + if ($args =~ /--DELAY-POST-LIVE-CALLS/i) + { + $delay_post_live=1; + if ($q < 1) {print "\n----- DELAY POST LIVE SET: $delay_post_live -----\n\n";} + } } if ($args =~ /--CLEAR-POST-NO-MATCH/i) { @@ -222,6 +238,20 @@ if ($sthArows > 0) } $sthA->finish(); +##### Get the settings from servers ##### +$vicidial_recording_limit=0; +$stmtA = "SELECT vicidial_recording_limit FROM servers where server_ip='$server_ip';"; +$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; +$sthA->execute or die "executing: $stmtA ", $dbhA->errstr; +$sthArows=$sthA->rows; +if ($sthArows > 0) + { + @aryA = $sthA->fetchrow_array; + $vicidial_recording_limit = $aryA[0]; + $start_epoch_test = ($now_epoch - ( ($vicidial_recording_limit * 2) * 60) ); + } +$sthA->finish(); + ### find soxi to gather the length info if needed $soxibin = ''; if ( -e ('/usr/bin/soxi')) {$soxibin = '/usr/bin/soxi';} @@ -264,13 +294,18 @@ sleep(15); ### Loop through files a second time to gather filesizes again 5 seconds later $i=0; +$active_recordings=0; +$delay_ct=0; +$processed_ct=0; +$post_status_change_ct=0; foreach(@FILES) { + $lead_id=0; + $vicidial_id=''; $FILEsize2[$i] = 0; if ( (length($FILES[$i]) > 4) && (!-d "$dir1/$FILES[$i]") ) { - $FILEsize2[$i] = (-s "$dir1/$FILES[$i]"); if ($DBX) {print "$FILES[$i] $FILEsize2[$i]\n\n";} @@ -289,7 +324,8 @@ foreach(@FILES) $length_in_sec=0; $rec_ended=0; - $stmtA = "SELECT recording_id,length_in_sec,lead_id,vicidial_id,start_time,end_time,user from recording_log where filename='$SQLFILE' order by recording_id desc LIMIT 1;"; + $start_epoch=0; + $stmtA = "SELECT recording_id,length_in_sec,lead_id,vicidial_id,start_time,end_time,user,UNIX_TIMESTAMP(start_time) from recording_log where filename='$SQLFILE' order by recording_id desc LIMIT 1;"; if($DBX){print STDERR "\n|$stmtA|\n";} $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; @@ -304,11 +340,13 @@ foreach(@FILES) $start_time = $aryA[4]; $end_time = $aryA[5]; $user = $aryA[6]; + $start_epoch = $aryA[7]; if (length($end_time) > 15) {$rec_ended=1;} } $sthA->finish(); $process_recording=1; + ### check for muted recordings, if found then delay processing if ( ($SSmute_recordings > 0) && ($rec_ended < 1) ) { ### check for active muted recordings @@ -353,6 +391,7 @@ foreach(@FILES) { if ($DBX > 0) {print "DEBUG2: recording muting recently for this call, do not process: ($rs_recent_on) |$SQLFILE|ended: $rec_ended|\n";} $process_recording=0; + $delay_ct++; } else { @@ -361,6 +400,62 @@ foreach(@FILES) } } + ### check for POST variables, and delay setting + if ( ($delay_post_live > 0) && ($POST > 0) && ($ALLfile =~ /POSTVLC|POSTSP|POSTADDR3|POSTSTATUS/) ) + { + if ($lead_id > 0) + { + $VLA_count=0; + $VAC_count=0; + if ( ($start_epoch > 0) && ($start_epoch < $start_epoch_test) ) + { + if ($DBX > 0) {print "DEBUG6: delay processing past server limit, process now: ($start_epoch|$start_epoch_test) |$SQLFILE|ended: $rec_ended|\n";} + } + else + { + if ($DBX > 0) {print "DEBUG7: delay processing not past server limit yet: ($start_epoch|$start_epoch_test) |$SQLFILE|ended: $rec_ended|\n";} + + ### check if any live agents are connected to this lead_id right now + $stmtA = "SELECT count(*) from vicidial_live_agents where lead_id='$lead_id';"; + if($DBX){print STDERR "\n|$stmtA|\n";} + $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; + $sthArows=$sthA->rows; + if ($sthArows > 0) + { + @aryA = $sthA->fetchrow_array; + $VLA_count = $aryA[0]; + } + $sthA->finish(); + + ### check if any live calls are tied to this lead_id right now + $stmtA = "SELECT count(*) from vicidial_auto_calls where lead_id='$lead_id';"; + if($DBX){print STDERR "\n|$stmtA|\n";} + $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; + $sthArows=$sthA->rows; + if ($sthArows > 0) + { + @aryA = $sthA->fetchrow_array; + $VAC_count = $aryA[0]; + } + $sthA->finish(); + } + + if ( ($VLA_count > 0) || ($VAC_count > 0) ) + { + if ($DBX > 0) {print "DEBUG4: delay processing active, do not process: ($VLA_count|$VAC_count) |$SQLFILE|ended: $rec_ended|\n";} + $process_recording=0; + $delay_ct++; + } + else + { + if ($DBX > 0) {print "DEBUG5: delay processing - no calls or agents found, OK to process: ($VLA_count|$VAC_count) |$SQLFILE|ended: $rec_ended|\n";} + } + } + } + + ### process the recording files if ($process_recording > 0) { if ($DB) {print "|$recording_id|$length_in_sec|$INfile| |$ALLfile|\n";} @@ -492,6 +587,32 @@ foreach(@FILES) {if ($DB) {print " POST processing ERROR: lead not found: |$lead_id|$ALLfile|\n";} } $sthA->finish(); + if ( ($status_post_logs > 0) && ($ALLfile =~ /POSTSTATUS/) ) + { + $log_status = $status; + if ($vicidial_id =~ /\./) + { + $log_lookupSQL = "SELECT status from vicidial_log where uniqueid='$vicidial_id' and lead_id='$lead_id' and user='$user' order by call_date desc limit 1;"; + } + else + { + $log_lookupSQL = "SELECT status from vicidial_closer_log where closecallid='$vicidial_id' and lead_id='$lead_id' and user='$user' order by call_date desc limit 1;"; + } + if($DBX){print STDERR "\n|$log_lookupSQL|\n";} + $sthA = $dbhA->prepare($log_lookupSQL) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $log_lookupSQL ", $dbhA->errstr; + $sthArows=$sthA->rows; + if ($sthArows > 0) + { + @aryA = $sthA->fetchrow_array; + $log_status = $aryA[0]; + } + + if ($log_status ne $status) + {$post_status_change_ct++;} + if ($DB) {print " POST processing Log status override: lead: |$status| log: |$log_status| $post_status_change_ct \n";} + $status = $log_status; + } $ALLfile =~ s/POSTVLC/$vendor_lead_code/gi; $ALLfile =~ s/POSTSP/$security_phrase/gi; $ALLfile =~ s/POSTADDR3/$address3/gi; @@ -541,14 +662,34 @@ foreach(@FILES) ### sleep for twenty hundredths of a second to not flood the server with disk activity usleep(1*200*1000); + + $processed_ct++; } } + else + {$active_recordings++;} } $i++; } +if($DBX) + { + $end_epoch = int(time()); + $run_length = ($end_epoch - $now_epoch); + + print "\nDebug output:\n"; + print "Total files: $i \n"; + print " Active recordings: $active_recordings \n"; + print " Delayed processing: $delay_ct \n"; + print " Processed files: $processed_ct \n"; + print " POST log status diff: $post_status_change_ct \n"; + print "\n"; + print "Run time: $run_length seconds \n"; + } + if ($DB) {print "DONE... EXITING\n\n";} +$sthA->finish(); $dbhA->disconnect();