forked from cgrates/cgrates
/
callcost.go
94 lines (83 loc) · 2.71 KB
/
callcost.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2013 ITsysCOM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"fmt"
"reflect"
"time"
)
// The output structure that will be returned with the call cost information.
type CallCost struct {
Direction, TOR, Tenant, Subject, Account, Destination string
Cost, ConnectFee float64
Timespans TimeSpans
}
// Pretty printing for call cost
func (cc *CallCost) String() (r string) {
r = fmt.Sprintf("%v[%v] : %s(%s) -> %s (", cc.Cost, cc.ConnectFee, cc.Subject, cc.Account, cc.Destination)
for _, ts := range cc.Timespans {
r += fmt.Sprintf(" %v,", ts.GetDuration())
}
r += " )"
return
}
// Merges the received timespan if they are similar (same activation period, same interval, same minute info.
func (cc *CallCost) Merge(other *CallCost) {
if len(cc.Timespans)-1 < 0 {
return
}
ts := cc.Timespans[len(cc.Timespans)-1]
otherTs := other.Timespans[0]
if reflect.DeepEqual(ts.ratingInfo, otherTs.ratingInfo) &&
reflect.DeepEqual(ts.RateInterval, otherTs.RateInterval) {
// extend the last timespan with
ts.TimeEnd = ts.TimeEnd.Add(otherTs.GetDuration())
// add the rest of the timspans
cc.Timespans = append(cc.Timespans, other.Timespans[1:]...)
} else {
// just add all timespans
cc.Timespans = append(cc.Timespans, other.Timespans...)
}
cc.Cost += other.Cost
}
func (cc *CallCost) GetStartTime() time.Time {
if len(cc.Timespans) == 0 {
return time.Now()
}
return cc.Timespans[0].TimeStart
}
func (cc *CallCost) GetEndTime() time.Time {
if len(cc.Timespans) == 0 {
return time.Now()
}
return cc.Timespans[len(cc.Timespans)-1].TimeEnd
}
func (cc *CallCost) GetDuration() (td time.Duration) {
for _, ts := range cc.Timespans {
td += ts.GetDuration()
}
return
}
// Creates a CallDescriptor structure copying related data from CallCost
func (cc *CallCost) CreateCallDescriptor() *CallDescriptor {
return &CallDescriptor{
Direction: cc.Direction,
TOR: cc.TOR,
Tenant: cc.Tenant,
Subject: cc.Subject,
Account: cc.Account,
Destination: cc.Destination,
}
}