You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

resources.go 6.2 kB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package admin
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/base"
  5. "code.gitea.io/gitea/modules/context"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/routers/response"
  8. "code.gitea.io/gitea/services/cloudbrain/resource"
  9. "net/http"
  10. )
  11. const (
  12. tplResourceQueue base.TplName = "admin/resources/queue"
  13. tplResourceSpecification base.TplName = "admin/resources/specification"
  14. tplResourceScene base.TplName = "admin/resources/scene"
  15. )
  16. func GetQueuePage(ctx *context.Context) {
  17. ctx.Data["PageIsAdmin"] = true
  18. ctx.Data["PageIsAdminResources"] = true
  19. ctx.Data["PageIsAdminResourcesQueue"] = true
  20. ctx.HTML(200, tplResourceQueue)
  21. }
  22. func GetSpecificationPage(ctx *context.Context) {
  23. ctx.Data["PageIsAdmin"] = true
  24. ctx.Data["PageIsAdminResources"] = true
  25. ctx.Data["PageIsAdminResourcesSpecification"] = true
  26. ctx.HTML(200, tplResourceSpecification)
  27. }
  28. func GetScenePage(ctx *context.Context) {
  29. ctx.Data["PageIsAdmin"] = true
  30. ctx.Data["PageIsAdminResources"] = true
  31. ctx.Data["PageIsAdminResourcesScene"] = true
  32. ctx.HTML(200, tplResourceScene)
  33. }
  34. func GetResourceQueueList(ctx *context.Context) {
  35. page := ctx.QueryInt("page")
  36. cluster := ctx.Query("cluster")
  37. aiCenterCode := ctx.Query("center")
  38. computeResource := ctx.Query("resource")
  39. accCardType := ctx.Query("card")
  40. list, err := resource.GetResourceQueueList(models.SearchResourceQueueOptions{
  41. ListOptions: models.ListOptions{Page: page, PageSize: 20},
  42. Cluster: cluster,
  43. AiCenterCode: aiCenterCode,
  44. ComputeResource: computeResource,
  45. AccCardType: accCardType,
  46. })
  47. if err != nil {
  48. log.Error("GetResourceQueueList error.%v", err)
  49. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  50. return
  51. }
  52. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  53. }
  54. func GetResourceQueueCodes(ctx *context.Context) {
  55. cluster := ctx.Query("cluster")
  56. list, err := resource.GetResourceQueueCodes(models.GetQueueCodesOptions{Cluster: cluster})
  57. if err != nil {
  58. log.Error("GetResourceQueueCodes error.%v", err)
  59. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  60. return
  61. }
  62. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  63. }
  64. func AddResourceQueue(ctx *context.Context, req models.ResourceQueueReq) {
  65. req.IsAutomaticSync = false
  66. req.CreatorId = ctx.User.ID
  67. err := resource.AddResourceQueue(req)
  68. if err != nil {
  69. log.Error("AddResourceQueue error. %v", err)
  70. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  71. return
  72. }
  73. ctx.JSON(http.StatusOK, response.Success())
  74. }
  75. func UpdateResourceQueue(ctx *context.Context, req models.ResourceQueueReq) {
  76. queueId := ctx.ParamsInt64(":id")
  77. //only CardsTotalNum permitted to change
  78. err := resource.UpdateResourceQueue(queueId, req)
  79. if err != nil {
  80. log.Error("UpdateResourceQueue error. %v", err)
  81. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  82. return
  83. }
  84. ctx.JSON(http.StatusOK, response.Success())
  85. }
  86. func GetResourceSpecificationList(ctx *context.Context) {
  87. page := ctx.QueryInt("page")
  88. queue := ctx.QueryInt64("queue")
  89. status := ctx.QueryInt("status")
  90. cluster := ctx.Query("cluster")
  91. list, err := resource.GetResourceSpecificationList(models.SearchResourceSpecificationOptions{
  92. ListOptions: models.ListOptions{Page: page, PageSize: 20},
  93. QueueId: queue,
  94. Status: status,
  95. Cluster: cluster,
  96. })
  97. if err != nil {
  98. log.Error("GetResourceSpecificationList error.%v", err)
  99. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  100. return
  101. }
  102. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  103. }
  104. func AddResourceSpecification(ctx *context.Context, req models.ResourceSpecificationReq) {
  105. req.IsAutomaticSync = false
  106. req.CreatorId = ctx.User.ID
  107. err := resource.AddResourceSpecification(req)
  108. if err != nil {
  109. log.Error("AddResourceQueue error. %v", err)
  110. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  111. return
  112. }
  113. ctx.JSON(http.StatusOK, response.Success())
  114. }
  115. func UpdateResourceSpecification(ctx *context.Context, req models.ResourceSpecificationReq) {
  116. id := ctx.ParamsInt64(":id")
  117. action := ctx.Query("action")
  118. var err error
  119. switch action {
  120. case "edit":
  121. if req.UnitPrice < 0 {
  122. ctx.JSON(http.StatusOK, response.ServerError("param error"))
  123. return
  124. }
  125. //only UnitPrice and permitted to change
  126. err = resource.UpdateResourceSpecification(id, req)
  127. case "on-shelf":
  128. err = resource.ResourceSpecOnShelf(id, req)
  129. case "off-shelf":
  130. err = resource.ResourceSpecOffShelf(id)
  131. }
  132. if err != nil {
  133. log.Error("UpdateResourceSpecification error. %v", err)
  134. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  135. return
  136. }
  137. ctx.JSON(http.StatusOK, response.Success())
  138. }
  139. func GetResourceSceneList(ctx *context.Context) {
  140. page := ctx.QueryInt("page")
  141. jobType := ctx.Query("jobType")
  142. aiCenterCode := ctx.Query("center")
  143. queueId := ctx.QueryInt64("queue")
  144. isExclusive := ctx.QueryInt("IsExclusive")
  145. list, err := resource.GetResourceSceneList(models.SearchResourceSceneOptions{
  146. ListOptions: models.ListOptions{Page: page, PageSize: 20},
  147. JobType: jobType,
  148. IsExclusive: isExclusive,
  149. AiCenterCode: aiCenterCode,
  150. QueueId: queueId,
  151. })
  152. if err != nil {
  153. log.Error("GetResourceSceneList error.%v", err)
  154. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  155. return
  156. }
  157. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  158. }
  159. func AddResourceScene(ctx *context.Context, req models.ResourceSceneReq) {
  160. req.CreatorId = ctx.User.ID
  161. err := resource.AddResourceScene(req)
  162. if err != nil {
  163. log.Error("AddResourceScene error. %v", err)
  164. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  165. return
  166. }
  167. ctx.JSON(http.StatusOK, response.Success())
  168. }
  169. func UpdateResourceScene(ctx *context.Context, req models.ResourceSceneReq) {
  170. id := ctx.ParamsInt64(":id")
  171. action := ctx.Query("action")
  172. req.ID = id
  173. var err error
  174. switch action {
  175. case "edit":
  176. err = resource.UpdateResourceScene(req)
  177. case "delete":
  178. err = resource.DeleteResourceScene(id)
  179. }
  180. if err != nil {
  181. log.Error("UpdateResourceScene error. %v", err)
  182. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  183. return
  184. }
  185. ctx.JSON(http.StatusOK, response.Success())
  186. }