-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.php
100 lines (84 loc) · 2.04 KB
/
stats.php
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
<?php
function readLogFile($filename)
{
try {
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
throw new Exception("Failed to read the file: $filename");
}
return $lines;
} catch (Exception $e) {
// Log and warn the error
error_log("Error: " . $e->getMessage());
return [];
}
}
function createDistribution($filename)
{
$lines = readLogFile($filename);
$distribution = [];
foreach ($lines as $line) {
$parts = explode(':', $line);
if (count($parts) === 2) {
$userId = $parts[0];
$count = (int)$parts[1];
$distribution[$userId] = $count;
}
}
// Sort the distribution array in descending order by value
arsort($distribution);
return $distribution;
}
function createReferrerList($filename)
{
$lines = readLogFile($filename);
$referrerList = [];
foreach ($lines as $line) {
if (preg_match('/^(https?:\/\/\S+)/', $line, $matches)) {
$referrerList[] = $matches[1];
}
}
return $referrerList;
}
// Create the distribution
$visitorDistribution = createDistribution('/var/log/asanai_visitors.log');
// Create the referrer list
$referrers = createReferrerList('/var/log/asanai_referrers.log');
// Generate data for Plotly
$users = array_keys($visitorDistribution);
$visits = array_values($visitorDistribution);
// Create a Plotly bar chart
$data = [
'x' => $users,
'y' => $visits,
'type' => 'bar',
];
$layout = [
'title' => 'User Visits Distribution',
'xaxis' => ['title' => 'User ID'],
'yaxis' => ['title' => 'Visits'],
];
$chart = [
'data' => [$data],
'layout' => $layout,
];
?>
<!DOCTYPE html>
<html>
<head>
<script src="libs/plotly-latest.min.js"></script>
</head>
<body>
<h2>Unique Users</h2>
<div id="chart"></div>
<script>
Plotly.newPlot('chart', <?php echo json_encode($chart); ?>);
</script>
<h2>Referrers</h2>
<pre>
<?php
print_r($referrers);
?>
</pre>
</body>
</html>