Last active
August 29, 2015 14:06
-
-
Save peterjaap/f1e850624020995d8e96 to your computer and use it in GitHub Desktop.
Analyze Voys CDRS file (downloadable from mijn.voys.nl)
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 | |
if(count($argv)==1) { | |
echo <<< EOT | |
Usagae; {$_SERVER['PHP_SELF']} {filename} {from} {to} {showUpper} | |
Filename defaults to cdrs.csv | |
From defaults to today | |
To defaults to From plus 1 year | |
ShowUpper only shows top X callers (defaults to 25) | |
Example; {$_SERVER['PHP_SELF']} cdrs.csv 01-01-2014 01-07-2014 | |
EOT; | |
exit; | |
} | |
$file = $argv[1]; | |
if(!file_exists($file)) { | |
echo 'File ' . $file .' is not found!' . PHP_EOL; | |
exit; | |
} | |
if(!isset($argv[3])) { | |
$to = date('U'); | |
} else { | |
$to = strtotime($argv[3]); | |
} | |
if(!isset($argv[2])) { | |
$from = strtotime('-1 year',$to); | |
} else { | |
$from = strtotime($argv[2]); | |
} | |
if(!isset($argv[4])) { | |
$showUpperX = 25; | |
} else { | |
$showUpperX = $argv[4]; | |
} | |
$phonenumbers = array( | |
'+31612345678' => 'Example', | |
// etc | |
); | |
if (($handle = fopen($file, "r")) !== FALSE) { | |
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { | |
$date = $data[3]; | |
$phonenumber = $data[7]; | |
if(strlen($phonenumber)==3) continue; // skip internal numbers | |
$type = $data[4]; | |
$minutes = $data[6]; | |
if(strtotime($date) > $from && strtotime($date) < $to) { | |
@$totals[$phonenumber]['total'] += $minutes; | |
@$totals[$phonenumber][$type] += $minutes; | |
} | |
} | |
fclose($handle); | |
} | |
foreach($totals as $phonenumber=>$data) { | |
if(isset($phonenumbers[$phonenumber])) { | |
@$output[$phonenumbers[$phonenumber]]['total'] += $data['total']; | |
@$output[$phonenumbers[$phonenumber]]['Inbound'] += $data['Inbound']; | |
@$output[$phonenumbers[$phonenumber]]['Outbound'] += $data['Outbound']; | |
} else { | |
@$output[$phonenumber]['total'] += $data['total']; | |
@$output[$phonenumber]['Inbound'] += $data['Inbound']; | |
@$output[$phonenumber]['Outbound'] += $data['Outbound']; | |
} | |
} | |
arsort($output); | |
$output = array_slice($output,0,$showUpperX,true); | |
echo 'Calls from ' . date('d-m-Y', $from) . ' to ' . date('d-m-Y', $to) . PHP_EOL . PHP_EOL; | |
foreach($output as $company=>$data) { | |
unset($ratio); | |
if(!isset($data['Outbound']) || $data['Outbound'] == 0) $ratio = 0; | |
if(!isset($data['Inbound']) || $data['Inbound'] == 0) $ratio = 0; | |
if(!isset($ratio)) $ratio = ceil($data['Inbound'] / $data['Outbound']); | |
echo $company . ': ' . gmdate('H:i:s', $data['total']) . ' (In; ' . gmdate('H:i:s', @$data['Inbound']) . ' / Out: ' . gmdate('H:i:s', @$data['Outbound']) .', Ratio: ' . $ratio . ')' . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment