added trends and rankings methods in dispatchers

This commit is contained in:
gezimbll
2024-11-11 17:01:35 +01:00
committed by Dan Christian Bogos
parent 0854ef0bbb
commit fc6aff8484
7 changed files with 294 additions and 1 deletions

View File

@@ -274,6 +274,36 @@ func (dT *DispatcherThresholdSv1) GetThreshold(ctx *context.Context, args *utils
return dT.dS.ThresholdSv1GetThreshold(ctx, args, th)
}
func NewDispatcherTrendSv1(dps *dispatchers.DispatcherService) *DispatcherThresholdSv1 {
return &DispatcherThresholdSv1{dS: dps}
}
type DispatcherTrendSv1 struct {
dS *dispatchers.DispatcherService
}
func (dT *DispatcherTrendSv1) Ping(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
return dT.dS.TrendSv1Ping(ctx, args, reply)
}
func (dT *DispatcherTrendSv1) ScheduleQueries(ctx *context.Context, args *utils.ArgScheduleTrendQueries,
reply *int) (err error) {
return dT.dS.TrendSv1ScheduleQueries(ctx, args, reply)
}
func (dT *DispatcherTrendSv1) GetTrend(ctx *context.Context, args *utils.ArgGetTrend, reply *engine.Trend) (err error) {
return dT.dS.TrendSv1GetTrend(ctx, args, reply)
}
func (dT *DispatcherTrendSv1) GetScheduledTrends(ctx *context.Context, args *utils.ArgScheduledTrends, reply *[]utils.ScheduledTrend) (err error) {
return dT.dS.TrendSv1GetScheduledTrends(ctx, args, reply)
}
func (dT *DispatcherTrendSv1) GetTrendSummary(ctx *context.Context, args utils.TenantIDWithAPIOpts, reply *engine.TrendSummary) error {
return dT.dS.TrendSv1GetTrendSummary(ctx, args, reply)
}
func NewDispatcherStatSv1(dps *dispatchers.DispatcherService) *DispatcherStatSv1 {
return &DispatcherStatSv1{dS: dps}
}
@@ -314,6 +344,34 @@ func (dSts *DispatcherStatSv1) ProcessEvent(ctx *context.Context, args *utils.CG
return dSts.dS.StatSv1ProcessEvent(ctx, args, reply)
}
func NewDispatcherRankingSv1(dps *dispatchers.DispatcherService) *DispatcherRankingSv1 {
return &DispatcherRankingSv1{ds: dps}
}
type DispatcherRankingSv1 struct {
ds *dispatchers.DispatcherService
}
func (dRn *DispatcherRankingSv1) Ping(ctx *context.Context, args *utils.CGREvent, reply *string) error {
return dRn.ds.RankingSv1Ping(ctx, args, reply)
}
func (dRn *DispatcherRankingSv1) GetRankingSummary(ctx *context.Context, args *utils.TenantIDWithAPIOpts, reply *engine.RankingSummary) error {
return dRn.ds.RankingSv1GetRankingSummary(ctx, args, reply)
}
func (dRn *DispatcherRankingSv1) GetSchedule(ctx *context.Context, args *utils.ArgScheduledRankings, reply *[]utils.ScheduledRanking) error {
return dRn.ds.RankingSv1GetSchedule(ctx, args, reply)
}
func (dRn *DispatcherRankingSv1) ScheduleQueries(ctx *context.Context, args *utils.ArgScheduleRankingQueries, reply *int) (err error) {
return dRn.ds.RankingSv1ScheduleQueries(ctx, args, reply)
}
func (dRn *DispatcherRankingSv1) GetRanking(ctx *context.Context, args *utils.TenantIDWithAPIOpts, reply *engine.Ranking) (err error) {
return dRn.ds.RankingSv1GetRanking(ctx, args, reply)
}
func NewDispatcherResourceSv1(dps *dispatchers.DispatcherService) *DispatcherResourceSv1 {
return &DispatcherResourceSv1{dRs: dps}
}

View File

