-
Notifications
You must be signed in to change notification settings - Fork 4
/
stats.pl
executable file
·193 lines (157 loc) · 5.12 KB
/
stats.pl
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/perl -w
# by Torben Menke https://entorb.net
# DESCRIPTION
# use gnuplot to display usage stats for this app
# TODO
# IDEAS
# DONE
# Modules: My Default Set
use strict;
use warnings;
use 5.010; # say
use Data::Dumper;
use utf8; # this script is written in UTF-8
binmode STDOUT, ':utf8'; # default encoding for linux print STDOUT
use autodie qw (open close)
; # Replace functions with ones that succeed or die: e.g. close
# pp -u -M Excel::Writer::XLSX -o script.exe script.pl & copy script.exe c:\tmp
# Modules: Perl Standard
# use Encode qw(encode decode);
# use open ":encoding(UFT-8)"; # for all files
# Modules: File Access
use File::Basename; # for basename, dirname, fileparse
use File::Path qw(make_path remove_tree);
# Modules: CPAN
# use LWP::UserAgent; # http requests
# use Excel::Writer::XLSX;
# perl -MCPAN -e "install Excel::Writer::XLSX"
use CGI;
my $cgi = new CGI;
#use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print $cgi->header(
-type => 'text/html',
-charset => 'utf-8'
);
print $cgi->start_html(
-title => 'Torben\'s Strava App Stats',
-meta => { 'author' => 'Torben Menke' },
-style => { -src => '/style.css' }
);
my %o;
$o{'dataFolderBase'} = '/var/www/virtual/entorb/data-web-pages/strava';
my $fileIn = $o{'dataFolderBase'} . '/login.log';
open my $fhIn, '<', $fileIn
or die "ERROR: Can't read from file '$fileIn': $!";
my %h; # temp
# remove multiple logins per day via reduction to
# yy-mm-dd<TAB>userid
foreach my $line (<$fhIn>) {
chomp $line; # remove \n;
my ( $date, $userid, $username, $scope ) = split /\t/, $line;
$userid += 0; # convert to number
next if $userid == 7656541; # that is me
$date =~ m/^(\d+)\.(\d+)\.(\d+) .*/;
$date = $_ = sprintf "%02d-%02d-%02d", $3 - 2000, $2, $1;
$_ = "$date\t$userid";
$h{$_}++;
} ## end foreach my $line (<$fhIn>)
close $fhIn;
my @visitingDays = sort keys %h; # yy-mm-dd<TAB>userid
undef %h;
say "<h1>Usage Stats</h1>";
# fill hashes
my %userVisitCount = ();
my %visitorsPerDay;
my %usersPerMonth = ();
my %newUsersPerMonth = ();
my @knownUsers;
foreach my $line (@visitingDays) {
my ( $day, $userid ) = split "\t", $line;
$userid += 0; # convert to number
$userVisitCount{$userid}++;
$visitorsPerDay{$day}++;
my $month = $day;
$month =~ s/\-\d+$//s;
$usersPerMonth{$month}++;
if ( not grep { $userid eq $_ } @knownUsers ) {
push @knownUsers, $userid;
$newUsersPerMonth{$month}++;
}
else {
$newUsersPerMonth{$month} += 0; # initialize if not present
}
} ## end foreach my $line (@visitingDays)
# unique users
@_ = keys %userVisitCount;
my $anzUsers = ( $#_ + 1 );
my $anzLogins = 0;
my $anzRepeatingVisitors = 0;
foreach my $user ( keys %userVisitCount ) {
$anzLogins += $userVisitCount{$user};
$anzRepeatingVisitors++ if ( $userVisitCount{$user} ) > 1;
}
say "<p>";
say
"$anzLogins logins by $anzUsers unique and $anzRepeatingVisitors returning users.";
say "</p>";
# # due to privacy reasons: not printed on web!
# %h = %userVisitCount;
# say "<h2>recurring user ranking</h2>";
# say "<table";
# say "<tr><th>user</th><th>total</th></tr>";
# foreach my $k ( sort { $h{ $b } <=> $h{ $a } } keys %h ) {
# last if $h{ $k } == 1;
# say "<tr><td>$k</td><td>$h{$k}</td></tr>"; # https://www.strava.com/athletes/$k
# }
# say "</table>";
my $fileOut = "stats.dat";
open my $fhOut, '>:encoding(UTF-8)', $fileOut
or die "ERROR: Can't write to file '$fileOut': $!";
say {$fhOut} "# month_dec\tmonth\ttotal\tnew\treturning";
%h = %usersPerMonth;
say "<h2>users per month</h2>";
say "<table>";
say
"<tr><th>month</th><th>total</th><th>new</th><th>returning</th><th>% returning</th></tr>";
# foreach my $mon ( reverse sort keys( %h ) ) {
my @dataforfile;
foreach my $mon ( reverse sort keys(%h) ) {
# say "$mon\t$usersPerMonth{$mon}";
say
"<tr><td>$mon</td><td>$h{$mon}</td><td>$newUsersPerMonth{$mon}</td><td>"
. ( $h{$mon} - $newUsersPerMonth{$mon} )
. "</td><td>"
. sprintf "%d</td></tr>",
100 * ( $h{$mon} - $newUsersPerMonth{$mon} ) / $h{$mon};
# 18-10 -> 2018 + 9/12
$_ = $mon;
@_ = split "-", $mon;
my $month_dec = sprintf "%.02f", 2000 + $_[0] + ( $_[1] - 1 ) / 12;
# unshift because html table is desc
unshift @dataforfile,
"$month_dec\t$mon\t$h{$mon}\t$newUsersPerMonth{$mon}\t"
. ( $h{$mon} - $newUsersPerMonth{$mon} );
} ## end foreach my $mon ( reverse sort...)
say "</table>";
foreach my $line (@dataforfile) {
say {$fhOut} $line;
}
close $fhOut;
# run gnuplot, but only if app-stats.png is older than 15 min
$_ = ( stat('stats.png') )[9]; # mtime as timestamp
if ( time - $_ > 900 ) {
`gnuplot gnuplot/app-stats.gp`;
}
say
"<p><img src=\"stats.png\" alt=\"stats.png\" width=\"1200\" height=\"600\" ></p>";
# %h = %visitorsPerDay;
# say "<h2>users per day</h2>";
# say "<table>";
# say "<tr><th>day</th><th>total</th></tr>";
# foreach my $k ( reverse sort keys(%h) ) {
# ## say "$k\t$usersPerMonth{$k}";
# say "<tr><td>$k</td><td>$h{$k}</td></tr>";
# }
# say "</table>";
unlink $fileOut;
print $cgi->end_html;