App-MBUtiny
view release on metacpan or search on metacpan
lib/App/MBUtiny/Collector.pm view on Meta::CPAN
Returns list of last backups from all collectors as array of info-hashes.
See L</info> method
=head1 PUBLIC FUNCTIONS
=head2 int2type
my $type = int2type(0); # internal
Returns name of specified type
NOTE: This variable NOT imported automatically
=head1 VARIABLES
=head2 COLLECTOR_TYPES
Returns hash-structure ("type_name" => int_value) of available collector types
NOTE: This variable imported automatically
=head1 HISTORY
See C<Changes> file
=head1 TO DO
See C<TODO> file
=head1 SEE ALSO
L<App::MBUtiny>
=head1 AUTHOR
Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
=head1 COPYRIGHT
Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
=head1 LICENSE
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See C<LICENSE> file and L<https://dev.perl.org/licenses/>
=cut
use vars qw/ $VERSION @EXPORT @EXPORT_OK /;
$VERSION = '1.03';
use Carp;
use CTK::ConfGenUtil;
use CTK::TFVals qw/ :ALL /;
use App::MBUtiny::Collector::DBI;
use App::MBUtiny::Collector::Client;
use App::MBUtiny::Util qw/hide_password/;
use constant {
COLLECTOR_TYPES => {
internal => 0,
external => 1,
},
};
use base qw/Exporter/;
@EXPORT_OK = qw/
int2type
/;
@EXPORT = qw/
COLLECTOR_TYPES
/;
sub new {
my $class = shift;
my %args = @_;
my $collector_config = $args{collector_config} || [];
my $dbi_config = $args{dbi_config} || {};
croak("Incorrect collector config. Array expected!") unless is_array($collector_config);
my $dbi = $args{dbi} || App::MBUtiny::Collector::DBI->new(%$dbi_config);
croak("Can't use incorrect dbi object") unless $dbi && ref($dbi) eq 'App::MBUtiny::Collector::DBI';
# Collectors
my @collectors = ();
my $internal = 0;
foreach my $cltr (@$collector_config) {
my $url = value($cltr, "url");
if ($url) {
push @collectors, {
type => "external",
url => $url,
comment => uv2null(value($cltr, "comment")),
timeout => uv2zero(value($cltr, "timeout")),
};
} else {
push @collectors, {
type => "internal",
comment => uv2null(value($cltr, "comment")),
} unless $internal;
$internal++;
}
}
unless (@collectors) {
push @collectors, {
type => "internal",
comment => "",
} unless $internal;
}
my $self = bless {
collectors => [@collectors],
dbi => $dbi,
errors => [],
}, $class;
return $self;
}
sub error {
my $cnt = @_;
my $self = shift;
my $s = shift;
my $errors = $self->{errors} || [];
if ($cnt >= 2) {
if ($s) {
push @$errors, $s;
} else {
$errors = [];
}
$self->{errors} = $errors;
}
return join("\n", @$errors);
}
sub dbi {
my $self = shift;
return $self->{dbi};
}
sub collectors {
my $self = shift;
my $collectors = $self->{collectors};
return scalar(@$collectors) unless wantarray;
return @$collectors;
}
sub check {
my $self = shift;
$self->error("");
my $dbi = $self->dbi;
return "" unless $self->collectors;
my @ret = (); # List of DSN/URL_wo_password
foreach my $collector ($self->collectors) {
my $type = $collector->{type};
if ($type eq 'internal') { # Internal
if ($dbi->error) {
$self->error($dbi->error());
next;
};
push @ret, $dbi->dsn;
} else { # External
my $url = $collector->{url} || "";
my $timeout = $collector->{timeout} || 0;
my $client = new App::MBUtiny::Collector::Client(
url => $url,
timeout => $timeout,
);
unless ($client->status) {
$self->error(join("%s\n%s", $client->transaction, $client->error));
next;
}
my $check = $client->check;
unless ($client->status) {
$self->error(join("\n", $client->transaction, $client->error));
next;
}
push @ret, hide_password($url);
}
}
return @ret ? join("\n", @ret) : "";
}
sub fixup {
my $self = shift;
my %args = @_;
$self->error("");
my $dbi = $self->dbi;
return "" unless $self->collectors;
my @ret = (); # List of DSN/URL_wo_password
foreach my $collector ($self->collectors) {
my $type = $collector->{type};
my $url = $collector->{url} || "";
my $timeout = $collector->{timeout} || 0;
my $comment = join("\n", grep {$_} ($args{comment}, $collector->{comment})) // "";
my $op = $args{operation} || '';
if ($op =~ /^(del)|(rem)/) { # Delete (op)
if ($type eq 'internal') { # Internal
$dbi->del(
type => _type2int($type),
name => $args{name},
file => $args{file},
) or do {
$self->error($dbi->error());
next;
};
push @ret, $dbi->dsn;
} else { # External
my $client = new App::MBUtiny::Collector::Client( url => $url, timeout => $timeout );
unless ($client->status) {
$self->error(join("%s\n%s", $client->transaction, $client->error));
next;
}
$client->del(
type => _type2int($type),
name => $args{name},
file => $args{file},
) or do {
$self->error(join("\n", $client->transaction, $client->error));
next;
};
push @ret, hide_password($url);
}
} else { # Put (op)
if ($type eq 'internal') { # Internal
$dbi->add(
type => _type2int($type),
name => $args{name},
file => $args{file},
size => $args{size},
md5 => $args{md5},
sha1 => $args{sha1},
status => $args{status},
error => $args{error},
comment => $comment,
) or do {
$self->error($dbi->error());
next;
};
push @ret, $dbi->dsn;
} else { # External
my $client = new App::MBUtiny::Collector::Client( url => $url, timeout => $timeout );
unless ($client->status) {
$self->error(join("%s\n%s", $client->transaction, $client->error));
next;
}
$client->add(
type => _type2int($type),
name => $args{name},
file => $args{file},
size => $args{size},
md5 => $args{md5},
sha1 => $args{sha1},
status => $args{status},
error => $args{error},
comment => $comment,
) or do {
$self->error(join("\n", $client->transaction, $client->error));
next;
};
push @ret, hide_password($url);
}
}
}
return @ret ? join("\n", @ret) : "";
}
sub info {
my $self = shift;
my %args = @_;
$self->error("");
my $dbi = $self->dbi;
return () unless $self->collectors;
my %info = (); # Information about file
foreach my $collector ($self->collectors) {
if ($collector->{type} eq 'internal') { # Internal
%info = $dbi->get(
name => $args{name},
file => $args{file},
) or do {
$self->error($dbi->error());
next;
};
} else { # External
my $client = new App::MBUtiny::Collector::Client(
url => $collector->{url} || "",
timeout => $collector->{timeout} || 0,
);
unless ($client->status) {
$self->error(join("%s\n%s", $client->transaction, $client->error));
next;
}
%info = $client->get(
name => $args{name},
file => $args{file},
);
if ($client->error) {
$self->error(join("\n", $client->transaction, $client->error));
next;
}
}
last if $info{id} && $info{status};
}
return %info;
}
sub report {
my $self = shift;
my %args = @_;
$self->error("");
my $dbi = $self->dbi;
return () unless $self->collectors;
my %mreport = (); # Report
foreach my $collector ($self->collectors) {
my @rep = ();
if ($collector->{type} eq 'internal') { # Internal
@rep = $dbi->report( start => $args{start});
if ($dbi->error()) {
$self->error($dbi->error());
next;
};
} else { # External
( run in 0.509 second using v1.01-cache-2.11-cpan-39bf76dae61 )