commons-go/sensorconfig/model/ResultSetCol.go
crusader 796da7bbfe ing
2018-04-12 18:38:04 +09:00

86 lines
1.7 KiB
Go

package model
import (
"encoding/json"
"fmt"
"strconv"
"strings"
cuej "git.loafle.net/commons_go/util/encoding/json"
)
type ResultSetCol struct {
*ResultSet
}
func (rsc *ResultSetCol) setMeta() {
meta := rsc.Item.Keys
arrayColumns := rsc.Item.MappingInfo.ArrayColumns
if nil == rsc.Meta {
rsc.Meta = make(map[string]json.Number, 0)
}
for i := 0; i < len(meta); i++ {
rsc.Meta[meta[i].Key] = json.Number(strconv.Itoa(i))
}
if nil != arrayColumns {
for i := 0; i < len(arrayColumns); i++ {
if _, ok := rsc.Meta[arrayColumns[i]]; ok {
continue
}
rsc.Meta[arrayColumns[i]] = json.Number(strconv.Itoa(i + len(meta)))
}
}
}
func (rsc *ResultSetCol) GetData() map[string]string {
return rsc.parse()
}
func (rsc *ResultSetCol) parse() map[string]string {
metrics := rsc.Item.Keys
arrayColumns := rsc.Item.MappingInfo.ArrayColumns
rm := make(map[string]string, 0)
for i := 0; i < len(rsc.Rows); i++ {
row := rsc.Rows[i]
for j := 0; j < len(row); j++ {
arrayValue := make([]string, 0)
if nil != arrayColumns {
for k := 0; k < len(arrayColumns); k++ {
idxN := rsc.Meta[arrayColumns[k]]
idx, _ := cuej.NumberToInt(idxN)
arrayValue = append(arrayValue, row[idx])
}
}
for k := 0; k < len(metrics); k++ {
metric := metrics[k].Metric
metric = rsc.convertMetric(metric, arrayValue)
rm[metric] = row[k]
}
}
}
return rm
}
func (rsc *ResultSetCol) convertMetric(metric string, arrayValue []string) string {
if nil == arrayValue || 0 == len(arrayValue) {
return metric
}
convertChar := "$"
for i := 0; i < len(arrayValue); i++ {
convertStr := fmt.Sprintf("%s%d", convertChar, i)
metric = strings.Replace(metric, convertStr, arrayValue[i], -1)
}
return metric
}