mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
- Set (but comment) serverAPI options (currently distinct api and create.size BSON field are deprecated + possible others that are untested) - Rename and refactor 'TimeDecodeValue1' to 'decodeTimeValue', add nil case handling - Implement 'buildURL' function to connect to mongo (can also be used for mysql and postgres) - Update function names, variable names, and comments for clarity - Replace 'bsonx.Regex' with the Regex primitive for v1.12 compatibility - Use simple concatenation instead of Sprintf - Declare 'timeType' locally, replace global 'tTime' - Use 'RegisterTypeDecoder' to replace deprecated 'RegisterDecoder' - Simplify several functions without altering functionality - Remove redundant 'storageType' field in 'mongoStorage', use 'isDataDB' instead
136 lines
3.7 KiB
Go
136 lines
3.7 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 engine
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/cgrates/cgrates/config"
|
|
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
// Various helpers to deal with database
|
|
|
|
// NewDataDBConn creates a DataDB connection
|
|
func NewDataDBConn(dbType, host, port, name, user,
|
|
pass, marshaler, sentinelName string,
|
|
itemsCacheCfg map[string]*config.ItemOpt) (d DataDB, err error) {
|
|
switch dbType {
|
|
case utils.MetaRedis:
|
|
var dbNo int
|
|
dbNo, err = strconv.Atoi(name)
|
|
if err != nil {
|
|
utils.Logger.Crit("Redis db name must be an integer!")
|
|
return nil, err
|
|
}
|
|
if port != "" && !strings.Contains(host, ":") {
|
|
host += ":" + port
|
|
}
|
|
d, err = NewRedisStorage(host, dbNo, pass, marshaler, utils.REDIS_MAX_CONNS, sentinelName)
|
|
case utils.MetaMongo:
|
|
d, err = NewMongoStorage(host, port, name, user, pass, marshaler, nil, true)
|
|
case utils.MetaInternal:
|
|
d = NewInternalDB(nil, nil, true, itemsCacheCfg)
|
|
default:
|
|
err = fmt.Errorf("unsupported db_type <%s>", dbType)
|
|
}
|
|
return
|
|
}
|
|
|
|
// NewStorDBConn returns a StorDB(implements Storage interface) based on dbType
|
|
func NewStorDBConn(dbType, host, port, name, user, pass, marshaler, sslmode string,
|
|
maxConn, maxIdleConn, connMaxLifetime int,
|
|
stringIndexedFields, prefixIndexedFields []string,
|
|
itemsCacheCfg map[string]*config.ItemOpt) (db StorDB, err error) {
|
|
switch dbType {
|
|
case utils.MetaMongo:
|
|
db, err = NewMongoStorage(host, port, name, user, pass, marshaler, stringIndexedFields, false)
|
|
case utils.MetaPostgres:
|
|
db, err = NewPostgresStorage(host, port, name, user, pass, sslmode, maxConn, maxIdleConn, connMaxLifetime)
|
|
case utils.MetaMySQL:
|
|
db, err = NewMySQLStorage(host, port, name, user, pass, maxConn, maxIdleConn, connMaxLifetime)
|
|
case utils.MetaInternal:
|
|
db = NewInternalDB(stringIndexedFields, prefixIndexedFields, false, itemsCacheCfg)
|
|
default:
|
|
err = fmt.Errorf("unknown db '%s' valid options are [%s, %s, %s, %s]",
|
|
dbType, utils.MetaMySQL, utils.MetaMongo, utils.MetaPostgres, utils.MetaInternal)
|
|
}
|
|
return
|
|
}
|
|
|
|
func buildURL(scheme, host, port, db, user, pass string) (*url.URL, error) {
|
|
u, err := url.Parse("//" + host)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if port != "0" {
|
|
u.Host = net.JoinHostPort(u.Host, port)
|
|
}
|
|
if user != "" && pass != "" {
|
|
u.User = url.UserPassword(user, pass)
|
|
}
|
|
if db != "" {
|
|
u.Path = path.Join(u.Path, db)
|
|
}
|
|
u.Scheme = scheme
|
|
return u, nil
|
|
}
|
|
|
|
// SMCost stores one Cost coming from SM
|
|
type SMCost struct {
|
|
CGRID string
|
|
RunID string
|
|
OriginHost string
|
|
OriginID string
|
|
CostSource string
|
|
Usage time.Duration
|
|
CostDetails *EventCost
|
|
}
|
|
|
|
type AttrCDRSStoreSMCost struct {
|
|
Cost *SMCost
|
|
CheckDuplicate bool
|
|
*utils.ArgDispatcher
|
|
*utils.TenantArg
|
|
}
|
|
|
|
type ArgsV2CDRSStoreSMCost struct {
|
|
Cost *V2SMCost
|
|
CheckDuplicate bool
|
|
*utils.ArgDispatcher
|
|
*utils.TenantArg
|
|
}
|
|
|
|
type V2SMCost struct {
|
|
CGRID string
|
|
RunID string
|
|
OriginHost string
|
|
OriginID string
|
|
CostSource string
|
|
Usage time.Duration
|
|
CostDetails *EventCost
|
|
}
|