-
Notifications
You must be signed in to change notification settings - Fork 4
/
activityTop10.pl
executable file
·206 lines (172 loc) · 6.13 KB
/
activityTop10.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
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/perl -w
# by Torben Menke https://entorb.net
# DESCRIPTION
# display the top 10 activities
# 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
# Modules: Perl Standard
use Encode qw(encode decode);
# use File::Path qw/remove_tree/;
use Time::Local;
use Storable; # read and write variables to
use File::Basename; # for basename, dirname, fileparse
use File::Copy;
use Cwd; # for my $dir = getcwd;
# Modules: Web
use CGI;
my $cgi = CGI->new;
#use CGI ":all";
#use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
# Modules: My Strava Module Lib
use lib ('.');
use lib ('/var/www/virtual/entorb/perl5/lib/perl5');
use lib "C:\\Users\\menketrb\\Documents\\Hacken\\Perl\\Strava-Web"
; # just for making Visual Studio Code happy
# use lib "d:\\files\\Hacken\\Perl\\Strava-Web";
use TMsStrava qw( %o %s)
; # at entorb.net some modules require use local::lib!!!
TMsStrava::htmlPrintHeader( $cgi, 'Activity Top10' );
TMsStrava::initSessionVariables( $cgi->param("session") );
TMsStrava::htmlPrintNavigation();
my $year = 'all';
my $type = 'Run';
my %actPerYear;
if ( $cgi->param('type') ) {
if ( grep { $cgi->param('type') eq $_ } qw (Run Ride Swim) ) {
$type = $cgi->param('type');
}
}
if ( $cgi->param('year') ) {
if ( $cgi->param('year') eq 'all' or $cgi->param('year') =~ m/\d{4}/ ) {
$year = $cgi->param('year');
}
}
my @allActivityHashes;
die "E: cache missing" unless ( -f $s{'pathToActivityListHashDump'} );
TMsStrava::logIt("reading activity data from dmp file");
my $ref = retrieve( $s{'pathToActivityListHashDump'} )
; # retrieve data from file (as ref)
@allActivityHashes = @{$ref}; # convert arrayref to array
# first loop through all activities to fetch years
TMsStrava::logIt("first loop through all activities to fetch years");
my %years;
foreach my $ref (@allActivityHashes) {
my %h = %{$ref}; # each $activity is a hashref
my $date = $h{'start_date_local'}; # 2016-12-27T14:00:50Z
my $thisYear = substr( $date, 0, 4 );
$years{$thisYear}++;
} ## end foreach my $ref (@allActivityHashes)
my @years = sort keys %years;
unshift @years, 'all';
TMsStrava::logIt("done");
say "<form action=\"activityTop10.pl\" method=\"post\">
<input type=\"hidden\" name=\"session\" value=\"$s{ 'session' }\">
<table>
<tr><td>Activity Type</td>
<td>
<select name=\"type\">
<option value=\"Run\""
. ( $type eq 'Run' ? 'selected' : '' ) . ">Run</option>
<option value=\"Ride\""
. ( $type eq 'Ride' ? 'selected' : '' ) . ">Ride</option>
<option value=\"Swim\""
. ( $type eq 'Swim' ? 'selected' : '' ) . ">Swim</option>
</select>
</td><td> </td>
</tr>
<tr><td>Year</td>
<td>
<select name=\"year\">";
foreach my $y (@years) {
say "<option value=\"$y\""
. ( $year eq "$y" ? 'selected' : '' )
. ">$y</option>";
}
say "</select>
</td>
<td>
<input type=\"submit\" name=\"submit\" value=\"Submit\">
</td>
</tr>
</table>
</form>";
my %database;
foreach my $ref (@allActivityHashes) {
my %h = %{$ref}; # each $activity is a hashref
my $thisType = $h{'type'};
# filter only relevant activity types
if ( not $thisType eq $type ) {
$ref = undef;
next;
}
# filter only relevant years
my $date = $h{'start_date_local'}; # 2016-12-27T14:00:50Z
my $thisYear = substr( $date, 0, 4 ); # YYYY
if ( $year ne 'all' and $year != $thisYear ) {
$ref = undef;
next;
}
# add to database hash
$database{ $h{'id'} } = $ref;
} ## end foreach my $ref (@allActivityHashes)
@allActivityHashes = grep { defined($_) } @allActivityHashes;
# my @rankDisciplines = qw (x_min x_km km/h x_min/km total_elevation_gain x_elev_m/km x_dist_start_end_km average_heartrate);
my %parametersToRank;
$parametersToRank{'x_min'} = 'Duration (min)';
$parametersToRank{'x_km'} = 'Distance (km)';
$parametersToRank{'km/h'} = 'Speed (km/h)';
$parametersToRank{'total_elevation_gain'} = 'Elevation Gain (m)';
$parametersToRank{'x_elev_m/km'} = 'Elevation Gain per Distance (m/km)';
$parametersToRank{'x_dist_start_end_km'} = 'Distance Start-End (km)';
$parametersToRank{'average_heartrate'} = 'Average Heartrate (bpm)';
my @Reihenfolge
= qw (x_km x_min km/h total_elevation_gain x_elev_m/km x_dist_start_end_km average_heartrate);
say "<p>Jump to<ul>";
foreach my $column (@Reihenfolge) {
say "<li><a href=#sec_$column>$parametersToRank{ $column }</a></li>";
}
say "</ul></p>";
foreach my $column (@Reihenfolge) {
die "E: $column is missing in parametersToRank"
unless defined( $parametersToRank{$column} );
my $columnTitle = $parametersToRank{$column};
my @sorted
= TMsStrava::sortArrayHashRefsNumDesc( $column, @allActivityHashes );
say "<h3 id=sec_$column>$columnTitle</h3>";
my $count = 0;
say '<table width="100%" cellpadding="2" cellspacing="0">';
say "<tr><th>Rank</th><th>Date</th><th>Name</th>";
say "<th>$columnTitle</th>" if ( $column ne 'x_min' and $column ne 'x_km' );
say "<th>Pace (min/km)"
if ( $column eq 'km/h' ); # pace only for km/h ranking
say "<th>Distance<br>(km)</th><th>Duration (min)</th></tr>";
foreach my $ref (@sorted) {
my %h = %{$ref};
next if ( not defined $h{$column} );
$count++;
say "<tr>";
say "<td>$count</td>";
say "<td>"
. TMsStrava::formatDate( $h{'start_date_local'}, 'date' ) . "</td>";
say "<td>" . TMsStrava::activityUrl( $h{"id"}, $h{"name"} ) . "</td>";
say "<td>$h{ $column }</td>"
if ( $column ne 'x_min' and $column ne 'x_km' );
say "<td>$h{ 'x_min/km' }</td>"
if ( $column eq 'km/h' ); # pace only for km/h ranking
say "<td>" . ( sprintf '%.1f', $h{'x_km'} ) . "</td>";
say "<td>" . ( sprintf '%d', $h{'x_min'} ) . "</td>";
say "</tr>";
last if $count == 25;
} ## end foreach my $ref (@sorted)
say "</table>";
} ## end foreach my $column (@Reihenfolge)
TMsStrava::htmlPrintFooter($cgi);