diff options
author | Daniel Friesel <derf@finalrewind.org> | 2015-02-20 14:19:50 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2015-02-20 14:19:50 +0100 |
commit | 2bee7816ee19afbaee17376dc44177a53e569f91 (patch) | |
tree | 288fbcfa1c68ef69ca3f4498516652a8952581d3 | |
parent | 9f8c64bacc2b791e5e5a029e36999cbfeb88bd79 (diff) |
some kind of code layout (unfinished)
-rw-r--r-- | index.pl | 72 |
1 files changed, 50 insertions, 22 deletions
@@ -10,28 +10,40 @@ no if $] >= 5.018, warnings => "experimental::smartmatch"; #our $VERSION = qx{git describe --dirty} || '0.01'; +my $table = $ENV{DBDB_TABLE} // 'departures'; + app->defaults( layout => 'default' ); -app->attr(dbh => sub { - my $self = shift; +app->attr( + dbh => sub { + my $self = shift; - my $dbname = $ENV{DBDB_FILE} // 'iris.sqlite'; - my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", q{}, q{} ); + my $dbname = $ENV{DBDB_FILE} // 'iris.sqlite'; + my $dbh = DBI->connect( "dbi:SQLite:dbname=$dbname", q{}, q{} ); - return $dbh; -}); + return $dbh; + } +); get '/by_hour.json' => sub { my $self = shift; my $json = []; - my $res = $self->app->dbh->selectall_arrayref(qq{ + my $res = $self->app->dbh->selectall_arrayref( + qq{ select strftime("%H", scheduled_time, "unixepoch") as time, - avg(delay) as date from departures group by time}); + avg(delay) as date from $table group by time} + ); - for my $row (@{$res}) { - push(@{$json}, { hour => $row->[0], avgdelay => $row->[1] } ); + for my $row ( @{$res} ) { + push( + @{$json}, + { + hour => $row->[0], + avgdelay => $row->[1] + } + ); } $self->render( json => $json ); @@ -40,15 +52,31 @@ get '/by_hour.json' => sub { get '/by_hour.tsv' => sub { my $self = shift; + my $metric = $self->param('metric') // 'delay'; + + my ( $query, $text ); + + given ($metric) { + when ('delay') { + $text = "hour\tavgdelay\n"; + $query = qq{ + select strftime("%H", scheduled_time, "unixepoch") as time, + avg(delay) from $table group by time + }; + } + when ('cancel_num') { + $text = "hour\tavgdelay\n"; + $query = qq{ + select strftime("%H", scheduled_time, "unixepoch") as time, + avg(delay) as date from $table group by time + }; + } + } - my $text = "hour\tavgdelay\n"; - - my $res = $self->app->dbh->selectall_arrayref(qq{ - select strftime("%H", scheduled_time, "unixepoch") as time, - avg(delay) as date from departures group by time}); + my $res = $self->app->dbh->selectall_arrayref($query); - for my $row (@{$res}) { - $text .= sprintf("%s\t%s\n", @{$row}); + for my $row ( @{$res} ) { + $text .= sprintf( "%s\t%s\n", @{$row} ); } $self->render( data => $text ); @@ -57,12 +85,12 @@ get '/by_hour.tsv' => sub { get '/' => sub { my $self = shift; - my $dbh = $self->app->dbh; - - my $table = $ENV{DBDB_TABLE} // 'departures'; + my $dbh = $self->app->dbh; - my $num_departures = $dbh->selectall_arrayref(qq{ - select count() from $table})->[0][0]; + my $num_departures = $dbh->selectall_arrayref( + qq{ + select count() from $table} + )->[0][0]; $self->render( 'main', |