summaryrefslogtreecommitdiff
path: root/index.pl
diff options
context:
space:
mode:
Diffstat (limited to 'index.pl')
-rw-r--r--index.pl84
1 files changed, 84 insertions, 0 deletions
diff --git a/index.pl b/index.pl
new file mode 100644
index 0000000..37f85a3
--- /dev/null
+++ b/index.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/env perl
+use Mojolicious::Lite;
+use DBI;
+use Travel::Status::DE::IRIS;
+use Travel::Status::DE::IRIS::Stations;
+use 5.014;
+use utf8;
+
+no if $] >= 5.018, warnings => "experimental::smartmatch";
+
+#our $VERSION = qx{git describe --dirty} || '0.01';
+
+app->defaults( layout => 'default' );
+
+app->attr(dbh => sub {
+ my $self = shift;
+
+ my $dbname = $ENV{DBDB_FILE} // 'iris.sqlite';
+ my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", q{}, q{} );
+
+ return $dbh;
+});
+
+get '/by_hour.json' => sub {
+ my $self = shift;
+
+ my $json = [];
+
+ 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});
+
+ for my $row (@{$res}) {
+ push(@{$json}, { hour => $row->[0], avgdelay => $row->[1] } );
+ }
+
+ $self->render( json => $json );
+ return;
+};
+
+get '/by_hour.tsv' => sub {
+ my $self = shift;
+
+ 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});
+
+ for my $row (@{$res}) {
+ $text .= sprintf("%s\t%s\n", @{$row});
+ }
+
+ $self->render( data => $text );
+ return;
+};
+
+get '/' => sub {
+ my $self = shift;
+ my $dbh = $self->app->dbh;
+
+ my $table = $ENV{DBDB_TABLE} // 'departures';
+
+ my $num_departures = $dbh->selectall_arrayref(qq{
+ select count() from $table})->[0][0];
+
+ $self->render(
+ 'main',
+ num_departures => $num_departures,
+ );
+ return;
+};
+
+app->config(
+ hypnotoad => {
+ accepts => 10,
+ listen => ['http://*:8093'],
+ pid_file => '/tmp/db-fake.pid',
+ workers => $ENV{DBDB_WORKERS} // 2,
+ },
+);
+
+app->types->type( json => 'application/json; charset=utf-8' );
+app->start();