Skip to content

Commit

Permalink
feat(misc) improved statistics.php report output
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchezzzhak committed Aug 15, 2020
1 parent 9e75645 commit 37328ce
Showing 1 changed file with 42 additions and 34 deletions.
76 changes: 42 additions & 34 deletions misc/statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@

require_once(__DIR__.'/../vendor/autoload.php');

if (php_sapi_name() !== 'cli') {
die("web not supported");
}
if(count($argv) != 2) {
die("Invalid arguments. Usage: php statistics.php filetoparse.txt");
}

use DeviceDetector\Parser\Device\DeviceParserAbstract;
use DeviceDetector\DeviceDetector;



$parsedUAs = $unknownDeviceTypes =
$detectedBots = 0;

\DeviceDetector\Parser\Device\DeviceParserAbstract::setVersionTruncation(\DeviceDetector\Parser\Device\DeviceParserAbstract::VERSION_TRUNCATION_NONE);

$deviceTypes = (array_fill(0, count(\DeviceDetector\Parser\Device\DeviceParserAbstract::getAvailableDeviceTypes()), 0));
$deviceAvailableDeviceTypes = array_flip(DeviceParserAbstract::getAvailableDeviceTypes());

DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE);

$deviceTypes = (array_fill(0, count($deviceAvailableDeviceTypes), 0));

$startTime = microtime(true);

Expand Down Expand Up @@ -72,6 +83,28 @@ function format($str, $length)
return sprintf("%".strlen($length)."d", $str);
}

$line = "-------------------------------------------\n";
$mask = "%-24s %8s %8s \n";
$reportStat = [];
$reportStat[] = sprintf($mask, 'Type', 'Count', 'Percent');
$reportStat[] = $line;
foreach ($deviceTypes as $deviceTypeId => $deviceCount) {
$reportStat[] = sprintf(
$mask,
sprintf('%s',mb_convert_case($deviceAvailableDeviceTypes[$deviceTypeId], MB_CASE_TITLE)),
format($deviceCount, $parsedUAs),
sprintf('(%s%%)', trim(getPercentage($deviceCount, $parsedUAs)))
);
}

$reportStat[] = sprintf(
$mask,
'Unknown',
format($unknownDeviceTypes, $parsedUAs),
sprintf('(%s%%)',trim(getPercentage($unknownDeviceTypes, $parsedUAs)))
);
$reportStat[] = $line;

echo sprintf("
Parsed user agents: %u
Expand All @@ -82,36 +115,11 @@ function format($str, $length)
Detected Bots: %s (%s%%)
Detected device types:
----------------------------------
Desktop: %s (%s%%)
Smartphone: %s (%s%%)
Tablet: %s (%s%%)
Feature Phone: %s (%s%%)
Console: %s (%s%%)
TV: %s (%s%%)
Car Browser: %s (%s%%)
Smart Display: %s (%s%%)
Camera: %s (%s%%)
Media Player: %s (%s%%)
Phablet: %s (%s%%)
Smart Speaker: %s (%s%%)
Wearable: %s (%s%%)
Unknown: %s (%s%%)
----------------------------------
",
$parsedUAs, round($timeElapsed, 2), round($timeElapsed/$parsedUAs, 6), format($detectedBots, $parsedUAs), getPercentage($detectedBots, $parsedUAs),
format($deviceTypes[0], $parsedUAs), getPercentage($deviceTypes[0], $parsedUAs),
format($deviceTypes[1], $parsedUAs), getPercentage($deviceTypes[1], $parsedUAs),
format($deviceTypes[2], $parsedUAs), getPercentage($deviceTypes[2], $parsedUAs),
format($deviceTypes[3], $parsedUAs), getPercentage($deviceTypes[3], $parsedUAs),
format($deviceTypes[4], $parsedUAs), getPercentage($deviceTypes[4], $parsedUAs),
format($deviceTypes[5], $parsedUAs), getPercentage($deviceTypes[5], $parsedUAs),
format($deviceTypes[6], $parsedUAs), getPercentage($deviceTypes[6], $parsedUAs),
format($deviceTypes[7], $parsedUAs), getPercentage($deviceTypes[7], $parsedUAs),
format($deviceTypes[8], $parsedUAs), getPercentage($deviceTypes[8], $parsedUAs),
format($deviceTypes[9], $parsedUAs), getPercentage($deviceTypes[9], $parsedUAs),
format($deviceTypes[10], $parsedUAs), getPercentage($deviceTypes[10], $parsedUAs),
format($deviceTypes[11], $parsedUAs), getPercentage($deviceTypes[11], $parsedUAs),
format($deviceTypes[12], $parsedUAs), getPercentage($deviceTypes[12], $parsedUAs),
format($unknownDeviceTypes, $parsedUAs), getPercentage($unknownDeviceTypes, $parsedUAs)
%s
", $parsedUAs,
round($timeElapsed, 2),
round($timeElapsed / $parsedUAs, 6),
format($detectedBots, $parsedUAs),
getPercentage($detectedBots, $parsedUAs),
implode('', $reportStat)
);

0 comments on commit 37328ce

Please sign in to comment.