commons-go/model/sensorconfig/ResultSetRow.go

106 lines
2.0 KiB
Go
Raw Normal View History

2018-04-26 07:37:59 +00:00
package sensorconfig
2018-04-12 09:38:04 +00:00
import (
"encoding/json"
"fmt"
"strconv"
"strings"
2018-04-19 08:37:19 +00:00
cuej "git.loafle.net/commons/util-go/encoding/json"
2018-04-12 09:38:04 +00:00
)
type ResultSetRow struct {
*ResultSet
}
func (rsr *ResultSetRow) setMeta() {
meta := make([]string, 0)
arrayColumns := rsr.Item.MappingInfo.ArrayColumns
keyColumns := rsr.Item.MappingInfo.KeyColumns
valueColumn := rsr.Item.MappingInfo.ValueColumn
if nil != arrayColumns {
for _, c := range arrayColumns {
meta = append(meta, c)
}
}
if nil != keyColumns {
for _, c := range keyColumns {
meta = append(meta, c)
}
}
if "" != valueColumn {
meta = append(meta, valueColumn)
}
if nil == rsr.Meta {
rsr.Meta = make(map[string]json.Number, 0)
}
for i := 0; i < len(meta); i++ {
rsr.Meta[meta[i]] = json.Number(strconv.Itoa(i))
}
}
func (rsr *ResultSetRow) GetData() map[string]string {
return rsr.parse()
}
func (rsr *ResultSetRow) parse() map[string]string {
valueColumn := rsr.Item.MappingInfo.ValueColumn
rm := make(map[string]string, 0)
for _, row := range rsr.Rows {
key := rsr.makeKey(row)
if "" == key {
continue
}
idx, _ := cuej.NumberToInt(rsr.Meta[valueColumn])
rm[key] = row[idx]
}
return rm
}
func (rsr *ResultSetRow) makeKey(data []string) string {
metrics := rsr.Item.Keys
arrayColumns := rsr.Item.MappingInfo.ArrayColumns
keyColumns := rsr.Item.MappingInfo.KeyColumns
keys := rsr.Item.Keys
findIndex := -1
Loop:
for _, keyColumn := range keyColumns {
idx, _ := cuej.NumberToInt(rsr.Meta[keyColumn])
row := data[idx]
for i := 0; i < len(keys); i++ {
if row == keys[i].Key {
findIndex = i
break Loop
}
}
}
if 0 > findIndex {
return ""
}
metric := metrics[findIndex].Metric
convertChar := "$"
if nil != arrayColumns {
for i := 0; i < len(arrayColumns); i++ {
convertStr := fmt.Sprintf("%s%d", convertChar, i)
idx, _ := cuej.NumberToInt(rsr.Meta[arrayColumns[i]])
replaceString := data[idx]
metric = strings.Replace(metric, convertStr, "'"+replaceString+"'", -1)
}
}
return metric
}