Archived
1
0

Trace: can export as string, can addId

This commit is contained in:
Mercier Pierre-Olivier 2013-11-13 00:09:54 +01:00
parent 5d2b1e80fb
commit 81fd3a04e2
2 changed files with 103 additions and 28 deletions

View File

@ -340,13 +340,15 @@ sub compute ($$$;$$$)
my $login = shift; my $login = shift;
my $ref = $self->{ref}; my $ref = $self->{ref};
if ($login && $ref) {
$ref =~ s/\$LOGIN/$login/;
}
# Handle $LOGIN in ref
$ref =~ s/\$LOGIN/$login/ if ($login && $ref);
# Handle globbing in ref
if (defined $ref) if (defined $ref)
{ {
eval { eval
{
my $glob = Tinyglob::tinyglob($ref); my $glob = Tinyglob::tinyglob($ref);
if ($glob ne $ref) if ($glob ne $ref)
{ {

View File

@ -97,6 +97,38 @@ sub getInfos ($)
return $self->{infos}; return $self->{infos};
} }
sub addId
{
my $self = shift;
my $key = shift;
my $value = shift;
my $e = Trace::Eval->new($key);
$e->addId(undef, $value);
push @{ $self->{groups} }, $e;
}
sub delId
{
my $self = shift;
my $key = shift;
my $value = shift;
foreach my $group (@{ $self->{groups} })
{
if ($group->{id} eq $key)
{
if (!$value || $value == $group->getValue())
{
#$self->{groups} = \{ grep { ! } @{ $self->{groups} } };
}
last;
}
$group->delId($key, $value);
}
}
sub getIds sub getIds
{ {
my $self = shift; my $self = shift;
@ -156,30 +188,19 @@ sub getWhos
return $ret; return $ret;
} }
sub toString ($;$) sub toString ($)
{ {
my $self = shift; my $self = shift;
my $main_grp = shift // "bonus_malus";
my $doc = XML::LibXML::Document->new('1.0'); my $doc = XML::LibXML::Document->new('1.0');
my $root = $doc->createElement("trace"); my $root = $doc->createElement("trace");
my $group = $doc->createElement("group"); foreach my $group (@{ $self->{groups} })
$group->addChild( $doc->createAttribute("id", $main_grp) ); {
$root->appendChild( $group->toString($doc) );
for my $k (keys %{ $self->{ids} }) {
my $e = $doc->createElement("eval");
my $v = $doc->createElement("value");
$e->addChild( $doc->createAttribute("id", $k) );
$v->appendText( $self->{ids}{$k} );
$e->appendChild( $v );
$group->appendChild( $e );
} }
$root->appendChild( $group );
$doc->setDocumentElement( $root ); $doc->setDocumentElement( $root );
return $doc->toString(); return $doc->toString();
@ -214,6 +235,19 @@ sub append ($@)
push @{ $self->{groups} }, @_; push @{ $self->{groups} }, @_;
} }
sub delId
{
my $self = shift;
my $key = shift;
my $value = shift;
foreach my $item (@{ $self->{groups} })
{
if ($item->{id} eq )
$group->delId(@_);
}
}
sub getIds sub getIds
{ {
my $self = shift; my $self = shift;
@ -276,6 +310,21 @@ sub getWhos
return $ret; return $ret;
} }
sub toString($$)
{
my $self = shift;
my $doc = shift;
my $gr = $doc->createElement("group");
foreach my $item (@{ $self->{groups} })
{
$gr->appendChild( $item->toString() );
}
return $gr;
}
package Trace::Eval; package Trace::Eval;
@ -316,15 +365,8 @@ sub parseEval
if ($node->nodeName eq "value") if ($node->nodeName eq "value")
{ {
my $key; $self->addValue($node->getAttribute("id"),
if ($node->hasAttribute("id")) { $val);
$key = $node->getAttribute("id");
} else {
$key = "";
}
$self->{values}{ $key } = 0 if (!exists $self->{values}{ $key });
$self->{values}{ $key } += $val;
} }
elsif ($node->nodeName eq "name") elsif ($node->nodeName eq "name")
{ {
@ -369,6 +411,16 @@ sub getIds
return %ids; return %ids;
} }
sub addValue
{
my $self = shift;
my $key = shift // "";
my $val = shift;
$self->{values}{ $key } = 0 if (!exists $self->{values}{ $key });
$self->{values}{ $key } += $val;
}
sub getValue sub getValue
{ {
my $self = shift; my $self = shift;
@ -393,4 +445,25 @@ sub getWhos
return { $self->{id} => $self->{who} }; return { $self->{id} => $self->{who} };
} }
sub toString($$)
{
my $self = shift;
my $doc = shift;
my $e = $doc->createElement("eval");
$e->setAttribute("id", $self->{id});
$e->setAttribute("type", $self->{type});
for my $k (keys %{ $self->{values} })
{
my $v = $doc->createElement("value");
$v->setAttribute("id", $k) if ($k);
$v->appendTextNode( $self->{values}{$k} );
$e->appendChild( $v );
}
return $e;
}
1; 1;