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