Started tpes for attributes

This commit is contained in:
porosnicuadrian
2022-03-08 17:17:25 +02:00
committed by Dan Christian Bogos
parent bdc124be1f
commit 8cb837b39c
3 changed files with 101 additions and 14 deletions

61
tpes/tpe_attributes.go Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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
}

View File

@@ -19,9 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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
}

View File

@@ -19,13 +19,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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
}