Created
July 16, 2020 01:20
-
-
Save codemonkey76/e69a88321cb187e46359787e2c6edf34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Cdr; | |
use Carbon\Carbon; | |
use Illuminate\Http\Request; | |
const HOURS=8; | |
const MAX_PHONECALL = 2; | |
class ActiveCallController extends Controller | |
{ | |
public function index(Request $request) | |
{ | |
if ($request->has('date')) { | |
$startDate = Carbon::parse($request->date); | |
} else { | |
$startDate = Carbon::now(); | |
} | |
$date1 = Carbon::parse($startDate)->subHours(HOURS); | |
$date2 = Carbon::parse($startDate)->subHours(HOURS+MAX_PHONECALL); | |
$date3 = Carbon::parse($startDate); | |
$startActiveCalls = Cdr::where('answer_stamp', '>', $date2) | |
->where('answer_stamp', '<', $date1) | |
->where('end_stamp', '>', $date1)->count(); | |
$cdrs = Cdr::where('answer_stamp', '<', $date3) | |
->where('answer_stamp', '>', $date1)->get(); | |
$answered = $cdrs->map(function($cdr) { | |
return (object)[ | |
'id' => $cdr->xml_cdr_uuid, | |
'action' => 'answer', | |
'timestamp' => $cdr->answer_stamp]; | |
}); | |
$ended = $cdrs->map(function($cdr) { | |
return (object)[ | |
'id' => $cdr->xml_cdr_uuid, | |
'action' => 'end', | |
'timestamp' => $cdr->end_stamp]; | |
}); | |
$events = $ended | |
->merge($answered) | |
->sortBy('timestamp') | |
->values(); | |
$minutes = collect(); | |
for ($i = (HOURS*60); $i >= 0; $i--) { | |
$minutes->push(Carbon::parse($date3)->subMinutes($i)->format('YmdHi')); | |
} | |
$maxActiveCalls = collect(); | |
$currentActiveCalls = $startActiveCalls; | |
$maxActiveThisMinute = $currentActiveCalls; | |
foreach ($minutes as $minute) { | |
$eventsForThisMinute = $events->filter(function ($event) use ($minute){ | |
return Carbon::parse($event->timestamp)->format('YmdHi') === $minute; | |
}); | |
foreach ($eventsForThisMinute as $event) { | |
if ($event->action === 'answer') { | |
$currentActiveCalls++; | |
if ($maxActiveThisMinute < $currentActiveCalls) | |
$maxActiveThisMinute = $currentActiveCalls; | |
} else if ($event->action === 'end') { | |
$currentActiveCalls--; | |
} | |
} | |
$maxActiveCalls->push( | |
(object)[ | |
'time' => $minute, | |
'max' => $maxActiveThisMinute | |
]); | |
$maxActiveThisMinute = $currentActiveCalls; | |
} | |
return $maxActiveCalls; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment