New tpe_rates

This commit is contained in:
porosnicuadrian
2022-03-14 15:24:13 +02:00
committed by Dan Christian Bogos
parent a5d3db5b5e
commit 83741cbf94
4 changed files with 85 additions and 3 deletions

View File

@@ -62,7 +62,7 @@ func (tpAttr TPAttributes) exportItems(ctx *context.Context, wrtr io.Writer, tnt
if len(attrMdl) == 0 {
return
}
// for every profile, convert it into model to be writable in csv format
// for every profile, convert it into model to be compatible in csv format
for _, tpItem := range attrMdl {
// transform every record into a []string
record, err := engine.CsvDump(tpItem)

View File

@@ -62,7 +62,7 @@ func (tpFltr TPFilters) exportItems(ctx *context.Context, wrtr io.Writer, tnt st
if len(fltrMdls) == 0 {
return
}
// for every profile, convert it into model to be writable in csv format
// for every profile, convert it into model to be compatible in csv format
for _, tpItem := range fltrMdls {
// transform every record into a []string
record, err := engine.CsvDump(tpItem)

82
tpes/tpe_rates.go Normal file
View File

@@ -0,0 +1,82 @@
/*
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 tpes
import (
"encoding/csv"
"fmt"
"io"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
type TPRates struct {
dm *engine.DataManager
}
// newTPRates is the constructor for TPRates
func newTPRates(dm *engine.DataManager) *TPRates {
return &TPRates{
dm: dm,
}
}
// exportItems for TPRates will implement the method for tpExporter interface
func (tpRts TPRates) exportItems(ctx *context.Context, wrtr io.Writer, tnt string, itmIDs []string) (err error) {
csvWriter := csv.NewWriter(wrtr)
csvWriter.Comma = utils.CSVSep
// before writing the profiles, we must write the headers
if err = csvWriter.Write([]string{"#Tenant", "ID", "FilterIDs", "Weights", "MinCost", "MaxCost", "MaxCostStrategy", "RateID", "RateFilterIDs", "RateActivationStart", "RateWeights", "RateBlocker", "RateIntervalStart", "RateFixedFee", "RateRecurrentFee", "RateUnit", "RateIncrement"}); err != nil {
return
}
for _, rateID := range itmIDs {
var ratePrf *utils.RateProfile
ratePrf, err = tpRts.dm.GetRateProfile(ctx, tnt, rateID, true, true, utils.NonTransactional)
if err != nil {
if err.Error() == utils.ErrNotFound.Error() {
utils.Logger.Warning(fmt.Sprintf("<%s> cannot find RateProfile with id: <%v>", utils.TPeS, rateID))
continue
}
return err
}
utils.Logger.Crit(fmt.Sprintf("RateProfileToAPI: %v", utils.ToJSON(engine.RateProfileToAPI(ratePrf))))
ratePrfMdls := engine.APItoModelTPRateProfile(engine.RateProfileToAPI(ratePrf))
utils.Logger.Crit(fmt.Sprintf("ratePrfMdls: %v", utils.ToJSON(ratePrfMdls)))
if len(ratePrfMdls) == 0 {
return
}
// for every profile, convert it into model to be compatible in csv format
for _, tpItem := range ratePrfMdls {
// transform every record into a []string
record, err := engine.CsvDump(tpItem)
if err != nil {
return err
}
// record is a line of a csv file
if err := csvWriter.Write(record); err != nil {
return err
}
}
}
csvWriter.Flush()
return
}

View File

@@ -62,7 +62,7 @@ func (tpRes TPResources) exportItems(ctx *context.Context, wrtr io.Writer, tnt s
if len(resMdl) == 0 {
return
}
// for every profile, convert it into model to be writable in csv format
// for every profile, convert it into model to be compatible in csv format
for _, tpItem := range resMdl {
// transform every record into a []string
record, err := engine.CsvDump(tpItem)