From 8f10edc76c8ede3ab19aca5e0d6cec532cfc32e6 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 7 Mar 2015 16:27:43 +0100 Subject: add filters (mostly working, even) --- index.pl | 41 ++++++++++++++++++++++++++++++++++++++++- templates/bargraph.html.ep | 2 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/index.pl b/index.pl index 2dfb5ca..648d685 100644 --- a/index.pl +++ b/index.pl @@ -121,7 +121,7 @@ helper barplot_filters => sub { ], destinations => [ q{}, - map { decode('utf8', $_->[0]) } @{ + map { decode( 'utf8', $_->[0] ) } @{ $dbh->selectall_arrayref( "select distinct destination from $table order by destination" ) @@ -221,6 +221,24 @@ get '/2ddata.tsv' => sub { my $metric = $self->param('metric') // 'avg_delay'; my $msgnum = int( $self->param('msgnum') // 0 ); + my %filter = ( + line => scalar $self->param('filter_line'), + train_type => scalar $self->param('filter_train_type'), + station => scalar $self->param('filter_station'), + destination => scalar $self->param('filter_destination'), + delay_min => scalar $self->param('filter_delay_min'), + delay_max => scalar $self->param('filter_delay_max'), + ); + + for my $key ( keys %filter ) { + $filter{$key} =~ tr{a-zA-Z0-9öäüÖÄÜß }{}cd; + } + + $filter{delay_min} + = length( $filter{delay_min} ) ? int( $filter{delay_min} ) : undef; + $filter{delay_max} + = length( $filter{delay_max} ) ? int( $filter{delay_max} ) : undef; + my @weekdays = qw(So Mo Di Mi Do Fr Sa); if ( $msgnum < 0 or $msgnum > 99 ) { @@ -253,6 +271,27 @@ get '/2ddata.tsv' => sub { } } + if ( $filter{line} ) { + my ( $train_type, $line_no ) = split( / /, $filter{line} ); + $where_clause + .= " and train_type = '$train_type' and line_no = '$line_no'"; + } + if ( $filter{train_type} ) { + $where_clause .= " and train_type = '$filter{train_type}'"; + } + if ( $filter{station} ) { + $where_clause .= " and station = '$filter{station}'"; + } + if ( $filter{destination} ) { + $where_clause .= " and destination = '$filter{destination}'"; + } + if ( defined $filter{delay_min} ) { + $where_clause .= " and delay >= $filter{delay_min}"; + } + if ( defined $filter{delay_max} ) { + $where_clause .= " and delay <= $filter{delay_max}"; + } + given ($metric) { when ('avg_delay') { $query = qq{ diff --git a/templates/bargraph.html.ep b/templates/bargraph.html.ep index 7e989e6..5b6282d 100644 --- a/templates/bargraph.html.ep +++ b/templates/bargraph.html.ep @@ -2,7 +2,7 @@ %= javascript begin -show_bargraph('/2ddata.tsv?aggregate=<%= param('xsource') %>&metric=<%= param('ysource') %>&msgnum=<%= param('msgnum') %>', +show_bargraph('/2ddata.tsv?aggregate=<%= param('xsource') %>&metric=<%= param('ysource') %>&msgnum=<%= param('msgnum') %>&filter_line=<%= param('filter_line') %>&filter_train_type=<%= param('filter_train_type') %>&filter_station=<%= param('filter_station') %>&filter_destination=<%= param('filter_destination') %>&filter_delay_min=<%= param('filter_delay_min') %>&filter_delay_max=<%= param('filter_delay_max') %>', '<%= param('title') %>', '<%= param('xlabel') %>', '<%= param('ylabel') %>', '<%= param('yformat') %>', '<%= param('width') %>', '<%= param('height') %>'); -- cgit v1.2.3