File: statistics.php

package info (click to toggle)
matomo-device-detector 6.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,684 kB
  • sloc: php: 7,758; xml: 79; makefile: 15; sh: 3
file content (136 lines) | stat: -rw-r--r-- 3,139 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

/**
 * Device Detector - The Universal Device Detection library for parsing User Agents
 *
 * @link https://matomo.org
 *
 * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
 */

declare(strict_types=1);

use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;

if ('cli' !== PHP_SAPI) {
    die('web not supported');
}

if (2 !== count($argv)) {
    die('Invalid arguments. Usage: php statistics.php filetoparse.txt');
}

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

$parsedUAs = $unknownDeviceTypes = $detectedBots = 0;

$deviceAvailableDeviceTypes = array_flip(AbstractDeviceParser::getAvailableDeviceTypes());

AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);

$deviceTypes = (array_fill(0, count(AbstractDeviceParser::getAvailableDeviceTypes()), 0));

$startTime = microtime(true);

$handle = @fopen($argv[1], 'rb');

$parser = new DeviceDetector();

if ($handle) {
    while (false !== ($line = fgets($handle, 4096))) {
        if (empty($line)) {
            continue;
        }

        if ($parsedUAs > 0 && 0 === $parsedUAs % 80) {
            echo " {$parsedUAs}\n";
        }

        $parser->setUserAgent(trim($line));
        $parser->parse();

        echo '.';

        $parsedUAs++;

        if ($parser->isBot()) {
            $detectedBots++;

            continue;
        }

        if (null !== $parser->getDevice()) {
            $deviceTypes[$parser->getDevice()]++;
        } else {
            $unknownDeviceTypes++;
        }
    }

    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }

    fclose($handle);
}

$timeElapsed = microtime(true) - $startTime;

function getPercentage(int $cur, int $max): string
{
    return format((int) round($cur * 100 / $max), '   ');
}

/**
 * @param int|string $str
 * @param int|string $length
 * @return string
 */
function format($str, $length): string
{
    return sprintf('%' . strlen((string) $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

Total time elapsed:            %s
Average time per user agent:   %s

Detected Bots:    %s    (%s%%)

Detected device types:
%s
',
    $parsedUAs,
    round($timeElapsed, 2),
    round($timeElapsed / $parsedUAs, 6),
    format($detectedBots, $parsedUAs),
    getPercentage($detectedBots, $parsedUAs),
    implode('', $reportStat)
);