source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Json/Stat.pm @ 1936

Last change on this file since 1936 was 1936, checked in by nanardon, 7 years ago

Fix stat value selection

File size: 2.3 KB
Line 
1package LATMOS::Accounts::Web::Controller::Json::Stat;
2use Moose;
3use namespace::autoclean;
4use POSIX;
5
6BEGIN { extends 'Catalyst::Controller'; }
7
8=head1 NAME
9
10LATMOS::Accounts::Web::Controller::Json::Stat - Catalyst Controller
11
12=head1 DESCRIPTION
13
14Catalyst Controller.
15
16=head1 METHODS
17
18=cut
19
20
21=head2 index
22
23=cut
24
25sub index :Path :Args(1) {
26    my ( $self, $c, $statname ) = @_;
27
28    my $base = $c->model('Accounts')->db;
29
30    my $ostat = $base->get_object('stat', $statname) or return;
31
32    my $res = $ostat->compute or return;
33
34    $c->stash->{data} = [ map { [ $_, $res->{$_} +0 ] } sort { $res->{$b} <=> $res->{$a} } keys %$res ];
35}
36
37sub history :Local :Args(1) {
38    my ( $self, $c, $statname ) = @_;
39
40    my $base = $c->model('Accounts')->db;
41
42    my $ostat = $base->get_object('stat', $statname) or return;
43
44    my $data = $ostat->getAllStat or return;
45
46    my $res = $ostat->compute or return;
47
48    my $nowdate = POSIX::strftime('%Y-%m-%d %H:%M', localtime);
49    foreach my $val (sort keys %$res) {
50        $data->{$nowdate}{$val} = $res->{$val} + 0;
51    }
52
53    my $graph = {
54        series => [],
55        data => [],
56    };
57
58    my %lines;
59
60    my @dates = sort keys %$data;
61
62    for (my $i = 0; $i <= $#dates; $i++) {
63        my $date = $dates[$i];
64
65        if ($i >= 1) {
66            my $tdate = $dates[$i - 1];
67            foreach my $val (sort keys %{ $data->{$tdate} }) {
68                $data->{$tdate}{$val} or next;
69                $data->{$date}{$val} ||= 0;
70            }
71        }
72        if ($i < $#dates) {
73            my $tdate = $dates[$i + 1];
74            foreach my $val (sort keys %{ $data->{$tdate} }) {
75                $data->{$tdate}{$val} or next;
76                $data->{$date}{$val} ||= 0;
77            }
78        }
79        foreach my $val (sort keys %{ $data->{$date} }) {
80            $res->{ $val } ||= 0;
81            push(@{ $lines{$val} }, [ $date, $data->{$date}{$val} + 0 ]);
82        }
83    }
84
85    foreach (sort { $res->{$b} <=> $res->{$a} } keys %lines) {
86        push(@{ $graph->{data} }, $lines{$_});
87        push(@{ $graph->{series} }, { label => $_ });
88    }
89
90    $c->stash->{data} = $graph;
91}
92
93
94=encoding utf8
95
96=head1 AUTHOR
97
98Olivier Thauvin
99
100=head1 LICENSE
101
102This library is free software. You can redistribute it and/or modify
103it under the same terms as Perl itself.
104
105=cut
106
107__PACKAGE__->meta->make_immutable;
108
1091;
Note: See TracBrowser for help on using the repository browser.