/
database.go
119 lines (114 loc) · 3.47 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// 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 PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package chartmeta
import (
"encoding/json"
"fmt"
"strings"
)
type Table struct {
Name string `gorm:"column:name"`
Title string `gorm:"column:title"`
MetricsName string `gorm:"column:metricsName"`
Fields string `gorm:"column:fields"`
Parameters string `gorm:"column:parameters"`
Type string `gorm:"column:type"`
Order int `gorm:"column:order"`
Unit string `gorm:"column:unit"`
}
type FieldInfo struct {
DataMeta
// ChartType string `yaml:"chart_type" json:"chart_type"`
}
func (m *Manager) LoadDatabase() error {
var list []*Table
if err := m.db.Table("chart_meta").Order("`type`,`order` ASC").Find(&list).Error; err != nil {
return err
}
nameMap := make(map[string]*ChartMeta)
typeMap := make(map[string][]*ChartMeta)
for _, line := range list {
if _, ok := nameMap[line.Name]; ok {
m.log.Warnf("name %s conflicts", line.Name)
}
var defines map[string]*DataMeta
var parameters map[string][]string
// var chartType string
line.Fields = strings.TrimSpace(line.Fields)
if len(line.Fields) != 0 {
var fields map[string]*FieldInfo
err := json.Unmarshal([]byte(line.Fields), &fields)
if err != nil {
m.log.Warnf("%s invalid fields format: %s", line.Name, err)
continue
}
defines = make(map[string]*DataMeta)
for key, field := range fields {
field.DataMeta.OriginalUnit = &line.Unit
defines[key] = &field.DataMeta
// if chartType == "" {
// chartType = field.ChartType
// } else if chartType != field.ChartType {
// fmt.Println(line.Name, chartType, field.ChartType)
// }
if field.AxisIndex == nil || field.ChartType == nil || field.Label == nil ||
field.Unit == nil || field.UnitType == nil {
m.log.Warnf("database chart field unset some key")
}
}
}
line.Parameters = strings.TrimSpace(line.Parameters)
if len(line.Parameters) != 0 {
var ps map[string]interface{}
err := json.Unmarshal([]byte(line.Parameters), &ps)
if err != nil {
m.log.Warnf("%s invalid parameters format: %s", line.Name, err)
continue
}
parameters = make(map[string][]string)
for key, vals := range ps {
switch value := vals.(type) {
case []interface{}:
for _, val := range value {
parameters[key] = append(parameters[key], fmt.Sprint(val))
}
case []string:
parameters[key] = value
case string:
parameters[key] = []string{value}
default:
parameters[key] = []string{fmt.Sprint(value)}
}
}
}
typ := line.Type
if typ == "0" || typ == "" {
typ = "default"
}
cm := &ChartMeta{
Name: line.Name,
Title: line.Title,
MetricNames: line.MetricsName,
Defines: defines,
Parameters: parameters,
Type: typ,
// ChartType: chartType,
Order: line.Order,
}
cm.mergeParams()
nameMap[line.Name] = cm
typeMap[typ] = append(typeMap[typ], cm)
}
m.reloadCharts(nameMap, typeMap)
return nil
}