From 8cb837b39cb353810451554c3733a1ecf27ea9d0 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Tue, 8 Mar 2022 17:17:25 +0200 Subject: [PATCH] Started tpes for attributes --- tpes/tpe_attributes.go | 61 ++++++++++++++++++++++++++++++++++++++++++ tpes/tpes.go | 23 +++++++++------- tpes/tpexporter.go | 31 +++++++++++++++++---- 3 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 tpes/tpe_attributes.go diff --git a/tpes/tpe_attributes.go b/tpes/tpe_attributes.go new file mode 100644 index 000000000..a6e5d6b59 --- /dev/null +++ b/tpes/tpe_attributes.go @@ -0,0 +1,61 @@ +/* +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 ( + "fmt" + + "github.com/cgrates/birpc/context" + + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +type TPAttributes struct { + dm *engine.DataManager +} + +// newTPAttributes is the constructor for TPAttributes +func newTPAttributes(dm *engine.DataManager) *TPAttributes { + return &TPAttributes{ + dm: dm, + } +} + +// exportItems for TPAttributes will implement the imethod for tpExporter interface +func (tpAttr TPAttributes) exportItems(ctx *context.Context, tnt string, itmIDs []string) (expContent []byte, err error) { + //attrBts := make(map[string][]byte) + for _, attrID := range itmIDs { + attrPrf, err := tpAttr.dm.GetAttributeProfile(ctx, tnt, attrID, true, true, utils.NonTransactional) + if err != nil { + if err.Error() == utils.ErrNotFound.Error() { + utils.Logger.Warning(fmt.Sprintf("<%s> cannot find AttributeProfile with id: <%v>", utils.TPeS, attrID)) + continue + } + return nil, err + } + var attrMdl []interface{} + attrMdl = engine.APItoModelTPAttribute(engine.AttributeProfileToAPI(attrPrf)) + if err := writeOut(utils.AttributesCsv, attrMdl); err != nil { + return nil, err + } + + } + return +} diff --git a/tpes/tpes.go b/tpes/tpes.go index affa9ef0d..9fa0af091 100644 --- a/tpes/tpes.go +++ b/tpes/tpes.go @@ -19,9 +19,10 @@ along with this program. If not, see package tpes import ( - "context" "fmt" + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" @@ -60,19 +61,23 @@ type ArgsExportTP struct { } // V1ExportTariffPlan is the API executed to export tariff plan items -func (tpE *TPeS) V1ExportTariffPlan(ctx *context.Context, args *ArgsExportTP, reply *utils.Account) (err error) { +func (tpE *TPeS) V1ExportTariffPlan(ctx *context.Context, args *ArgsExportTP, reply map[string][]byte) (err error) { + if args.Tenant == utils.EmptyString { + args.Tenant = tpE.cfg.GeneralCfg().DefaultTenant + } for eType := range args.ExportItems { if _, has := tpE.exps[eType]; !has { return utils.ErrPrefix(utils.ErrUnsupportedTPExporterType, eType) } } - /* - code to export to zip comes here - for expType, expItms := range args.ExportItems { - if expCnt, err = tpE.exps[expType].exportItems(expItms); err != nil { - return utils.NewErrServerError(err) - } + + expotedItems := make(map[string][]byte, len(tpE.exps)) + // code to export to zip comes here + for expType, expItms := range args.ExportItems { + if expotedItems[expType], err = tpE.exps[expType].exportItems(ctx, args.Tenant, expItms); err != nil { + return utils.NewErrServerError(err) } - */ + } + reply = expotedItems return } diff --git a/tpes/tpexporter.go b/tpes/tpexporter.go index 8d26adb58..72b695a1e 100644 --- a/tpes/tpexporter.go +++ b/tpes/tpexporter.go @@ -19,13 +19,17 @@ along with this program. If not, see package tpes import ( + "bytes" + "encoding/csv" + + "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) var tpExporterTypes = utils.NewStringSet([]string{ utils.MetaAttributes, - utils.MetaResources, + /* utils.MetaResources, utils.MetaFilters, utils.MetaStats, utils.MetaThresholds, @@ -35,20 +39,37 @@ var tpExporterTypes = utils.NewStringSet([]string{ utils.MetaDispatcherHosts, utils.MetaRateProfiles, utils.MetaActions, - utils.MetaAccounts}) + utils.MetaAccounts */}) // tpExporter is the interface implementing exports of tariff plan items type tpExporter interface { - exportItems(tnt string, itmIDs []string) (expContent []byte, err error) + exportItems(ctx *context.Context, tnt string, itmIDs []string) (expContent []byte, err error) } // newTPExporter constructs tpExporters func newTPExporter(expType string, dm *engine.DataManager) (tpE tpExporter, err error) { switch expType { case utils.MetaAttributes: - return //newTPAttributes() + return newTPAttributes(dm), nil default: return nil, utils.ErrPrefix(utils.ErrUnsupportedTPExporterType, expType) } - return +} + +func writeOut(fileName string, tpData []interface{}) error { + buff := new(bytes.Buffer) + + csvWriter := csv.NewWriter(buff) + for _, tpItem := range tpData { + record, err := engine.CsvDump(tpItem) + if err != nil { + return err + } + if err := csvWriter.Write(record); err != nil { + return err + } + } + + utils.Logger.Debug(buff.String()) + return nil }