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 8.5 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. "strconv"
  11. "strings"
  12. )
  13. const (
  14. tplResourceQueue base.TplName = "admin/resources/queue"
  15. tplResourceSpecification base.TplName = "admin/resources/specification"
  16. tplResourceScene base.TplName = "admin/resources/scene"
  17. )
  18. func GetQueuePage(ctx *context.Context) {
  19. ctx.Data["PageIsAdmin"] = true
  20. ctx.Data["PageIsAdminResources"] = true
  21. ctx.Data["PageIsAdminResourcesQueue"] = true
  22. ctx.HTML(200, tplResourceQueue)
  23. }
  24. func GetSpecificationPage(ctx *context.Context) {
  25. ctx.Data["PageIsAdmin"] = true
  26. ctx.Data["PageIsAdminResources"] = true
  27. ctx.Data["PageIsAdminResourcesSpecification"] = true
  28. ctx.HTML(200, tplResourceSpecification)
  29. }
  30. func GetScenePage(ctx *context.Context) {
  31. ctx.Data["PageIsAdmin"] = true
  32. ctx.Data["PageIsAdminResources"] = true
  33. ctx.Data["PageIsAdminResourcesScene"] = true
  34. ctx.HTML(200, tplResourceScene)
  35. }
  36. func GetResourceQueueList(ctx *context.Context) {
  37. page := ctx.QueryInt("page")
  38. cluster := ctx.Query("cluster")
  39. aiCenterCode := ctx.Query("center")
  40. computeResource := ctx.Query("resource")
  41. accCardType := ctx.Query("card")
  42. list, err := resource.GetResourceQueueList(models.SearchResourceQueueOptions{
  43. ListOptions: models.ListOptions{Page: page, PageSize: 10},
  44. Cluster: cluster,
  45. AiCenterCode: aiCenterCode,
  46. ComputeResource: computeResource,
  47. AccCardType: accCardType,
  48. })
  49. if err != nil {
  50. log.Error("GetResourceQueueList error.%v", err)
  51. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  52. return
  53. }
  54. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  55. }
  56. func GetResourceQueueCodes(ctx *context.Context) {
  57. cluster := ctx.Query("cluster")
  58. list, err := resource.GetResourceQueueCodes(models.GetQueueCodesOptions{Cluster: cluster})
  59. if err != nil {
  60. log.Error("GetResourceQueueCodes error.%v", err)
  61. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  62. return
  63. }
  64. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  65. }
  66. func GetResourceAiCenters(ctx *context.Context) {
  67. list, err := resource.GetResourceAiCenters()
  68. if err != nil {
  69. log.Error("GetResourceAiCenters error.%v", err)
  70. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  71. return
  72. }
  73. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  74. }
  75. func AddResourceQueue(ctx *context.Context, req models.ResourceQueueReq) {
  76. req.IsAutomaticSync = false
  77. req.CreatorId = ctx.User.ID
  78. err := resource.AddResourceQueue(req)
  79. if err != nil {
  80. log.Error("AddResourceQueue 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 UpdateResourceQueue(ctx *context.Context, req models.ResourceQueueReq) {
  87. queueId := ctx.ParamsInt64(":id")
  88. //only CardsTotalNum permitted to change
  89. err := resource.UpdateResourceQueue(queueId, req)
  90. if err != nil {
  91. log.Error("UpdateResourceQueue error. %v", err)
  92. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  93. return
  94. }
  95. ctx.JSON(http.StatusOK, response.Success())
  96. }
  97. func SyncGrampusQueue(ctx *context.Context) {
  98. err := resource.SyncGrampusQueue(ctx.User.ID)
  99. if err != nil {
  100. log.Error("AddResourceQueue error. %v", err)
  101. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  102. return
  103. }
  104. ctx.JSON(http.StatusOK, response.Success())
  105. }
  106. func GetResourceSpecificationList(ctx *context.Context) {
  107. page := ctx.QueryInt("page")
  108. queue := ctx.QueryInt64("queue")
  109. status := ctx.QueryInt("status")
  110. cluster := ctx.Query("cluster")
  111. available := ctx.QueryInt("available")
  112. list, err := resource.GetResourceSpecificationList(models.SearchResourceSpecificationOptions{
  113. ListOptions: models.ListOptions{Page: page, PageSize: 10},
  114. QueueId: queue,
  115. Status: status,
  116. Cluster: cluster,
  117. AvailableCode: available,
  118. })
  119. if err != nil {
  120. log.Error("GetResourceSpecificationList error.%v", err)
  121. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  122. return
  123. }
  124. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  125. }
  126. func GetResourceSpecificationScenes(ctx *context.Context) {
  127. specId := ctx.ParamsInt64(":id")
  128. list, err := resource.GetResourceSpecificationScenes(specId)
  129. if err != nil {
  130. log.Error("GetResourceSpecificationScenes error.%v", err)
  131. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  132. return
  133. }
  134. r := make(map[string]interface{})
  135. r["List"] = list
  136. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  137. }
  138. func AddResourceSpecification(ctx *context.Context, req models.ResourceSpecificationReq) {
  139. req.IsAutomaticSync = false
  140. req.CreatorId = ctx.User.ID
  141. err := resource.AddResourceSpecification(ctx.User.ID, req)
  142. if err != nil {
  143. log.Error("AddResourceQueue error. %v", err)
  144. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  145. return
  146. }
  147. ctx.JSON(http.StatusOK, response.Success())
  148. }
  149. func UpdateResourceSpecification(ctx *context.Context, req models.ResourceSpecificationReq) {
  150. id := ctx.ParamsInt64(":id")
  151. action := ctx.Query("action")
  152. var err *response.BizError
  153. switch action {
  154. case "edit":
  155. if req.UnitPrice < 0 {
  156. ctx.JSON(http.StatusOK, response.ServerError("param error"))
  157. return
  158. }
  159. //only UnitPrice and permitted to change
  160. err = resource.UpdateSpecUnitPrice(ctx.User.ID, id, req.UnitPrice)
  161. case "on-shelf":
  162. err = resource.ResourceSpecOnShelf(ctx.User.ID, id, req.UnitPrice)
  163. case "off-shelf":
  164. err = resource.ResourceSpecOffShelf(ctx.User.ID, id)
  165. }
  166. if err != nil {
  167. log.Error("UpdateResourceSpecification error. %v", err)
  168. ctx.JSON(http.StatusOK, response.ResponseError(err))
  169. return
  170. }
  171. ctx.JSON(http.StatusOK, response.Success())
  172. }
  173. func SyncGrampusSpecs(ctx *context.Context) {
  174. err := resource.SyncGrampusSpecs(ctx.User.ID)
  175. if err != nil {
  176. log.Error("AddResourceQueue error. %v", err)
  177. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  178. return
  179. }
  180. ctx.JSON(http.StatusOK, response.Success())
  181. }
  182. func GetResourceSceneList(ctx *context.Context) {
  183. page := ctx.QueryInt("page")
  184. jobType := ctx.Query("jobType")
  185. aiCenterCode := ctx.Query("center")
  186. queueId := ctx.QueryInt64("queue")
  187. isExclusive := ctx.QueryInt("IsExclusive")
  188. list, err := resource.GetResourceSceneList(models.SearchResourceSceneOptions{
  189. ListOptions: models.ListOptions{Page: page, PageSize: 10},
  190. JobType: jobType,
  191. IsExclusive: isExclusive,
  192. AiCenterCode: aiCenterCode,
  193. QueueId: queueId,
  194. })
  195. if err != nil {
  196. log.Error("GetResourceSceneList error.%v", err)
  197. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  198. return
  199. }
  200. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  201. }
  202. func AddResourceScene(ctx *context.Context, req models.ResourceSceneReq) {
  203. req.CreatorId = ctx.User.ID
  204. req.ExclusiveOrg = strings.ReplaceAll(req.ExclusiveOrg, " ", "")
  205. err := resource.AddResourceScene(req)
  206. if err != nil {
  207. log.Error("AddResourceScene error. %v", err)
  208. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  209. return
  210. }
  211. ctx.JSON(http.StatusOK, response.Success())
  212. }
  213. func UpdateResourceScene(ctx *context.Context, req models.ResourceSceneReq) {
  214. id := ctx.ParamsInt64(":id")
  215. action := ctx.Query("action")
  216. req.ID = id
  217. var err error
  218. switch action {
  219. case "edit":
  220. req.ExclusiveOrg = strings.ReplaceAll(req.ExclusiveOrg, " ", "")
  221. err = resource.UpdateResourceScene(req)
  222. case "delete":
  223. err = resource.DeleteResourceScene(id)
  224. }
  225. if err != nil {
  226. log.Error("UpdateResourceScene error. %v", err)
  227. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  228. return
  229. }
  230. ctx.JSON(http.StatusOK, response.Success())
  231. }
  232. func RefreshHistorySpec(ctx *context.Context) {
  233. scope := ctx.Query("scope")
  234. list := ctx.Query("list")
  235. var scopeAll = false
  236. if scope == "all" {
  237. scopeAll = true
  238. }
  239. var ids = make([]int64, 0)
  240. if list != "" {
  241. strs := strings.Split(list, "|")
  242. for _, s := range strs {
  243. i, err := strconv.ParseInt(s, 10, 64)
  244. if err != nil {
  245. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  246. return
  247. }
  248. ids = append(ids, i)
  249. }
  250. }
  251. total, success, err := resource.RefreshHistorySpec(scopeAll, ids)
  252. if err != nil {
  253. log.Error("RefreshHistorySpec error. %v", err)
  254. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  255. return
  256. }
  257. r := make(map[string]interface{}, 0)
  258. r["success"] = success
  259. r["total"] = total
  260. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  261. }