File Coverage

blib/lib/App/Dochazka/CLI/Shared.pm
Criterion Covered Total %
statement 35 95 36.8
branch 0 46 0.0
condition 0 2 0.0
subroutine 12 15 80.0
pod 3 3 100.0
total 50 161 31.0


line stmt bran cond sub pod time code
1             # *************************************************************************
2             # Copyright (c) 2014-2016, SUSE LLC
3             #
4             # All rights reserved.
5             #
6             # Redistribution and use in source and binary forms, with or without
7             # modification, are permitted provided that the following conditions are met:
8             #
9             # 1. Redistributions of source code must retain the above copyright notice,
10             # this list of conditions and the following disclaimer.
11             #
12             # 2. Redistributions in binary form must reproduce the above copyright
13             # notice, this list of conditions and the following disclaimer in the
14             # documentation and/or other materials provided with the distribution.
15             #
16             # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17             # used to endorse or promote products derived from this software without
18             # specific prior written permission.
19             #
20             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30             # POSSIBILITY OF SUCH DAMAGE.
31             # *************************************************************************
32             #
33             # Shared routines
34             package App::Dochazka::CLI::Shared;
35              
36 20     20   253 use 5.012;
  20         44  
37 20     20   60 use strict;
  20         21  
  20         335  
38 20     20   66 use warnings;
  20         24  
  20         435  
39              
40 20     20   54 use App::CELL qw( $CELL );
  20         21  
  20         1393  
41 20     20   78 use App::Dochazka::CLI qw( $current_emp $debug_mode );
  20         19  
  20         1336  
42 20     20   80 use App::Dochazka::CLI::Util qw( lookup_employee rest_error );
  20         25  
  20         806  
43 20     20   68 use Data::Dumper;
  20         16  
  20         642  
44 20     20   62 use Exporter 'import';
  20         27  
  20         402  
45 20     20   9293 use File::Slurp;
  20         59270  
  20         1063  
46 20     20   99 use File::Temp;
  20         22  
  20         1115  
47 20     20   72 use JSON;
  20         78  
  20         112  
48 20     20   1805 use Web::MREST::CLI qw( send_req );
  20         23  
  20         12596  
