mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-12 10:36:24 +05:00
218 lines
8.2 KiB
Go
218 lines
8.2 KiB
Go
/*
|
|
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 apis
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/cgrates/birpc/context"
|
|
"github.com/cgrates/cgrates/engine"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
// GetStatQueueProfile returns a StatQueue profile
|
|
func (adms *AdminSv1) GetStatQueueProfile(ctx *context.Context, arg *utils.TenantIDWithAPIOpts, reply *engine.StatQueueProfile) (err error) {
|
|
if missing := utils.MissingStructFields(arg, []string{utils.ID}); len(missing) != 0 { //Params missing
|
|
return utils.NewErrMandatoryIeMissing(missing...)
|
|
}
|
|
tnt := arg.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = adms.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
sCfg, err := adms.dm.GetStatQueueProfile(ctx, tnt, arg.ID,
|
|
true, true, utils.NonTransactional)
|
|
if err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
*reply = *sCfg
|
|
return
|
|
}
|
|
|
|
// GetStatQueueProfileIDs returns list of statQueueProfile IDs registered for a tenant
|
|
func (adms *AdminSv1) GetStatQueueProfileIDs(ctx *context.Context, args *utils.ArgsItemIDs, stsPrfIDs *[]string) (err error) {
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = adms.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
prfx := utils.StatQueueProfilePrefix + tnt + utils.ConcatenatedKeySep
|
|
lenPrfx := len(prfx)
|
|
prfx += args.ItemsPrefix
|
|
var keys []string
|
|
if keys, err = adms.dm.DataDB().GetKeysForPrefix(ctx, prfx); err != nil {
|
|
return
|
|
}
|
|
if len(keys) == 0 {
|
|
return utils.ErrNotFound
|
|
}
|
|
retIDs := make([]string, len(keys))
|
|
for i, key := range keys {
|
|
retIDs[i] = key[lenPrfx:]
|
|
}
|
|
var limit, offset, maxItems int
|
|
if limit, offset, maxItems, err = utils.GetPaginateOpts(args.APIOpts); err != nil {
|
|
return
|
|
}
|
|
*stsPrfIDs, err = utils.Paginate(retIDs, limit, offset, maxItems)
|
|
return
|
|
}
|
|
|
|
// GetStatQueueProfiles returns a list of stats profiles registered for a tenant
|
|
func (admS *AdminSv1) GetStatQueueProfiles(ctx *context.Context, args *utils.ArgsItemIDs, sqPrfs *[]*engine.StatQueueProfile) (err error) {
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = admS.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
var sqPrfIDs []string
|
|
if err = admS.GetStatQueueProfileIDs(ctx, args, &sqPrfIDs); err != nil {
|
|
return
|
|
}
|
|
*sqPrfs = make([]*engine.StatQueueProfile, 0, len(sqPrfIDs))
|
|
for _, sqPrfID := range sqPrfIDs {
|
|
var sqPrf *engine.StatQueueProfile
|
|
sqPrf, err = admS.dm.GetStatQueueProfile(ctx, tnt, sqPrfID, true, true, utils.NonTransactional)
|
|
if err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
*sqPrfs = append(*sqPrfs, sqPrf)
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetStatQueueProfilesCount returns the total number of StatQueueProfileIDs registered for a tenant
|
|
// returns ErrNotFound in case of 0 StatQueueProfileIDs
|
|
func (admS *AdminSv1) GetStatQueueProfilesCount(ctx *context.Context, args *utils.ArgsItemIDs, reply *int) (err error) {
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = admS.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
prfx := utils.StatQueueProfilePrefix + tnt + utils.ConcatenatedKeySep + args.ItemsPrefix
|
|
var keys []string
|
|
if keys, err = admS.dm.DataDB().GetKeysForPrefix(ctx, prfx); err != nil {
|
|
return err
|
|
}
|
|
if len(keys) == 0 {
|
|
return utils.ErrNotFound
|
|
}
|
|
*reply = len(keys)
|
|
return
|
|
}
|
|
|
|
// SetStatQueueProfile alters/creates a StatQueueProfile
|
|
func (adms *AdminSv1) SetStatQueueProfile(ctx *context.Context, arg *engine.StatQueueProfileWithAPIOpts, reply *string) (err error) {
|
|
if missing := utils.MissingStructFields(arg.StatQueueProfile, []string{utils.ID}); len(missing) != 0 {
|
|
return utils.NewErrMandatoryIeMissing(missing...)
|
|
}
|
|
if arg.Tenant == utils.EmptyString {
|
|
arg.Tenant = adms.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
if err = adms.dm.SetStatQueueProfile(ctx, arg.StatQueueProfile, true); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
//generate a loadID for CacheStatQueueProfiles and CacheStatQueues and store it in database
|
|
//make 1 insert for both StatQueueProfile and StatQueue instead of 2
|
|
loadID := time.Now().UnixNano()
|
|
if err = adms.dm.SetLoadIDs(ctx, map[string]int64{utils.CacheStatQueueProfiles: loadID, utils.CacheStatQueues: loadID}); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
//handle caching for StatQueueProfile
|
|
if err = adms.CallCache(ctx, utils.IfaceAsString(arg.APIOpts[utils.MetaCache]), arg.Tenant, utils.CacheStatQueueProfiles,
|
|
arg.TenantID(), utils.EmptyString, &arg.FilterIDs, arg.APIOpts); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
*reply = utils.OK
|
|
return nil
|
|
}
|
|
|
|
// RemoveStatQueueProfile remove a specific stat configuration
|
|
func (adms *AdminSv1) RemoveStatQueueProfile(ctx *context.Context, args *utils.TenantIDWithAPIOpts, reply *string) error {
|
|
if missing := utils.MissingStructFields(args, []string{utils.ID}); len(missing) != 0 { //Params missing
|
|
return utils.NewErrMandatoryIeMissing(missing...)
|
|
}
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = adms.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
if err := adms.dm.RemoveStatQueueProfile(ctx, tnt, args.ID, true); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
//handle caching for StatQueueProfile
|
|
if err := adms.CallCache(ctx, utils.IfaceAsString(args.APIOpts[utils.MetaCache]), tnt, utils.CacheStatQueueProfiles,
|
|
utils.ConcatenatedKey(tnt, args.ID), utils.EmptyString, nil, args.APIOpts); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
//generate a loadID for CacheStatQueueProfiles and CacheStatQueues and store it in database
|
|
//make 1 insert for both StatQueueProfile and StatQueue instead of 2
|
|
loadID := time.Now().UnixNano()
|
|
if err := adms.dm.SetLoadIDs(ctx, map[string]int64{utils.CacheStatQueueProfiles: loadID, utils.CacheStatQueues: loadID}); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
*reply = utils.OK
|
|
return nil
|
|
}
|
|
|
|
// NewStatSv1 initializes StatSV1
|
|
func NewStatSv1(sS *engine.StatS) *StatSv1 {
|
|
return &StatSv1{sS: sS}
|
|
}
|
|
|
|
// StatSv1 exports RPC from RLs
|
|
type StatSv1 struct {
|
|
ping
|
|
sS *engine.StatS
|
|
}
|
|
|
|
// GetQueueIDs returns list of queueIDs registered for a tenant
|
|
func (stsv1 *StatSv1) GetQueueIDs(ctx *context.Context, args *utils.TenantWithAPIOpts, qIDs *[]string) error {
|
|
return stsv1.sS.V1GetQueueIDs(ctx, args, qIDs)
|
|
}
|
|
|
|
// ProcessEvent returns processes a new Event
|
|
func (stsv1 *StatSv1) ProcessEvent(ctx *context.Context, args *utils.CGREvent, reply *[]string) error {
|
|
return stsv1.sS.V1ProcessEvent(ctx, args, reply)
|
|
}
|
|
|
|
// GetStatQueuesForEvent returns the list of queues IDs in the system
|
|
func (stsv1 *StatSv1) GetStatQueuesForEvent(ctx *context.Context, args *utils.CGREvent, reply *[]string) (err error) {
|
|
return stsv1.sS.V1GetStatQueuesForEvent(ctx, args, reply)
|
|
}
|
|
|
|
// GetStatQueue returns a StatQueue object
|
|
func (stsv1 *StatSv1) GetStatQueue(ctx *context.Context, args *utils.TenantIDWithAPIOpts, reply *engine.StatQueue) (err error) {
|
|
return stsv1.sS.V1GetStatQueue(ctx, args, reply)
|
|
}
|
|
|
|
// GetQueueStringMetrics returns the string metrics for a Queue
|
|
func (stsv1 *StatSv1) GetQueueStringMetrics(ctx *context.Context, args *utils.TenantIDWithAPIOpts, reply *map[string]string) (err error) {
|
|
return stsv1.sS.V1GetQueueStringMetrics(ctx, args, reply)
|
|
}
|
|
|
|
// GetQueueFloatMetrics returns the float metrics for a Queue
|
|
func (stsv1 *StatSv1) GetQueueFloatMetrics(ctx *context.Context, args *utils.TenantIDWithAPIOpts, reply *map[string]float64) (err error) {
|
|
return stsv1.sS.V1GetQueueFloatMetrics(ctx, args, reply)
|
|
}
|
|
|
|
// GetQueueDecimalMetrics returns the decimal metrics for a Queue
|
|
func (stsv1 *StatSv1) GetQueueDecimalMetrics(ctx *context.Context, args *utils.TenantIDWithAPIOpts, reply *map[string]*utils.Decimal) (err error) {
|
|
return stsv1.sS.V1GetQueueDecimalMetrics(ctx, args, reply)
|
|
}
|
|
|
|
// ResetStatQueue resets the stat queue
|
|
func (stsv1 *StatSv1) ResetStatQueue(ctx *context.Context, tntID *utils.TenantIDWithAPIOpts, reply *string) error {
|
|
return stsv1.sS.V1ResetStatQueue(ctx, tntID, reply)
|
|
}
|