mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-17 06:09:53 +05:00
Merge branch 'master' into hapool
This commit is contained in:
@@ -19,8 +19,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/cgrates/cgrates/cache2go"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
@@ -57,6 +60,7 @@ const (
|
||||
type MongoStorage struct {
|
||||
session *mgo.Session
|
||||
db *mgo.Database
|
||||
ms Marshaler
|
||||
}
|
||||
|
||||
func NewMongoStorage(host, port, db, user, pass string) (*MongoStorage, error) {
|
||||
@@ -77,7 +81,7 @@ func NewMongoStorage(host, port, db, user, pass string) (*MongoStorage, error) {
|
||||
Background: false, // Build index in background and return immediately
|
||||
Sparse: false, // Only index documents containing the Key fields
|
||||
}
|
||||
collections := []string{colAct, colApl, colAtr, colDcs, colAls, colUsr, colLcr, colLht}
|
||||
collections := []string{colAct, colApl, colAtr, colDcs, colAls, colUsr, colLcr, colLht, colRpl, colDst}
|
||||
for _, col := range collections {
|
||||
if err = ndb.C(col).EnsureIndex(index); err != nil {
|
||||
return nil, err
|
||||
@@ -90,7 +94,7 @@ func NewMongoStorage(host, port, db, user, pass string) (*MongoStorage, error) {
|
||||
Background: false,
|
||||
Sparse: false,
|
||||
}
|
||||
collections = []string{colDst, colRpf, colRpl, colDst, colShg, colAcc, colCrs}
|
||||
collections = []string{colRpf, colShg, colAcc, colCrs}
|
||||
for _, col := range collections {
|
||||
if err = ndb.C(col).EnsureIndex(index); err != nil {
|
||||
return nil, err
|
||||
@@ -200,7 +204,8 @@ func NewMongoStorage(host, port, db, user, pass string) (*MongoStorage, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &MongoStorage{db: ndb, session: session}, err
|
||||
|
||||
return &MongoStorage{db: ndb, session: session, ms: NewCodecMsgpackMarshaler()}, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) Close() {
|
||||
@@ -275,10 +280,10 @@ func (ms *MongoStorage) cacheRating(dKeys, rpKeys, rpfKeys, lcrKeys, dcsKeys, ac
|
||||
if dKeys == nil || (float64(cache2go.CountEntries(utils.DESTINATION_PREFIX))*utils.DESTINATIONS_LOAD_THRESHOLD < float64(len(dKeys))) {
|
||||
// if need to load more than a half of exiting keys load them all
|
||||
utils.Logger.Info("Caching all destinations")
|
||||
iter := ms.db.C(colDst).Find(nil).Select(bson.M{"id": 1}).Iter()
|
||||
iter := ms.db.C(colDst).Find(nil).Select(bson.M{"key": 1}).Iter()
|
||||
dKeys = make([]string, 0)
|
||||
for iter.Next(&idResult) {
|
||||
dKeys = append(dKeys, utils.DESTINATION_PREFIX+idResult.Id)
|
||||
for iter.Next(&keyResult) {
|
||||
dKeys = append(dKeys, utils.DESTINATION_PREFIX+keyResult.Key)
|
||||
}
|
||||
if err := iter.Close(); err != nil {
|
||||
cache2go.RollbackTransaction()
|
||||
@@ -304,10 +309,10 @@ func (ms *MongoStorage) cacheRating(dKeys, rpKeys, rpfKeys, lcrKeys, dcsKeys, ac
|
||||
}
|
||||
if rpKeys == nil {
|
||||
utils.Logger.Info("Caching all rating plans")
|
||||
iter := ms.db.C(colRpl).Find(nil).Select(bson.M{"id": 1}).Iter()
|
||||
iter := ms.db.C(colRpl).Find(nil).Select(bson.M{"key": 1}).Iter()
|
||||
rpKeys = make([]string, 0)
|
||||
for iter.Next(&idResult) {
|
||||
rpKeys = append(rpKeys, utils.RATING_PLAN_PREFIX+idResult.Id)
|
||||
for iter.Next(&keyResult) {
|
||||
rpKeys = append(rpKeys, utils.RATING_PLAN_PREFIX+keyResult.Key)
|
||||
}
|
||||
if err := iter.Close(); err != nil {
|
||||
cache2go.RollbackTransaction()
|
||||
@@ -572,10 +577,10 @@ func (ms *MongoStorage) cacheAccounting(alsKeys []string) (err error) {
|
||||
func (ms *MongoStorage) HasData(category, subject string) (bool, error) {
|
||||
switch category {
|
||||
case utils.DESTINATION_PREFIX:
|
||||
count, err := ms.db.C(colDst).Find(bson.M{"id": subject}).Count()
|
||||
count, err := ms.db.C(colDst).Find(bson.M{"key": subject}).Count()
|
||||
return count > 0, err
|
||||
case utils.RATING_PLAN_PREFIX:
|
||||
count, err := ms.db.C(colRpl).Find(bson.M{"id": subject}).Count()
|
||||
count, err := ms.db.C(colRpl).Find(bson.M{"key": subject}).Count()
|
||||
return count > 0, err
|
||||
case utils.RATING_PROFILE_PREFIX:
|
||||
count, err := ms.db.C(colRpf).Find(bson.M{"id": subject}).Count()
|
||||
@@ -602,15 +607,44 @@ func (ms *MongoStorage) GetRatingPlan(key string, skipCache bool) (rp *RatingPla
|
||||
}
|
||||
}
|
||||
rp = new(RatingPlan)
|
||||
err = ms.db.C(colRpl).Find(bson.M{"id": key}).One(rp)
|
||||
var kv struct {
|
||||
Key string
|
||||
Value []byte
|
||||
}
|
||||
err = ms.db.C(colRpl).Find(bson.M{"key": key}).One(&kv)
|
||||
if err == nil {
|
||||
b := bytes.NewBuffer(kv.Value)
|
||||
r, err := zlib.NewReader(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.Close()
|
||||
err = ms.ms.Unmarshal(out, &rp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cache2go.Cache(utils.RATING_PLAN_PREFIX+key, rp)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetRatingPlan(rp *RatingPlan) error {
|
||||
_, err := ms.db.C(colRpl).Upsert(bson.M{"id": rp.Id}, rp)
|
||||
result, err := ms.ms.Marshal(rp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var b bytes.Buffer
|
||||
w := zlib.NewWriter(&b)
|
||||
w.Write(result)
|
||||
w.Close()
|
||||
_, err = ms.db.C(colRpl).Upsert(bson.M{"key": rp.Id}, &struct {
|
||||
Key string
|
||||
Value []byte
|
||||
}{Key: rp.Id, Value: b.Bytes()})
|
||||
if err == nil && historyScribe != nil {
|
||||
var response int
|
||||
historyScribe.Call("HistoryV1.Record", rp.GetHistoryRecord(), &response)
|
||||
@@ -690,20 +724,50 @@ func (ms *MongoStorage) SetLCR(lcr *LCR) error {
|
||||
|
||||
func (ms *MongoStorage) GetDestination(key string) (result *Destination, err error) {
|
||||
result = new(Destination)
|
||||
err = ms.db.C(colDst).Find(bson.M{"id": key}).One(result)
|
||||
var kv struct {
|
||||
Key string
|
||||
Value []byte
|
||||
}
|
||||
err = ms.db.C(colDst).Find(bson.M{"key": key}).One(&kv)
|
||||
if err == nil {
|
||||
b := bytes.NewBuffer(kv.Value)
|
||||
r, err := zlib.NewReader(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.Close()
|
||||
err = ms.ms.Unmarshal(out, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// create optimized structure
|
||||
for _, p := range result.Prefixes {
|
||||
cache2go.Push(utils.DESTINATION_PREFIX+p, result.Id)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
result = nil
|
||||
return
|
||||
}
|
||||
// create optimized structure
|
||||
for _, p := range result.Prefixes {
|
||||
cache2go.Push(utils.DESTINATION_PREFIX+p, result.Id)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetDestination(dest *Destination) (err error) {
|
||||
_, err = ms.db.C(colDst).Upsert(bson.M{"id": dest.Id}, dest)
|
||||
result, err := ms.ms.Marshal(dest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var b bytes.Buffer
|
||||
w := zlib.NewWriter(&b)
|
||||
w.Write(result)
|
||||
w.Close()
|
||||
_, err = ms.db.C(colDst).Upsert(bson.M{"key": dest.Id}, &struct {
|
||||
Key string
|
||||
Value []byte
|
||||
}{Key: dest.Id, Value: b.Bytes()})
|
||||
if err == nil && historyScribe != nil {
|
||||
var response int
|
||||
historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(), &response)
|
||||
@@ -1037,20 +1101,31 @@ func (ms *MongoStorage) GetActionPlan(key string, skipCache bool) (ats *ActionPl
|
||||
}
|
||||
var kv struct {
|
||||
Key string
|
||||
Value *ActionPlan
|
||||
Value []byte
|
||||
}
|
||||
err = ms.db.C(colApl).Find(bson.M{"key": key}).One(&kv)
|
||||
if err == nil {
|
||||
ats = kv.Value
|
||||
b := bytes.NewBuffer(kv.Value)
|
||||
r, err := zlib.NewReader(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.Close()
|
||||
err = ms.ms.Unmarshal(out, &ats)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cache2go.Cache(utils.ACTION_PLAN_PREFIX+key, ats)
|
||||
}
|
||||
ats.AccountIDs = utils.YesDots(ats.AccountIDs)
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetActionPlan(key string, ats *ActionPlan) error {
|
||||
// clean dots from account ids map
|
||||
ats.AccountIDs = utils.NoDots(ats.AccountIDs)
|
||||
if len(ats.ActionTimings) == 0 {
|
||||
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
|
||||
err := ms.db.C(colApl).Remove(bson.M{"key": key})
|
||||
@@ -1059,10 +1134,18 @@ func (ms *MongoStorage) SetActionPlan(key string, ats *ActionPlan) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
_, err := ms.db.C(colApl).Upsert(bson.M{"key": key}, &struct {
|
||||
result, err := ms.ms.Marshal(ats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var b bytes.Buffer
|
||||
w := zlib.NewWriter(&b)
|
||||
w.Write(result)
|
||||
w.Close()
|
||||
_, err = ms.db.C(colApl).Upsert(bson.M{"key": key}, &struct {
|
||||
Key string
|
||||
Value *ActionPlan
|
||||
}{Key: key, Value: ats})
|
||||
Value []byte
|
||||
}{Key: key, Value: b.Bytes()})
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -927,7 +927,7 @@ func (rs *RedisStorage) SetActionPlan(key string, ats *ActionPlan) (err error) {
|
||||
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
|
||||
return err
|
||||
}
|
||||
result, err := rs.ms.Marshal(&ats)
|
||||
result, err := rs.ms.Marshal(ats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1341,6 +1341,21 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose bool) (err error) {
|
||||
AccountID: accID,
|
||||
ActionsID: at.ActionsID,
|
||||
}
|
||||
if verbose {
|
||||
log.Println("\tTask: ", t)
|
||||
}
|
||||
if err = tpr.ratingStorage.PushTask(t); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(ap.AccountIDs) == 0 {
|
||||
t := &Task{
|
||||
Uuid: utils.GenUUID(),
|
||||
ActionsID: at.ActionsID,
|
||||
}
|
||||
if verbose {
|
||||
log.Println("\tTask: ", t)
|
||||
}
|
||||
if err = tpr.ratingStorage.PushTask(t); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user