[Bps-public-commit] r19430 - in RT-Extension-SLA/lib/RT/Extension: SLA

ruz at bestpractical.com ruz at bestpractical.com
Mon May 4 18:32:52 EDT 2009


Author: ruz
Date: Mon May  4 18:32:51 2009
New Revision: 19430

Added:
   RT-Extension-SLA/lib/RT/Extension/SLA/Summary.pm
Modified:
   RT-Extension-SLA/lib/RT/Extension/SLA.pm
   RT-Extension-SLA/lib/RT/Extension/SLA/Report.pm

Log:
* more changes here and there regarding reporting
* new Summary.pm to help combine multiple reports
  together

Modified: RT-Extension-SLA/lib/RT/Extension/SLA.pm
==============================================================================
--- RT-Extension-SLA/lib/RT/Extension/SLA.pm	(original)
+++ RT-Extension-SLA/lib/RT/Extension/SLA.pm	Mon May  4 18:32:51 2009
@@ -435,10 +435,12 @@
     return $RT::ServiceAgreements{'Default'};
 }
 
-sub ReportOnTicket {
+sub Report {
     my $self = shift;
-    my $id = shift;
+    my $ticket = shift;
 
+    require RT::Extension::SLA::Report;
+    return RT::Extension::SLA::Report->new( Ticket => $ticket )->Run;
 }
 
 =head1 TODO and CAVEATS

Modified: RT-Extension-SLA/lib/RT/Extension/SLA/Report.pm
==============================================================================
--- RT-Extension-SLA/lib/RT/Extension/SLA/Report.pm	(original)
+++ RT-Extension-SLA/lib/RT/Extension/SLA/Report.pm	Mon May  4 18:32:51 2009
@@ -19,6 +19,31 @@
     return $self;
 }
 
+sub Run {
+    my $self = shift;
+    my $txns = shift || $self->{'Ticket'}->Transactions;
+
+    my $state = $self->State;
+    my $handler = $self->Handlers;
+
+    while ( my $txn = $txns->Next ) {
+        my ($type, $field) = ($txn->Type, $txn->Field);
+
+        my $h = $handler->{ $type };
+        unless ( $h ) {
+            $RT::Logger->debug( "No handler for $type transaction, skipping" );
+        } elsif ( ref $h ) {
+            unless ( $h = $h->{ $field } ) {
+                $RT::Logger->debug( "No handler for ($type, $field) transaction, skipping" );
+            }
+        }
+        next unless $h;
+
+        $self->$h( Ticket => $self->{'Ticket'}, Transaction => $txn, State => $state );
+    }
+    return $self;
+}
+
 sub State {
     my $self = shift;
     return $self->{State};
@@ -41,49 +66,27 @@
             Owner => 'OnOwnerChange',
         },
         Correspond => 'OnResponse',
-        CustomField => { map $_ => 'OnServiceLevelChange', $self->ServiceLevelCustomFields },
+        CustomField => { map { $_->id => 'OnServiceLevelChange' } $self->ServiceLevelCustomFields },
         AddWatcher => { Requestor => 'OnRequestorChange' },
         DelWatcher => { Requestor => 'OnRequestorChange' },
     };
 
+    use Data::Dumper;
+    Test::More::diag( Dumper $cache );
+
     return $cache;
 } }
 
-sub Run {
-    my $self = shift;
-    my $txns = shift || $self->{'Ticket'}->Transactions;
-
-    my $state = $self->State;
-    my $handler = $self->Handlers;
-
-    while ( my $txn = $txns->Next ) {
-        my ($type, $field) = ($txn->Type, $txn->Field);
-
-        my $h = $handler->{ $type };
-        unless ( $h ) {
-            $RT::Logger->debug( "No handler for $type transaction, skipping" );
-        } elsif ( ref $h ) {
-            unless ( $h = $h->{ $field } ) {
-                $RT::Logger->debug( "No handler for ($type, $field) transaction, skipping" );
-            }
-        }
-        next unless $h;
-
-        $self->$h( Ticket => $self->{'Ticket'}, Transaction => $txn, State => $state );
-    }
-    return $self;
-}
-
 sub OnCreate {
     my $self = shift;
     my %args = ( Ticket => undef, Transaction => undef, State => undef, @_);
 
     my $state = $args{'State'};
     %$state = ();
-    $state->{'level'} = $self->InitialServiceLevel( $args{'Ticket'} );
-    $state->{'requestors'} = [ $self->InitialRequestors( $args{'Ticket'} ) ];
-    $state->{'owner'} = $self->InitialOwner( $args{'Ticket'} );
-    return;
+    $state->{'level'} = $self->InitialServiceLevel( Ticket => $args{'Ticket'} );
+    $state->{'requestors'} = [ $self->InitialRequestors( Ticket => $args{'Ticket'} ) ];
+    $state->{'owner'} = $self->InitialOwner( Ticket => $args{'Ticket'} );
+    return $self->OnResponse( %args );
 }
 
 sub OnRequestorChange {
@@ -100,15 +103,21 @@
     }
 }
 
