-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
187 lines (157 loc) · 5.29 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
180
181
182
183
184
185
186
187
package main
import (
"cluster-inspection/db"
"cluster-inspection/handlers"
"cluster-inspection/report"
"cluster-inspection/utils"
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
func main() {
// 加载配置
config, err := utils.LoadConfig("config.yaml")
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
// 构建 DSN
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
config.Database.User, url.QueryEscape(config.Database.Password),
config.Database.Host, config.Database.Port, config.Database.DBName)
// 初始化数据库连接
err = db.InitDB(dsn)
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
// 初始化报告处理器
reportHandler, err := report.NewReportHandler(config)
if err != nil {
log.Fatalf("Error creating report handler: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
// 创建一个 WaitGroup 来等待所有 goroutine 完成
var wg sync.WaitGroup
// 启动定时任务
wg.Add(1)
go func() {
defer wg.Done()
startPeriodicDataCollection(reportHandler, ctx)
}()
// 设置路由(直接获取)
http.HandleFunc("/api/report/nodes", reportHandler.GetNodeReport)
http.HandleFunc("/api/report/nodes/", reportHandler.GetNodeReport)
http.HandleFunc("/api/report/events", reportHandler.GetEventReport)
http.HandleFunc("/api/report/events/", reportHandler.GetEventReport)
http.HandleFunc("/api/report/pods", reportHandler.GetPodReport)
http.HandleFunc("/api/report/pods/", reportHandler.GetPodReport)
http.HandleFunc("/api/report/full", reportHandler.GetFullReport)
http.HandleFunc("/api/clusters", reportHandler.GetAvailableClusters)
// 设置路由(从数据库获取)
http.HandleFunc("/api/nodes", handlers.GetNodeInfoFromDB)
http.HandleFunc("/api/events", handlers.GetEventInfoFromDB)
http.HandleFunc("/api/pods", handlers.GetPodInfoFromDB)
http.HandleFunc("/api/all", reportHandler.GetAllReports)
//获取所有数据(慎用)
http.HandleFunc("/api/collect", triggerDataCollection(reportHandler))
//获取部署资源推荐
http.HandleFunc("/api/deployment-resource-recommendations", handlers.GetDeploymentResourceRecommendations)
// 创建一个通道来接收终止信号
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
// 在一个 goroutine 中启动 HTTP 服务器
serverErrors := make(chan error, 1)
go func() {
log.Println("Server is running on http://localhost:8088")
serverErrors <- http.ListenAndServe(":8088", nil)
}()
// 等待终止信号或服务器错误
select {
case <-stop:
log.Println("Shutting down gracefully...")
cancel()
case err := <-serverErrors:
log.Fatalf("Could not start server: %v", err)
}
wg.Wait()
log.Println("Server stopped")
}
// startPeriodicDataCollection 开始采集操作
func startPeriodicDataCollection(reportHandler *report.ReportHandler, ctx context.Context) {
log.Println("Starting periodic data collection setup...")
// 计算当前时间到下一个整点的时间间隔
now := time.Now()
nextHour := now.Truncate(time.Hour).Add(time.Hour)
waitDuration := nextHour.Sub(now)
// 日志记录等待时间,直到下一个整点
log.Printf("Waiting %v until next hour to collect data", waitDuration)
// 创建一个定时器,用于等待直到下一个整点
timer := time.NewTimer(waitDuration)
defer timer.Stop()
select {
case <-ctx.Done():
log.Println("kill collection func")
return
case <-timer.C:
// 到达整点,执行数据收集
log.Println("Starting hourly data collection...")
collectData(reportHandler)
}
ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
log.Println("kill collection func")
return
case <-ticker.C:
// 到达整点,执行数据收集
log.Println("Starting hourly data collection...")
collectData(reportHandler)
}
}
}
func collectData(reportHandler *report.ReportHandler) {
log.Println("Starting periodic data collection...")
req, _ := http.NewRequest("GET", "/api/report/nodes", nil)
rr := httptest.NewRecorder()
// 调用 GetNodeReport 处理函数
reportHandler.GetNodeReport(rr, req)
if rr.Code != http.StatusOK {
log.Printf("Error collecting node data: %s", rr.Body.String())
} else {
log.Println("Node data collected and saved successfully")
}
req, _ = http.NewRequest("GET", "/api/report/pods", nil)
rr = httptest.NewRecorder()
reportHandler.GetPodReport(rr, req)
if rr.Code != http.StatusOK {
log.Printf("Error collecting pod data: %s", rr.Body.String())
} else {
log.Println("Pod data collected and saved successfully")
}
req, _ = http.NewRequest("GET", "/api/report/events", nil)
rr = httptest.NewRecorder()
reportHandler.GetEventReport(rr, req)
if rr.Code != http.StatusOK {
log.Printf("Error collecting event data: %s", rr.Body.String())
} else {
log.Println("Event data collected and saved successfully")
}
log.Println("Periodic data collection completed.")
}
func triggerDataCollection(reportHandler *report.ReportHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("Manual data collection triggered")
collectData(reportHandler)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Data collection completed"))
}
}