From 83741cbf94683e25ca64a5afd344483f77549128 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Mon, 14 Mar 2022 15:24:13 +0200 Subject: [PATCH] New tpe_rates --- tpes/tpe_attributes.go | 2 +- tpes/tpe_filters.go | 2 +- tpes/tpe_rates.go | 82 ++++++++++++++++++++++++++++++++++++++++++ tpes/tpe_resources.go | 2 +- 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 tpes/tpe_rates.go diff --git a/tpes/tpe_attributes.go b/tpes/tpe_attributes.go index 6e0a363de..589891f90 100644 --- a/tpes/tpe_attributes.go +++ b/tpes/tpe_attributes.go @@ -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) diff --git a/tpes/tpe_filters.go b/tpes/tpe_filters.go index 95c7410ff..86688f551 100644 --- a/tpes/tpe_filters.go +++ b/tpes/tpe_filters.go @@ -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) diff --git a/tpes/tpe_rates.go b/tpes/tpe_rates.go new file mode 100644 index 000000000..a5b00c3b8 --- /dev/null +++ b/tpes/tpe_rates.go @@ -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 +*/ + +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 +} diff --git a/tpes/tpe_resources.go b/tpes/tpe_resources.go index 5b15c838a..964bd7761 100644 --- a/tpes/tpe_resources.go +++ b/tpes/tpe_resources.go @@ -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)