Example #1
0
func (r *RazladkiSender) send(data tasks.DataType, timestamp uint64) (*RazladkiResult, error) {
	logger.Debugf("%s Data to send: %v", r.id, data)
	result := RazladkiResult{
		Timestamp: timestamp,
		Params:    make(map[string]Param),
		Alarms:    make(map[string]Alarm),
	}
	for aggname, title := range r.Items {
		var root, metricname string
		items := strings.SplitN(aggname, ".", 2)
		if len(items) > 1 {
			root, metricname = items[0], items[1]
		} else {
			root = items[0]
		}
		for subgroup, value := range data[root] {
			rv := reflect.ValueOf(value)
			switch rv.Kind() {
			case reflect.Slice, reflect.Array:
				// unsupported
			case reflect.Map:
				if len(metricname) == 0 {
					continue
				}

				key := reflect.ValueOf(metricname)
				mapVal := rv.MapIndex(key)
				if !mapVal.IsValid() {
					continue
				}

				value := reflect.ValueOf(mapVal.Interface())

				switch value.Kind() {
				case reflect.Slice, reflect.Array:
					// unsupported
				case reflect.Map:
					// unsupported
				default:
					name := fmt.Sprintf("%s_%s", subgroup, metricname)
					result.Push(name, common.InterfaceToString(value.Interface()), title)
				}
			default:
				if len(metricname) != 0 {
					continue
				}
				name := fmt.Sprintf("%s_%s", subgroup, root)
				result.Push(name, common.InterfaceToString(value), title)
			}
		}
	}
	return &result, nil
}
Example #2
0
func (as *AgaveSender) send(data tasks.DataType) (map[string][]string, error) {
	// Repack data by subgroups
	logger.Debugf("%s Data to send: %v", as.Id, data)
	var repacked map[string][]string = make(map[string][]string)
	for _, aggname := range as.Items {
		var root, metricname string
		items := strings.SplitN(aggname, ".", 2)
		if len(items) > 1 {
			root, metricname = items[0], items[1]
		} else {
			root = items[0]
		}
		for subgroup, value := range data[root] {
			rv := reflect.ValueOf(value)
			switch rv.Kind() {
			case reflect.Slice, reflect.Array:
				if len(metricname) != 0 {
					// we expect neted map here
					continue
				}
				if len(as.Fields) == 0 || len(as.Fields) != rv.Len() {
					logger.Errf("%s Unable to send a slice. Fields len %d, len of value %d", as.Id, len(as.Fields), rv.Len())
					continue
				}

				forJoin := make([]string, 0, len(as.Fields))
				for i, field := range as.Fields {
					forJoin = append(forJoin, fmt.Sprintf("%s:%s", field, common.InterfaceToString(rv.Index(i).Interface())))
				}

				repacked[subgroup] = append(repacked[subgroup], strings.Join(forJoin, "+"))
			case reflect.Map:
				if len(metricname) == 0 {
					continue
				}

				key := reflect.ValueOf(metricname)
				mapVal := rv.MapIndex(key)
				if !mapVal.IsValid() {
					continue
				}

				value := reflect.ValueOf(mapVal.Interface())

				switch value.Kind() {
				case reflect.Slice, reflect.Array:
					if len(as.Fields) == 0 || len(as.Fields) != value.Len() {
						logger.Errf("%s Unable to send a slice. Fields len %d, len of value %d", as.Id, len(as.Fields), rv.Len())
						continue
					}
					forJoin := make([]string, 0, len(as.Fields))
					for i, field := range as.Fields {
						forJoin = append(forJoin, fmt.Sprintf("%s:%s", field, common.InterfaceToString(value.Index(i).Interface())))
					}
					repacked[subgroup] = append(repacked[subgroup], strings.Join(forJoin, "+"))
				case reflect.Map:
					//unsupported
				default:
					repacked[subgroup] = append(repacked[subgroup], fmt.Sprintf("%s:%s", metricname, common.InterfaceToString(value.Interface())))
				}

				// }
			default:
				if len(metricname) != 0 {
					// we expect neted map here
					continue
				}
				repacked[subgroup] = append(repacked[subgroup], fmt.Sprintf("%s:%s", root, common.InterfaceToString(value)))
			}
		}
	}

	return repacked, nil
}