Ejemplo n.º 1
0
Archivo: cdr.go Proyecto: rinor/cgrates
func (cdr *CDR) formatField(cfgFld *config.CfgCdrField, httpSkipTlsCheck bool, groupedCDRs []*CDR) (fmtOut string, err error) {
	layout := cfgFld.Layout
	if layout == "" {
		layout = time.RFC3339
	}
	var outVal string
	switch cfgFld.Type {
	case utils.META_FILLER:
		outVal = cfgFld.Value.Id()
		cfgFld.Padding = "right"
	case utils.META_CONSTANT:
		outVal = cfgFld.Value.Id()
	case utils.MetaDateTime: // Convert the requested field value into datetime with layout
		rawVal, err := cdr.exportFieldValue(cfgFld)
		if err != nil {
			return "", err
		}
		if dtFld, err := utils.ParseTimeDetectLayout(rawVal, cfgFld.Timezone); err != nil { // Only one rule makes sense here
			return "", err
		} else {
			outVal = dtFld.Format(layout)
		}
	case utils.META_HTTP_POST:
		var outValByte []byte
		httpAddr := cfgFld.Value.Id()
		jsn, err := json.Marshal(cdr)
		if err != nil {
			return "", err
		}
		if len(httpAddr) == 0 {
			err = fmt.Errorf("Empty http address for field %s type %s", cfgFld.Tag, cfgFld.Type)
		} else if outValByte, err = utils.HttpJsonPost(httpAddr, httpSkipTlsCheck, jsn); err == nil {
			outVal = string(outValByte)
			if len(outVal) == 0 && cfgFld.Mandatory {
				err = fmt.Errorf("Empty result for http_post field: %s", cfgFld.Tag)
			}
		}
	case utils.META_COMBIMED:
		outVal, err = cdr.combimedCdrFieldVal(cfgFld, groupedCDRs)
	case utils.META_COMPOSED:
		outVal, err = cdr.exportFieldValue(cfgFld)
	case utils.MetaMaskedDestination:
		if len(cfgFld.MaskDestID) != 0 && CachedDestHasPrefix(cfgFld.MaskDestID, cdr.Destination) {
			outVal = "1"
		} else {
			outVal = "0"
		}
	}
	if err != nil {
		return "", err
	}
	return utils.FmtFieldWidth(outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory)

}
Ejemplo n.º 2
0
func fieldOutVal(m *diam.Message, cfgFld *config.CfgCdrField, extraParam interface{}, processorVars map[string]string) (fmtValOut string, err error) {
	var outVal string
	passAtIndex := -1
	passedAllFilters := true
	for _, fldFilter := range cfgFld.FieldFilter {
		var pass bool
		if pass, passAtIndex = passesFieldFilter(m, fldFilter, processorVars); !pass {
			passedAllFilters = false
			break
		}
	}
	if !passedAllFilters {
		return "", ErrFilterNotPassing // Not matching field filters, will have it empty
	}
	if passAtIndex == -1 {
		passAtIndex = 0 // No filter
	}
	switch cfgFld.Type {
	case utils.META_FILLER:
		outVal = cfgFld.Value.Id()
		cfgFld.Padding = "right"
	case utils.META_CONSTANT:
		outVal = cfgFld.Value.Id()
	case utils.META_HANDLER:
		switch cfgFld.HandlerId {
		case META_VALUE_EXPONENT:
			outVal, err = metaValueExponent(m, cfgFld.Value, 10) // FixMe: add here configured number of decimals
		case META_SUM:
			outVal, err = metaSum(m, cfgFld.Value, passAtIndex, 10)
		default:
			outVal, err = metaHandler(m, cfgFld.HandlerId, cfgFld.Layout, extraParam.(time.Duration))
			if err != nil {
				utils.Logger.Warning(fmt.Sprintf("<Diameter> Ignoring processing of metafunction: %s, error: %s", cfgFld.HandlerId, err.Error()))
			}
		}
	case utils.META_COMPOSED:
		outVal = composedFieldvalue(m, cfgFld.Value, 0, processorVars)
	case utils.MetaGrouped: // GroupedAVP
		outVal = composedFieldvalue(m, cfgFld.Value, passAtIndex, processorVars)
	}
	if fmtValOut, err = utils.FmtFieldWidth(outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory); err != nil {
		utils.Logger.Warning(fmt.Sprintf("<Diameter> Error when processing field template with tag: %s, error: %s", cfgFld.Tag, err.Error()))
		return "", err
	}
	return fmtValOut, nil
}
Ejemplo n.º 3
0
func fieldOutVal(m *diam.Message, cfgFld *config.CfgCdrField, extraParam interface{}) (fmtValOut string, err error) {
	var outVal string
	passAtIndex := -1
	passedAllFilters := true
	for _, fldFilter := range cfgFld.FieldFilter {
		var pass bool
		if pass, passAtIndex = passesFieldFilter(m, fldFilter); !pass {
			passedAllFilters = false
			break
		}
	}
	if !passedAllFilters {
		return "", ErrFilterNotPassing // Not matching field filters, will have it empty
	}
	if passAtIndex == -1 {
		passAtIndex = 0 // No filter
	}
	switch cfgFld.Type {
	case utils.META_FILLER:
		outVal = cfgFld.Value.Id()
		cfgFld.Padding = "right"
	case utils.META_CONSTANT:
		outVal = cfgFld.Value.Id()
	case utils.META_HANDLER:
		if cfgFld.HandlerId == META_CCA_USAGE { // Exception, usage is passed in the dur variable by CCA
			outVal = strconv.FormatFloat(extraParam.(float64), 'f', -1, 64)
		} else {
			outVal, err = metaHandler(m, cfgFld.HandlerId, cfgFld.Layout, extraParam.(time.Duration))
			if err != nil {
				utils.Logger.Warning(fmt.Sprintf("<Diameter> Ignoring processing of metafunction: %s, error: %s", cfgFld.HandlerId, err.Error()))
			}
		}
	case utils.META_COMPOSED:
		outVal = composedFieldvalue(m, cfgFld.Value, 0)
	case utils.MetaGrouped: // GroupedAVP
		outVal = composedFieldvalue(m, cfgFld.Value, passAtIndex)
	}
	if fmtValOut, err = utils.FmtFieldWidth(outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory); err != nil {
		utils.Logger.Warning(fmt.Sprintf("<Diameter> Error when processing field template with tag: %s, error: %s", cfgFld.Tag, err.Error()))
		return "", err
	}
	return fmtValOut, nil
}