49              
50              
51              
52              
53             =head1 NAME
54              
55             App::Dochazka::CLI::Shared - Shared routines
56              
57              
58              
59              
60             =head1 PACKAGE VARIABLES
61              
62             =cut
63              
64             our @EXPORT_OK = qw(
65             print_schedule_object
66             shared_generate_report
67             show_as_at
68             );
69              
70              
71              
72              
73             =head1 FUNCTIONS
74              
75             The functions in this module are called from handlers.
76              
77             =cut
78              
79              
80             =head2 print_schedule_object
81              
82             Use this function to "print" a schedule object (passed an an argument). The
83             "printed schedule" (string) is returned.
84              
85             =cut
86              
87             sub print_schedule_object {
88 0     0 1   my ( $sch, %ARGS ) = @_;
89 0 0         die "AAGH! Not a schedule object" unless ref( $sch ) eq 'App::Dochazka::Common::Model::Schedule';
90 0           my $ps = '';
91              
92 0 0         $ps .= ' 'x$ARGS{'indent'} if exists( $ARGS{'indent'} );
93 0 0         $ps .= "DISABLED | " if $sch->disabled;
94 0           $ps .= "Schedule ID (SID): " . $sch->sid . "\n";
95 0 0         if ( my $scode = $sch->scode ) {
96 0 0         $ps .= ' 'x$ARGS{'indent'} if exists( $ARGS{'indent'} );
97 0 0         $ps .= "DISABLED | " if $sch->disabled;
98 0           $ps .= "Schedule code (scode): " . $scode . "\n";
99             }
100              
101             # decode the schedule
102 0           my $sch_array = decode_json( $sch->schedule );
103 0           foreach my $entry ( @$sch_array ) {
104 0 0         $ps .= ' 'x$ARGS{'indent'} if exists( $ARGS{'indent'} );
105 0 0         $ps .= "DISABLED | " if $sch->disabled;
106             # each entry is a hash with properties low_dow, low_time, high_dow, high_time
107             $ps .= "[ " . $entry->{'low_dow'} . " " . $entry->{'low_time'} . ", " .
108 0           $entry->{'high_dow'} . " " . $entry->{'high_time'} . " )\n";
109             }
110              
111             # remark
112 0 0         if ( my $remark = $sch->remark ) {
113 0 0         $ps .= ' 'x$ARGS{'indent'} if exists( $ARGS{'indent'} );
114 0 0         $ps .= "DISABLED | " if $sch->disabled;
115 0           $ps .= "Remark: " . $sch->remark . "\n";
116             }
117              
118 0           return $ps;
119             }
120              
121              
122             =head2 shared_generate_report
123              
124             Given an entity, call POST genreport and open the results in web browser.
125              
126             =cut
127              
128             sub shared_generate_report {
129 0 0   0 1   print "Entering " . __PACKAGE__ . "::shared_generate_report\n" if $debug_mode;
130 0           my ( $entity ) = @_;
131              
132 0           my $status = send_req( 'POST', 'genreport', $entity );
133 0 0         return rest_error( $status, "GENERATE REPORT" ) unless $status->ok;
134              
135             # report output in $status->payload: write it to a file
136 0           my $tmp = File::Temp->new( DIR => '/tmp' );
137 0           write_file( $tmp->filename, $status->payload );
138 0           system( "xdg-open " . $tmp->filename );
139              
140 0           return $CELL->status_ok(
141             'DOCHAZKA_CLI_NORMAL_COMPLETION',
142             payload => "Report written to " . $tmp->filename . " and attempted to open in web browser"
143             );
144             }
145              
146              
147             =head2 show_as_at
148              
149             Given $type (either "priv" or "schedule") and $th hashref from the command
150             parser, return status object.
151              
152             =cut
153              
154             sub show_as_at {
155 0 0   0 1   print "Entering " . __PACKAGE__ . "::show_as_at\n" if $debug_mode;
156 0           my ( $type, $th ) = @_;
157              
158             my $emp_spec = ( $th->{'EMPLOYEE_SPEC'} )
159 0 0         ? $th->{'EMPLOYEE_SPEC'}
160             : $current_emp;
161            
162             my $date = ( $th->{'_DATE'} )
163 0 0         ? $th->{'_DATE'} . ' 12:00'
164             : '';
165              
166 0           my ( $eid, $nick, $status, $resource );
167 0 0         if ( $emp_spec->can('eid') ) {
    0          
168 0           $eid = $emp_spec->eid;
169 0           $nick = $emp_spec->nick;
170 0           $resource = "$type/self";
171             } elsif ( ref( $emp_spec ) eq '' ) {
172 0           $status = lookup_employee( key => $emp_spec );
173 0 0         return rest_error( $status, "Employee lookup" ) unless $status->ok;
174 0           $eid = $status->payload->{'eid'};
175 0           $nick = $status->payload->{'nick'};
176 0           $resource = "$type/eid/$eid";
177             } else {
178 0           die "AGHHAH! bad employee specifier";
179             }
180              
181 0           my $display_date;
182 0 0         if ( $date ) {
183 0           $resource .= "/$date";
184 0           $display_date = $th->{'_DATE'};
185             } else {
186 0           $display_date = "now";
187             }
188              
189 0           $status = send_req( 'GET', $resource );
190 0 0         if ( $status->ok ) {
191 0           my $pl = '';
192 0 0         if ( $type eq 'priv' ) {
    0          
193 0           $pl .= "Privilege level of $nick (EID $eid) as of $display_date: " . $status->payload->{'priv'} . "\n";
194             } elsif ( $type eq 'schedule' ) {
195 0           my $sch_obj = App::Dochazka::Common::Model::Schedule->spawn( %{ $status->payload->{'schedule'} } );
  0            
196 0           $pl .= "Schedule of $nick (EID $eid) as of $display_date:\n";
197 0           $pl .= print_schedule_object( $sch_obj, indent => 4 );
198             } else {
199 0   0       die "AGH! bad type " . $type || "undefined";
200             }
201 0           return $CELL->status_ok( 'DOCHAZKA_CLI_NORMAL_COMPLETION', payload => $pl );
202             }
203              
204 0           return rest_error( $status, "GET $resource" );
205             }
206              
207              
208             1;