File Coverage

DataFax/StudyDB.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package DataFax::StudyDB;
2              
3 1     1   25264 use strict;
  1         2  
  1         51  
4 1     1   5 use vars qw(@ISA $VERSION @EXPORT @EXPORT_OK @IMPORT_OK %EXPORT_TAGS);
  1         3  
  1         106  
5 1     1   6 use Carp;
  1         6  
  1         95  
6 1     1   2195 use DataFax;
  0            
  0            
7             use DataFax::StudySubs qw(:all);
8              
9             $VERSION = 0.11;
10             @ISA = qw(Exporter DataFax);
11             @EXPORT = qw(readDFstudies);
12             @EXPORT_OK = qw(readDFstudies);
13             @IMPORT_OK = qw(dfparam get_dfparam exec_cmd);
14             %EXPORT_TAGS= (
15             all =>[@EXPORT_OK],
16             );
17              
18             =head1 NAME
19              
20             DataFax::StudyDB - DataFax DFstudies.db parser
21              
22             =head1 SYNOPSIS
23              
24             use DataFax::StudyDB;
25              
26             my $db = DataFax::StudyDB->new('datafax_dir'=>'/opt/datafax',
27             'datafax_host'=>'mydfsvr');
28             # or
29             my $db = new DataFax::StudyDB 'datafax_dir'=>'/opt/datafax',
30             'datafax_host'=>'mydfsvr';
31              
32             =head1 DESCRIPTION
33              
34             This class locates DataFax DFstudies.db, parse it and load it to
35             a relational database such as Oracle.
36              
37             =cut
38              
39             =head2 new (datafax_dir=>'/opt/datafax',datafax_host=>'my_svr')
40              
41             Input variables:
42              
43             datafax_dir - full path to where DataFax system is installled
44             If not specified, it will try to get it from
45             $ENV{DATAFAX_DIR}.
46             datafax_host - DataFax server name or IP address
47             If not specified, it will try to get it from
48             $ENV{DATAFAX_HOST} or `hostname` on UNIX system.
49              
50             Variables used or routines called:
51              
52             None
53              
54             How to use:
55              
56             my $db = DataFax::StudyDB->new('datafax_dir'=>'/opt/datafax',
57             'datafax_host'=>'mydfsvr');
58             Return: an empty or initialized class object.
59              
60             This method constructs a Perl object and capture any parameters if
61             specified. It creates and defaults the following variables:
62              
63             datafax_dir = $ENV{DATAFAX_DIR}
64             datafax_host = $ENV{DATAFAX_HOST} | `hostname`
65             unix_os = 'linux|solaris'
66              
67             =cut
68              
69             sub new {
70             my ($s, %args) = @_;
71             return $s->SUPER::new(%args);
72             }
73              
74             # ---------------------------------------------------------------------
75              
76             =head2 Export Tag: all
77              
78             The :all tag includes the all the methods in this module.
79              
80             use DataFax::StudyDB qw(:all);
81              
82             It includes the following sub-routines:
83              
84             =head3 readDFstudies($q, $ar)
85              
86             Input variables:
87              
88             $ifn - input file name
89             $ar - a parameter array ref
90             source_dir - source directory
91             datafax_dir - DataFax directory
92             datafax_host - DataFax server name/IP address
93             real_time - whether to ge real time data
94              
95             Variables used or routines called:
96              
97             DataFax::StudySubs
98             get_dfparam - get parameters
99            
100             How to use:
101              
102             my $s = new DataFax::StudyDB;
103             my $ifn = '/opt/datafax/lib/DFstudies.db';
104             my $pr = { real_time=>1,datafax_host=>'df_svr',
105             datafax_usr=>'datafax', datafax_pwd=>'secret'};
106             my ($c, $d) = $s->readDFstudies{$ifn);
107             my ($c, $d) = $s->readDFstudies{"", $pr);
108              
109             Return: ($c,$d) where $c is an array ref while $d is hash ref.
110              
111             $c->[$i][$j] - array ref where
112             $i is row number and
113             $j is column number;
114             $i=0 - the first row contains the column names in the
115             following order
116             study_number,study_title,client_name,study_dir,
117             source_dir,datafax_dir,host_name,rpc_program,
118             rpc_program_no,rpc_version_no,study_status,comments
119             $d->{$sn}{$itm} hash ref where
120             $sn is three-digit study number padding with leading zeros
121             $itm is column names as listed in $c->[0].
122              
123             This method reads DFstudies and parse the file into two arrays.
124              
125             =cut
126              
127             sub readDFstudies {
128             my $s = shift;
129             my ($ifn, $ar) = @_;
130             my $vs = 'source_dir,datafax_dir,datafax_host,dir_sep,local_host,';
131             $vs .= 'unix_os,real_time';
132             my ($sdr,$dfd,$dfh,$ds,$svr,$uos,$rt) = $s->get_dfparam($vs, $ar);
133             croak "ERR (readDFstudies): DATAFAX_DIR is not specified."
134             if !$ifn && !$dfd;
135             croak "ERR: could not get real time DFstudies.db on this OS"
136             if $rt && $svr && $dfh && $svr ne $dfh && $^O !~ /^($uos)/i;
137             $ds = '/' if ! $ds;
138             $svr = `hostname` if !$svr && $^O =~ /^($uos)/i;
139             my $dir = ($rt) ? $dfd : $sdr;
140             my $cmd = 'cat ' . (($ifn) ? $ifn :
141             (join $ds, $dir, 'lib', 'DFstudies.db'));
142             $s->echo_msg(" - running $cmd...", 1);
143             my @a = $s->exec_cmd($cmd,$ar);
144             my $c = bless [], ref($s)||$s;
145             my $d = bless {}, ref($s)||$s;
146             my $vars = 'study_number,study_title,client_name,study_dir,';
147             $vars .= 'source_dir,datafax_dir,host_name,rpc_program,';
148             $vars .= 'rpc_program_no,rpc_version_no,study_status,comments';
149             push @$c, [split /,/, $vars];
150             my ($rpc);
151             foreach (@a) {
152             # Fields in DFstudies.db:
153             # 0 - study number 4 - command to start server
154             # 1 - host name 5 - candidate host names
155             # 2 - RPC program number 6 - label
156             # 3 - RPC version number
157             next if $_ =~ /^(#|\s*$)/;
158             chomp;
159             my @b = split(/\|/,$_);
160             my $sn = $b[0]; # add leading zeros
161             $sn = sprintf "%03d", $b[0] if $b[0] =~ /^\d+$/;
162             $d->{$sn} = {};
163             $d->{$sn}{host_name} = ($b[1])?$b[1]:$b[5];
164             $d->{$sn}{rpc_program_no} = $b[2];
165             $d->{$sn}{rpc_version_no} = $b[3];
166             ($rpc,$dir) =
167             ($b[4] =~ /(.*)\s*-c\s*(.+)\/lib\/DFserver\.cf/);
168             $d->{$sn}{rpc_program} = $rpc;
169             $d->{$sn}{study_dir} = $dir;
170             $d->{$sn}{source_dir} = join $ds, $sdr, "S$sn";
171             $d->{$sn}{client_name} = $b[5];
172             $d->{$sn}{study_title} = $b[6];
173             $d->{$sn}{datafax_dir} = $dfd;
174             if ($b[1] =~ /^\-/) {
175             $d->{$sn}{comments} = $b[1];
176             $d->{$sn}{study_status} = 'Down';
177             $d->{$sn}{host_name} = $b[5];
178             } else {
179             $d->{$sn}{comments} = "";
180             $d->{$sn}{study_status} = 'Up';
181             }
182             $d->{$sn}{study_number} = $sn;
183             push @$c, [map { $d->{$sn}{$_} } (split /,/, $vars) ];
184             }
185             close FILE;
186             my $n = $#$c+1;
187             $s->echo_msg(" $n valid records.", 2);
188             return ($c,$d);
189             }
190              
191             1;
192              
193             =head1 HISTORY
194              
195             =over 4
196              
197             =item * Version 0.10
198              
199             This version is to create a method to read in DFstudies.db.
200              
201             0.11 - use new method from DataFax
202              
203             =item * Version 0.20
204              
205             =cut
206              
207             =head1 SEE ALSO (some of docs that I check often)
208              
209             Oracle::Loader, Oracle::Trigger, CGI::Getopt, File::Xcopy,
210             DataFax, CGI::AppBuilder, etc.
211              
212             =head1 AUTHOR
213              
214             Copyright (c) 2005 Hanming Tu. All rights reserved.
215              
216             This package is free software and is provided "as is" without express
217             or implied warranty. It may be used, redistributed and/or modified
218             under the terms of the Perl Artistic License (see
219             http://www.perl.com/perl/misc/Artistic.html)
220              
221             =cut
222