blib/lib/Asterisk/LCR/Comparer/XERAND.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 182 CC 5 SCAR 16.4
CC
5
Cov
0%
sortme
12.0
_simulate_cost_per_call
12.0
normalize
2.0
CRAP 5.2
1 =head1 NAME
2  
3 Asterisk::LCR::Comparer::XERAND - More advanced rate comparer for Asterisk::LCR
4  
5  
6 =head1 SUMMARY
7  
8 This comparer is a bit cleverer than L<Asterisk::LCR::Comparer::Dummy>.
9  
10 It does currency conversion using Finance::Currency::Convert::XE, and then adjust
11 cost per minute using a traffic simulator.
12  
13 Currently the traffic simulator is a bit simplistic: it generates a call length between
14 0 and 200 seconds (to have an everage of 100 seconds call length).
15  
16 In the future there will be a more sophisticated simulator capable of running rates
17 against past traffic CDRs to measure real cost.
18  
19 =head1 ATTRIBUTES
20  
21 none.
22  
23  
24 =head1 METHODS
25  
26  
27 =cut
28 package Asterisk::LCR::Comparer::XERAND;
29 use base qw /Asterisk::LCR::Comparer/;
30 use Finance::Currency::Convert::XE;
31 use warnings;
32 use strict;
33  
34 our %CURRENCY_RATES = ();
35 our $XE = Finance::Currency::Convert::XE->new();
36 our $SUITE = undef;
37  
38  
39 sub normalize
40 {
41 my $self = shift;
42 my $rate = shift;
43
44 # fetch the rate itself
45 my $price = $rate->rate();
46 my $curr = $rate->currency();
47 my $cfee = $rate->connection_fee();
48 my $finc = $rate->first_increment();
49 my $ninc = $rate->increment();
50  
51 # overwrite the attributes with corrected rate
52 $SUITE ||= [ map { _random_normalized ($self->average(), $self->variance()) } 1..10000 ];
53 $price = _simulate_cost_suite ($SUITE, $price, $cfee, $finc, $ninc);
54 my $totsecs = 0;
55 for (@{$SUITE}) { $totsecs += $_ };
56 $price = int (10000 * 60 * ($price / $totsecs)) / 10000;
57
58 $rate->{rate} = $self->_convert ($price, $curr);
59 $rate->{currency} = $self->currency();
60 $rate->{increment} = 1;
61 $rate->{first_increment} = 1;
62 $rate->{connection_fee} = 0;
63 }
64  
65  
66 sub currency
67 {
68 my $self = shift;
69 return uc ($self->{currency}) || 'EUR';
70 }
71  
72  
73 # converts $amount of $currency into base currency
74 sub _convert
75 {
76 my $self = shift;
77 my $amount = shift;
78 my $currency = shift;
79 my $rate = $self->_fetch_rate ($currency);
80 return $amount * $rate;
81 }
82  
83  
84 sub _fetch_rate
85 {
86 my $self = shift;
87 my $cur = shift;
88 my $base_cur = $self->currency();
89 $cur eq $base_cur and return 1;
90 $CURRENCY_RATES{$cur} ||= $XE->convert (
91 source => $cur,
92 target => $base_cur,
93 value => 1,
94 format => 'number',
95 );
96
97 return $CURRENCY_RATES{$cur};
98 }
99  
100  
101 sub sortme
102 {
103 my $self = shift;
104 my $arg1 = shift;
105 my $arg2 = shift;
106 $arg1->{_is_normal} && $arg2->{_is_normal} && return $arg1->rate() <=> $arg2->rate();
107
108 my $rate1 = $arg1->rate();
109 my $rate2 = $arg2->rate();
110  
111 my $cur1 = $arg1->currency();
112 my $cur2 = $arg2->currency();
113  
114 $rate1 = $self->_convert ($rate1, $cur1);
115 $rate2 = $self->_convert ($rate2, $cur2);
116
117 my ($cost1, $cost2) = $self->_simulate_cost (
118 $rate1, $arg1->connection_fee(), $arg1->first_increment(), $arg1->increment(),
119 $rate2, $arg2->connection_fee(), $arg2->first_increment(), $arg2->increment()
120 );
121  
122 return +1 if ($cost1 > $cost2);
123 return -1 if ($cost2 > $cost1);
124 return 0;
125 }
126  
127  
128 sub average
129 {
130 my $self = shift;
131 return $self->{average} || 100;
132 }
133  
134  
135 sub variance
136 {
137 my $self = shift;
138 return $self->{variance} || 10000;
139 }
140  
141  
142 sub _simulate_cost
143 {
144 my $self = shift;
145
146 my $rate1 = shift;
147 my $conn1 = shift;
148 my $first_inc1 = shift;
149 my $next_inc1 = shift;
150  
151 my $rate2 = shift;
152 my $conn2 = shift;
153 my $first_inc2 = shift;
154 my $next_inc2 = shift;
155  
156 my $avg = $self->average();
157 my $std = $self->variance();
158 my $var = $std**2;
159  
160 $SUITE ||= [ map { _random_normalized ($avg, $var) } 1..10000 ];
161 my $cost1 = _simulate_cost_suite ($SUITE, $rate1, $conn1, $first_inc1, $next_inc1);
162 my $cost2 = _simulate_cost_suite ($SUITE, $rate2, $conn2, $first_inc2, $next_inc2);
163
164 return ($cost1, $cost2);
165 }
166  
167  
168 our %CACHE_SIMULATE_COST_SUITE = ();
169  
170 sub _simulate_cost_suite
171 {
172 my $SUITE = shift;
173 my $rate = shift;
174 my $conn = shift;
175 my $first_inc = shift;
176 my $next_inc = shift;
177
178 my $key = "$rate-$conn-$first_inc-$next_inc";
179
180 $CACHE_SIMULATE_COST_SUITE{$key} ||= do {
181 my $tot_len = 0;
182 my $cost = 0;
183 for my $length ( @{$SUITE} )
184 {
185 $cost += _simulate_cost_per_call ($length, $rate, $conn, $first_inc, $next_inc);
186 $tot_len += $length;
187 }
188
189 $cost;
190 };
191
192 return $CACHE_SIMULATE_COST_SUITE{$key};
193 }
194  
195  
196 sub _simulate_cost_per_call
197 {
198 my $length = shift;
199 my $rate = shift;
200 my $conn = shift;
201 my $first_inc = shift;
202 my $next_inc = shift;
203
204 my $bk = $length;
205 $length = $first_inc if ($length < $first_inc);
206 $length = $next_inc * ( 1 + int ($length / $next_inc) ) if ($length % $next_inc);
207 my $cost = $length * ($rate/60);
208 return $cost;
209 }
210  
211  
212 # algo from:
213 # http://psweb.sbs.ohio-state.edu/faculty/rtimpone/computer_resources/polar.htm
214 sub _random_normalized
215 {
216 return int (rand (200));
217  
218 # This was meant to be clever but It Doesn't Work, aaaargh!
219 # my $average = shift;
220 # my $variance = shift;
221 #
222 # my $v1 = 0;
223 # my $v2 = 0;
224 # my $s = 2;
225 # while ($s > 1)
226 # {
227 # # Step 1: Generate random numbers, U1 and U2
228 # # Step 2: Calculate V1, V2, and S
229 # $v1 = 2 * rand() - 1;
230 # $v2 = 2 * rand() - 1;
231 # $s = $v1 ** 2 + $v2 ** 2;
232 #
233 # # Step 3: If S=>1 get new values for U1 and U2
234 # # (go back to while loop)
235 # }
236 #
237 # # Step 4: Calculate normal
238 # my $z = (((-2 * log ($s)) / $s) ** (1/2)) * $v1;
239 # my $stdev = ($variance) ** (1/2);
240 # my $res = abs ( ($z * $stdev) + $average );
241 # if ($res > 6400) { return _random_normalized ($average, $variance) }
242 # else { return $res }
243 }
244  
245  
246 1;
247  
248  
249 __END__