-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
179 lines (158 loc) · 4.08 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"os"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/mattn/go-colorable"
"github.com/urfave/cli"
)
func init() {
log.SetLevel(log.InfoLevel)
log.SetFormatter(&log.TextFormatter{ForceColors: true, FullTimestamp: true})
log.SetOutput(colorable.NewColorableStdout())
}
func processConfiguration(cont *cli.Context) (*Config, error) {
path := cont.String("config")
cfg, err := LoadConf(path)
if err != nil {
log.Fatal(err)
return nil, err
}
return cfg, nil
}
func showConfiguration(cont *cli.Context) {
_, _ = processConfiguration(cont)
}
func NewDataAdapterAndClickHouseDBStoreAndDoCheck(cfg *Config, mon *Monitor, m *Metrics) (*DataAdapter, *ClickHouseDBStore, error) {
da, err := NewDataAdapter(cfg, mon, m)
if err != nil {
return da, nil, err
}
item := da.CheckJsonSample(cfg.AdapterInfo.Jsonsample)
time.Sleep(time.Duration(cfg.General.Sleepinterval) * time.Millisecond)
chDBS, err := NewClickHouseDBStore(cfg, mon, m, da.MessageTableDDL, &da.MessageTableKeys)
if err != nil {
return da, chDBS, err
} else {
err = chDBS.DoInsertRawMessageTableForCheck(cfg.AdapterInfo.Jsonsample)
if err != nil {
return da, chDBS, err
}
err = chDBS.DoQueryRawMessageTableForCheck()
if err != nil {
return da, chDBS, err
}
err = chDBS.DoInsertAdapterMessageTableForCheck(*item)
if err != nil {
return da, chDBS, err
}
err = chDBS.DoQueryAdapterMessageTableForCheck()
if err != nil {
return da, chDBS, err
}
}
return da, chDBS, nil
}
func checkDataAdapterConfiguration(cont *cli.Context) {
cfg, _ := processConfiguration(cont)
m := NewMetrics()
mon, _ := NewMonitor(cfg, m)
_, _, _ = NewDataAdapterAndClickHouseDBStoreAndDoCheck(cfg, mon, m)
}
func runPump(cont *cli.Context) {
cfg, _ := processConfiguration(cont)
m := NewMetrics()
mon, err1 := NewMonitor(cfg, m)
if err1 != nil {
log.Fatal(err1)
return
}
go mon.Start()
time.Sleep(time.Duration(cfg.General.Sleepinterval) * time.Millisecond)
da, chDBS, err2 := NewDataAdapterAndClickHouseDBStoreAndDoCheck(cfg, mon, m)
if err2 != nil {
log.Fatal(err2)
return
}
dp, err3 := NewDataProcessor(cfg, mon, m, da, chDBS)
if err3 != nil {
log.Fatal(err3)
return
}
wg := sync.WaitGroup{}
wg.Add(1)
go chDBS.StartDBCommitTask()
time.Sleep(time.Duration(cfg.General.Sleepinterval) * time.Millisecond)
wg.Add(1)
go da.StartDataAdapterTask()
time.Sleep(time.Duration(cfg.General.Sleepinterval) * time.Millisecond)
wg.Add(1)
go dp.StartDataProcessTaskWithAdapter()
time.Sleep(time.Duration(cfg.General.Sleepinterval) * time.Millisecond)
if cfg.PipeTopic.Enablegroupnum {
for idx := cfg.PipeTopic.Begingroupnum; idx <= cfg.PipeTopic.Endgroupnum; idx++ {
wg.Add(1)
time.Sleep(time.Duration(cfg.PipeInfo.Taskinterval) * time.Millisecond)
pp := NewPipe(cfg, idx, mon, m, &dp.DataChan)
go pp.Start()
}
} else {
wg.Add(1)
pp := NewPipe(cfg, 0, mon, m, &dp.DataChan)
go pp.Start()
}
log.Infof("%s", m.GetMetricsInfo("Metrics Information"))
wg.Wait()
}
func main() {
app := cli.NewApp()
app.Name = "json message pump"
app.Version = "21.01.20"
app.Compiled = time.Now()
app.Authors = []cli.Author{
{
Name: "Abe.Chua",
},
}
app.Usage = "A command-line simulator for generating messages published to the MQTT broker."
app.Commands = []cli.Command{
{
Name: "run",
Usage: "pump the json-style messages.",
Action: runPump,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "config file path",
Value: "~/.pump-plus.ini",
},
},
},
{
Name: "show",
Usage: "print the configuration information.",
Action: showConfiguration,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "config file path",
Value: "~/.pump-plus.ini",
},
},
},
{
Name: "check",
Usage: "Check the configuration information for correct data adapter operation.",
Action: checkDataAdapterConfiguration,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "config file path",
Value: "~/.pump-plus.ini",
},
},
},
}
_ = app.Run(os.Args)
}