/
storage_utils.go
135 lines (120 loc) · 3.69 KB
/
storage_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
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
}