mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-25 09:08:45 +05:00
327 lines
11 KiB
Go
327 lines
11 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"
|
|
"github.com/cgrates/cgrates/cache"
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
func NewDataManager(dataDB DataDB) *DataManager {
|
|
return &DataManager{dataDB: dataDB, cacheCfg: config.CgrConfig().CacheConfig}
|
|
}
|
|
|
|
// DataManager is the data storage manager for CGRateS
|
|
// transparently manages data retrieval, further serialization and caching
|
|
type DataManager struct {
|
|
dataDB DataDB
|
|
cacheCfg config.CacheConfig
|
|
}
|
|
|
|
// DataDB exports access to dataDB
|
|
func (dm *DataManager) DataDB() DataDB {
|
|
return dm.dataDB
|
|
}
|
|
|
|
func (dm *DataManager) CacheDataFromDB(prfx string, ids []string, mustBeCached bool) (err error) {
|
|
if !utils.IsSliceMember([]string{utils.DESTINATION_PREFIX,
|
|
utils.REVERSE_DESTINATION_PREFIX,
|
|
utils.RATING_PLAN_PREFIX,
|
|
utils.RATING_PROFILE_PREFIX,
|
|
utils.ACTION_PREFIX,
|
|
utils.ACTION_PLAN_PREFIX,
|
|
utils.AccountActionPlansPrefix,
|
|
utils.ACTION_TRIGGER_PREFIX,
|
|
utils.SHARED_GROUP_PREFIX,
|
|
utils.DERIVEDCHARGERS_PREFIX,
|
|
utils.LCR_PREFIX,
|
|
utils.ALIASES_PREFIX,
|
|
utils.REVERSE_ALIASES_PREFIX,
|
|
utils.ResourceProfilesPrefix,
|
|
utils.TimingsPrefix,
|
|
utils.ResourcesPrefix}, prfx) {
|
|
return utils.NewCGRError(utils.MONGO,
|
|
utils.MandatoryIEMissingCaps,
|
|
utils.UnsupportedCachePrefix,
|
|
fmt.Sprintf("prefix <%s> is not a supported cache prefix", prfx))
|
|
}
|
|
if ids == nil {
|
|
keyIDs, err := dm.DataDB().GetKeysForPrefix(prfx)
|
|
if err != nil {
|
|
return utils.NewCGRError(utils.MONGO,
|
|
utils.ServerErrorCaps,
|
|
err.Error(),
|
|
fmt.Sprintf("DataManager error <%s> querying keys for prefix: <%s>", err.Error(), prfx))
|
|
}
|
|
for _, keyID := range keyIDs {
|
|
if mustBeCached { // Only consider loading ids which are already in cache
|
|
if _, hasIt := cache.Get(keyID); !hasIt {
|
|
continue
|
|
}
|
|
}
|
|
ids = append(ids, keyID[len(prfx):])
|
|
}
|
|
var nrItems int
|
|
if cCfg, has := dm.cacheCfg[utils.CachePrefixToInstance[prfx]]; has {
|
|
nrItems = cCfg.Limit
|
|
}
|
|
if nrItems > 0 && nrItems < len(ids) { // More ids than cache config allows it, limit here
|
|
ids = ids[:nrItems]
|
|
}
|
|
}
|
|
for _, dataID := range ids {
|
|
if mustBeCached {
|
|
if _, hasIt := cache.Get(prfx + dataID); !hasIt { // only cache if previously there
|
|
continue
|
|
}
|
|
}
|
|
switch prfx {
|
|
case utils.DESTINATION_PREFIX:
|
|
_, err = dm.DataDB().GetDestination(dataID, true, utils.NonTransactional)
|
|
case utils.REVERSE_DESTINATION_PREFIX:
|
|
_, err = dm.DataDB().GetReverseDestination(dataID, true, utils.NonTransactional)
|
|
case utils.RATING_PLAN_PREFIX:
|
|
_, err = dm.DataDB().GetRatingPlan(dataID, true, utils.NonTransactional)
|
|
case utils.RATING_PROFILE_PREFIX:
|
|
_, err = dm.DataDB().GetRatingProfile(dataID, true, utils.NonTransactional)
|
|
case utils.ACTION_PREFIX:
|
|
_, err = dm.DataDB().GetActions(dataID, true, utils.NonTransactional)
|
|
case utils.ACTION_PLAN_PREFIX:
|
|
_, err = dm.DataDB().GetActionPlan(dataID, true, utils.NonTransactional)
|
|
case utils.AccountActionPlansPrefix:
|
|
_, err = dm.DataDB().GetAccountActionPlans(dataID, true, utils.NonTransactional)
|
|
case utils.ACTION_TRIGGER_PREFIX:
|
|
_, err = dm.DataDB().GetActionTriggers(dataID, true, utils.NonTransactional)
|
|
case utils.SHARED_GROUP_PREFIX:
|
|
_, err = dm.DataDB().GetSharedGroup(dataID, true, utils.NonTransactional)
|
|
case utils.DERIVEDCHARGERS_PREFIX:
|
|
_, err = dm.DataDB().GetDerivedChargers(dataID, true, utils.NonTransactional)
|
|
case utils.LCR_PREFIX:
|
|
_, err = dm.DataDB().GetLCR(dataID, true, utils.NonTransactional)
|
|
case utils.ALIASES_PREFIX:
|
|
_, err = dm.DataDB().GetAlias(dataID, true, utils.NonTransactional)
|
|
case utils.REVERSE_ALIASES_PREFIX:
|
|
_, err = dm.DataDB().GetReverseAlias(dataID, true, utils.NonTransactional)
|
|
case utils.ResourceProfilesPrefix:
|
|
tntID := utils.NewTenantID(dataID)
|
|
_, err = dm.DataDB().GetResourceProfile(tntID.Tenant, tntID.ID, true, utils.NonTransactional)
|
|
case utils.ResourcesPrefix:
|
|
tntID := utils.NewTenantID(dataID)
|
|
_, err = dm.DataDB().GetResource(tntID.Tenant, tntID.ID, true, utils.NonTransactional)
|
|
case utils.TimingsPrefix:
|
|
_, err = dm.DataDB().GetTiming(dataID, true, utils.NonTransactional)
|
|
case utils.ThresholdProfilePrefix:
|
|
tntID := utils.NewTenantID(dataID)
|
|
_, err = dm.GetThresholdProfile(tntID.Tenant, tntID.ID, true, utils.NonTransactional)
|
|
case utils.ThresholdPrefix:
|
|
tntID := utils.NewTenantID(dataID)
|
|
_, err = dm.GetThreshold(tntID.Tenant, tntID.ID, true, utils.NonTransactional)
|
|
case utils.FilterPrefix:
|
|
tntID := utils.NewTenantID(dataID)
|
|
_, err = dm.GetFilter(tntID.Tenant, tntID.ID, true, utils.NonTransactional)
|
|
}
|
|
if err != nil {
|
|
return utils.NewCGRError(utils.MONGO,
|
|
utils.ServerErrorCaps,
|
|
err.Error(),
|
|
fmt.Sprintf("error <%s> querying mongo for category: <%s>, dataID: <%s>", err.Error(), prfx, dataID))
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetStatQueue retrieves a StatQueue from dataDB
|
|
// handles caching and deserialization of metrics
|
|
func (dm *DataManager) GetStatQueue(tenant, id string, skipCache bool, transactionID string) (sq *StatQueue, err error) {
|
|
key := utils.StatQueuePrefix + utils.ConcatenatedKey(tenant, id)
|
|
if !skipCache {
|
|
if x, ok := cache.Get(key); ok {
|
|
if x == nil {
|
|
return nil, utils.ErrNotFound
|
|
}
|
|
return x.(*StatQueue), nil
|
|
}
|
|
}
|
|
ssq, err := dm.dataDB.GetStoredStatQueue(tenant, id)
|
|
if err != nil {
|
|
if err == utils.ErrNotFound {
|
|
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
|
}
|
|
return nil, err
|
|
}
|
|
if sq, err = ssq.AsStatQueue(dm.dataDB.Marshaler()); err != nil {
|
|
return nil, err
|
|
}
|
|
cache.Set(key, sq, cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
// SetStatQueue converts to StoredStatQueue and stores the result in dataDB
|
|
func (dm *DataManager) SetStatQueue(sq *StatQueue) (err error) {
|
|
ssq, err := NewStoredStatQueue(sq, dm.dataDB.Marshaler())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return dm.dataDB.SetStoredStatQueue(ssq)
|
|
}
|
|
|
|
// RemStatQueue removes the StoredStatQueue and clears the cache for StatQueue
|
|
func (dm *DataManager) RemStatQueue(tenant, id string, transactionID string) (err error) {
|
|
if err = dm.dataDB.RemStoredStatQueue(tenant, id); err != nil {
|
|
return
|
|
}
|
|
cache.RemKey(utils.StatQueuePrefix+utils.ConcatenatedKey(tenant, id), cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
func (dm *DataManager) GetFilter(tenant, id string, skipCache bool, transactionID string) (fltr *Filter, err error) {
|
|
key := utils.FilterPrefix + utils.ConcatenatedKey(tenant, id)
|
|
if !skipCache {
|
|
if x, ok := cache.Get(key); ok {
|
|
if x == nil {
|
|
return nil, utils.ErrNotFound
|
|
}
|
|
return x.(*Filter), nil
|
|
}
|
|
}
|
|
fltr, err = dm.dataDB.GetFilterDrv(tenant, id)
|
|
if err != nil {
|
|
if err == utils.ErrNotFound {
|
|
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
|
}
|
|
return nil, err
|
|
}
|
|
cache.Set(key, fltr, cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
func (dm *DataManager) SetFilter(fltr *Filter) (err error) {
|
|
return dm.DataDB().SetFilterDrv(fltr)
|
|
}
|
|
|
|
func (dm *DataManager) RemoveFilter(tenant, id, transactionID string) (err error) {
|
|
if err = dm.DataDB().RemoveFilterDrv(tenant, id); err != nil {
|
|
return
|
|
}
|
|
cache.RemKey(utils.FilterPrefix+utils.ConcatenatedKey(tenant, id),
|
|
cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
func (dm *DataManager) GetThreshold(tenant, id string, skipCache bool, transactionID string) (th *Threshold, err error) {
|
|
key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id)
|
|
if !skipCache {
|
|
if x, ok := cache.Get(key); ok {
|
|
if x == nil {
|
|
return nil, utils.ErrNotFound
|
|
}
|
|
return x.(*Threshold), nil
|
|
}
|
|
}
|
|
th, err = dm.dataDB.GetThresholdDrv(tenant, id)
|
|
if err != nil {
|
|
if err == utils.ErrNotFound {
|
|
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
|
}
|
|
return nil, err
|
|
}
|
|
cache.Set(key, th, cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
func (dm *DataManager) SetThreshold(th *Threshold) (err error) {
|
|
return dm.DataDB().SetThresholdDrv(th)
|
|
}
|
|
|
|
func (dm *DataManager) RemoveThreshold(tenant, id, transactionID string) (err error) {
|
|
if err = dm.DataDB().RemoveThresholdDrv(tenant, id); err != nil {
|
|
return
|
|
}
|
|
cache.RemKey(utils.ThresholdPrefix+utils.ConcatenatedKey(tenant, id),
|
|
cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
func (dm *DataManager) GetThresholdProfile(tenant, id string, skipCache bool, transactionID string) (th *ThresholdProfile, err error) {
|
|
key := utils.ThresholdProfilePrefix + utils.ConcatenatedKey(tenant, id)
|
|
if !skipCache {
|
|
if x, ok := cache.Get(key); ok {
|
|
if x == nil {
|
|
return nil, utils.ErrNotFound
|
|
}
|
|
return x.(*ThresholdProfile), nil
|
|
}
|
|
}
|
|
th, err = dm.dataDB.GetThresholdProfileDrv(tenant, id)
|
|
if err != nil {
|
|
if err == utils.ErrNotFound {
|
|
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
|
}
|
|
return nil, err
|
|
}
|
|
cache.Set(key, th, cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
func (dm *DataManager) SetThresholdProfile(th *ThresholdProfile) (err error) {
|
|
return dm.DataDB().SetThresholdProfileDrv(th)
|
|
}
|
|
|
|
func (dm *DataManager) RemoveThresholdProfile(tenant, id, transactionID string) (err error) {
|
|
if err = dm.DataDB().RemThresholdProfileDrv(tenant, id); err != nil {
|
|
return
|
|
}
|
|
cache.RemKey(utils.ThresholdProfilePrefix+utils.ConcatenatedKey(tenant, id),
|
|
cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
func (dm *DataManager) GetStatQueueProfile(tenant, id string, skipCache bool, transactionID string) (sqp *StatQueueProfile, err error) {
|
|
key := utils.StatQueueProfilePrefix + utils.ConcatenatedKey(tenant, id)
|
|
if !skipCache {
|
|
if x, ok := cache.Get(key); ok {
|
|
if x == nil {
|
|
return nil, utils.ErrNotFound
|
|
}
|
|
return x.(*StatQueueProfile), nil
|
|
}
|
|
}
|
|
sqp, err = dm.dataDB.GetStatQueueProfileDrv(tenant, id)
|
|
if err != nil {
|
|
if err == utils.ErrNotFound {
|
|
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
|
}
|
|
return nil, err
|
|
}
|
|
cache.Set(key, sqp, cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|
|
|
|
func (dm *DataManager) SetStatQueueProfile(sqp *StatQueueProfile) (err error) {
|
|
return dm.DataDB().SetStatQueueProfileDrv(sqp)
|
|
}
|
|
|
|
func (dm *DataManager) RemoveStatQueueProfile(tenant, id, transactionID string) (err error) {
|
|
if err = dm.DataDB().RemStatQueueProfileDrv(tenant, id); err != nil {
|
|
return
|
|
}
|
|
cache.RemKey(utils.StatQueueProfilePrefix+utils.ConcatenatedKey(tenant, id),
|
|
cacheCommit(transactionID), transactionID)
|
|
return
|
|
}
|