Data controller with stats
[MojoFacets.git] / lib / MojoFacets / Data.pm
1 package MojoFacets::Example;
2
3 use strict;
4 use warnings;
5
6 use base 'Mojolicious::Controller';
7
8 our $data;
9
10 sub _data {
11         my $self = shift;
12         my $json = Mojo::JSON->new;
13
14         $data ||= $json->decode(  File::Spec->catfile( $self->root, 'data', 'bibpsi.js' ) );
15 }
16
17
18 sub stats {
19     my $self = shift;
20
21         $self->_data;
22
23         foreach my $e ( @{ $data->{items} } ) {
24                 foreach my $n ( keys %$e ) {
25                         $stats->{column}->{$n}->{count}++;
26                         $stats->{column}->{$n}->{number}++ if $e->{$n} =~ m/[-+]?([0-9]*\.[0-9]+|[0-9]+)/;
27                 }
28         }
29         warn "# stats ", $self->Dumper( $stats );
30     # Render template "example/welcome.html.ep" with message
31     $self->render(
32                 message => 'Welcome to the Mojolicious Web Framework!',
33                 stats => $stats,
34         );
35 }
36
37 1;