From b3c664ba49f9555038025e16dd8474bd99e90d13 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Mon, 18 Apr 2011 21:58:46 +0200 Subject: Switch graph generation programs to Perl. This means they access the database the same way all the other scripts do, preventing any issues due to different setups. Also tidy up state grouping in graph, add Norway start date. --- bin/problem-creation-graph | 170 +++++++++++++++++++++++---------------------- 1 file changed, 87 insertions(+), 83 deletions(-) (limited to 'bin/problem-creation-graph') diff --git a/bin/problem-creation-graph b/bin/problem-creation-graph index 2ee38783c..e1b6f2cc4 100755 --- a/bin/problem-creation-graph +++ b/bin/problem-creation-graph @@ -1,83 +1,91 @@ -#!/bin/bash -# problem-creation-graph +#!/usr/bin/env perl + +# problem-creation-graph: # Plot graph of rate of problem creation # -# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. -# Email: francis@mysociety.org. WWW: http://www.mysociety.org/ -# -# $Id: problem-creation-graph,v 1.6 2008-11-12 18:06:34 matthew Exp $ - -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" - -cd `dirname $0` -cd ../../ -source fixmystreet/commonlib/shlib/deployfns - -read_conf fixmystreet/conf/general.yml - -if [ $OPTION_BASE_URL = "http://reportemptyhomes.com" ]; then - DATE="2008-10-01" -else - DATE="2007-02-01" -fi - -SOURCEA=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM -SOURCEB=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM -SOURCEC=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM -SOURCED=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM -SOURCEE=/tmp/fms-creation-rate-graph-data-$RANDOM$RANDOM -GPSCRIPT=/tmp/fms-creation-rate-graph-script-$RANDOM$RANDOM - -# where status in ('draft') - -function grab_data { - echo "select - date(created), count(*) - from problem - $1 - group by date(created) - order by date(created) - ;" | psql --host $OPTION_FMS_DB_HOST --port $OPTION_FMS_DB_PORT -A -F " " $OPTION_FMS_DB_NAME $OPTION_FMS_DB_USER | egrep -v "date|rows" >$2 +# 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 "" $SOURCEA -grab_data "where state not in ('unconfirmed')" $SOURCEB -grab_data "where state not in ('unconfirmed', 'confirmed')" $SOURCEC -grab_data "where state not in ('unconfirmed', 'confirmed', 'fixed', 'fixed - council', 'fixed - user')" $SOURCED -grab_data "where state not in ('unconfirmed', 'confirmed', 'fixed', 'fixed - council', 'fixed - user', 'hidden')" $SOURCEE - - -#state = 'unconfirmed' -#or state = 'confirmed' -#or state in ('fixed', 'fixed - council', 'fixed - user') -#or state = 'hidden' -#or state = 'partial' - - -cat >$GPSCRIPT < { -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 = <$GPSCRIPT <>$GPSCRIPT " plot \"$SOURCEA\" using 1:2 with impulses lt 3 lw 15 title \"unconfirmed\"," -if [ -s $SOURCEB ]; then -echo -n >>$GPSCRIPT " \"$SOURCEB\" using 1:2 with impulses lt 4 lw 15 title \"confirmed\"," -fi -if [ -s $SOURCEC ]; then -echo -n >>$GPSCRIPT " \"$SOURCEC\" using 1:2 with impulses lt 5 lw 15 title \"fixed\"," -fi -if [ -s $SOURCED ]; then -echo -n >>$GPSCRIPT " \"$SOURCED\" using 1:2 with impulses lt 6 lw 15 title \"hidden\"," -fi -if [ -s $SOURCEE ]; then -echo -n >>$GPSCRIPT " \"$SOURCEE\" using 1:2 with impulses lt 7 lw 15 title \"any other type\"," -fi -cat >>$GPSCRIPT < fixmystreet/web/fms-live-creation$EXTENSION 2>/dev/null - +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; -- cgit v1.2.3