+sub OnServiceLevelChange {
+    my $self = shift;
+    my %args = ( Ticket => undef, Transaction => undef, State => undef, @_);
+    $self->State->{'level'} = $args{'Transaction'}->NewValue;
+}
+
 sub OnResponse {
     my $self = shift;
     my %args = ( Ticket => undef, Transaction => undef, State => undef, @_);
 
     my $txn = $args{'Transaction'};
-    unless ( $args{'State'}->{'level'} ) {
-        $RT::Logger->debug('No service level -> ignore txn #'. $txn->id );
-        return;
-    }
+#    unless ( $args{'State'}->{'level'} ) {
+#        $RT::Logger->debug('No service level -> ignore txn #'. $txn->id );
+#        return;
+#    }
 
     my $act = $args{'State'}->{'act'};
     if ( $self->IsRequestorsAct( $txn ) ) {
@@ -143,6 +152,7 @@
                 owner     => $args{'State'}->{'owner'},
                 failed    => $failed,
                 owner_act => $owner,
+                actor     => $txn->Creator,
                 shift     => $txn->CreatedObj->Unix - $deadline,                
             };
             push @{ $self->Stats }, $stat;
@@ -152,22 +162,25 @@
             my $deadline = RT::Extension::SLA->Due(
                 Type  => 'Response',
                 Level => $args{'State'}->{'level'},
-                Time  => $args{'State'}->{'acted'},
+                Time  => $args{'State'}->{'act'}->{'acted'},
             );
             unless ( defined $deadline ) {
                 $RT::Logger->debug( "Non-requestors' reply after requestors', without response deadline");
                 return;
             }
 
+            Test::More::diag( 'deadline '. $deadline .' '. Dumper( $args{'State'} ) );
+
             # repsonse
             my $failed = $txn->CreatedObj->Unix > $deadline? 1 : 0;
             my $owner = $args{'State'}->{'owner'} == $txn->Creator? 1 : 0;
             my $stat = {
-                type      => 'KeepInLoop',
+                type      => 'Response',
                 owner     => $args{'State'}->{'owner'},
                 failed    => $failed,
                 owner_act => $owner,
-                shift     => $txn->CreatedObj->Unix - $deadline,                
+                actor     => $txn->Creator,
+                shift     => ($txn->CreatedObj->Unix - $deadline),
             };
             push @{ $self->Stats }, $stat;
         }
@@ -195,23 +208,23 @@
 
 sub InitialServiceLevel {
     my $self = shift;
-    my $ticket = shift;
+    my %args = @_;
 
     return $self->InitialValue(
-        Ticket   => $ticket,
-        Current  => $ticket->FirstCustomFieldValue('SLA'),
+        Ticket   => $args{'Ticket'},
+        Current  => $args{'Ticket'}->FirstCustomFieldValue('SLA'),
         Criteria => { CustomField => [ map $_->id, $self->ServiceLevelCustomFields ] },
     );
 }
 
 sub InitialRequestors {
     my $self = shift;
-    my $ticket = shift;
+    my %args = @_;
 
-    my @current = map $_->Member, @{ $ticket->Requestors->MembersObj->ItemsArrayRef };
+    my @current = map $_->MemberId, @{ $args{'Ticket'}->Requestors->MembersObj->ItemsArrayRef };
 
     my $txns = $self->Transactions(
-        Ticket => $ticket,
+        Ticket => $args{'Ticket'},
         Order => 'DESC',
         Criteria => { 'AddWatcher' => 'Requestor', DelWatcher => 'Requestor' },
     );
@@ -302,9 +315,7 @@
     $cfs->Limit( FIELD => 'LookupType', VALUE => RT::Ticket->CustomFieldLookupType );
     # XXX: limit to applied custom fields only
 
-    push @cache, $_ while $_ = $cfs->Next;
-
-    return @cache;
+    return @cache = @{ $cfs->ItemsArrayRef };
 } }
 
 1;

Added: RT-Extension-SLA/lib/RT/Extension/SLA/Summary.pm
==============================================================================
--- (empty file)
+++ RT-Extension-SLA/lib/RT/Extension/SLA/Summary.pm	Mon May  4 18:32:51 2009
@@ -0,0 +1,68 @@
+use 5.8.0;
+use strict;
+use warnings;
+
+package RT::Extension::SLA::Summary;
+
+sub new {
+    my $proto = shift;
+    my $self = bless {}, ref($proto)||$proto;
+    return $self->init( @_ );
+}
+
+sub init {
+    my $self = shift;
+    return $self;
+}
+
+sub Result {
+    my $self = shift;
+    return $self->{'Result'} ||= { };
+}
+
+sub AddReport {
+    my $self = shift;
+    my $report = shift;
+
+    my $new = $self->OnReport( $report );
+
+    my $total = $self->Result;
+    while ( my ($user, $stat) = each %$new ) {
+        my $tmp = $total->{$user} ||= {};
+        while ( my ($action, $count) = each %$stat ) {
+            $tmp->{$action} += $count;
+        }
+    }
+
+    return $self;
+}
+
+sub OnReport {
+    my $self = shift;
+    my $report = shift;
+
+    my $res = {};
+    foreach my $stat ( @{ $report->Stats } ) {
+        if ( $stat->{'owner_act'} ) {
+            my $owner = $res->{ $stat->{'owner'} } ||= { };
+            if ( $stat->{'failed'} ) {
+                $owner->{'failed'}++;
+            } else {
+                $owner->{'passed'}++;
+            }
+        } else {
+            my $owner = $res->{ $stat->{'owner'} } ||= { };
+            my $actor = $res->{ $stat->{'actor'} } ||= { };
+            if ( $stat->{'failed'} ) {
+                $owner->{'failed'}++;
+                $actor->{'late help'}++;
+            } else {
+                $owner->{'got help'}++;
+                $actor->{'helped'}++;
+            }
+        }
+    }
+    return $res;
+}
+
+1;



More information about the Bps-public-commit mailing list