@@ -143,14 +143,17 @@ func (rnks *RankingSv1) GetRankingSummary(ctx *context.Context, arg *utils.Tenan
return rnks.rnkS.V1GetRankingSummary(ctx, arg, reply)
}
// GetRanking queries a Ranking
func (rnkS *RankingSv1) GetRanking(ctx *context.Context, arg *utils.TenantIDWithAPIOpts, reply *engine.Ranking) (err error) {
return rnkS.rnkS.V1GetRanking(ctx, arg, reply)
}
// GetSchedule return a list of scheduled Rankings
func (rnkS *RankingSv1) GetSchedule(ctx *context.Context, args *utils.ArgScheduledRankings, schedRankings *[]utils.ScheduledRanking) (err error) {
return rnkS.rnkS.V1GetSchedule(ctx, args, schedRankings)
}
// ScheduleQueries schedules a list of Rankings
func (rnkS *RankingSv1) ScheduleQueries(ctx *context.Context, args *utils.ArgScheduleRankingQueries, scheduled *int) (err error) {
return rnkS.rnkS.V1ScheduleQueries(ctx, args, scheduled)
}

View File

@@ -83,18 +83,22 @@ type TrendSv1 struct {
trS *engine.TrendS
}
// ScheduleQueries schedules a list of trends
func (trs *TrendSv1) ScheduleQueries(ctx *context.Context, args *utils.ArgScheduleTrendQueries, scheduled *int) error {
return trs.trS.V1ScheduleQueries(ctx, args, scheduled)
}
// GetTrend queries a Trend
func (trs *TrendSv1) GetTrend(ctx *context.Context, args *utils.ArgGetTrend, trend *engine.Trend) error {
return trs.trS.V1GetTrend(ctx, args, trend)
}
// GetScheduledTrends returns a list of Trends already scheduled
func (trs *TrendSv1) GetScheduledTrends(ctx *context.Context, args *utils.ArgScheduledTrends, schedTrends *[]utils.ScheduledTrend) error {
return trs.trS.V1GetScheduledTrends(ctx, args, schedTrends)
}
// GetTrendSummary return a Trend with only latest updated metrics
func (trs *TrendSv1) GetTrendSummary(ctx *context.Context, arg utils.TenantIDWithAPIOpts, reply *engine.TrendSummary) error {
return trs.trS.V1GetTrendSummary(ctx, arg, reply)
}

107
dispatchers/rankings.go Normal file
View File

