From 1146e2ef2f57e5e848cd39df7ebdd26be309a72e Mon Sep 17 00:00:00 2001 From: Trial97 Date: Mon, 7 Dec 2020 11:56:31 +0200 Subject: [PATCH] Updated console commands with constants --- console/accounts.go | 2 +- console/active_sessions.go | 8 ++++---- console/attributes_process_event.go | 2 +- console/chargers_process_event.go | 2 +- console/command.go | 6 ++++-- console/command_executer.go | 22 +++++++++++----------- console/cost.go | 10 +++++----- console/cost_details.go | 8 ++++---- console/datacost.go | 10 +++++----- console/debit.go | 2 +- console/debit_max.go | 2 +- console/maxduration.go | 4 ++-- console/maxusage.go | 13 ++++++++----- console/parse.go | 7 ++++--- console/passive_sessions.go | 8 ++++---- console/resources_profile.go | 2 +- console/session_initiate.go | 4 ++-- console/session_process_message.go | 4 ++-- console/session_update.go | 4 ++-- console/stats_profile.go | 2 +- console/threshold.go | 2 +- console/thresholds_profile.go | 2 +- console/triggers.go | 2 +- utils/consts.go | 10 ++++++++++ 24 files changed, 77 insertions(+), 61 deletions(-) diff --git a/console/accounts.go b/console/accounts.go index 2844c96ed..ff5bd2a69 100644 --- a/console/accounts.go +++ b/console/accounts.go @@ -67,6 +67,6 @@ func (self *CmdGetAccounts) RpcResult() interface{} { func (self *CmdGetAccounts) GetFormatedResult(result interface{}) string { return GetFormatedSliceResult(result, utils.StringSet{ - "MinSleep": {}, + utils.MinSleep: {}, }) } diff --git a/console/active_sessions.go b/console/active_sessions.go index 2657cf607..1381f0414 100644 --- a/console/active_sessions.go +++ b/console/active_sessions.go @@ -69,9 +69,9 @@ func (self *CmdActiveSessions) RpcResult() interface{} { func (self *CmdActiveSessions) GetFormatedResult(result interface{}) string { return GetFormatedSliceResult(result, utils.StringSet{ - "Usage": {}, - "DurationIndex": {}, - "MaxRateUnit": {}, - "DebitInterval": {}, + utils.Usage: {}, + utils.DurationIndex: {}, + utils.MaxRateUnit: {}, + utils.DebitInterval: {}, }) } diff --git a/console/attributes_process_event.go b/console/attributes_process_event.go index c0a520e37..5277f8fb2 100644 --- a/console/attributes_process_event.go +++ b/console/attributes_process_event.go @@ -76,6 +76,6 @@ func (self *CmdAttributesProcessEvent) RpcResult() interface{} { func (self *CmdAttributesProcessEvent) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "Usage": {}, + utils.Usage: {}, }) } diff --git a/console/chargers_process_event.go b/console/chargers_process_event.go index 2f704cc2d..8b882d1ae 100644 --- a/console/chargers_process_event.go +++ b/console/chargers_process_event.go @@ -75,6 +75,6 @@ func (self *CmdChargersProcessEvent) RpcResult() interface{} { func (self *CmdChargersProcessEvent) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "Usage": {}, + utils.Usage: {}, }) } diff --git a/console/command.go b/console/command.go index 85124d3ec..7907aa527 100644 --- a/console/command.go +++ b/console/command.go @@ -21,6 +21,8 @@ package console import ( "fmt" "strings" + + "github.com/cgrates/cgrates/utils" ) var ( @@ -62,8 +64,8 @@ func GetCommandValue(command string, verbose bool) (Commander, error) { var cmdName string var cmdArgs string if firstSpace <= 0 { - cmdName = command[:len(command)] - cmdArgs = "" + cmdName = command + cmdArgs = utils.EmptyString } else { cmdName = command[:firstSpace] cmdArgs = command[firstSpace+1:] diff --git a/console/command_executer.go b/console/command_executer.go index 53be29c4b..5fc7e039e 100644 --- a/console/command_executer.go +++ b/console/command_executer.go @@ -104,11 +104,11 @@ func (ce *CommandExecuter) ClientArgs() (args []string) { // To be overwritten by commands that do not need a rpc call func (ce *CommandExecuter) LocalExecute() string { - return "" + return utils.EmptyString } func ToJSON(line string) (jsn []byte) { - if !strings.Contains(line, "=") && line != "" { + if !strings.Contains(line, utils.AttrValueSep) && line != utils.EmptyString { line = fmt.Sprintf("Item=\"%s\"", line) } jsn = append(jsn, '{') @@ -117,7 +117,7 @@ func ToJSON(line string) (jsn []byte) { jsn = append(jsn, []byte(fmt.Sprintf("\"%s\":%s,", group[1], group[2]))...) } } - jsn = bytes.TrimRight(jsn, ",") + jsn = bytes.TrimRight(jsn, utils.FIELDS_SEP) jsn = append(jsn, '}') return } @@ -155,11 +155,11 @@ func getStringValue(v interface{}, defaultDurationFields utils.StringSet) string } func getSliceAsString(mp []interface{}, defaultDurationFields utils.StringSet) (out string) { - out = "[" + out = utils.IdxStart for _, v := range mp { out += fmt.Sprintf(`%s,`, getStringValue(v, defaultDurationFields)) } - return strings.TrimSuffix(out, ",") + "]" + return strings.TrimSuffix(out, utils.FIELDS_SEP) + utils.IdxEnd } func getMapAsString(mp map[string]interface{}, defaultDurationFields utils.StringSet) (out string) { @@ -175,19 +175,19 @@ func getMapAsString(mp map[string]interface{}, defaultDurationFields utils.Strin keylist = append(keylist, fmt.Sprintf(`"%s":%s`, k, getStringValue(v, defaultDurationFields))) } sort.Strings(keylist) - return fmt.Sprintf(`{%s}`, strings.Join(keylist, ",")) + return fmt.Sprintf(`{%s}`, strings.Join(keylist, utils.FIELDS_SEP)) } func GetFormatedResult(result interface{}, defaultDurationFields utils.StringSet) string { jsonResult, _ := json.Marshal(result) var mp map[string]interface{} if err := json.Unmarshal(jsonResult, &mp); err != nil { - out, _ := json.MarshalIndent(result, "", " ") + out, _ := json.MarshalIndent(result, utils.EmptyString, " ") return string(out) } mpstr := getMapAsString(mp, defaultDurationFields) var out bytes.Buffer - json.Indent(&out, []byte(mpstr), "", " ") + json.Indent(&out, []byte(mpstr), utils.EmptyString, " ") return out.String() } @@ -195,16 +195,16 @@ func GetFormatedSliceResult(result interface{}, defaultDurationFields utils.Stri jsonResult, _ := json.Marshal(result) var mp []interface{} if err := json.Unmarshal(jsonResult, &mp); err != nil { - out, _ := json.MarshalIndent(result, "", " ") + out, _ := json.MarshalIndent(result, utils.EmptyString, " ") return string(out) } mpstr := getSliceAsString(mp, defaultDurationFields) var out bytes.Buffer - json.Indent(&out, []byte(mpstr), "", " ") + json.Indent(&out, []byte(mpstr), utils.EmptyString, " ") return out.String() } func (ce *CommandExecuter) GetFormatedResult(result interface{}) string { - out, _ := json.MarshalIndent(result, "", " ") + out, _ := json.MarshalIndent(result, utils.EmptyString, " ") return string(out) } diff --git a/console/cost.go b/console/cost.go index 08e35b57f..0a25918ac 100644 --- a/console/cost.go +++ b/console/cost.go @@ -28,7 +28,7 @@ func init() { c := &CmdGetCost{ name: "cost", rpcMethod: utils.APIerSv1GetCost, - clientArgs: []string{"Tenant", "Category", "Subject", "AnswerTime", "Destination", "Usage"}, + clientArgs: []string{utils.Tenant, utils.Category, utils.Subject, utils.AnswerTime, utils.Destination, utils.Usage}, rpcParams: &v1.AttrGetCost{}, } commands[c.Name()] = c @@ -73,9 +73,9 @@ func (self *CmdGetCost) ClientArgs() []string { func (self *CmdGetCost) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "Usage": {}, - "GroupIntervalStart": {}, - "RateIncrement": {}, - "RateUnit": {}, + utils.Usage: {}, + utils.GroupIntervalStart: {}, + utils.RateIncrement: {}, + utils.RateUnit: {}, }) } diff --git a/console/cost_details.go b/console/cost_details.go index af38265ad..a11a60d08 100644 --- a/console/cost_details.go +++ b/console/cost_details.go @@ -66,9 +66,9 @@ func (self *CmdGetCostDetails) RpcResult() interface{} { func (self *CmdGetCostDetails) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "Usage": {}, - "GroupIntervalStart": {}, - "RateIncrement": {}, - "RateUnit": {}, + utils.Usage: {}, + utils.GroupIntervalStart: {}, + utils.RateIncrement: {}, + utils.RateUnit: {}, }) } diff --git a/console/datacost.go b/console/datacost.go index 8b986ef3f..fa7647951 100644 --- a/console/datacost.go +++ b/console/datacost.go @@ -28,7 +28,7 @@ func init() { c := &CmdGetDataCost{ name: "datacost", rpcMethod: utils.APIerSv1GetDataCost, - clientArgs: []string{"Category", "Tenant", "Account", "Subject", "StartTime", "Usage"}, + clientArgs: []string{utils.Category, utils.Tenant, utils.Account, utils.Subject, utils.StartTime, utils.Usage}, } commands[c.Name()] = c c.CommandExecuter = &CommandExecuter{c} @@ -72,9 +72,9 @@ func (self *CmdGetDataCost) ClientArgs() []string { func (self *CmdGetDataCost) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "Usage": {}, - "GroupIntervalStart": {}, - "RateIncrement": {}, - "RateUnit": {}, + utils.Usage: {}, + utils.GroupIntervalStart: {}, + utils.RateIncrement: {}, + utils.RateUnit: {}, }) } diff --git a/console/debit.go b/console/debit.go index 5e9c8084c..06da7493a 100644 --- a/console/debit.go +++ b/console/debit.go @@ -27,7 +27,7 @@ func init() { c := &CmdDebit{ name: "debit", rpcMethod: utils.ResponderDebit, - clientArgs: []string{"Category", "ToR", "Tenant", "Subject", "Account", "Destination", "TimeStart", "TimeEnd", "CallDuration", "FallbackSubject", "DryRun"}, + clientArgs: []string{utils.Category, utils.ToR, utils.Tenant, utils.Subject, utils.Account, utils.Destination, utils.TimeStart, utils.TimeEnd, utils.CallDuration, utils.FallbackSubject, utils.DryRun}, } commands[c.Name()] = c c.CommandExecuter = &CommandExecuter{c} diff --git a/console/debit_max.go b/console/debit_max.go index 494a04296..b39f507a6 100644 --- a/console/debit_max.go +++ b/console/debit_max.go @@ -27,7 +27,7 @@ func init() { c := &CmdMaxDebit{ name: "debit_max", rpcMethod: utils.ResponderMaxDebit, - clientArgs: []string{"Category", "ToR", "Tenant", "Subject", "Account", "Destination", "TimeStart", "TimeEnd", "CallDuration", "FallbackSubject"}, + clientArgs: []string{utils.Category, utils.ToR, utils.Tenant, utils.Subject, utils.Account, utils.Destination, utils.TimeStart, utils.TimeEnd, utils.CallDuration, utils.FallbackSubject}, } commands[c.Name()] = c c.CommandExecuter = &CommandExecuter{c} diff --git a/console/maxduration.go b/console/maxduration.go index a172fb994..a57bc9d14 100644 --- a/console/maxduration.go +++ b/console/maxduration.go @@ -31,7 +31,7 @@ func init() { c := &CmdGetMaxDuration{ name: "maxduration", rpcMethod: utils.ResponderGetMaxSessionTime, - clientArgs: []string{"Category", "ToR", "Tenant", "Subject", "Account", "Destination", "TimeStart", "TimeEnd", "CallDuration", "FallbackSubject"}, + clientArgs: []string{utils.Category, utils.ToR, utils.Tenant, utils.Subject, utils.Account, utils.Destination, utils.TimeStart, utils.TimeEnd, utils.CallDuration, utils.FallbackSubject}, } commands[c.Name()] = c c.CommandExecuter = &CommandExecuter{c} @@ -81,6 +81,6 @@ func (self *CmdGetMaxDuration) GetFormatedResult(result interface{}) string { if tv, canCast := result.(*time.Duration); canCast { return fmt.Sprintf(`"%s"`, tv.String()) } - out, _ := json.MarshalIndent(result, "", " ") + out, _ := json.MarshalIndent(result, utils.EmptyString, " ") return string(out) } diff --git a/console/maxusage.go b/console/maxusage.go index e992f7b7b..0792607c8 100644 --- a/console/maxusage.go +++ b/console/maxusage.go @@ -18,15 +18,18 @@ along with this program. If not, see package console -import "github.com/cgrates/cgrates/engine" +import ( + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) func init() { c := &CmdGetMaxUsage{ name: "maxusage", - rpcMethod: "APIerSv1.GetMaxUsage", - clientArgs: []string{"ToR", "RequestType", "Tenant", - "Category", "Account", "Subject", "Destination", - "SetupTime", "AnswerTime", "Usage", "ExtraFields"}, + rpcMethod: utils.APIerSv1GetMaxUsage, + clientArgs: []string{utils.ToR, utils.RequestType, utils.Tenant, + utils.Category, utils.Account, utils.Subject, utils.Destination, + utils.SetupTime, utils.AnswerTime, utils.Usage, utils.ExtraFields}, } commands[c.Name()] = c c.CommandExecuter = &CommandExecuter{c} diff --git a/console/parse.go b/console/parse.go index 3328b9375..ebce7b134 100644 --- a/console/parse.go +++ b/console/parse.go @@ -20,6 +20,7 @@ package console import ( "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" ) func init() { @@ -48,7 +49,7 @@ func (self *CmdParse) Name() string { } func (self *CmdParse) RpcMethod() string { - return "" + return utils.EmptyString } func (self *CmdParse) RpcParams(reset bool) interface{} { @@ -67,10 +68,10 @@ func (self *CmdParse) PostprocessRpcParams() error { } func (self *CmdParse) LocalExecute() string { - if self.rpcParams.Expression == "" { + if self.rpcParams.Expression == utils.EmptyString { return "Empty expression error" } - if self.rpcParams.Value == "" { + if self.rpcParams.Value == utils.EmptyString { return "Empty value error" } if rsrField, err := config.NewRSRParser(self.rpcParams.Expression); err != nil { diff --git a/console/passive_sessions.go b/console/passive_sessions.go index 05286ec75..430205e4b 100644 --- a/console/passive_sessions.go +++ b/console/passive_sessions.go @@ -68,9 +68,9 @@ func (self *CmdPassiveSessions) RpcResult() interface{} { func (self *CmdPassiveSessions) GetFormatedResult(result interface{}) string { return GetFormatedSliceResult(result, utils.StringSet{ - "Usage": {}, - "DurationIndex": {}, - "MaxRateUnit": {}, - "DebitInterval": {}, + utils.Usage: {}, + utils.DurationIndex: {}, + utils.MaxRateUnit: {}, + utils.DebitInterval: {}, }) } diff --git a/console/resources_profile.go b/console/resources_profile.go index 1afd5e297..6d82cf634 100644 --- a/console/resources_profile.go +++ b/console/resources_profile.go @@ -67,6 +67,6 @@ func (self *CmdGetResourceProfile) RpcResult() interface{} { func (self *CmdGetResourceProfile) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "UsageTTL": {}, + utils.UsageTTL: {}, }) } diff --git a/console/session_initiate.go b/console/session_initiate.go index 2d16eb7ab..791e43f1f 100644 --- a/console/session_initiate.go +++ b/console/session_initiate.go @@ -77,7 +77,7 @@ func (self *CmdSessionsInitiate) RpcResult() interface{} { func (self *CmdSessionsInitiate) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "Usage": {}, - "MaxUsage": {}, + utils.Usage: {}, + utils.CapMaxUsage: {}, }) } diff --git a/console/session_process_message.go b/console/session_process_message.go index 59d3f5fb6..8ee5fe0bd 100644 --- a/console/session_process_message.go +++ b/console/session_process_message.go @@ -77,7 +77,7 @@ func (self *CmdSessionsProcessEvent) RpcResult() interface{} { func (self *CmdSessionsProcessEvent) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "Usage": {}, - "MaxUsage": {}, + utils.Usage: {}, + utils.CapMaxUsage: {}, }) } diff --git a/console/session_update.go b/console/session_update.go index a8af6c98e..48c35244e 100644 --- a/console/session_update.go +++ b/console/session_update.go @@ -77,7 +77,7 @@ func (self *CmdSessionsUpdate) RpcResult() interface{} { func (self *CmdSessionsUpdate) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "Usage": {}, - "MaxUsage": {}, + utils.Usage: {}, + utils.CapMaxUsage: {}, }) } diff --git a/console/stats_profile.go b/console/stats_profile.go index b33532c27..ffd7adb22 100644 --- a/console/stats_profile.go +++ b/console/stats_profile.go @@ -67,6 +67,6 @@ func (self *CmdGetStatQueueProfile) RpcResult() interface{} { func (self *CmdGetStatQueueProfile) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "TTL": {}, + utils.TTL: {}, }) } diff --git a/console/threshold.go b/console/threshold.go index 732a6b8d1..159c30980 100644 --- a/console/threshold.go +++ b/console/threshold.go @@ -69,6 +69,6 @@ func (self *CmdGetThreshold) RpcResult() interface{} { func (self *CmdGetThreshold) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "MinSleep": {}, + utils.MinSleep: {}, }) } diff --git a/console/thresholds_profile.go b/console/thresholds_profile.go index 53d67097b..fe208593f 100644 --- a/console/thresholds_profile.go +++ b/console/thresholds_profile.go @@ -69,6 +69,6 @@ func (self *CmdGetThresholdProfile) RpcResult() interface{} { func (self *CmdGetThresholdProfile) GetFormatedResult(result interface{}) string { return GetFormatedResult(result, utils.StringSet{ - "MinSleep": {}, + utils.MinSleep: {}, }) } diff --git a/console/triggers.go b/console/triggers.go index c6587cfc6..07b523ad2 100644 --- a/console/triggers.go +++ b/console/triggers.go @@ -68,6 +68,6 @@ func (self *CmdGetTriggers) RpcResult() interface{} { func (self *CmdGetTriggers) GetFormatedResult(result interface{}) string { return GetFormatedSliceResult(result, utils.StringSet{ - "MinSleep": {}, + utils.MinSleep: {}, }) } diff --git a/utils/consts.go b/utils/consts.go index f304b6a60..8313b40c0 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -311,6 +311,15 @@ const ( SetupTime = "SetupTime" AnswerTime = "AnswerTime" Usage = "Usage" + DurationIndex = "DurationIndex" + MaxRateUnit = "MaxRateUnit" + DebitInterval = "DebitInterval" + TimeStart = "TimeStart" + TimeEnd = "TimeEnd" + CallDuration = "CallDuration" + FallbackSubject = "FallbackSubject" + DryRun = "DryRun" + ExtraFields = "ExtraFields" CustomValue = "CustomValue" Value = "Value" LastUsed = "LastUsed" @@ -1403,6 +1412,7 @@ const ( APIerSv1RemoveActionTrigger = "APIerSv1.RemoveActionTrigger" APIerSv1GetAccount = "APIerSv1.GetAccount" APIerSv1GetAttributeProfileIDsCount = "APIerSv1.GetAttributeProfileIDsCount" + APIerSv1GetMaxUsage = "APIerSv1.GetMaxUsage" ) // APIerSv1 TP APIs