-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: add group monitoring module #2951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
muqing-lt
wants to merge
3
commits into
QuantumNous:main
Choose a base branch
from
muqing-lt:feature/group-monitor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,364
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package group_monitor | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strconv" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // GetGroupMonitorLogs 分页查询监控日志(管理员) | ||
| func GetGroupMonitorLogsHandler(c *gin.Context) { | ||
| pageInfo := common.GetPageQuery(c) | ||
| groupName := c.Query("group") | ||
| startTs, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64) | ||
| endTs, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64) | ||
|
|
||
| logs, total, err := GetGroupMonitorLogs(groupName, startTs, endTs, pageInfo.GetStartIdx(), pageInfo.GetPageSize()) | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
| pageInfo.SetTotal(int(total)) | ||
| pageInfo.SetItems(logs) | ||
| common.ApiSuccess(c, pageInfo) | ||
| } | ||
|
|
||
| // GetGroupMonitorLatestHandler 获取所有分组最新状态(管理员) | ||
| func GetGroupMonitorLatestHandler(c *gin.Context) { | ||
| logs, err := GetGroupMonitorLatest() | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
| common.ApiSuccess(c, logs) | ||
| } | ||
|
|
||
| // GetGroupMonitorStatsHandler 获取聚合统计(管理员) | ||
| func GetGroupMonitorStatsHandler(c *gin.Context) { | ||
| startTs, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64) | ||
| endTs, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64) | ||
|
|
||
| // 默认查询最近 1 小时 | ||
| if startTs == 0 { | ||
| startTs = common.GetTimestamp() - 3600 | ||
| } | ||
|
|
||
| stats, err := GetGroupMonitorStats(startTs, endTs) | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
| common.ApiSuccess(c, stats) | ||
| } | ||
|
|
||
| // GetGroupMonitorTimeSeriesHandler 获取时间序列数据(趋势图) | ||
| func GetGroupMonitorTimeSeriesHandler(c *gin.Context) { | ||
| groupName := c.Query("group") | ||
| startTs, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64) | ||
| endTs, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64) | ||
|
|
||
| // 默认最近 1 小时 | ||
| if startTs == 0 { | ||
| startTs = common.GetTimestamp() - 3600 | ||
| } | ||
|
|
||
| logs, err := GetGroupMonitorTimeSeries(groupName, startTs, endTs) | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
| common.ApiSuccess(c, logs) | ||
| } | ||
|
|
||
| // GetGroupMonitorConfigsHandler 获取所有分组监控配置(管理员) | ||
| func GetGroupMonitorConfigsHandler(c *gin.Context) { | ||
| configs, err := GetAllGroupMonitorConfigs() | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
| common.ApiSuccess(c, configs) | ||
| } | ||
|
|
||
| // SaveGroupMonitorConfigHandler 保存分组监控配置(管理员) | ||
| func SaveGroupMonitorConfigHandler(c *gin.Context) { | ||
| var cfg GroupMonitorConfig | ||
| if err := c.ShouldBindJSON(&cfg); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{ | ||
| "success": false, | ||
| "message": "invalid request body", | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| if cfg.GroupName == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{ | ||
| "success": false, | ||
| "message": "group_name is required", | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| if err := SaveGroupMonitorConfig(&cfg); err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
| common.ApiSuccess(c, nil) | ||
| } | ||
|
|
||
| // DeleteGroupMonitorConfigHandler 删除分组监控配置(管理员) | ||
| func DeleteGroupMonitorConfigHandler(c *gin.Context) { | ||
| groupName := c.Param("group") | ||
| if groupName == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{ | ||
| "success": false, | ||
| "message": "group name is required", | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| if err := DeleteGroupMonitorConfig(groupName); err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
| common.ApiSuccess(c, nil) | ||
| } | ||
|
|
||
| // GetGroupMonitorStatusHandler 用户可见的简化状态 | ||
| func GetGroupMonitorStatusHandler(c *gin.Context) { | ||
| // 获取最近 1 小时的聚合统计 | ||
| startTs := common.GetTimestamp() - 3600 | ||
| stats, err := GetGroupMonitorStats(startTs, 0) | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
|
|
||
| // 获取每个分组的最新记录 | ||
| latest, err := GetGroupMonitorLatest() | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
|
|
||
| type GroupStatus struct { | ||
| GroupName string `json:"group_name"` | ||
| LatestLatency int64 `json:"latest_latency"` | ||
| LatestSuccess bool `json:"latest_success"` | ||
| LatestTime int64 `json:"latest_time"` | ||
| AvgLatency float64 `json:"avg_latency"` | ||
| Availability float64 `json:"availability"` // 百分比 | ||
| TotalChecks int64 `json:"total_checks"` | ||
| } | ||
|
|
||
| // 构建 stats map | ||
| statsMap := make(map[string]*GroupMonitorStat) | ||
| for i := range stats { | ||
| statsMap[stats[i].GroupName] = &stats[i] | ||
| } | ||
|
|
||
| var result []GroupStatus | ||
| for _, log := range latest { | ||
| status := GroupStatus{ | ||
| GroupName: log.GroupName, | ||
| LatestLatency: log.LatencyMs, | ||
| LatestSuccess: log.Success, | ||
| LatestTime: log.CreatedAt, | ||
| } | ||
| if stat, ok := statsMap[log.GroupName]; ok { | ||
| status.AvgLatency = stat.AvgLatency | ||
| status.TotalChecks = stat.TotalCount | ||
| if stat.TotalCount > 0 { | ||
| status.Availability = float64(stat.SuccessCount) / float64(stat.TotalCount) * 100 | ||
| } | ||
| } | ||
| result = append(result, status) | ||
| } | ||
| common.ApiSuccess(c, result) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package group_monitor | ||
|
|
||
| import ( | ||
| "github.com/QuantumNous/new-api/model" | ||
| ) | ||
|
|
||
| // Migrate 执行分组监控模块的数据库迁移 | ||
| func Migrate() error { | ||
| return model.DB.AutoMigrate(&GroupMonitorLog{}, &GroupMonitorConfig{}) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migration error is silently discarded.
group_monitor.Migrate()returns an error that is ignored. If the migration fails, the background monitor goroutine (Line 108) will encounter runtime errors querying non-existent tables. Other migrations in this function propagate errors.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents