/
eventcost.go
1136 lines (1090 loc) · 34 KB
/
eventcost.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
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 (
"errors"
"fmt"
"net"
"time"
"github.com/cgrates/cgrates/utils"
)
// NewBareEventCost will initialize the EventCost with minimum information
func NewBareEventCost() *EventCost {
return &EventCost{
Rating: make(Rating),
Accounting: make(Accounting),
RatingFilters: make(RatingFilters),
Rates: make(ChargedRates),
Timings: make(ChargedTimings),
Charges: make([]*ChargingInterval, 0),
cache: utils.MapStorage{},
}
}
// NewEventCostFromCallCost will initilaize the EventCost from a CallCost
func NewEventCostFromCallCost(cc *CallCost, cgrID, runID string) (ec *EventCost) {
ec = NewBareEventCost()
ec.CGRID = cgrID
ec.RunID = runID
ec.AccountSummary = cc.AccountSummary
if len(cc.Timespans) != 0 {
ec.Charges = make([]*ChargingInterval, len(cc.Timespans))
ec.StartTime = cc.Timespans[0].TimeStart
}
for i, ts := range cc.Timespans {
cIl := &ChargingInterval{CompressFactor: ts.CompressFactor}
rf := RatingMatchedFilters{"Subject": ts.MatchedSubject, "DestinationPrefix": ts.MatchedPrefix,
"DestinationID": ts.MatchedDestId, "RatingPlanID": ts.RatingPlanId}
cIl.RatingID = ec.ratingIDForRateInterval(ts.RateInterval, rf)
if len(ts.Increments) != 0 {
cIl.Increments = make([]*ChargingIncrement, 0, len(ts.Increments)+1)
}
for _, incr := range ts.Increments {
cIl.Increments = append(cIl.Increments, ec.newChargingIncrement(incr, rf, false))
}
if ts.RoundIncrement != nil {
cIl.Increments = append(cIl.Increments, ec.newChargingIncrement(ts.RoundIncrement, rf, true))
}
ec.Charges[i] = cIl
}
return
}
// newChargingIncrement creates ChargingIncrement from a Increment
// special case if is the roundIncrement the rateID is *rounding
func (ec *EventCost) newChargingIncrement(incr *Increment, rf RatingMatchedFilters, roundedIncrement bool) (cIt *ChargingIncrement) {
cIt = &ChargingIncrement{
Usage: incr.Duration,
Cost: incr.Cost,
CompressFactor: incr.CompressFactor,
}
if incr.BalanceInfo == nil {
return
}
rateID := utils.MetaRounding
//AccountingID
if incr.BalanceInfo.Unit != nil {
// 2 balances work-around
ecUUID := utils.META_NONE // populate no matter what due to Unit not nil
if incr.BalanceInfo.Monetary != nil {
if !roundedIncrement {
rateID = ec.ratingIDForRateInterval(incr.BalanceInfo.Monetary.RateInterval, rf)
}
if uuid := ec.Accounting.GetIDWithSet(
&BalanceCharge{
AccountID: incr.BalanceInfo.AccountID,
BalanceUUID: incr.BalanceInfo.Monetary.UUID,
Units: incr.Cost,
RatingID: rateID,
}); uuid != "" {
ecUUID = uuid
}
}
if !roundedIncrement {
rateID = ec.ratingIDForRateInterval(incr.BalanceInfo.Unit.RateInterval, rf)
}
cIt.AccountingID = ec.Accounting.GetIDWithSet(
&BalanceCharge{
AccountID: incr.BalanceInfo.AccountID,
BalanceUUID: incr.BalanceInfo.Unit.UUID,
Units: incr.BalanceInfo.Unit.Consumed,
RatingID: rateID,
ExtraChargeID: ecUUID,
})
} else if incr.BalanceInfo.Monetary != nil { // Only monetary
if !roundedIncrement {
rateID = ec.ratingIDForRateInterval(incr.BalanceInfo.Monetary.RateInterval, rf)
}
cIt.AccountingID = ec.Accounting.GetIDWithSet(
&BalanceCharge{
AccountID: incr.BalanceInfo.AccountID,
BalanceUUID: incr.BalanceInfo.Monetary.UUID,
Units: incr.Cost,
RatingID: rateID,
})
}
return
}
// EventCost stores cost for an Event
type EventCost struct {
CGRID string
RunID string
StartTime time.Time
Usage *time.Duration
Cost *float64 // pointer so we can nil it when dirty
Charges []*ChargingInterval
AccountSummary *AccountSummary // Account summary at the end of the event calculation
Rating Rating
Accounting Accounting
RatingFilters RatingFilters
Rates ChargedRates
Timings ChargedTimings
cache utils.MapStorage
}
func (ec *EventCost) initCache() {
if ec != nil {
ec.cache = utils.MapStorage{}
}
}
func (ec *EventCost) ratingIDForRateInterval(ri *RateInterval, rf RatingMatchedFilters) string {
if ri == nil || ri.Rating == nil {
return ""
}
var rfUUID string
if rf != nil {
rfUUID = ec.RatingFilters.GetIDWithSet(rf)
}
var tmID string
if ri.Timing != nil {
tmID = ec.Timings.GetIDWithSet(
&ChargedTiming{
Years: ri.Timing.Years,
Months: ri.Timing.Months,
MonthDays: ri.Timing.MonthDays,
WeekDays: ri.Timing.WeekDays,
StartTime: ri.Timing.StartTime})
}
var rtUUID string
if len(ri.Rating.Rates) != 0 {
rtUUID = ec.Rates.GetIDWithSet(ri.Rating.Rates)
}
return ec.Rating.GetIDWithSet(
&RatingUnit{
ConnectFee: ri.Rating.ConnectFee,
RoundingMethod: ri.Rating.RoundingMethod,
RoundingDecimals: ri.Rating.RoundingDecimals,
MaxCost: ri.Rating.MaxCost,
MaxCostStrategy: ri.Rating.MaxCostStrategy,
TimingID: tmID,
RatesID: rtUUID,
RatingFiltersID: rfUUID})
}
func (ec *EventCost) rateIntervalForRatingID(ratingID string) (ri *RateInterval) {
if ratingID == "" {
return
}
cIlRU := ec.Rating[ratingID]
ri = new(RateInterval)
ri.Rating = &RIRate{ConnectFee: cIlRU.ConnectFee,
RoundingMethod: cIlRU.RoundingMethod,
RoundingDecimals: cIlRU.RoundingDecimals,
MaxCost: cIlRU.MaxCost, MaxCostStrategy: cIlRU.MaxCostStrategy}
if cIlRU.RatesID != "" {
ri.Rating.Rates = ec.Rates[cIlRU.RatesID]
}
if cIlRU.TimingID != "" {
cIlTm := ec.Timings[cIlRU.TimingID]
ri.Timing = &RITiming{Years: cIlTm.Years, Months: cIlTm.Months, MonthDays: cIlTm.MonthDays,
WeekDays: cIlTm.WeekDays, StartTime: cIlTm.StartTime}
}
return
}
// Clone will create a clone of the object
func (ec *EventCost) Clone() (cln *EventCost) {
if ec == nil {
return
}
cln = new(EventCost)
cln.initCache()
cln.CGRID = ec.CGRID
cln.RunID = ec.RunID
cln.StartTime = ec.StartTime
if ec.Usage != nil {
cln.Usage = utils.DurationPointer(*ec.Usage)
}
if ec.Cost != nil {
cln.Cost = utils.Float64Pointer(*ec.Cost)
}
if ec.Charges != nil {
cln.Charges = make([]*ChargingInterval, len(ec.Charges))
for i, cIl := range ec.Charges {
cln.Charges[i] = cIl.Clone()
}
}
if ec.AccountSummary != nil {
cln.AccountSummary = ec.AccountSummary.Clone()
}
if ec.Rating != nil {
cln.Rating = ec.Rating.Clone()
}
if ec.Accounting != nil {
cln.Accounting = ec.Accounting.Clone()
}
if ec.RatingFilters != nil {
cln.RatingFilters = ec.RatingFilters.Clone()
}
if ec.Rates != nil {
cln.Rates = ec.Rates.Clone()
}
if ec.Timings != nil {
cln.Timings = ec.Timings.Clone()
}
return
}
// Compute aggregates all the compute methods on EventCost
func (ec *EventCost) Compute() {
ec.GetUsage()
ec.ComputeEventCostUsageIndexes()
ec.GetCost()
}
// ResetCounters will reset all the computed cached values
func (ec *EventCost) ResetCounters() {
ec.Cost = nil
ec.Usage = nil
for _, cIl := range ec.Charges {
cIl.cost = nil
cIl.usage = nil
cIl.ecUsageIdx = nil
}
}
// GetCost iterates through Charges, computing EventCost.Cost
func (ec *EventCost) GetCost() float64 {
if ec.Cost == nil {
var cost float64
for _, ci := range ec.Charges {
cost += ci.TotalCost()
}
cost = utils.Round(cost, globalRoundingDecimals, utils.ROUNDING_MIDDLE)
ec.Cost = &cost
}
return *ec.Cost
}
// GetUsage iterates through Charges, computing EventCost.Usage
func (ec *EventCost) GetUsage() time.Duration {
if ec.Usage == nil {
var usage time.Duration
for _, ci := range ec.Charges {
usage += time.Duration(ci.Usage().Nanoseconds() * int64(ci.CompressFactor))
}
ec.Usage = &usage
}
return *ec.Usage
}
// ComputeEventCostUsageIndexes will iterate through Chargers and populate their ecUsageIdx
func (ec *EventCost) ComputeEventCostUsageIndexes() {
var totalUsage time.Duration
for _, cIl := range ec.Charges {
if cIl.ecUsageIdx == nil {
cIl.ecUsageIdx = utils.DurationPointer(totalUsage)
}
totalUsage += time.Duration(cIl.Usage().Nanoseconds() * int64(cIl.CompressFactor))
}
}
// AsRefundIncrements converts an EventCost into a CallDescriptor
func (ec *EventCost) AsRefundIncrements(tor string) (cd *CallDescriptor) {
cd = &CallDescriptor{
CgrID: ec.CGRID,
RunID: ec.RunID,
ToR: tor,
TimeStart: ec.StartTime,
TimeEnd: ec.StartTime.Add(ec.GetUsage()),
DurationIndex: ec.GetUsage(),
}
if len(ec.Charges) == 0 {
return
}
var nrIcrms int
for _, cIl := range ec.Charges {
nrIcrms += (cIl.CompressFactor * len(cIl.Increments))
}
cd.Increments = make(Increments, nrIcrms)
var iIdx int
for _, cIl := range ec.Charges {
for i := 0; i < cIl.CompressFactor; i++ {
for _, cIcrm := range cIl.Increments {
cd.Increments[iIdx] = &Increment{
Cost: cIcrm.Cost,
Duration: cIcrm.Usage,
CompressFactor: cIcrm.CompressFactor,
}
if cIcrm.AccountingID != utils.EmptyString {
cd.Increments[iIdx].BalanceInfo = &DebitInfo{
AccountID: ec.Accounting[cIcrm.AccountingID].AccountID,
}
blncSmry := ec.AccountSummary.BalanceSummaries.BalanceSummaryWithUUD(ec.Accounting[cIcrm.AccountingID].BalanceUUID)
if blncSmry.Type == utils.MONETARY {
cd.Increments[iIdx].BalanceInfo.Monetary = &MonetaryInfo{UUID: blncSmry.UUID}
} else if utils.NonMonetaryBalances.Has(blncSmry.Type) {
cd.Increments[iIdx].BalanceInfo.Unit = &UnitInfo{UUID: blncSmry.UUID}
}
if ec.Accounting[cIcrm.AccountingID].ExtraChargeID == utils.META_NONE ||
ec.Accounting[cIcrm.AccountingID].ExtraChargeID == utils.EmptyString {
iIdx++
continue
}
// extra charges, ie: non-free *voice
extraSmry := ec.AccountSummary.BalanceSummaries.BalanceSummaryWithUUD(
ec.Accounting[ec.Accounting[cIcrm.AccountingID].ExtraChargeID].BalanceUUID)
if extraSmry.Type == utils.MONETARY {
cd.Increments[iIdx].BalanceInfo.Monetary = &MonetaryInfo{UUID: extraSmry.UUID}
} else if utils.NonMonetaryBalances.Has(blncSmry.Type) {
cd.Increments[iIdx].BalanceInfo.Unit = &UnitInfo{UUID: extraSmry.UUID}
}
}
iIdx++
}
}
}
return
}
// AsCallCost converts an EventCost into a CallCost
func (ec *EventCost) AsCallCost(tor string) *CallCost {
cc := &CallCost{
ToR: utils.FirstNonEmpty(tor, utils.VOICE),
Cost: ec.GetCost(),
RatedUsage: float64(ec.GetUsage().Nanoseconds()),
AccountSummary: ec.AccountSummary,
}
cc.Timespans = make(TimeSpans, len(ec.Charges))
for i, cIl := range ec.Charges {
ts := &TimeSpan{
Cost: cIl.Cost(),
DurationIndex: *cIl.Usage(),
CompressFactor: cIl.CompressFactor,
}
if cIl.ecUsageIdx == nil { // index was not populated yet
ec.ComputeEventCostUsageIndexes()
}
ts.TimeStart = ec.StartTime.Add(*cIl.ecUsageIdx)
ts.TimeEnd = ts.TimeStart.Add(
time.Duration(cIl.Usage().Nanoseconds() * int64(cIl.CompressFactor)))
if cIl.RatingID != "" &&
ec.Rating[cIl.RatingID].RatingFiltersID != "" {
rfs := ec.RatingFilters[ec.Rating[cIl.RatingID].RatingFiltersID]
ts.MatchedSubject = rfs[utils.Subject].(string)
ts.MatchedPrefix = rfs[utils.DestinationPrefix].(string)
ts.MatchedDestId = rfs[utils.DestinationID].(string)
ts.RatingPlanId = rfs[utils.RatingPlanID].(string)
}
ts.RateInterval = ec.rateIntervalForRatingID(cIl.RatingID)
incrs := cIl.Increments
if l := len(cIl.Increments); l != 0 {
if cIl.Increments[l-1].Cost != 0 &&
ec.Accounting[cIl.Increments[l-1].AccountingID].RatingID == utils.MetaRounding {
// special case: if the last increment has the ratingID equal to *roundig
// we consider it as the roundIncrement
l--
incrs = incrs[:l]
ts.RoundIncrement = ec.newIntervalFromCharge(cIl.Increments[l-1])
}
ts.Increments = make(Increments, l)
}
for j, cInc := range incrs {
ts.Increments[j] = ec.newIntervalFromCharge(cInc)
}
cc.Timespans[i] = ts
}
return cc
}
// newIntervalFromCharge creates Increment from a ChargingIncrement
func (ec *EventCost) newIntervalFromCharge(cInc *ChargingIncrement) (incr *Increment) {
incr = &Increment{
Duration: cInc.Usage,
Cost: cInc.Cost,
CompressFactor: cInc.CompressFactor,
BalanceInfo: new(DebitInfo),
}
if len(cInc.AccountingID) == 0 {
return
}
cBC := ec.Accounting[cInc.AccountingID]
incr.BalanceInfo.AccountID = cBC.AccountID
var balanceType string
if cBC.BalanceUUID != "" {
if ec.AccountSummary != nil {
for _, b := range ec.AccountSummary.BalanceSummaries {
if b.UUID == cBC.BalanceUUID {
balanceType = b.Type
break
}
}
}
}
if utils.SliceHasMember([]string{utils.DATA, utils.VOICE}, balanceType) && cBC.ExtraChargeID == "" {
cBC.ExtraChargeID = utils.META_NONE // mark the balance to be exported as Unit type
}
if cBC.ExtraChargeID != "" { // have both monetary and data
// Work around, enforce logic with 2 balances for *voice/*monetary combination
// so we can stay compatible with CallCost
incr.BalanceInfo.Unit = &UnitInfo{UUID: cBC.BalanceUUID, Consumed: cBC.Units}
incr.BalanceInfo.Unit.RateInterval = ec.rateIntervalForRatingID(cBC.RatingID)
if cBC.ExtraChargeID != utils.META_NONE {
cBC = ec.Accounting[cBC.ExtraChargeID] // overwrite original balance so we can process it in one place
}
}
if cBC.ExtraChargeID != utils.META_NONE {
incr.BalanceInfo.Monetary = &MonetaryInfo{UUID: cBC.BalanceUUID}
incr.BalanceInfo.Monetary.RateInterval = ec.rateIntervalForRatingID(cBC.RatingID)
}
return
}
// ratingGetIDFomEventCost retrieves UUID based on data from another EventCost
func (ec *EventCost) ratingGetIDFomEventCost(oEC *EventCost, oRatingID string) string {
if oRatingID == "" {
return ""
}
oCIlRating := oEC.Rating[oRatingID].Clone() // clone so we don't influence the original data
oCIlRating.TimingID = ec.Timings.GetIDWithSet(oEC.Timings[oCIlRating.TimingID])
oCIlRating.RatingFiltersID = ec.RatingFilters.GetIDWithSet(oEC.RatingFilters[oCIlRating.RatingFiltersID])
oCIlRating.RatesID = ec.Rates.GetIDWithSet(oEC.Rates[oCIlRating.RatesID])
return ec.Rating.GetIDWithSet(oCIlRating)
}
// accountingGetIDFromEventCost retrieves UUID based on data from another EventCost
func (ec *EventCost) accountingGetIDFromEventCost(oEC *EventCost, oAccountingID string) string {
if oAccountingID == "" || oAccountingID == utils.META_NONE {
return ""
}
oBC := oEC.Accounting[oAccountingID].Clone()
oBC.RatingID = ec.ratingGetIDFomEventCost(oEC, oBC.RatingID)
oBC.ExtraChargeID = ec.accountingGetIDFromEventCost(oEC, oBC.ExtraChargeID)
return ec.Accounting.GetIDWithSet(oBC)
}
// appendCIl appends a ChargingInterval to existing chargers
// no compression done at ChargingInterval level, attempted on ChargingIncrement level
func (ec *EventCost) appendCIlFromEC(oEC *EventCost, cIlIdx int) {
cIl := oEC.Charges[cIlIdx]
cIlCln := cIl.Clone() // add/modify data on clone instead of original
cIlCln.RatingID = ec.ratingGetIDFomEventCost(oEC, cIl.RatingID)
lastCIl := ec.Charges[len(ec.Charges)-1]
lastCIt := lastCIl.Increments[len(lastCIl.Increments)-1]
appendChargingIncrement := lastCIl.CompressFactor == 1 &&
lastCIl.RatingID == cIlCln.RatingID // attempt compressing of the ChargingIncrements
var idxFirstCIt *int // keep here the reference towards last not appended charging increment so we can create separate ChargingInterval
var idxLastCF *int // reference towards last compress not absorbed by ec.Charges
for cF := cIl.CompressFactor; cF > 0; cF-- {
for i, cIt := range cIl.Increments {
cIlCln.Increments[i].AccountingID = ec.accountingGetIDFromEventCost(oEC, cIt.AccountingID)
if idxFirstCIt != nil {
continue
}
if !appendChargingIncrement ||
!lastCIt.PartiallyEquals(cIlCln.Increments[i]) {
idxFirstCIt = utils.IntPointer(i)
idxLastCF = utils.IntPointer(cF)
continue
}
lastCIt.CompressFactor += cIt.CompressFactor // compress the iterated ChargingIncrement
}
}
if lastCIl.PartiallyEquals(cIlCln) { // the two CIls are equal, compress the original one
lastCIl.CompressFactor += cIlCln.CompressFactor
return
}
if idxFirstCIt != nil { // CIt was not completely absorbed
cIl.RatingID = cIlCln.RatingID // reuse cIl so we don't clone again
cIl.CompressFactor = 1
cIl.Increments = cIlCln.Increments[*idxFirstCIt:]
ec.Charges = append(ec.Charges, cIl)
if *idxLastCF > 1 { // add the remaining part out of original ChargingInterval
cIlCln.CompressFactor = *idxLastCF - 1
// append the cloned charge in order to not keep references
// of the increments used for previous charge
ec.Charges = append(ec.Charges, cIlCln.Clone())
}
}
}
// AppendChargingInterval appends or compresses a &ChargingInterval to existing ec.Chargers
func (ec *EventCost) appendChargingIntervalFromEventCost(oEC *EventCost, cIlIdx int) {
lenChargers := len(ec.Charges)
if lenChargers != 0 && ec.Charges[lenChargers-1].PartiallyEquals(oEC.Charges[cIlIdx]) {
ec.Charges[lenChargers-1].CompressFactor++
} else {
ec.appendCIlFromEC(oEC, cIlIdx)
}
}
// SyncKeys will sync the keys present into ec with the ones in refEC
func (ec *EventCost) SyncKeys(refEC *EventCost) {
// sync RatingFilters
sncedRFilterIDs := make(map[string]string)
for key, rf := range ec.RatingFilters {
for refKey, refRf := range refEC.RatingFilters {
if rf.Equals(refRf) {
delete(ec.RatingFilters, key)
sncedRFilterIDs[key] = refKey
ec.RatingFilters[refKey] = rf
break
}
}
}
// sync Rates
sncedRateIDs := make(map[string]string)
for key, rt := range ec.Rates {
for refKey, refRt := range refEC.Rates {
if rt.Equals(refRt) {
delete(ec.Rates, key)
sncedRateIDs[key] = refKey
ec.Rates[refKey] = rt
break
}
}
}
// sync Timings
sncedTimingIDs := make(map[string]string)
for key, tm := range ec.Timings {
for refKey, refTm := range refEC.Timings {
if tm.Equals(refTm) {
delete(ec.Timings, key)
sncedTimingIDs[key] = refKey
ec.Timings[refKey] = tm
break
}
}
}
// sync Rating
sncedRatingIDs := make(map[string]string)
for key, ru := range ec.Rating {
if tmRefKey, has := sncedTimingIDs[ru.TimingID]; has {
ru.TimingID = tmRefKey
}
if rtRefID, has := sncedRateIDs[ru.RatesID]; has {
ru.RatesID = rtRefID
}
if rfRefID, has := sncedRFilterIDs[ru.RatingFiltersID]; has {
ru.RatingFiltersID = rfRefID
}
for refKey, refRU := range refEC.Rating {
if ru.Equals(refRU) {
delete(ec.Rating, key)
sncedRatingIDs[key] = refKey
ec.Rating[refKey] = ru
break
}
}
}
// sync Accounting
sncedAcntIDs := make(map[string]string)
for key, acnt := range ec.Accounting {
if rtRefKey, has := sncedRatingIDs[acnt.RatingID]; has {
acnt.RatingID = rtRefKey
}
for refKey, refAcnt := range refEC.Accounting {
if acnt.Equals(refAcnt) {
delete(ec.Accounting, key)
sncedAcntIDs[key] = refKey
ec.Accounting[refKey] = acnt
break
}
}
}
// correct the ExtraCharge
for _, acnt := range ec.Accounting {
if acntRefID, has := sncedAcntIDs[acnt.ExtraChargeID]; has {
acnt.ExtraChargeID = acntRefID
}
}
// need another sync for the corrected ExtraChargeIDs
for key, acnt := range ec.Accounting {
for refKey, refAcnt := range refEC.Accounting {
if acnt.Equals(refAcnt) {
delete(ec.Accounting, key)
sncedAcntIDs[key] = refKey
ec.Accounting[refKey] = acnt
break
}
}
}
// sync Charges
for _, ci := range ec.Charges {
if refRatingID, has := sncedRatingIDs[ci.RatingID]; has {
ci.RatingID = refRatingID
}
for _, cIcrmt := range ci.Increments {
if refAcntID, has := sncedAcntIDs[cIcrmt.AccountingID]; has {
cIcrmt.AccountingID = refAcntID
}
}
}
}
// Merge will merge a list of EventCosts into this one
func (ec *EventCost) Merge(ecs ...*EventCost) {
for _, newEC := range ecs {
newEC.AccountSummary.UpdateBalances(ec.AccountSummary)
ec.AccountSummary = newEC.AccountSummary // updated AccountSummary information
for cIlIdx := range newEC.Charges {
ec.appendChargingIntervalFromEventCost(newEC, cIlIdx)
}
}
ec.ResetCounters()
}
// RemoveStaleReferences iterates through cached data and makes sure it is still referenced from Charging
func (ec *EventCost) RemoveStaleReferences() {
// RatingIDs
for key := range ec.Rating {
var keyUsed bool
for _, cIl := range ec.Charges {
if cIl.RatingID == key {
keyUsed = true
break
}
}
if !keyUsed { // look also in accounting for references
for _, bc := range ec.Accounting {
if bc.RatingID == key {
keyUsed = true
break
}
}
}
if !keyUsed { // not used, remove it
delete(ec.Rating, key)
}
}
for key := range ec.Accounting {
var keyUsed bool
for _, cIl := range ec.Charges {
for _, cIt := range cIl.Increments {
if cIt.AccountingID == key {
keyUsed = true
break
}
}
if !keyUsed {
for _, bCharge := range ec.Accounting {
if bCharge.ExtraChargeID == key {
keyUsed = true
break
}
}
}
}
if !keyUsed {
delete(ec.Accounting, key)
}
}
for key := range ec.RatingFilters {
var keyUsed bool
for _, ru := range ec.Rating {
if ru.RatingFiltersID == key {
keyUsed = true
break
}
}
if !keyUsed {
delete(ec.RatingFilters, key)
}
}
for key := range ec.Rates {
var keyUsed bool
for _, ru := range ec.Rating {
if ru.RatesID == key {
keyUsed = true
break
}
}
if !keyUsed {
delete(ec.Rates, key)
}
}
for key := range ec.Timings {
var keyUsed bool
for _, ru := range ec.Rating {
if ru.TimingID == key {
keyUsed = true
break
}
}
if !keyUsed {
delete(ec.Rates, key)
}
}
}
// Trim will cut the EventCost at specific duration
// returns the srplusEC as separate EventCost
func (ec *EventCost) Trim(atUsage time.Duration) (srplusEC *EventCost, err error) {
if ec.Usage == nil {
ec.GetUsage()
}
origECUsage := ec.GetUsage()
if atUsage >= *ec.Usage {
return // no trim
}
if atUsage == 0 {
//clone the event because we need to overwrite ec
srplusEC = ec.Clone()
// modify the value of ec
*ec = *NewBareEventCost()
ec.CGRID = srplusEC.CGRID
ec.RunID = srplusEC.RunID
ec.StartTime = srplusEC.StartTime
ec.AccountSummary = srplusEC.AccountSummary.Clone()
return // trim all, fresh EC with 0 usage
}
srplusEC = NewBareEventCost()
srplusEC.CGRID = ec.CGRID
srplusEC.RunID = ec.RunID
srplusEC.StartTime = ec.StartTime
srplusEC.AccountSummary = ec.AccountSummary.Clone()
var lastActiveCIlIdx *int // mark last index which should stay with ec
for i, cIl := range ec.Charges {
if cIl.ecUsageIdx == nil {
ec.ComputeEventCostUsageIndexes()
}
if cIl.usage == nil {
ec.GetUsage()
}
if *cIl.ecUsageIdx+*cIl.TotalUsage() >= atUsage {
lastActiveCIlIdx = utils.IntPointer(i)
break
}
}
if lastActiveCIlIdx == nil {
return nil, errors.New("cannot find last active ChargingInterval")
}
lastActiveCIl := ec.Charges[*lastActiveCIlIdx]
if *lastActiveCIl.ecUsageIdx >= atUsage {
return nil, errors.New("failed detecting last active ChargingInterval")
} else if lastActiveCIl.CompressFactor == 0 {
return nil, errors.New("ChargingInterval with 0 compressFactor")
}
srplusEC.Charges = append(srplusEC.Charges, ec.Charges[*lastActiveCIlIdx+1:]...) // direct assignment will wrongly reference later
ec.Charges = ec.Charges[:*lastActiveCIlIdx+1]
ec.Usage = nil
ec.Cost = nil
if lastActiveCIl.CompressFactor != 1 &&
*lastActiveCIl.ecUsageIdx+*lastActiveCIl.TotalUsage() > atUsage { // Split based on compress factor if needed
var laCF int
for ciCnt := 1; ciCnt <= lastActiveCIl.CompressFactor; ciCnt++ {
if *lastActiveCIl.ecUsageIdx+
time.Duration(lastActiveCIl.usage.Nanoseconds()*int64(ciCnt)) >= atUsage {
laCF = ciCnt
break
}
}
if laCF == 0 {
return nil, errors.New("cannot detect last active CompressFactor in ChargingInterval")
}
if laCF != lastActiveCIl.CompressFactor {
srplsCIl := lastActiveCIl.Clone()
srplsCIl.CompressFactor = lastActiveCIl.CompressFactor - laCF
srplusEC.Charges = append([]*ChargingInterval{srplsCIl}, srplusEC.Charges...) // prepend surplus CIl
lastActiveCIl.CompressFactor = laCF // correct compress factor
ec.Usage = nil
ec.Cost = nil
}
}
if atUsage != ec.GetUsage() { // lastInterval covering more than needed, need split
atUsage -= (ec.GetUsage() - *lastActiveCIl.Usage()) // remaining duration to cover in increments of the last charging interval
// find out last increment covering duration
var lastActiveCItIdx *int
var incrementsUsage time.Duration
for i, cIt := range lastActiveCIl.Increments {
incrementsUsage += cIt.TotalUsage()
if incrementsUsage >= atUsage {
lastActiveCItIdx = utils.IntPointer(i)
break
}
}
if lastActiveCItIdx == nil { // bug in increments
return nil, errors.New("no active increment found")
}
lastActiveCIts := lastActiveCIl.Increments // so we can modify the reference in case we have surplus
lastIncrement := lastActiveCIts[*lastActiveCItIdx]
if lastIncrement.CompressFactor == 0 {
return nil, errors.New("empty compress factor in increment")
}
var srplsIncrements []*ChargingIncrement
if *lastActiveCItIdx < len(lastActiveCIl.Increments)-1 { // less that complete increments, have surplus
srplsIncrements = lastActiveCIts[*lastActiveCItIdx+1:]
lastActiveCIts = lastActiveCIts[:*lastActiveCItIdx+1]
ec.Usage = nil
ec.Cost = nil
}
var laItCF int
if lastIncrement.CompressFactor != 1 && atUsage != incrementsUsage {
// last increment compress factor is higher that we need to cover
incrementsUsage -= lastIncrement.TotalUsage()
for cnt := 1; cnt <= lastIncrement.CompressFactor; cnt++ {
incrementsUsage += lastIncrement.Usage
if incrementsUsage >= atUsage {
laItCF = cnt
break
}
}
if laItCF == 0 {
return nil, errors.New("cannot detect last active CompressFactor in ChargingIncrement")
}
if laItCF != lastIncrement.CompressFactor {
srplsIncrement := lastIncrement.Clone()
srplsIncrement.CompressFactor = srplsIncrement.CompressFactor - laItCF
srplsIncrements = append([]*ChargingIncrement{srplsIncrement}, srplsIncrements...) // prepend the surplus out of compress
}
}
if len(srplsIncrements) != 0 { // partially covering, need trim
if lastActiveCIl.CompressFactor > 1 { // ChargingInterval not covering in full, need to split it
lastActiveCIl.CompressFactor--
ec.Charges = append(ec.Charges, lastActiveCIl.Clone())
lastActiveCIl = ec.Charges[len(ec.Charges)-1]
lastActiveCIl.CompressFactor = 1
ec.Usage = nil
ec.Cost = nil
}
srplsCIl := lastActiveCIl.Clone()
srplsCIl.Increments = srplsIncrements
srplusEC.Charges = append([]*ChargingInterval{srplsCIl}, srplusEC.Charges...)
lastActiveCIl.Increments = make([]*ChargingIncrement, len(lastActiveCIts))
for i, incr := range lastActiveCIts {
lastActiveCIl.Increments[i] = incr.Clone() // avoid pointer references to the other interval
}
if laItCF != 0 {
lastActiveCIl.Increments[len(lastActiveCIl.Increments)-1].CompressFactor = laItCF // correct the compressFactor for the last increment
ec.Usage = nil
ec.Cost = nil
}
}
}
ec.ResetCounters()
if usage := ec.GetUsage(); usage < atUsage {
return nil, errors.New("usage of EventCost smaller than requested")
}
srplusEC.ResetCounters()
srplusEC.StartTime = ec.StartTime.Add(ec.GetUsage())
if srplsUsage := srplusEC.GetUsage(); srplsUsage > origECUsage-atUsage {
return nil, errors.New("surplus EventCost too big")
}
// close surplus with missing cache
for _, cIl := range srplusEC.Charges {
cIl.RatingID = srplusEC.ratingGetIDFomEventCost(ec, cIl.RatingID)
for _, incr := range cIl.Increments {
incr.AccountingID = srplusEC.accountingGetIDFromEventCost(ec, incr.AccountingID)
}
}
ec.RemoveStaleReferences() // data should be transferred by now, can clean the old one
return
}
// FieldAsInterface func to implement DataProvider
func (ec *EventCost) FieldAsInterface(fldPath []string) (val any, err error) {
if val, err = ec.cache.FieldAsInterface(fldPath); err != nil {
if err != utils.ErrNotFound { // item found in cache
return
}
err = nil // cancel previous err
} else if val == nil {
return nil, utils.ErrNotFound
} else {
return // data found in cache
}
val, err = ec.fieldAsInterface(fldPath)
if err == nil {
ec.cache.Set(fldPath, val)
} else if err == utils.ErrNotFound {
ec.cache.Set(fldPath, nil)
}
return
}
// FieldAsInterface the implementation of FieldAsInterface
func (ec *EventCost) fieldAsInterface(fldPath []string) (val any, err error) {
switch fldPath[0] {
default: // "Charges[1]"
opath, indx := utils.GetPathIndex(fldPath[0])
if opath != utils.Charges {
return nil, fmt.Errorf("unsupported field prefix: <%s>", opath)
}
if indx != nil {
if len(ec.Charges) <= *indx {
return nil, utils.ErrNotFound
}
return ec.getChargesForPath(fldPath[1:], ec.Charges[*indx])
}
case utils.Charges:
if len(fldPath) != 1 { // slice has no members
return nil, utils.ErrNotFound
}
return ec.Charges, nil
case utils.CGRID:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return ec.CGRID, nil
case utils.RunID:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return ec.RunID, nil
case utils.StartTime:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return ec.StartTime, nil
case utils.Usage:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return ec.Usage, nil
case utils.Cost:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return ec.Cost, nil
case utils.AccountSummary:
if len(fldPath) == 1 {
return ec.AccountSummary, nil
}
return ec.AccountSummary.FieldAsInterface(fldPath[1:])
case utils.Timings:
if len(fldPath) == 1 {
return ec.Timings, nil
}
return ec.Timings.FieldAsInterface(fldPath[1:])
case utils.Rates:
if len(fldPath) == 1 {
return ec.Rates, nil
}
return ec.Rates.FieldAsInterface(fldPath[1:])
case utils.RatingFilters:
if len(fldPath) == 1 {
return ec.RatingFilters, nil
}
return ec.RatingFilters.FieldAsInterface(fldPath[1:])
case utils.Accounting:
if len(fldPath) == 1 {
return ec.Accounting, nil
}
return ec.Accounting.FieldAsInterface(fldPath[1:])
case utils.Rating:
if len(fldPath) == 1 {
return ec.Rating, nil
}
return ec.Rating.FieldAsInterface(fldPath[1:])
}
return nil, fmt.Errorf("unsupported field prefix: <%s>", fldPath[0])
}
func (ec *EventCost) getChargesForPath(fldPath []string, chr *ChargingInterval) (val any, err error) {
if chr == nil {