Skip to content

Commit

Permalink
* remove legacy json decode/encode call
Browse files Browse the repository at this point in the history
* make aggregate more robust
  • Loading branch information
oetiker committed Feb 2, 2015
1 parent 49429c4 commit b77a756
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 72 deletions.
17 changes: 7 additions & 10 deletions software/backend/lib/EP/Cache.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ information.
use Mojo::Base -base;
use Carp;
use DBI;
use Mojo::JSON;
use Mojo::JSON qw(decode_json encode_json);
use Encode;
use EP::Exception qw(mkerror);

Expand Down Expand Up @@ -159,7 +159,6 @@ has 'dbhPe';

has encodeUtf8 => sub { find_encoding('utf8') };
has tree => sub { [] };
has json => sub { Mojo::JSON->new };


=head2 B<new>(I<config>)
Expand Down Expand Up @@ -324,7 +323,7 @@ sub add {
}
$self->log->debug("keygen $rawNodeId => $nodeId");
eval {
$dbc->do("INSERT INTO node (rowid,data) VALUES (?,?)",{},$nodeId,$self->json->encode($nodeData));
$dbc->do("INSERT INTO node (rowid,data) VALUES (?,?)",{},$nodeId,encode_json($nodeData));
};
if ($@){
$self->log->warn("$@");
Expand Down Expand Up @@ -433,10 +432,9 @@ sub getNodes {
my $dbh = $self->dbhCa;
my $sth = $dbh->prepare("SELECT docid,data FROM node WHERE data MATCH ? LIMIT ? OFFSET ?");
$sth->execute($self->encodeUtf8->encode($expression),$limit,$offset);
my $json = $self->json;
my @return;
while (my $row = $sth->fetchrow_hashref){
my $data = $json->decode($row->{data});
my $data = decode_json($row->{data});
my $entry = { map { $_ => $data->{$_} } @{$self->searchCols} };
$entry->{__epId} = $row->{docid};
push @return, $entry;
Expand All @@ -455,8 +453,7 @@ sub getNode {
my $nodeId = shift;
my $dbh = $self->dbhCa;
my @row = $dbh->selectrow_array("SELECT data FROM node WHERE docid = ?",{},$nodeId);
my $json = $self->json;
my $ret = $json->decode($row[0]);
my $ret = decode_json($row[0]);
$ret->{__epId} = $nodeId;
return $ret;
}
Expand Down Expand Up @@ -493,7 +490,7 @@ sub getBranch {
$branch->[1] =~ s/^{SORT:.+?}//;
my @leaves;
while (my ($docid,$row) = $sth->fetchrow_array()){
my $data = $self->json->decode($row);
my $data = decode_json($row);
$data->{__epId} = $docid;
push @leaves, [ map { $data->{$_} } @{$self->treeCols} ];
}
Expand Down Expand Up @@ -556,7 +553,7 @@ SQL_END
lb => $row->{label}, # label
login => $row->{login}, # owner - login
private => $row->{private}, # private
cfg => $self->json->decode($row->{cf}), #cfg
cfg => decode_json($row->{cf}), #cfg
mine => $row->{mine},
};
}
Expand All @@ -583,7 +580,7 @@ Returns:

sub saveDash {
my $self = shift;
my $cfg = $self->json->encode(shift);
my $cfg = encode_json(shift);
my $label = shift;
my $id = shift;
my $updateTime = shift;
Expand Down
44 changes: 25 additions & 19 deletions software/backend/lib/EP/Visualizer/TorrusAggregateBrowserChart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ all the methods from L<EP::Visualizer::base>. As well as these:
use Mojo::Base 'EP::Visualizer::base';
use Mojo::Util qw(url_unescape);
use Mojo::URL;
use Mojo::JSON;
use Mojo::JSON qw(decode_json);
use Data::Dumper;

use Mojo::UserAgent;
use Mojo::Template;
Expand All @@ -64,8 +65,6 @@ use POSIX qw(strftime);
has 'hostauth';
has view => 'embedded';

has json => sub {Mojo::JSON->new};

sub new {
my $self = shift->SUPER::new(@_);
if( defined($self->cfg->{hostauth}) ){
Expand Down Expand Up @@ -157,6 +156,7 @@ sub rpcService {
Gend => int($end),
Gstep =>int($step || 1),
Gmaxrows => 4000,
DATAONLY => 1,
nodeid=>$item->{node_id}.'//'.$view
);
$self->app->log->debug($url->to_string);
Expand All @@ -167,36 +167,42 @@ sub rpcService {
if (my $res=$tx->success) {
if ($res->headers->content_type =~ m'application/json'i){
# $self->app->log->debug($res->body);
my $ret = $self->json->decode($res->body);
my $ret = eval { decode_json($res->body) };
if ($@){
$self->log->error("Error JSON Decode:".$@);
push @$return, {
status => 'failed',
message => "JSON Decode Problem $url: $@"
};
next;
}
if ($ret->{success}){
push @$return, {
status => 'ok',
start => $ret->{data}{start},
step => $ret->{data}{step},
values => [ map { $dataExtract ? $dataExtract->(@$_) : $_->[0] } @{$ret->{data}{data}} ],
}
}
else {
push @$return, {
status => 'failed',
message => "Torrus Problem $url: $ret->{error}"
};
next;
}
}
else {
$self->log->error("Torrus Problem $url: $ret->{error}");
push @$return, {
status => 'failed',
message => "Faild Fetch $url: Data Type ".$res->headers->content_type,
status => 'failed',
message => "Torrus Problem $url: $ret->{error}"
};
next;
}
}
else {
my $error = $tx->error;
push @$return, {
status => 'faild',
message => "Faild Fetch $url: $error->{message}",
status => 'failed',
message => "Faild Fetch $url: Data Type ".$res->headers->content_type,
};
next;
}
my $error = $tx->error;
push @$return, {
status => 'failed',
message => "Faild Fetch $url: $error->{message}",
};
}
return $return
}
Expand Down
4 changes: 2 additions & 2 deletions software/backend/lib/EP/Visualizer/TorrusBrowserChart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ all the methods from L<EP::Visualizer::TorrusChart>. As well as these:

use Mojo::Base 'EP::Visualizer::TorrusChart';
use EP::Exception qw(mkerror);

use Mojo::JSON qw(decode_json);

=head2 matchRecord(type,args)
Expand Down Expand Up @@ -125,7 +125,7 @@ sub getChartData {
if (my $res=$tx->success) {
if ($res->headers->content_type =~ m'application/json'i){
# $self->app->log->debug($res->body);
my $ret = eval { $self->json->decode($res->body) };
my $ret = eval { decode_json($res->body) };
if ($@){
return {
status => 'failed',
Expand Down
10 changes: 7 additions & 3 deletions software/backend/lib/EP/Visualizer/TorrusChart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ all the methods from L<EP::Visualizer::base>. As well as these:
use Mojo::Base 'EP::Visualizer::base';
use Mojo::Util qw(url_unescape);
use Mojo::URL;
use Mojo::JSON;
use Mojo::JSON qw(decode_json);

use Mojo::UserAgent;
use Mojo::Template;
Expand All @@ -90,7 +90,7 @@ use POSIX qw(strftime);

has 'hostauth';
has view => 'embedded';
has json => sub {Mojo::JSON->new};

has 'printtemplate';
has 'mode' => 'traffic';
has root => sub {'torrusChart_'.shift->instance};
Expand Down Expand Up @@ -219,7 +219,11 @@ sub getLeaves {
my $tx = Mojo::UserAgent->new->get($url);
if (my $res=$tx->success) {
if ($res->headers->content_type =~ m'application/json'i){
my $ret = $self->json->decode($res->body);
my $ret = eval { decode_json($res->body) };
if ($@){
$log->error("Running $rpcCall on ".join(', ',map{"$_: $callParams->{$_}"} keys %$callParams).$@);
return {};
}
if ($ret->{success}){
return $ret->{data};
} else {
Expand Down
13 changes: 10 additions & 3 deletions software/backend/lib/EP/Visualizer/TorrusData.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ all the methods from L<EP::Visualizer::base>. As well as these:
use Mojo::Base 'EP::Visualizer::base';
use Mojo::Util qw(url_unescape);
use Mojo::URL;
use Mojo::JSON;
use Mojo::JSON qw(decode_json);
use Mojo::UserAgent;
use Mojo::Template;

Expand All @@ -68,7 +68,7 @@ has 'hostauth';
has root => sub {'torrusCSV_'.shift->instance };

has view => 'embedded';
has json => sub { Mojo::JSON->new};


sub new {
my $self = shift->SUPER::new(@_);
Expand Down Expand Up @@ -279,7 +279,14 @@ sub getData {
my $data;
if (my $res=$tx->success) {
if ($res->headers->content_type =~ m'application/json'i){
my $ret = $self->json->decode($res->body);
my $ret = eval { decode_json($res->body) };
if ($@){
$self->app->log->error("Fetching ".$url->to_string.": ".$@);
return {
status => 0,
error => $@
};
}
if ($ret->{success}){
my $key = (keys %{$ret->{data}})[0];
$data{$subNode} = rrd2float($ret->{data}{$key});
Expand Down
7 changes: 5 additions & 2 deletions software/backend/lib/EP/Visualizer/TorrusIframe.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use EP::Exception qw(mkerror);
has 'hostauth';
#has 'view' => 'expanded-dir-html';
has view => 'iframe-rrd';
has json => sub {Mojo::JSON->new};


sub new {
Expand Down Expand Up @@ -109,7 +108,11 @@ sub getLeaves {
my $tx = Mojo::UserAgent->new->get($url);
if (my $res=$tx->success) {
if ($res->headers->content_type =~ m'application/json'i){
my $ret = $self->json->decode($res->body);
my $ret = eval { decode_json($res->body) };
if ($@){
$log->error("JSON decode Problem:".$@);
return {};
}
if ($ret->{success}){
return $ret->{data};
} else {
Expand Down
20 changes: 15 additions & 5 deletions software/backend/lib/EP/Visualizer/base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ a pointer to the application object

has 'app';

=head2 log
log object
=cut

has log => sub {
shift->app->log;
};

=head2 instance
the name of the instance
Expand Down Expand Up @@ -71,7 +81,7 @@ used on extopus side. It returns either undef (no match) or an array of maps:

sub matchRecord {
my $self= shift;
my $rec = shift;
my $rec = shift;
return;
}

Expand All @@ -83,7 +93,7 @@ Can the Visualizer deal with multiple records of the given type?

sub matchMultiRecord {
my $self= shift;
my $rec = shift;
my $rec = shift;
return;
}

Expand Down Expand Up @@ -115,7 +125,7 @@ rpcService wants to provide. The config option is called caption_live.
sub caption_live {
my $self = shift;
my $rec = shift;

return $self->caption($rec) unless $self->cfg->{caption_live};

my $conf = shift;
Expand All @@ -137,7 +147,7 @@ sub rpcService { ## no critic (RequireArgUnpacking)
my $self = shift;
my $controller = shift;
my @args = @_;
die "sorry, no rpc service support";
die "sorry, no rpc service support";
}

=head2 calcHash(ref)
Expand All @@ -148,7 +158,7 @@ Returns a hash for authenticating access to the ref

sub calcHash { ## no critic (RequireArgUnpacking)
my $self = shift;
# $self->log->debug('HASH '.join(',',@_));
# $self->log->debug('HASH '.join(',',@_));
my $hash = hmac_sha1_sum(join('::',@_),$self->app->secrets->[0]);
return $hash;
}
Expand Down
Loading

0 comments on commit b77a756

Please sign in to comment.