diff options
author | Daniel Friesel <derf@finalrewind.org> | 2015-02-24 16:12:35 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2015-02-24 16:12:35 +0100 |
commit | d991d48df768cfc990848d7aa661ce632a262847 (patch) | |
tree | 26c94e83b6685c92725c744ff3a08a689e3b135a | |
parent | 60365f7ee8b34a3d4c92fde708e5fa7110e0ce34 (diff) |
map weekdays to their name, start our week on monday
-rw-r--r-- | index.pl | 28 |
1 files changed, 22 insertions, 6 deletions
@@ -51,12 +51,14 @@ get '/by_hour.json' => sub { }; get '/2ddata.tsv' => sub { - my $self = shift; + my $self = shift; my $aggregate = $self->param('aggregate') // 'hour'; - my $metric = $self->param('metric') // 'delay'; - my $msgnum = int($self->param('msgnum') // 0); + my $metric = $self->param('metric') // 'delay'; + my $msgnum = int( $self->param('msgnum') // 0 ); + + my @weekdays = qw(So Mo Di Mi Do Fr Sa); - if ($msgnum < 0 or $msgnum > 99) { + if ( $msgnum < 0 or $msgnum > 99 ) { $msgnum = 0; } @@ -67,7 +69,7 @@ get '/2ddata.tsv' => sub { my $query; my $format = 'strftime("%H", scheduled_time, "unixepoch")'; - given($aggregate) { + given ($aggregate) { when ('weekday') { $format = 'strftime("%w", scheduled_time, "unixepoch")'; } @@ -75,7 +77,7 @@ get '/2ddata.tsv' => sub { $format = 'strftime("%w%H", scheduled_time, "unixepoch")'; } when ('line') { - $format = 'train_type || " " || line_no'; + $format = 'train_type || " " || line_no'; $where_clause = 'line_no is not null'; } when ('train_type') { @@ -114,6 +116,20 @@ get '/2ddata.tsv' => sub { my $dbres = $self->app->dbh->selectall_arrayref($query); + if ( $aggregate eq 'weekday' ) { + @{$dbres} = map { [ $weekdays[ $_->[0] ], $_->[1] ] } + ( @{$dbres}[ 1 .. 6 ], $dbres->[0] ); + } + elsif ( $aggregate eq 'weekhour' ) { + @{$dbres} = map { + [ + $weekdays[ substr( $_->[0], 0, 1 ) ] . q{ } + . substr( $_->[0], 1 ), + $_->[1] + ] + } ( @{$dbres}[ 1*24 .. 6*24 ], $dbres->[0] ); + } + for my $row ( @{$dbres} ) { $res .= sprintf( "%s\t%s\n", @{$row} ); } |