API-Eulerian
view release on metacpan or search on metacpan
lib/API/Eulerian/EDW/Peer/Rest.pm view on Meta::CPAN
# @brief Accept attribute accessors.
#
# @param $self - Eulerian Data Warehouse Peer.
# @param $accept - Accept.
#
# @return Accept.
#
sub accept
{
my ( $self, $accept ) = @_;
$self->{ _ACCEPT } = $accept if defined( $accept );
return $self->{ _ACCEPT };
}
#
# @brief Working directory attribute accessors.
#
# @param $self - Eulerian Data Warehouse Peer.
# @param $wdir - Working directory.
#
# @return Working Directory.
#
sub wdir
{
my ( $self, $wdir ) = @_;
$self->{ _WDIR } = $wdir if defined( $wdir );
return $self->{ _WDIR };
}
#
# @brief Setup Eulerian Data Warehouse Peer.
#
# @param $self - Eulerian Data Warehouse Peer.
# @param $setup - Setup entries.
#
sub setup
{
my ( $self, $setup ) = @_;
# Setup base interface values
$self->SUPER::setup( $setup );
# Setup Rest specifics options
$self->accept( $setup->{ accept } ) if exists( $setup->{ accept } );
$self->encoding( $setup->{ encoding } ) if exists( $setup->{ encoding } );
$self->wdir( $setup->{ wdir } ) if exists( $setup->{ wdir } );
return $self;
}
#
# @brief Dump Eulerian Data Warehouse Peer setup.
#
# @param $self - Eulerian Data Warehouse Peer.
#
sub dump
{
my $self = shift;
my $dump = '';
$self->SUPER::dump();
$dump .= 'Accept : ' . $self->accept() . "\n";
$dump .= 'Encoding : ' . $self->encoding() . "\n";
$dump .= 'WorkDir : ' . $self->wdir() . "\n\n";
print( $dump );
return $self;
}
#
# @brief Get remote URL to Eulerian Data Warehouse Platform.
#
# @param $self - Eulerian Data Warehouse Peer.
#
# @return Remote URL to Eulerian Data Warehouse Platform.
#
sub url
{
my $self = shift;
my $platform;
my $host;
my $url;
$url = $self->secure() ? 'https://' : 'http://';
$platform = $self->platform();
$host = $self->host();
if( $host ) {
$url .= $host . ':';
$url .= $self->ports()->[ $self->secure() ];
} elsif( $platform eq 'fr' ) {
$url .= 'edw.ea.eulerian.com';
} elsif( $platform eq 'ca' ) {
$url .= 'edw.ea.eulerian.ca';
} else {
$url = undef;
}
return $url;
}
#
# @brief Get HTTP Request Body used to send command to Eulerian Data Warehouse
# Platform.
#
# @param $self - Eulerian Data Warehouse Rest Peer.
# @param $command - Eulerian Data Warehouse Command.
#
# @return HTTP Request Body.
#
sub body
{
my ( $self, $command ) = @_;
$command =~ s/\n//gm;
return JSON::encode_json({
kind => 'edw#request',
query => $command,
creationTime => POSIX::strftime( '%d/%m/%Y %H:%M:%S', gmtime() ),
location => Sys::Hostname::hostname(),
expiration => undef,
});
};
#
# @brief Setup HTTP Request Headers.
#
# @param $self - Eulerian Data Warehouse Peer.
#
# @return HTTP Headers.
#
lib/API/Eulerian/EDW/Peer/Rest.pm view on Meta::CPAN
my( $self, $zipped ) = @_;
my $unzipped;
# Parse zipped file
$zipped =~ /(.*)\.gz/;
# Unzipped file name
$unzipped = $1;
# Gunzip zipped file into unzipped file
IO::Uncompress::Gunzip::gunzip(
$zipped, $unzipped, BinModeOut => 1
);
# Remove zipped file
unlink $zipped;
# Return path to unzipped file
return $unzipped;
}
#
# @brief Download Job reply file.
#
# @param $self - Eulerian Data Warehouse Rest Peer.
# @param $rc - Reply context.
#
# @return Reply context
#
sub download
{
my ( $self, $status ) = @_;
# From Last status message compute local file path
$status = $self->path( $status->{ response } );
# If no error
if( ! $status->{ error } ) {
my $path = $status->{ path };
my $url = $status->{ url };
my $response;
# Get HTTP request headers
$status = $self->headers();
if( ! $status->{ error } ) {
# Send Download request to remote host, reply is
# writen into $path file
$status = API::Eulerian::EDW::Request->get(
$url, $status->{ headers }, $path
);
# Handle errors
if( ! $status->error() ) {
my $encoding = $status->{ 'encoding' };
if( defined( $encoding ) && ( $encoding eq 'gzip' ) ) {
rename $path, "$path.gz";
$status->{ path } = $self->unzip( "$path.gz" );
} else {
$status->{ path } = $path;
}
} else {
print "ERROR : " . $status->{ _CODE } . "\n";
}
}
}
return $status;
}
#
# @brief Parse local file path and invoke hook handlers.
#
# @param $self - API::Eulerian::EDW::Rest instance.
# @param $rc - Reply context.
#
# @return Reply context.
#
sub parse
{
my ( $self, $status ) = @_;
my $path = $status->{ path };
my $parser;
my $name;
my %rc;
my $pattern = '[0-9]*\.(json|csv|parquet)';
# Parse file path, get file type
if( ( $path =~ m/$pattern/ ) ) {
# Lookup for parser matching file type
if( ( $name = $PARSERS{ $1 } ) ) {
# Create new instance of Parser
if( ( $parser = $name->new( $path, $self->uuid() ) ) ) {
# Parse reply file raise callback hook
$parser->do( $self->hook() );
} else {
$status->error( 1 );
$status->msg( "Failed to create Parser" );
$status->code( 401 );
}
} else {
$status->error( 1 );
$status->msg( "Unknown Parser" );
$status->code( 501 );
}
} else {
$status->error( 1 );
$status->msg( "Unknown file format" );
$status->code( 401 );
}
return $status;
}
#
# @brief Do Request on Eulerian Data Warehouse Platform.
#
# @param $self - Eulerian Data Warehouse Peer.
# @param $command - Eulerian Data Warehouse Command.
#
use Data::Dumper;
sub request
{
my ( $self, $command ) = @_;
my $bench = new API::Eulerian::EDW::Bench();
my $response;
my $status;
my $json;
# Create Job on Eulerian Data Warehouse Platform
$bench->start();
$status = $self->create( $command );
$bench->stage( 'create' );
# Wait end of Job
$bench->start();
while( ! $status->error() && $self->running( $status ) ) {
$status = $self->status( $status );
sleep( 2 );
}
$bench->stage( 'running' );
# If Done, download reply file
if( ! $status->error() && $self->done( $status ) ) {
$bench->start();
$status = $self->download( $status );
$bench->stage( 'download' );
if( ! $status->error() ) {
# Parse reply file, call hooks
$bench->start();
$status = $self->parse( $status );
$bench->stage( 'parse' );
}
$status->{ bench } = $bench;
} else {
print "ERROR : " . Dumper( $status ) . "\n";
}
return $status;
}
#
# @brief Cancel Job on Eulerian Data Warehouse Platform.
#
# @param $self - API::Eulerian::EDW::Rest instance.
# @param $rc - Reply context.
#
sub cancel
{
my ( $self ) = @_;
my $status;
# Get HTTP request headers
$status = $self->headers();
if( ! $status->error() ) {
my $headers = $status->{ headers };
delete $status->{ headers };
# Create cancel job url
if( ! $self->uuid() ) {
$status->error( 1 );
$status->msg( "Failed to cancel Job. Unknown UUID" );
$status->code( 404 );
} else {
my $url = $self->url() . '/edw/jobs/';
$url .= $self->uuid() . '/cancel';
# Send Cancel request to remote host
$status = API::Eulerian::EDW::Request->get( $url, $headers );
}
}
return $status;
}
#
# @brief Drop Eulerian Data Warehouse Object Events.
#
# @param $self - Peer.
# @param $what - Object Path.
# @param $site - Site.
# @param $from - Day begin.
# @param $end - Day end.
#
sub drop
{
my ( $self, $what, $site, $from, $to ) = @_;
my $status;
# Get HTTP request headers
$status = $self->headers();
if( ! $status->error() ) {
my $headers = $status->{ headers };
my $url = $self->url() . '/edw/store';
my $cmd = "DROP $what\@$site FROM $from TO $to;";
delete $status->{ headers };
# Send Cancel request to remote host
( run in 1.035 second using v1.01-cache-2.11-cpan-39bf76dae61 )