From 1ab927e2fa0d9af93bca9604e0430e31d58631b0 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Tue, 15 Mar 2022 17:21:05 +0200 Subject: [PATCH] TPe thresholds --- loaders/loader.go | 2 - tpes/tpe_thresholds.go | 98 ++++++++++++++++++++++++++++++++++++++++++ tpes/tpexporter.go | 4 +- 3 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 tpes/tpe_thresholds.go diff --git a/loaders/loader.go b/loaders/loader.go index eb6ce3aec..b3d0977cc 100644 --- a/loaders/loader.go +++ b/loaders/loader.go @@ -237,7 +237,6 @@ func (l *loader) process(ctx *context.Context, obj profile, lType, action, cachi cacheArgs[utils.CacheActionProfiles] = []string{tntId} case utils.MetaAccounts: cacheIDs = []string{utils.CacheAccounts, utils.CacheAccountsFilterIndexes} - } return engine.CallCache(l.connMgr, ctx, l.cacheConns, caching, cacheArgs, cacheIDs, nil, false, l.ldrCfg.Tenant) @@ -367,7 +366,6 @@ func (l *loader) moveUnprocessedFiles() (err error) { return } } - } return } diff --git a/tpes/tpe_thresholds.go b/tpes/tpe_thresholds.go new file mode 100644 index 000000000..d1c0ddd39 --- /dev/null +++ b/tpes/tpe_thresholds.go @@ -0,0 +1,98 @@ +/* +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 TPThresholds struct { + dm *engine.DataManager +} + +// newTPThresholds is the constructor for TPThresholds +func newTPThresholds(dm *engine.DataManager) *TPThresholds { + return &TPThresholds{ + dm: dm, + } +} + +// exportItems for TPThresholds will implement the method for tpExporter interface +func (tpThd TPThresholds) exportItems(ctx *context.Context, wrtr io.Writer, tnt string, itmIDs []string) (err error) { + if len(itmIDs) == 0 { + prfx := utils.ThresholdProfilePrefix + tnt + utils.ConcatenatedKeySep + // dbKeys will contain the full name of the key, but we will need just the IDs e.g. "acn_cgrates.org:THD_1" -- just THD_1 + var dbKeys []string + if dbKeys, err = tpThd.dm.DataDB().GetKeysForPrefix(ctx, prfx); err != nil { + return err + } + profileIDs := make([]string, 0, len(dbKeys)) + for _, key := range dbKeys { + profileIDs = append(profileIDs, key[len(prfx):]) + } + // if there are not any profiles in db, we do not write in our zip + if len(profileIDs) == 0 { + return + } + // the map e.g. : *filters: {"THD_1", "THD_1"} + itmIDs = profileIDs + } + 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", "Schedule", "TargetType", "TargetIDs", "ActionID", "ActionFilterIDs", "ActionBlocker", "ActionTTL", "ActionType", "ActionOpts", "ActionPath", "ActionValue"}); err != nil { + return + } + for _, thdID := range itmIDs { + var thdPrf *engine.ThresholdProfile + thdPrf, err = tpThd.dm.GetThresholdProfile(ctx, tnt, thdID, true, true, utils.NonTransactional) + if err != nil { + if err.Error() == utils.ErrNotFound.Error() { + return fmt.Errorf("<%s> cannot find Actions id: <%v>", err, thdID) + } + return err + } + thdMdls := engine.APItoModelTPThreshold(engine.ThresholdProfileToAPI(thdPrf)) + if len(thdMdls) == 0 { + return + } + // for every profile, convert it into model to be compatible in csv format + for _, tpItem := range thdMdls { + // transform every record into a []string + var record []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/tpexporter.go b/tpes/tpexporter.go index 085479fc3..3c4767bb7 100644 --- a/tpes/tpexporter.go +++ b/tpes/tpexporter.go @@ -36,7 +36,7 @@ var tpExporterTypes = utils.NewStringSet([]string{ utils.MetaAccounts, utils.MetaStats, utils.MetaActions, - //utils.MetaThresholds, + utils.MetaThresholds, /* utils.MetaDispatchers, // utils.MetaDispatcherHosts, // @@ -83,6 +83,8 @@ func newTPExporter(expType string, dm *engine.DataManager) (tpE tpExporter, err return newTPStats(dm), nil case utils.MetaActions: return newTPActions(dm), nil + case utils.MetaThresholds: + return newTPThresholds(dm), nil default: return nil, utils.ErrPrefix(utils.ErrUnsupportedTPExporterType, expType) }