MT#22992 journal time range query parameters

+fix journal action method config

Change-Id: I3e72e0928d185a5052ad556597661517920dd6b4
changes/52/8852/3
Rene Krenn 9 years ago
parent ae5e887dc2
commit 7a89ab811d

@ -62,8 +62,8 @@ sub add_journal_item_hal {
@hal_from_item = ( $h ); @hal_from_item = ( $h );
} }
} }
$id_name = $params->{id_name} if defined $params->{id_name}; $id_name = $params->{id_name} if defined $params->{id_name};
$id = $params->{id} if defined $params->{id}; $id = $params->{id} if defined $params->{id};
} elsif (ref $arg eq 'ARRAY') { } elsif (ref $arg eq 'ARRAY') {
$params = {}; $params = {};
@hal_from_item = @$arg; @hal_from_item = @$arg;
@ -79,7 +79,7 @@ sub add_journal_item_hal {
$params->{operation} = $operation; $params->{operation} = $operation;
$params->{resource_name} //= $controller->resource_name; $params->{resource_name} //= $controller->resource_name;
$params->{format} //= $cfg->{format}; $params->{format} //= $cfg->{format};
my $resource = shift @hal_from_item; my $resource = shift @hal_from_item;
my $hal; my $hal;
if (ref $resource eq 'CODE') { if (ref $resource eq 'CODE') {
@ -105,62 +105,62 @@ sub add_journal_item_hal {
sub get_api_journal_action_config { sub get_api_journal_action_config {
my ($path_part,$action_template,$journal_methods) = @_; my ($path_part,$action_template,$journal_methods) = @_;
my %journal_actions_found = map { $_ => 1 } @$journal_methods; my %journal_actions_found = map { $_ => 1 } @$journal_methods;
if (exists $journal_actions_found{'item_base_journal'}) { if (exists $journal_actions_found{'handle_item_base_journal'}) {
my @result = (); my @result = ();
if (exists $journal_actions_found{'journals_get'}) { if (exists $journal_actions_found{'handle_journals_get'}) {
my $action_config = Storable::dclone($action_template); my $action_config = Storable::dclone($action_template);
$action_config->{Chained} = 'item_base_journal'; $action_config->{Chained} = 'handle_item_base_journal';
$action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME; $action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME;
$action_config->{Args} = 0; $action_config->{Args} = 0;
$action_config->{Method} = 'GET'; $action_config->{Method} = 'GET';
push(@result,$action_config,'journals_get'); push(@result,$action_config,'handle_journals_get');
} }
if (exists $journal_actions_found{'journals_options'}) { if (exists $journal_actions_found{'handle_journals_options'}) {
my $action_config = Storable::dclone($action_template); my $action_config = Storable::dclone($action_template);
$action_config->{Chained} = 'item_base_journal'; $action_config->{Chained} = 'handle_item_base_journal';
$action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME; $action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME;
$action_config->{Args} = 0; $action_config->{Args} = 0;
$action_config->{Method} = 'OPTIONS'; $action_config->{Method} = 'OPTIONS';
push(@result,$action_config,'journals_options'); push(@result,$action_config,'handle_journals_options');
} }
if (exists $journal_actions_found{'journals_head'}) { if (exists $journal_actions_found{'handle_journals_head'}) {
my $action_config = Storable::dclone($action_template); my $action_config = Storable::dclone($action_template);
$action_config->{Chained} = 'item_base_journal'; $action_config->{Chained} = 'handle_item_base_journal';
$action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME; $action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME;
$action_config->{Args} = 0; $action_config->{Args} = 0;
$action_config->{Method} = 'HEAD'; $action_config->{Method} = 'HEAD';
push(@result,$action_config,'journals_head'); push(@result,$action_config,'handle_journals_head');
} }
if (exists $journal_actions_found{'journalsitem_get'}) { if (exists $journal_actions_found{'handle_journalsitem_get'}) {
my $action_config = Storable::dclone($action_template); my $action_config = Storable::dclone($action_template);
$action_config->{Chained} = 'item_base_journal'; $action_config->{Chained} = 'handle_item_base_journal';
$action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME; $action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME;
$action_config->{Args} = 1; $action_config->{Args} = 1;
$action_config->{Method} = 'GET'; $action_config->{Method} = 'GET';
push(@result,$action_config,'journalsitem_get'); push(@result,$action_config,'handle_journalsitem_get');
} }
if (exists $journal_actions_found{'journalsitem_options'}) { if (exists $journal_actions_found{'handle_journalsitem_options'}) {
my $action_config = Storable::dclone($action_template); my $action_config = Storable::dclone($action_template);
$action_config->{Chained} = 'item_base_journal'; $action_config->{Chained} = 'handle_item_base_journal';
$action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME; $action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME;
$action_config->{Args} = 1; $action_config->{Args} = 1;
$action_config->{Method} = 'OPTIONS'; $action_config->{Method} = 'OPTIONS';
push(@result,$action_config,'journalsitem_options'); push(@result,$action_config,'handle_journalsitem_options');
} }
if (exists $journal_actions_found{'journalsitem_head'}) { if (exists $journal_actions_found{'handle_journalsitem_head'}) {
my $action_config = Storable::dclone($action_template); my $action_config = Storable::dclone($action_template);
$action_config->{Chained} = 'item_base_journal'; $action_config->{Chained} = 'handle_item_base_journal';
$action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME; $action_config->{PathPart} //= API_JOURNAL_RESOURCE_NAME;
$action_config->{Args} = 1; $action_config->{Args} = 1;
$action_config->{Method} = 'HEAD'; $action_config->{Method} = 'HEAD';
push(@result,$action_config,'journalsitem_head'); push(@result,$action_config,'handle_journalsitem_head');
} }
if ((scalar @result) > 0) { if ((scalar @result) > 0) {
push(@result,{ push(@result,{
Chained => '/', Chained => '/',
PathPart => $path_part, PathPart => $path_part,
CaptureArgs => 1, CaptureArgs => 1,
},'item_base_journal'); },'handle_item_base_journal');
@result = reverse @result; @result = reverse @result;
return \@result; return \@result;
} }
@ -181,6 +181,38 @@ sub get_api_journal_query_params {
}, },
second => sub { }, second => sub { },
}, },
},{
param => 'username',
description => 'Filter for journal items by the name of the user who performed the operation',
query => {
first => sub {
my $q = shift;
{ 'username' => $q };
},
second => sub { },
},
},{
param => 'from',
description => 'Filter for journal items recorded after or at the specified time stamp',
query => {
first => sub {
my $q = shift;
my $dt = NGCP::Panel::Utils::DateTime::from_string($q);
return { 'timestamp' => { '>=' => $dt->epoch } };
},
second => sub { },
},
},{
param => 'to',
description => 'Filter for journal items recorded before or at the specified time stamp',
query => {
first => sub {
my $q = shift;
my $dt = NGCP::Panel::Utils::DateTime::from_string($q);
return { 'timestamp' => { '<=' => $dt->epoch } };
},
second => sub { },
},
}); });
return \@params; return \@params;
} }
@ -198,7 +230,7 @@ sub handle_api_journals_get {
{ {
my $item_id = $c->stash->{item_id_journal}; my $item_id = $c->stash->{item_id_journal};
last unless $controller->valid_id($c, $item_id); last unless $controller->valid_id($c, $item_id);
my $journals = get_journal_rs($controller,$c,$item_id); my $journals = get_journal_rs($controller,$c,$item_id);
(my $total_count, $journals) = $controller->paginate_order_collection($c,$journals); (my $total_count, $journals) = $controller->paginate_order_collection($c,$journals);
@ -229,12 +261,12 @@ sub handle_api_journals_get {
$hal->resource({ $hal->resource({
total_count => $total_count, total_count => $total_count,
}); });
my $response = HTTP::Response->new(HTTP_OK, undef, my $response = HTTP::Response->new(HTTP_OK, undef,
HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json); HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json);
$c->response->headers($response->headers); $c->response->headers($response->headers);
$c->response->body($response->content); $c->response->body($response->content);
return; return;
} }
return; return;
} }
@ -255,9 +287,9 @@ sub handle_api_journalsitem_get {
} else { } else {
last; last;
} }
last unless $controller->resource_exists($c, journal => $journal); last unless $controller->resource_exists($c, journal => $journal);
if ($journal->resource_id != $item_id) { if ($journal->resource_id != $item_id) {
$c->log->error("Journal item '" . $id . "' does not belong to '" . $controller->resource_name . '/' . $item_id . "'"); $c->log->error("Journal item '" . $id . "' does not belong to '" . $controller->resource_name . '/' . $item_id . "'");
$controller->error($c, HTTP_NOT_FOUND, "Entity 'journal' not found."); $controller->error($c, HTTP_NOT_FOUND, "Entity 'journal' not found.");
@ -289,7 +321,7 @@ sub handle_api_journals_options {
push(@allowed_methods,'GET'); push(@allowed_methods,'GET');
if (exists $journal_actions_found{'journals_head'}) { if (exists $journal_actions_found{'journals_head'}) {
push(@allowed_methods,'HEAD'); push(@allowed_methods,'HEAD');
} }
} }
$c->response->headers(HTTP::Headers->new( $c->response->headers(HTTP::Headers->new(
Allow => join(', ',@allowed_methods), Allow => join(', ',@allowed_methods),
@ -307,7 +339,7 @@ sub handle_api_journalsitem_options {
push(@allowed_methods,'GET'); push(@allowed_methods,'GET');
if (exists $journal_actions_found{'journalsitem_head'}) { if (exists $journal_actions_found{'journalsitem_head'}) {
push(@allowed_methods,'HEAD'); push(@allowed_methods,'HEAD');
} }
} }
$c->response->headers(HTTP::Headers->new( $c->response->headers(HTTP::Headers->new(
Allow => join(', ',@allowed_methods), Allow => join(', ',@allowed_methods),
@ -322,7 +354,7 @@ sub _has_journal_method {
my ($controller, $action) = @_; my ($controller, $action) = @_;
my %journal_actions_found = map { $_ => 1 } @{ $controller->get_journal_methods }; my %journal_actions_found = map { $_ => 1 } @{ $controller->get_journal_methods };
return exists $journal_actions_found{$action}; return exists $journal_actions_found{$action};
} }
sub handle_api_journals_head { sub handle_api_journals_head {
@ -363,11 +395,11 @@ sub get_journal_rs {
columns => JOURNAL_FIELDS, columns => JOURNAL_FIELDS,
#alias => 'me', #alias => 'me',
})); }));
if ($controller->can('journal_query_params')) { if ($controller->can('journal_query_params')) {
return $controller->apply_query_params($c,$controller->journal_query_params,$rs); return $controller->apply_query_params($c,$controller->journal_query_params,$rs);
} }
return $rs; return $rs;
} }
@ -395,7 +427,7 @@ sub get_top_n_journalitems {
})->all; })->all;
return \@journals; return \@journals;
} }
sub hal_from_journal { sub hal_from_journal {
my ($controller,$c,$journal) = @_; my ($controller,$c,$journal) = @_;
@ -414,7 +446,7 @@ sub hal_from_journal {
#delta stuff... #delta stuff...
} }
} }
my $hal = Data::HAL->new( my $hal = Data::HAL->new(
links => [ links => [
Data::HAL::Link->new( Data::HAL::Link->new(
@ -431,22 +463,22 @@ sub hal_from_journal {
], ],
relation => API_JOURNAL_RELATION, #API_JOURNALITEM_RELATION, relation => API_JOURNAL_RELATION, #API_JOURNALITEM_RELATION,
); );
my $datetime_fmt = DateTime::Format::Strptime->new( my $datetime_fmt = DateTime::Format::Strptime->new(
pattern => '%F %T', pattern => '%F %T',
); );
$resource{timestamp} = $datetime_fmt->format_datetime($resource{timestamp}); $resource{timestamp} = $datetime_fmt->format_datetime($resource{timestamp});
delete $resource{resource_name}; delete $resource{resource_name};
delete $resource{resource_id}; delete $resource{resource_id};
delete $resource{content_format}; delete $resource{content_format};
$hal->resource({%resource}); $hal->resource({%resource});
return $hal; return $hal;
} }
sub get_journal_relation_link { sub get_journal_relation_link {
my ($resource,$item_id,$id,$relation) = @_; my ($resource,$item_id,$id,$relation) = @_;
my $resource_name = undef; my $resource_name = undef;
if (ref $resource eq 'HASH') { if (ref $resource eq 'HASH') {
@ -512,7 +544,7 @@ sub _get_journal_op_config {
} }
} }
return { format => $format, operation_enabled => $operation_enabled}; return { format => $format, operation_enabled => $operation_enabled};
} }
sub get_journal_resource_config { sub get_journal_resource_config {
@ -532,7 +564,7 @@ sub get_journal_resource_config {
} }
} }
return { journal_resource_enabled => $journal_resource_enabled }; return { journal_resource_enabled => $journal_resource_enabled };
} }
sub _serialize_content { #run this in eval only, deflate somehow inflicts a segfault in subsequent catalyst action when not consuming all args there. sub _serialize_content { #run this in eval only, deflate somehow inflicts a segfault in subsequent catalyst action when not consuming all args there.
@ -598,7 +630,7 @@ sub _create_journal {
$journal{username} = $c->user->login; $journal{username} = $c->user->login;
} elsif($c->user->roles eq 'subscriber' || $c->user->roles eq 'subscriberadmin') { } elsif($c->user->roles eq 'subscriber' || $c->user->roles eq 'subscriberadmin') {
$journal{username} = $c->user->webusername . '@' . $c->user->domain->domain; $journal{username} = $c->user->webusername . '@' . $c->user->domain->domain;
} }
} }
try { try {
$journal{content} = _serialize_content($content_format,$resource); $journal{content} = _serialize_content($content_format,$resource);
@ -621,4 +653,4 @@ sub _create_journal {
return undef; return undef;
} }
1; 1;

Loading…
Cancel
Save