File Coverage

blib/lib/StatusBoard/Graph.pm
Criterion Covered Total %
statement 64 95 67.3
branch 8 26 30.7
condition n/a
subroutine 16 26 61.5
pod 17 17 100.0
total 105 164 64.0


line stmt bran cond sub pod time code
1             package StatusBoard::Graph;
2             {
3             $StatusBoard::Graph::VERSION = '1.0.1';
4             }
5              
6             # ABSTRACT: create JSON with graph data for Status Board iPad App
7              
8              
9 1     1   97738 use strict;
  1         3  
  1         36  
10 1     1   6 use warnings;
  1         1  
  1         34  
11 1     1   914 use utf8;
  1         15  
  1         5  
12              
13 1     1   29 use Carp;
  1         2  
  1         73  
14 1     1   6 use JSON;
  1         2  
  1         6  
15 1     1   996 use File::Slurp;
  1         18701  
  1         101  
16 1     1   939 use Clone qw(clone);
  1         14471  
  1         2483  
17              
18             my $true = 1;
19             my $false = '';
20              
21              
22             sub new {
23 1     1 1 132 my ($class, %opts) = @_;
24              
25 1 50       5 croak "Constructor new() does not need any parameters." if %opts;
26              
27 1         2 my $self = {};
28 1         5 bless $self, $class;
29              
30 1         3 return $self;
31             }
32              
33              
34             sub get_json {
35 0     0 1 0 my ($self) = @_;
36              
37 0         0 my $json = to_json(
38             $self->__get_data()
39             );
40              
41 0         0 return $json;
42             }
43              
44              
45             sub get_pretty_json {
46 1     1 1 5 my ($self) = @_;
47              
48 1         5 my $pretty_json = to_json(
49             $self->__get_data(),
50             {
51             pretty => 1,
52             },
53             );
54              
55 1         151 return $pretty_json;
56             }
57              
58              
59             sub write_json {
60 0     0 1 0 my ($self, $file_name) = @_;
61              
62 0         0 write_file(
63             $file_name,
64             {binmode => ':utf8'},
65             $self->get_json(),
66             );
67              
68 0         0 return $false;
69             }
70              
71              
72             sub set_title {
73 1     1 1 6 my ($self, $title) = @_;
74              
75 1         7 $self->{__title} = $title;
76              
77 1         2 return $false;
78             }
79              
80              
81             sub has_title {
82 0     0 1 0 my ($self) = @_;
83              
84 0 0       0 return defined($self->{__title}) ? $true : $false;
85             }
86              
87              
88             sub get_title {
89 0     0 1 0 my ($self) = @_;
90              
91 0 0       0 croak "No title. Stopped" if not $self->has_title();
92              
93 0         0 return $self->{__title};
94             }
95              
96              
97             sub set_type {
98 0     0 1 0 my ($self, $type) = @_;
99              
100 0         0 $self->{__type} = $type;
101              
102 0         0 return $false;
103             }
104              
105              
106             sub has_type {
107 1     1 1 16 my ($self) = @_;
108              
109 1 50       11 return defined($self->{__type}) ? $true : $false;
110             }
111              
112              
113             sub get_type {
114 0     0 1 0 my ($self) = @_;
115              
116 0 0       0 croak "No type. Stopped" if not $self->has_type();
117              
118 0         0 return $self->{__type};
119             }
120              
121              
122             sub add_data_seq {
123 2     2 1 9 my ($self, $data_seq) = @_;
124              
125 2         4 push @{$self->{__data_seqs}}, $data_seq;
  2         6  
126              
127 2         5 return $false;
128             }
129              
130              
131             sub set_min_y_value {
132 0     0 1 0 my ($self, $number) = @_;
133              
134 0         0 $self->{__min_y_value} = $number;
135              
136 0         0 return $false;
137             }
138              
139              
140             sub has_min_y_value {
141 1     1 1 2 my ($self) = @_;
142              
143 1 50       7 return defined($self->{__min_y_value}) ? $true : $false;
144             }
145              
146              
147             sub get_min_y_value {
148 0     0 1 0 my ($self) = @_;
149              
150 0 0       0 croak "No min y value. Stopped" if not $self->has_min_y_value();
151              
152 0         0 return $self->{__min_y_value};
153             }
154              
155              
156             sub set_max_y_value {
157 0     0 1 0 my ($self, $number) = @_;
158              
159 0         0 $self->{__max_y_value} = $number;
160              
161 0         0 return $false;
162             }
163              
164              
165             sub has_max_y_value {
166 1     1 1 2 my ($self) = @_;
167              
168 1 50       7 return defined($self->{__max_y_value}) ? $true : $false;
169             }
170              
171              
172             sub get_max_y_value {
173 0     0 1 0 my ($self) = @_;
174              
175 0 0       0 croak "No max y value. Stopped" if not $self->has_max_y_value();
176              
177 0         0 return $self->{__max_y_value};
178             }
179              
180             sub __get_data {
181 1     1   3 my ($self) = @_;
182              
183 1 50       4 my $data = {
184             graph => {
185             title => $self->{__title},
186             ( $self->has_type() ? ( type => $self->get_type() ) : () ),
187             datasequences => $self->__get_datasequences(),
188             }
189             };
190              
191 1 50       4 if ($self->has_min_y_value()) {
192 0         0 $data->{graph}->{yAxis}->{minValue} = $self->get_min_y_value() + 0;
193             }
194              
195 1 50       4 if ($self->has_max_y_value()) {
196 0         0 $data->{graph}->{yAxis}->{maxValue} = $self->get_max_y_value() + 0;
197             }
198              
199 1         6 return $data;
200             }
201              
202             sub __get_datasequences {
203 1     1   2 my ($self) = @_;
204              
205 1         2 my $datasequences = [];
206              
207 1         2 foreach my $ds (@{$self->{__data_seqs}}) {
  1         3  
208 2         8 my $values = clone $ds->get_values();
209              
210 2         5 my $datapoints;
211 2         3 while (@{$values}) {
  12         28  
212 10         10 my $title = shift @{$values};
  10         16  
213 10         12 my $value = shift @{$values};
  10         14  
214 10         12 push @{$datapoints}, {
  10         39  
215             title => $title . "",
216             value => $value,
217             };
218             }
219              
220 2 50       3 push @{$datasequences}, {
  2         11  
221             title => $ds->{__title},
222             ( $ds->has_color ? ( color => $ds->get_color() ) : () ),
223             datapoints => $datapoints,
224             };
225             }
226              
227 1         5 return $datasequences;
228             }
229              
230              
231             1;
232              
233             __END__