-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
162 lines (140 loc) · 3.7 KB
/
config.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package clickhouse
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"time"
_ "github.com/ClickHouse/clickhouse-go/v2"
)
type Duration time.Duration
func (d *Duration) Parse(v string) error {
duration, err := time.ParseDuration(v)
if err == nil {
*d = Duration(duration)
}
return err
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*d = Duration(value)
return nil
case string:
err := d.Parse(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
type config struct {
URL string `json:"url"`
PushInterval Duration `json:"pushInterval"`
Name string `json:"name"`
dbName string
tableTests string
tableSamples string
id uint64
ts time.Time
params string
}
func newConfig() config {
return config{
URL: "http://localhost:8123/default?dial_timeout=1s&max_execution_time=60",
// URL: "http://localhost:8123/default?read_timeout=10s&write_timeout=20s",
PushInterval: Duration(10 * time.Second),
dbName: "default",
tableTests: "k6_tests",
tableSamples: "k6_samples",
}
}
func (c config) TableTests() string {
return c.tableTests
}
func (c config) TableSamples() string {
return c.tableSamples
}
func (c config) Id() uint64 {
return c.id
}
func (c config) StartTime() time.Time {
return c.ts
}
func (c config) apply(modifiedConf config) (config, error) {
if modifiedConf.URL != "" {
if u, dbName, err := parseURL(modifiedConf.URL); err == nil {
c.URL, c.dbName = u, dbName
} else {
return config{}, err
}
}
if modifiedConf.PushInterval > 0 {
c.PushInterval = modifiedConf.PushInterval
}
return c, nil
}
func parseURL(text string) (dbUrl, dbName string, err error) {
var u *url.URL
u, err = url.Parse(text)
if err != nil {
return
}
if u.Host == "" || u.Scheme == "" {
return "", "", errors.New("empty host url")
}
if dbName = strings.TrimPrefix(u.Path, "/"); dbName == "" {
dbName = "default"
}
dbUrl = text
return
}
func getConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string) (config, error) {
consolidatedConf := newConfig()
var err error
if jsonRawConf != nil {
var jsonConf config
if err := json.Unmarshal(jsonRawConf, &jsonConf); err != nil {
return config{}, fmt.Errorf("problem unmarshalling JSON: %w", err)
}
if consolidatedConf, err = consolidatedConf.apply(jsonConf); err != nil {
return config{}, fmt.Errorf("problem apply config: %w", err)
}
}
envPushInterval, ok := env["K6_OUT_CLICKHOUSE_PUSH_INTERVAL"]
if ok {
var pushInterval Duration
err := pushInterval.Parse(envPushInterval)
if err != nil {
return config{}, fmt.Errorf("invalid K6_OUT_CLICKHOUSE_PUSH_INTERVAL: %w", err)
}
if consolidatedConf, err = consolidatedConf.apply(config{PushInterval: pushInterval}); err != nil {
return config{}, fmt.Errorf("problem apply config from K6_OUT_CLICKHOUSE_PUSH_INTERVAL: %w", err)
}
}
name := env["K6_OUT_CLICKHOUSE_TESTNAME"]
consolidatedConf.ts = timeNow().UTC()
consolidatedConf.id = uint64(consolidatedConf.ts.UnixNano())
if name == "" {
consolidatedConf.Name = consolidatedConf.ts.Format(time.RFC3339Nano)
} else {
consolidatedConf.Name = name + " " + consolidatedConf.ts.Format(time.RFC3339Nano)
}
consolidatedConf.params = env["K6_OUT_CLICKHOUSE_PARAMS"]
tableTests := env["K6_OUT_CLICKHOUSE_TABLE_TESTS"]
if tableTests != "" {
consolidatedConf.tableTests = tableTests
}
tableSamples := env["K6_OUT_CLICKHOUSE_TABLE_SAMPLES"]
if tableSamples != "" {
consolidatedConf.tableSamples = tableSamples
}
return consolidatedConf, nil
}