aboutsummaryrefslogtreecommitdiffstats
path: root/bin/problem-creation-graph
blob: e1b6f2cc405f96e4e3f5cfb93aa734daf086b2b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env perl

# problem-creation-graph:
# Plot graph of rate of problem creation
#
# Copyright (c) 2014 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org

use strict;
use warnings;
require 5.8.0;

use FindBin;
use File::Temp qw(tempfile);

use FixMyStreet::App;

chdir("$FindBin::Bin/../");

my %config = (
    gplot_output => "set terminal png font 'Vera.ttf' 9 size 1200,400",
    extension => '.png',
    #gplot_output => "set terminal fig color big thickness 1"
    #extension => ".fig"
    #gplot_output => "set terminal svg size 800 250"
    #extension => ".svg"
);

my $base_url = FixMyStreet->config('BASE_URL');
$config{date} = '2007-02-01';
$config{date} = '2011-03-03' if $base_url =~ /fiksgatami/;
$config{date} = '2008-10-01' if $base_url =~ /emptyhomes/;

my %sources;
sub grab_data {
    my ($type, $selection) = @_;

    my ($fh, $filename) = tempfile("fms-creation-rate-graph-data-XXXXXXXXXX", UNLINK => 1);
    $sources{$type} = $filename;

    my @entries = FixMyStreet::App->model('DB::Problem')->search(
        $selection, {
            columns => [
                { 'date' => { date => 'created' } },
                { 'count' => { count => '*' } }
            ],
            group_by => [ 'date' ],
            order_by => [ 'date' ],
        }
    );
    @entries = map { { $_->get_columns } } @entries;
    foreach (@entries) {
        $fh->print($_->{date}, ' ', $_->{count}, "\n");
    }
}

# rather nastily, work out the cumulative heights in reverse, so can
# plot impulses on top of each other
grab_data('unconfirmed', {});
my @states_to_ignore = ('unconfirmed');
grab_data('open', { state => { -not_in => \@states_to_ignore } });
push @states_to_ignore, FixMyStreet::DB::Result::Problem->open_states();
grab_data('fixed', { state => { -not_in => \@states_to_ignore } });
push @states_to_ignore, FixMyStreet::DB::Result::Problem->fixed_states();
grab_data('closed', { state => { -not_in => \@states_to_ignore } });
push @states_to_ignore, FixMyStreet::DB::Result::Problem->closed_states();
grab_data('hidden', { state => { -not_in => \@states_to_ignore } });
push @states_to_ignore, 'hidden';
grab_data('other', { state => { -not_in => \@states_to_ignore } });

my $gp = <<END;
    unset border
    unset arrow
    set key left
    set tics out
    $config{gplot_output}

    set xdata time
    set timefmt "%Y-%m-%d"
    set xrange ["$config{date}":]
    set format x "%d %b %Y"
    set xtics nomirror
    set xlabel "status of problems that were created on each calendar day"

    set ytics nomirror 
    set ylabel "number of problems created on the calendar day" 
    set y2tics tc lt 0
    set y2label "cumulative total number of problems" tc lt 0
    set format y2 "%.0f"

#    set arrow 1 from '2005-02-14', 0 to '2005-02-14', 900 lt 0 nohead
#    set label 1 'launch of beta' at '2005-02-17', 900

    n = 0
END
$gp .= "    plot \"$sources{unconfirmed}\" using 1:2 with impulses lt 1 lw 2 title \"unconfirmed\",";
$gp .= " \"$sources{open}\" using 1:2 with impulses lt 3 lw 2 title \"open\","
    if -s $sources{open};
$gp .= " \"$sources{fixed}\" using 1:2 with impulses lt 2 lw 2 title \"fixed\","
    if -s $sources{fixed};
$gp .= " \"$sources{closed}\" using 1:2 with impulses lt 4 lw 2 title \"closed\","
    if -s $sources{closed};
$gp .= " \"$sources{hidden}\" using 1:2 with impulses lt 5 lw 2 title \"hidden\","
    if -s $sources{hidden};
$gp .= " \"$sources{other}\" using 1:2 with impulses lt 6 lw 2 title \"any other type\","
    if -s $sources{other};
$gp .= <<END;
    "< awk 'BEGIN { n = 0 } { n += \$2; print \$1, \$2, n; }' $sources{unconfirmed}" using 1:3 axes x1y2 with lines lt 0 title "cumulative total number of problems"
END

open(my $gnuplot, '|-', "GDFONTPATH=/usr/share/fonts/truetype/ttf-bitstream-vera gnuplot > web/fms-live-creation$config{extension} 2> /dev/null");
$gnuplot->print($gp);
close $gnuplot;