VBTK/Objects/UpgradeRules.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 136 CC 14 SCAR 40.9
CC
14
Cov
0%
checkForUpgrade
132.0
new
20.0
getRuleText
2.0
CRAP 59.8
1 #! /bin/perl
2 #############################################################################
3 #
4 # NOTE: This file under revision control using RCS
5 # Any changes made without RCS will be lost
6 #
7 # $Source: /usr/local/cvsroot/vbtk/VBTK/Objects/UpgradeRules.pm,v $
8 # $Revision: 1.6 $
9 # $Date: 2002/03/04 20:53:07 $
10 # $Author: bhenry $
11 # $Locker: $
12 # $State: Exp $
13 #
14 # Purpose: A perl library used to manage VBObject upgrade rules.
15 #
16 # Copyright (C) 1996 - 2002 Brent Henry
17 #
18 # This program is free software; you can redistribute it and/or
19 # modify it under the terms of version 2 of the GNU General Public
20 # License as published by the Free Software Foundation available at:
21 # http://http://www.gnu.org/copyleft/gpl.html
22 #
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
27 #
28 #############################################################################
29 #
30 #
31 # REVISION HISTORY:
32 #
33 # $Log: UpgradeRules.pm,v $
34 # Revision 1.6 2002/03/04 20:53:07 bhenry
35 # *** empty log message ***
36 #
37 # Revision 1.5 2002/03/04 16:49:10 bhenry
38 # Changed requirement back to perl 5.6.0
39 #
40 # Revision 1.4 2002/03/02 00:53:55 bhenry
41 # Documentation updates
42 #
43 # Revision 1.3 2002/02/19 19:12:36 bhenry
44 # Changed to use unix time internally, to avoid DST problems
45 #
46 # Revision 1.2 2002/01/21 17:07:50 bhenry
47 # Disabled 'uninitialized' warnings
48 #
49 # Revision 1.1.1.1 2002/01/17 18:05:57 bhenry
50 # VBTK Project
51 #
52  
53 package VBTK::Objects::UpgradeRules;
54  
55 use 5.6.0;
56 use strict;
57 use warnings;
58 # I like using undef as a value so I'm turning off the uninitialized warnings
59 no warnings qw(uninitialized);
60  
61 use VBTK::Common;
62 use Date::Manip;
63  
64 our $VERBOSE=$ENV{'VERBOSE'};
65  
66 #-------------------------------------------------------------------------------
67 # Function: new
68 # Description: Object constructor. Allocates memory for all class members
69 # Input Parms: Configuration filename
70 # Output Parms: Pointer to class
71 #-------------------------------------------------------------------------------
72 sub new
73 {
74 my $type = shift;
75 my $self = {};
76 bless $self, $type;
77  
78 $self->set(@_);
79  
80 if($self->{RuleText} =~
81 /^Upgrade\s+to\s+(\w+)\s+if\s+(\w+)\s+occurs\s+(\d+)\s+times\s+in\s+(.*)$/i)
82 {
83 $self->{NewStatus} = $1;
84 $self->{TestStatus} = $2;
85 $self->{Occurs} = $3;
86 $self->{TimeWindow} = $4;
87 delete $self->{RuleText};
88 }
89 elsif($self->{RuleText} ne undef)
90 {
91 &error("Invalid upgrade rule - $self->{RuleText}");
92 return undef;
93 }
94  
95 # Setup a hash of default parameters
96 my $defaultParms = {
97 NewStatus => $::REQUIRED,
98 TestStatus => $::REQUIRED,
99 Occurs => $::REQUIRED,
100 TimeWindow => $::REQUIRED
101 };
102  
103 # Run a validation on the passed parms, using the default parms
104 $self->validateParms($defaultParms) || return undef;
105  
106 my ($temp,$key);
107  
108 # Check the status text and map them.
109 foreach $key ('NewStatus','TestStatus')
110 {
111 $temp = &map_status($self->{$key});
112 if ($temp eq undef)
113 {
114 &error("Invalid status '$self->{$key}' in UpgradeStatusRules");
115 return undef;
116 }
117 $self->{$key} = $temp;
118 }
119  
120 # Check the other values
121  
122  
123 $self;
124 }
125  
126 #-------------------------------------------------------------------------------
127 # Function: checkForUpgrade
128 # Description: Step through the passed history object list, checking to see if
129 # the status should be upgraded
130 # Input Parms: History Object List
131 # Output Parms: New status
132 #-------------------------------------------------------------------------------
133 sub checkForUpgrade
134 {
135 my $self = shift;
136 my ($status,$historyObjList) = @_;
137  
138 my $TestStatus = $self->{TestStatus};
139  
140 # If the status does not equal the test status then there's no point in testing
141 # this rule.
142 return $status if ($TestStatus ne $status);
143  
144 my $Occurs = $self->{Occurs};
145 my $TimeWindow = $self->{TimeWindow};
146 my $NewStatus = $self->{NewStatus};
147  
148 my $timeWindowSec = &deltaSec($TimeWindow) || return $status;
149 my $startTimestampSec = time - $timeWindowSec;
150  
151 my $count = 0;
152  
153 &log("Looking for $Occurs $TestStatus" . "'s since $startTimestampSec")
154 if ($VERBOSE > 2);
155  
156 my($itemStatus,$itemTimestamp,$itemRepeat,$itemRawStatus,$itemRepeatStart);
157 my($itemRepeatStartSec,$itemTimestampSec,$ratio,$inRangeCount,$histObj);
158  
159 # Step through each history item later than $start_timestamp
160 foreach $histObj (@{$historyObjList})
161 {
162 $itemStatus = $histObj->getStatus;
163 $itemTimestamp = $histObj->getTimestamp;
164 $itemRepeat = $histObj->getRepeated;
165 $itemRawStatus = $histObj->getRawStatus;
166 $itemRepeatStart = $histObj->getRepeatStart;
167  
168 # We're done if the history entry is earlier than the start of the
169 # time window.
170 last if ($itemTimestamp < $startTimestampSec);
171 &log("History entry $itemTimestamp, $itemRawStatus repeated $itemRepeat times")
172 if ($VERBOSE > 3);
173  
174 # If this history item matches the test status, then try to estimate how
175 # many of the 'repeated' count fell within the time window specified in
176 # the rule.
177 if ($itemRawStatus eq $TestStatus)
178 {
179 if($itemRepeatStart > $startTimestampSec)
180 {
181 $count += $itemRepeat;
182 &log("Adding $itemRepeat to $TestStatus count") if ($VERBOSE > 3);
183 }
184 else
185 {
186 $ratio = (($itemTimestamp - $startTimestampSec) /
187 ($itemTimestamp - $itemRepeatStart ));
188 $inRangeCount = int(($itemRepeat * $ratio) + 1);
189 $count += $inRangeCount;
190 &log("Adding $inRangeCount of $itemRepeat to $TestStatus count")
191 if ($VERBOSE > 3);
192 }
193 }
194 }
195  
196 # Just return if there are less than $occurs occurances of the current status
197 return $status if($count < $Occurs);
198  
199 # If we made it this far, then it's time to upgrade the status
200 &log("$TestStatus occurred $count times since $startTimestampSec, " .
201 "Upgrading status to '$NewStatus'") if ($VERBOSE > 2);
202  
203 # Add a header message to the most recent history object
204 $historyObjList->[0]->addHeaderMsg("Upgraded status to '$NewStatus' because " .
205 "status was set to $TestStatus $Occurs times in $TimeWindow\n");
206  
207 &find_higher_status($status,$NewStatus);
208 }
209  
210 #-------------------------------------------------------------------------------
211 # Function: getRuleText
212 # Description: Construct text to describe the rule
213 # Input Parms:
214 # Output Parms: Rule text
215 #-------------------------------------------------------------------------------
216 sub getRuleText
217 {
218 my $self = shift;
219  
220 "Upgrade to '$self->{NewStatus}' if '$self->{TestStatus}' occurs " .
221 "$self->{Occurs} times in $self->{TimeWindow}";
222 }
223  
224 # Simple Get Methods
225 sub getTestStatus { $_[0]->{TestStatus}; }
226 sub getNewStatus { $_[0]->{NewStatus}; }
227 sub getOccurs { $_[0]->{Occurs}; }
228 sub getTimeWindow { $_[0]->{TimeWindow}; }
229  
230 1;
231 __END__