File Coverage

blib/lib/Net/Hadoop/YARN/Roles/AppMasterHistoryServer.pm
Criterion Covered Total %
statement 17 55 30.9
branch 0 6 0.0
condition 0 6 0.0
subroutine 6 11 54.5
pod n/a
total 23 78 29.4


line stmt bran cond sub pod time code
1             package Net::Hadoop::YARN::Roles::AppMasterHistoryServer;
2             $Net::Hadoop::YARN::Roles::AppMasterHistoryServer::VERSION = '0.201';
3 2     2   13503 use strict;
  2         3  
  2         53  
4 2     2   6 use warnings;
  2         3  
  2         40  
5 2     2   22 use 5.10.0;
  2         5  
6              
7 2     2   8 use Moo::Role;
  2         4  
  2         18  
8              
9 2     2   1189 use Hash::Path;
  2         505  
  2         373  
10              
11             my %validation_pattern = (
12             appid => 'application_[0-9]+_[0-9]+',
13             jobid => 'job_[0-9]+_[0-9]+',
14             taskid => 'task_[0-9]+_[0-9]+_[a-z]_[0-9]+',
15             attemptid => 'attempt_[0-9]+_[0-9]+_[a-z]_[0-9]+_[0-9]+',
16             );
17              
18             # used by consuming classes, for specific cases
19             sub _validate_id {
20 0     0     my $self = shift;
21 0           return $_[1] =~ /^$validation_pattern{$_[0]}$/;
22             }
23              
24             sub _mk_subs {
25 0     0     my $methods_urls = shift;
26              
27 0           for my $key ( keys %{$methods_urls} ) {
  0            
28              
29             # use a closure to only run the preprocessing once per method for
30             # validation. URL params are of the form {appid}, {taskid}, hence the
31             # regexes to find them
32 0           my @param_names = $methods_urls->{$key}[0] =~ m/\{([a-z]+)\}/g;
33             my @validations = map {
34 0           my $name = $_;
  0            
35             {
36             name => $name,
37 0     0     validate => sub { shift =~ /^$validation_pattern{$name}$/ },
38 0           };
39             } @param_names;
40              
41 0           my $url = $methods_urls->{$key}[0];
42 0           my $json_path = $methods_urls->{$key}[1];
43              
44             # insert the method for the endpoint in the using class
45 2     2   8 no strict 'refs';
  2         2  
  2         580  
46 0           *{ ( caller() )[0] . "::$key" } = sub {
47 0     0     my $self = shift;
48             # check the list of params validates against the list of
49             # placeholders gathered in the url split above
50 0           my $params_idx = 0;
51 0           for my $param ( @_ ) {
52 0           my $v = $validations[ $params_idx ];
53 0 0 0       if ( ! ref $param && ! $v->{validate}->( $param ) ) {
54             die sprintf "Param `%s` doesn't satisfy pattern /%s/ in call to `%s`.",
55             $param,
56             $validation_pattern{ $v->{name} },
57 0           $key,
58             ;
59             }
60 0           $params_idx++;
61             }
62              
63             # now replace all url placeholders with the params we were given;
64             # extra parameters (in a hashref) will be passed as regular URL
65             # params, not interpolated in the path
66 0           my $interp_url = $url;
67 0           my $extra_params;
68 0           while (my $param = shift) {
69 0 0 0       if (! @_ && ref $param) {
70 0           $extra_params = $param;
71 0           last;
72             }
73 0           $interp_url =~ s/\{[a-z]+\}/$param/;
74             }
75 0           my $res = $self->_get($interp_url, { params => $extra_params });
76              
77             # Only return the JSON fragment we need
78 0           return Hash::Path->get($res, split(/\./, $json_path));
79 0           };
80             }
81             }
82              
83             sub _mk_uri {
84 0     0     my $self = shift;
85 0           my ($server, $path, $params) = @_;
86 0           my $uri = $server . "/" . $path;
87 0           $uri =~ s#//+#/#g;
88 0           $uri = URI->new("http://" . $uri);
89 0 0         if ($params) {
90 0           $uri->query_form($params);
91             }
92 0           return $uri;
93             }
94              
95             1;
96              
97             __END__