| 1 | package Kwiki::GDGraphGenerator; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Kwiki::Plugin '-Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Kwiki::Installer '-base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = "0.04"; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | const class_title => 'Kwiki graphs'; | |||||||||||||||||||||||||||
| 11 | 2 | const class_id => 'graphgenerator'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 0 | my $registry = shift; | |||||||||||||||||||||||||||
| 15 | 0 | $registry->add( wafl => graph => 'Kwiki::GDGraphGenerator::Wafl' ); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package Kwiki::GDGraphGenerator::Wafl; | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Spiffy '-Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use base 'Spoon::Formatter::WaflBlock'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | field 'config'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub to_html { | ||||||||||||||||||||||||||
|
Subroutine
to_html: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # parse the config, make sure options are there | ||||||||||||||||||||||||||||
| 27 | 0 | require YAML; | |||||||||||||||||||||||||||
| 28 | 0 | $self->config( eval { YAML::Load( $self->block_text ) } ); | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | return $self->error("make sure your YAML is correct") if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | return $self->error("graph config isn't a hash") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->config and ref $self->config eq "HASH"
MC/DC0%0/2
$self->config and ref $self->config eq "HASH"
$self->config
ref $self->config eq "HASH"
Truth table0%0/3
A and B
A= $self->configB= ref $self->config eq "HASH"
| |||||||||||||||||||||||||||||
| 31 | unless $self->config && ref $self->config eq 'HASH'; | ||||||||||||||||||||||||||||
| 32 | 0 | foreach (qw( id data type )) { | |||||||||||||||||||||||||||
| 33 | return $self->error("graph config must specify '$_'") | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | unless exists $self->config->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # check to see if the graph exists -- if not, create it | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $error = $self->generate_image | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $self->checksum_path and io($self->checksum_path)->assert->scalar eq $self->checksum
MC/DC0%0/2
-e $self->checksum_path and io($self->checksum_path)->assert->scalar eq $self->checksum
-e $self->checksum_path
io($self->checksum_path)->assert->scalar eq $self->checksum
Truth table0%0/3
A and B
A= -e $self->checksum_pathB= io($self->checksum_path)->assert->scalar eq $self->checksum
| |||||||||||||||||||||||||||||
| 39 | unless -e $self->checksum_path | ||||||||||||||||||||||||||||
| 40 | && io( $self->checksum_path )->assert->scalar eq $self->checksum; | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return $self->error($error) if $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # return a simple link | ||||||||||||||||||||||||||||
| 44 | 0 | $self->hub->template->process( 'graphgenerator_inline.html', | |||||||||||||||||||||||||||
| 45 | src => $self->image_path ); | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub error { | ||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 0 | $self->hub->template->process( 'graphgenerator_error.html', | |||||||||||||||||||||||||||
| 50 | msg => "Couldn't create graph: " . shift ); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub checksum { | ||||||||||||||||||||||||||
|
Subroutine
checksum: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||
| 55 | 0 | require Digest::MD5; | |||||||||||||||||||||||||||
| 56 | 0 | my $d = new Data::Dumper( [ $self->config ] ); | |||||||||||||||||||||||||||
| 57 | 0 | Digest::MD5::md5_hex( $d->Sortkeys(1)->Indent(0)->Dump ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub image_path { | ||||||||||||||||||||||||||
|
Subroutine
image_path: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 0 | $self->hub->cgi->button; | |||||||||||||||||||||||||||
| 62 | $self->hub->graphgenerator->plugin_directory . '/' | ||||||||||||||||||||||||||||
| 63 | . $self->hub->pages->current->id . '.' | ||||||||||||||||||||||||||||
| 64 | 0 | . $self->config->{id} . '.png'; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub checksum_path { | ||||||||||||||||||||||||||
|
Subroutine
checksum_path: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 0 | $self->image_path . '.config.md5'; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub generate_image { | ||||||||||||||||||||||||||
|
Subroutine
generate_image: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # load config, put things in variables and strip out | ||||||||||||||||||||||||||||
| 74 | # options we're not going to give to set() | ||||||||||||||||||||||||||||
| 75 | # (NOTE: width and height are read-only) | ||||||||||||||||||||||||||||
| 76 | 0 | my %config = %{ $self->config }; | |||||||||||||||||||||||||||
| 77 | my ( $type, $width, $height, $data ) | ||||||||||||||||||||||||||||
| 78 | 0 | = @config{qw( type width height data )}; | |||||||||||||||||||||||||||
| 79 | 0 | delete @config{qw( type width height data id )}; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | $width ||= 300; | ||||||||||||||||||||||||||
|
Condition0%0/2
$width ||= 300
MC/DC0%0/1
$width ||= 300
$width
Truth table0%0/2
A
A= $width
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $height ||= 300; | ||||||||||||||||||||||||||
|
Condition0%0/2
$height ||= 300
MC/DC0%0/1
$height ||= 300
$height
Truth table0%0/2
A
A= $height
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # check for keys we don't allow | ||||||||||||||||||||||||||||
| 84 | 0 | foreach my $key (qw( logo )) { | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | return "specifying $key is not permitted" if $config{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # create a new graph object | ||||||||||||||||||||||||||||
| 89 | 0 | require GD::Graph; | |||||||||||||||||||||||||||
| 90 | 0 | my $class = "GD::Graph::$type"; | |||||||||||||||||||||||||||
| 91 | 0 | eval "require $class;"; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | return "couldn't create new $class" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | my $graph = $class->new( $width, $height ); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # set the options and plot the data | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $graph->set(%config) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | or return "couldn't use config: " . $graph->error; | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $gd = $graph->plot($data) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | or return "couldn't plot graph: " . $graph->error; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # save to the files | ||||||||||||||||||||||||||||
| 102 | 0 | io( $self->image_path ) < $gd->png; | |||||||||||||||||||||||||||
| 103 | 0 | io( $self->checksum_path ) < $self->checksum; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # undef means no error | ||||||||||||||||||||||||||||
| 106 | 0 | return; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | package Kwiki::GDGraphGenerator; | ||||||||||||||||||||||||||||
| 110 | 2 | 1; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | __DATA__ | |||||||||||||||||||||||||||