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 9.2 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 GetAllResourceSpecificationList(ctx *context.Context) {
  127. queue := ctx.QueryInt64("queue")
  128. status := ctx.QueryInt("status")
  129. cluster := ctx.Query("cluster")
  130. available := ctx.QueryInt("available")
  131. list, err := resource.GetAllDistinctResourceSpecification(models.SearchResourceSpecificationOptions{
  132. QueueId: queue,
  133. Status: status,
  134. Cluster: cluster,
  135. AvailableCode: available,
  136. })
  137. if err != nil {
  138. log.Error("GetResourceSpecificationList error.%v", err)
  139. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  140. return
  141. }
  142. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  143. }
  144. func GetResourceSpecificationScenes(ctx *context.Context) {
  145. specId := ctx.ParamsInt64(":id")
  146. list, err := resource.GetResourceSpecificationScenes(specId)
  147. if err != nil {
  148. log.Error("GetResourceSpecificationScenes error.%v", err)
  149. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  150. return
  151. }
  152. r := make(map[string]interface{})
  153. r["List"] = list
  154. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  155. }
  156. func AddResourceSpecification(ctx *context.Context, req models.ResourceSpecificationReq) {
  157. req.IsAutomaticSync = false
  158. req.CreatorId = ctx.User.ID
  159. err := resource.AddResourceSpecification(ctx.User.ID, req)
  160. if err != nil {
  161. log.Error("AddResourceQueue error. %v", err)
  162. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  163. return
  164. }
  165. ctx.JSON(http.StatusOK, response.Success())
  166. }
  167. func UpdateResourceSpecification(ctx *context.Context, req models.ResourceSpecificationReq) {
  168. id := ctx.ParamsInt64(":id")
  169. action := ctx.Query("action")
  170. var err *response.BizError
  171. switch action {
  172. case "edit":
  173. if req.UnitPrice < 0 {
  174. ctx.JSON(http.StatusOK, response.ServerError("param error"))
  175. return
  176. }
  177. //only UnitPrice and permitted to change
  178. err = resource.UpdateSpecUnitPrice(ctx.User.ID, id, req.UnitPrice)
  179. case "on-shelf":
  180. err = resource.ResourceSpecOnShelf(ctx.User.ID, id, req.UnitPrice)
  181. case "off-shelf":
  182. err = resource.ResourceSpecOffShelf(ctx.User.ID, id)
  183. }
  184. if err != nil {
  185. log.Error("UpdateResourceSpecification error. %v", err)
  186. ctx.JSON(http.StatusOK, response.ResponseBizError(err))
  187. return
  188. }
  189. ctx.JSON(http.StatusOK, response.Success())
  190. }
  191. func SyncGrampusSpecs(ctx *context.Context) {
  192. err := resource.SyncGrampusSpecs(ctx.User.ID)
  193. if err != nil {
  194. log.Error("AddResourceQueue error. %v", err)
  195. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  196. return
  197. }
  198. ctx.JSON(http.StatusOK, response.Success())
  199. }
  200. func GetResourceSceneList(ctx *context.Context) {
  201. page := ctx.QueryInt("page")
  202. jobType := ctx.Query("jobType")
  203. aiCenterCode := ctx.Query("center")
  204. queueId := ctx.QueryInt64("queue")
  205. isExclusive := ctx.QueryInt("IsExclusive")
  206. list, err := resource.GetResourceSceneList(models.SearchResourceSceneOptions{
  207. ListOptions: models.ListOptions{Page: page, PageSize: 10},
  208. JobType: jobType,
  209. IsExclusive: isExclusive,
  210. AiCenterCode: aiCenterCode,
  211. QueueId: queueId,
  212. })
  213. if err != nil {
  214. log.Error("GetResourceSceneList error.%v", err)
  215. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  216. return
  217. }
  218. ctx.JSON(http.StatusOK, response.SuccessWithData(list))
  219. }
  220. func AddResourceScene(ctx *context.Context, req models.ResourceSceneReq) {
  221. req.CreatorId = ctx.User.ID
  222. req.ExclusiveOrg = strings.ReplaceAll(req.ExclusiveOrg, " ", "")
  223. err := resource.AddResourceScene(req)
  224. if err != nil {
  225. log.Error("AddResourceScene error. %v", err)
  226. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  227. return
  228. }
  229. ctx.JSON(http.StatusOK, response.Success())
  230. }
  231. func UpdateResourceScene(ctx *context.Context, req models.ResourceSceneReq) {
  232. id := ctx.ParamsInt64(":id")
  233. action := ctx.Query("action")
  234. req.ID = id
  235. var err error
  236. switch action {
  237. case "edit":
  238. req.ExclusiveOrg = strings.ReplaceAll(req.ExclusiveOrg, " ", "")
  239. err = resource.UpdateResourceScene(req)
  240. case "delete":
  241. err = resource.DeleteResourceScene(id)
  242. }
  243. if err != nil {
  244. log.Error("UpdateResourceScene error. %v", err)
  245. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  246. return
  247. }
  248. ctx.JSON(http.StatusOK, response.Success())
  249. }
  250. func RefreshHistorySpec(ctx *context.Context) {
  251. scope := ctx.Query("scope")
  252. list := ctx.Query("list")
  253. var scopeAll = false
  254. if scope == "all" {
  255. scopeAll = true
  256. }
  257. var ids = make([]int64, 0)
  258. if list != "" {
  259. strs := strings.Split(list, "|")
  260. for _, s := range strs {
  261. i, err := strconv.ParseInt(s, 10, 64)
  262. if err != nil {
  263. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  264. return
  265. }
  266. ids = append(ids, i)
  267. }
  268. }
  269. total, success, err := resource.RefreshHistorySpec(scopeAll, ids)
  270. if err != nil {
  271. log.Error("RefreshHistorySpec error. %v", err)
  272. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  273. return
  274. }
  275. r := make(map[string]interface{}, 0)
  276. r["success"] = success
  277. r["total"] = total
  278. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  279. }