@@ -0,0 +1,107 @@
/*
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 dispatchers
import (
"time"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func (dS *DispatcherService) RankingSv1Ping(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
if args == nil {
args = new(utils.CGREvent)
}
args.Tenant = utils.FirstNonEmpty(args.Tenant, dS.cfg.GeneralCfg().DefaultTenant)
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.RankingSv1Ping, args.Tenant,
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), args.Time); err != nil {
return
}
}
return dS.Dispatch(args, utils.MetaRankings, utils.RankingSv1Ping, args, reply)
}
func (dS *DispatcherService) RankingSv1GetRankingSummary(ctx *context.Context, args *utils.TenantIDWithAPIOpts, rs *engine.RankingSummary) (err error) {
tnt := utils.FirstNonEmpty(args.Tenant, dS.cfg.GeneralCfg().DefaultTenant)
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.RankingSv1GetRankingSummary,
tnt,
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{
Tenant: tnt,
ID: args.ID,
APIOpts: args.APIOpts}, utils.MetaRankings, utils.RankingSv1GetRankingSummary, args, rs)
}
func (dS *DispatcherService) RankingSv1GetSchedule(ctx *context.Context, args *utils.ArgScheduledRankings, schedRankings *[]utils.ScheduledRanking) (err error) {
tnt := utils.FirstNonEmpty(args.Tenant, dS.cfg.GeneralCfg().DefaultTenant)
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.RankingSv1GetSchedule,
args.Tenant,
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]),
utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{
Tenant: tnt,
ID: args.ID,
APIOpts: args.APIOpts,
}, utils.MetaRankings, utils.RankingSv1GetSchedule, args, schedRankings)
}
func (dS *DispatcherService) RankingSv1ScheduleQueries(ctx *context.Context, args *utils.ArgScheduleRankingQueries, scheduled *int) (err error) {
tnt := dS.cfg.GeneralCfg().DefaultTenant
if args.Tenant != utils.EmptyString {
tnt = args.Tenant
}
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.RankingSv1ScheduleQueries,
tnt, utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{
Tenant: tnt,
APIOpts: args.APIOpts,
}, utils.MetaRankings, utils.RankingSv1ScheduleQueries, args, scheduled)
}
func (dS *DispatcherService) RankingSv1GetRanking(ctx *context.Context, args *utils.TenantIDWithAPIOpts, rnk *engine.Ranking) (err error) {
tnt := dS.cfg.GeneralCfg().DefaultTenant
if args.TenantID != nil && args.TenantID.Tenant != utils.EmptyString {
tnt = args.TenantID.Tenant
}
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.RankingSv1GetRanking, tnt,
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{
Tenant: tnt,
ID: args.ID,
APIOpts: args.APIOpts,
}, utils.MetaRankings, utils.RankingSv1GetRanking, args, rnk)
}

107
dispatchers/trends.go Normal file
View File

@@ -0,0 +1,107 @@
/*
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 dispatchers
import (
"time"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func (dS *DispatcherService) TrendSv1Ping(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
if args == nil {
args = new(utils.CGREvent)
}
args.Tenant = utils.FirstNonEmpty(args.Tenant, dS.cfg.GeneralCfg().DefaultTenant)
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.ThresholdSv1Ping, args.Tenant,
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), args.Time); err != nil {
return
}
}
return dS.Dispatch(args, utils.MetaTrends, utils.TrendSv1Ping, args, reply)
}
func (dS *DispatcherService) TrendSv1ScheduleQueries(ctx *context.Context, args *utils.ArgScheduleTrendQueries,
scheduled *int) (err error) {
tnt := utils.FirstNonEmpty(args.Tenant, dS.cfg.GeneralCfg().DefaultTenant)
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.TrendSv1ScheduleQueries,
tnt,
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: tnt,
ID: args.ID,
APIOpts: args.APIOpts}, utils.MetaTrends, utils.TrendSv1ScheduleQueries, args, scheduled)
}
func (dS *DispatcherService) TrendSv1GetTrend(ctx *context.Context, args *utils.ArgGetTrend, trend *engine.Trend) (err error) {
tnt := utils.FirstNonEmpty(args.Tenant, dS.cfg.GeneralCfg().DefaultTenant)
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.TrendSv1GetTrend,
args.Tenant,
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]),
utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{
Tenant: tnt,
ID: args.ID,
APIOpts: args.APIOpts,
}, utils.MetaTrends, utils.TrendSv1GetTrend, args, trend)
}
func (dS *DispatcherService) TrendSv1GetScheduledTrends(ctx *context.Context, args *utils.ArgScheduledTrends, schedTrends *[]utils.ScheduledTrend) (err error) {
tnt := dS.cfg.GeneralCfg().DefaultTenant
if args.Tenant != utils.EmptyString {
tnt = args.Tenant
}
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.TrendSv1GetScheduledTrends,
tnt, utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{
Tenant: tnt,
APIOpts: args.APIOpts,
}, utils.MetaTrends, utils.TrendSv1GetScheduledTrends, args, schedTrends)
}
func (dS *DispatcherService) TrendSv1GetTrendSummary(ctx *context.Context, args utils.TenantIDWithAPIOpts, reply *engine.TrendSummary) (err error) {
tnt := dS.cfg.GeneralCfg().DefaultTenant
if args.TenantID != nil && args.TenantID.Tenant != utils.EmptyString {
tnt = args.TenantID.Tenant
}
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.TrendSv1GetTrendSummary, tnt,
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{
Tenant: tnt,
ID: args.ID,
APIOpts: args.APIOpts,
}, utils.MetaTrends, utils.TrendSv1GetTrendSummary, args, reply)
}

View File

@@ -243,6 +243,18 @@ func newDispatcherServiceMap(val *dispatchers.DispatcherService) (engine.IntServ
}
srvMap[srv.Name] = srv
srv, err = birpc.NewService(v1.NewDispatcherRankingSv1(val), utils.RankingSv1, true)
if err != nil {
return nil, err
}
srvMap[srv.Name] = srv
srv, err = birpc.NewService(v1.NewDispatcherTrendSv1(val), utils.TrendSv1, true)
if err != nil {
return nil, err
}
srvMap[srv.Name] = srv
srv, err = birpc.NewService(v1.NewDispatcherResponder(val),
utils.Responder, true)
if err != nil {

View File

@@ -103,7 +103,7 @@ func TestNewDispatcherServiceMap(t *testing.T) {
if srvMap == nil {
t.Fatal("Expected non-nil map, but got nil")
}
expectedLength := 20
expectedLength := 22
if len(srvMap) != expectedLength {
t.Fatalf("Expected map length %d, but got %d", expectedLength, len(srvMap))
}
@@ -128,6 +128,8 @@ func TestNewDispatcherServiceMap(t *testing.T) {
utils.SchedulerSv1,
utils.SessionSv1,
utils.StatSv1,
utils.RankingSv1,
utils.TrendSv1,
}
for _, name := range expectedServiceNames {
if _, ok := srvMap[name]; !ok {