/* 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 PURPOev. 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 */ package utils import ( "fmt" "reflect" "strconv" "strings" "time" ) // CGREvent is a generic event processed by CGR services type CGREvent struct { Tenant string ID string Time *time.Time // event time Event map[string]interface{} } func (ev *CGREvent) HasField(fldName string) (has bool) { _, has = ev.Event[fldName] return } func (ev *CGREvent) CheckMandatoryFields(fldNames []string) error { for _, fldName := range fldNames { if _, has := ev.Event[fldName]; !has { return NewErrMandatoryIeMissing(fldName) } } return nil } // AnswerTime returns a field as string instance func (ev *CGREvent) FieldAsString(fldName string) (val string, err error) { iface, has := ev.Event[fldName] if !has { return "", ErrNotFound } val, err = IfaceAsString(iface) if err != nil { return "", fmt.Errorf("cannot cast %s to string", fldName) } return val, nil } // FieldAsTime returns a field as Time instance func (ev *CGREvent) FieldAsTime(fldName string, timezone string) (t time.Time, err error) { iface, has := ev.Event[fldName] if !has { err = ErrNotFound return } var canCast bool if t, canCast = iface.(time.Time); canCast { return } s, canCast := iface.(string) if !canCast { err = fmt.Errorf("cannot cast %s to string", fldName) return } return ParseTimeDetectLayout(s, timezone) } // FieldAsDuration returns a field as Duration instance func (ev *CGREvent) FieldAsDuration(fldName string) (d time.Duration, err error) { iface, has := ev.Event[fldName] if !has { err = ErrNotFound return } var canCast bool if d, canCast = iface.(time.Duration); canCast { return } if f, canCast := iface.(float64); canCast { return time.Duration(int64(f)), nil } s, canCast := iface.(string) if !canCast { err = fmt.Errorf("cannot cast %s to string", fldName) return } return ParseDurationWithNanosecs(s) } // FieldAsFloat64 returns a field as float64 instance func (ev *CGREvent) FieldAsFloat64(fldName string) (f float64, err error) { iface, has := ev.Event[fldName] if !has { return f, ErrNotFound } if val, canCast := iface.(float64); canCast { return val, nil } csStr, canCast := iface.(string) if !canCast { err = fmt.Errorf("cannot cast %s to string", fldName) return } return strconv.ParseFloat(csStr, 64) } func (ev *CGREvent) TenantID() string { return ConcatenatedKey(ev.Tenant, ev.ID) } func (ev *CGREvent) FilterableEvent(fltredFields []string) (fEv map[string]interface{}) { fEv = make(map[string]interface{}) if len(fltredFields) == 0 { i := 0 fltredFields = make([]string, len(ev.Event)) for k := range ev.Event { fltredFields[i] = k i++ } } for _, fltrFld := range fltredFields { fldVal, has := ev.Event[fltrFld] if !has { continue // the field does not exist in map, ignore it } valOf := reflect.ValueOf(fldVal) if valOf.Kind() == reflect.String { fEv[fltrFld] = StringToInterface(valOf.String()) // attempt converting from string to comparable interface } else { fEv[fltrFld] = fldVal } } return } func (ev *CGREvent) Clone() (clned *CGREvent) { clned = &CGREvent{ Tenant: ev.Tenant, ID: ev.ID, Event: make(map[string]interface{}), // a bit forced but safe } if ev.Time != nil { clned.Time = TimePointer(*ev.Time) } for k, v := range ev.Event { clned.Event[k] = v } return } // RemFldsWithPrefix will remove fields starting with prefix from event func (ev *CGREvent) RemFldsWithPrefix(prfx string) { for fldName := range ev.Event { if strings.HasPrefix(fldName, prfx) { delete(ev.Event, fldName) } } } // RemFldsWithPrefix will remove fields starting with prefix from event func (ev *CGREvent) ConsumeArgDispatcher() (arg *ArgDispatcher) { if ev == nil { return } //check if we have APIKey in event and in case it has add it in ArgDispatcher apiKeyIface, hasApiKey := ev.Event[MetaApiKey] if hasApiKey { delete(ev.Event, MetaApiKey) arg = &ArgDispatcher{ APIKey: StringPointer(apiKeyIface.(string)), } } //check if we have RouteID in event and in case it has add it in ArgDispatcher routeIDIface, hasRouteID := ev.Event[MetaRouteID] if !hasRouteID { return } delete(ev.Event, MetaRouteID) if !hasApiKey { //in case we don't have APIKey, but we have RouteID we need to initialize the struct return &ArgDispatcher{ RouteID: StringPointer(routeIDIface.(string)), } } arg.RouteID = StringPointer(routeIDIface.(string)) return } // CGREvents is a group of generic events processed by CGR services // ie: derived CDRs type CGREvents struct { Tenant string ID string Time *time.Time // event time Events []map[string]interface{} } type CGREventWithArgDispatcher struct { *CGREvent *ArgDispatcher } func (ev *CGREventWithArgDispatcher) Clone() (clned *CGREventWithArgDispatcher) { clned = &CGREventWithArgDispatcher{ CGREvent: &CGREvent{ Tenant: ev.Tenant, ID: ev.ID, Event: make(map[string]interface{}), // a bit forced but safe }, } if ev.Time != nil { clned.Time = TimePointer(*ev.Time) } for k, v := range ev.Event { clned.Event[k] = v } if ev.ArgDispatcher != nil { clned.ArgDispatcher = new(ArgDispatcher) *clned.ArgDispatcher = *ev.ArgDispatcher } return }