monitor.go 4.7 KB

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