monitor.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package controller
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "github.com/gin-gonic/gin"
  7. "github.com/labring/aiproxy/core/middleware"
  8. "github.com/labring/aiproxy/core/monitor"
  9. )
  10. // GetAllChannelModelErrorRates godoc
  11. //
  12. // @Summary Get all channel model error rates
  13. // @Description Returns a list of all channel model error rates
  14. // @Tags monitor
  15. // @Produce json
  16. // @Security ApiKeyAuth
  17. // @Success 200 {object} middleware.APIResponse{data=map[int64]map[string]float64}
  18. // @Router /api/monitor/ [get]
  19. func GetAllChannelModelErrorRates(c *gin.Context) {
  20. rates, err := monitor.GetAllChannelModelErrorRates(c.Request.Context())
  21. if err != nil {
  22. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  23. return
  24. }
  25. middleware.SuccessResponse(c, rates)
  26. }
  27. // GetChannelModelErrorRates godoc
  28. //
  29. // @Summary Get channel model error rates
  30. // @Description Returns a list of channel model error rates
  31. // @Tags monitor
  32. // @Produce json
  33. // @Security ApiKeyAuth
  34. // @Param id path int true "Channel ID"
  35. // @Success 200 {object} middleware.APIResponse{data=[]map[string]float64}
  36. // @Router /api/monitor/{id} [get]
  37. func GetChannelModelErrorRates(c *gin.Context) {
  38. channelID, err := strconv.ParseInt(c.Param("id"), 10, 64)
  39. if err != nil {
  40. middleware.ErrorResponse(c, http.StatusBadRequest, "Invalid channel ID")
  41. return
  42. }
  43. rates, err := monitor.GetChannelModelErrorRates(c.Request.Context(), channelID)
  44. if err != nil {
  45. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  46. return
  47. }
  48. middleware.SuccessResponse(c, rates)
  49. }
  50. // ClearAllModelErrors godoc
  51. //
  52. // @Summary Clear all model errors
  53. // @Description Clears all model errors
  54. // @Tags monitor
  55. // @Produce json
  56. // @Security ApiKeyAuth
  57. // @Success 200 {object} middleware.APIResponse
  58. // @Router /api/monitor/ [delete]
  59. func ClearAllModelErrors(c *gin.Context) {
  60. err := monitor.ClearAllModelErrors(c.Request.Context())
  61. if err != nil {
  62. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  63. return
  64. }
  65. middleware.SuccessResponse(c, nil)
  66. }
  67. // ClearChannelAllModelErrors godoc
  68. //
  69. // @Summary Clear channel all model errors
  70. // @Description Clears all model errors for a specific channel
  71. // @Tags monitor
  72. // @Produce json
  73. // @Security ApiKeyAuth
  74. // @Param id path int true "Channel ID"
  75. // @Success 200 {object} middleware.APIResponse
  76. // @Router /api/monitor/{id} [delete]
  77. func ClearChannelAllModelErrors(c *gin.Context) {
  78. channelID, err := strconv.ParseInt(c.Param("id"), 10, 64)
  79. if err != nil {
  80. middleware.ErrorResponse(c, http.StatusBadRequest, "Invalid channel ID")
  81. return
  82. }
  83. err = monitor.ClearChannelAllModelErrors(c.Request.Context(), int(channelID))
  84. if err != nil {
  85. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  86. return
  87. }
  88. middleware.SuccessResponse(c, nil)
  89. }
  90. // ClearChannelModelErrors godoc
  91. //
  92. // @Summary Clear channel model errors
  93. // @Description Clears model errors for a specific channel and model
  94. // @Tags monitor
  95. // @Produce json
  96. // @Security ApiKeyAuth
  97. // @Param id path int true "Channel ID"
  98. // @Param model path string true "Model name"
  99. // @Success 200 {object} middleware.APIResponse
  100. // @Router /api/monitor/{id}/{model} [delete]
  101. func ClearChannelModelErrors(c *gin.Context) {
  102. model := strings.TrimPrefix(c.Param("model"), "/")
  103. channelID, err := strconv.ParseInt(c.Param("id"), 10, 64)
  104. if err != nil {
  105. middleware.ErrorResponse(c, http.StatusBadRequest, "Invalid channel ID")
  106. return
  107. }
  108. err = monitor.ClearChannelModelErrors(c.Request.Context(), model, int(channelID))
  109. if err != nil {
  110. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  111. return
  112. }
  113. middleware.SuccessResponse(c, nil)
  114. }
  115. // GetModelsErrorRate godoc
  116. //
  117. // @Summary Get models error rate
  118. // @Description Returns a list of models error rate
  119. // @Tags monitor
  120. // @Produce json
  121. // @Security ApiKeyAuth
  122. // @Success 200 {object} middleware.APIResponse{data=map[string]float64}
  123. // @Router /api/monitor/models [get]
  124. func GetModelsErrorRate(c *gin.Context) {
  125. rates, err := monitor.GetModelsErrorRate(c.Request.Context())
  126. if err != nil {
  127. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  128. return
  129. }
  130. middleware.SuccessResponse(c, rates)
  131. }
  132. // GetAllBannedModelChannels godoc
  133. //
  134. // @Summary Get all banned model channels
  135. // @Description Returns a list of all banned model channels
  136. // @Tags monitor
  137. // @Produce json
  138. // @Security ApiKeyAuth
  139. // @Success 200 {object} middleware.APIResponse{data=map[string][]int64}
  140. // @Router /api/monitor/banned_channels [get]
  141. func GetAllBannedModelChannels(c *gin.Context) {
  142. channels, err := monitor.GetAllBannedModelChannels(c.Request.Context())
  143. if err != nil {
  144. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  145. return
  146. }
  147. middleware.SuccessResponse(c, channels)
  148. }