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.

task_config.go 8.7 kB

2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "xorm.io/builder"
  5. )
  6. const (
  7. PeriodNotCycle = "NOT_CYCLE"
  8. PeriodDaily = "DAILY"
  9. )
  10. type TaskType string
  11. const (
  12. TaskCreatePublicRepo TaskType = "CreatePublicRepo"
  13. TaskCreateIssue TaskType = "CreateIssue"
  14. TaskCreatePullRequest TaskType = "CreatePullRequest"
  15. TaskCommentIssue TaskType = "CommentIssue"
  16. TaskUploadAttachment TaskType = "UploadAttachment"
  17. TaskCreateNewModelTask TaskType = "CreateNewModelTask"
  18. TaskBindWechat TaskType = "BindWechat"
  19. TaskCreateCloudbrainTask TaskType = "CreateCloudbrainTask"
  20. TaskDatasetRecommended TaskType = "DatasetRecommended"
  21. TaskCreateImage TaskType = "CreateImage"
  22. TaskImageRecommend TaskType = "ImageRecommend"
  23. TaskChangeUserAvatar TaskType = "ChangeUserAvatar"
  24. TaskPushCommits TaskType = "PushCommits"
  25. )
  26. func GetTaskTypeFromAction(a ActionType) TaskType {
  27. switch a {
  28. case ActionCreateDebugGPUTask,
  29. ActionCreateDebugNPUTask,
  30. ActionCreateTrainTask,
  31. ActionCreateInferenceTask,
  32. ActionCreateBenchMarkTask,
  33. ActionCreateGPUTrainTask,
  34. ActionCreateGrampusGPUDebugTask,
  35. ActionCreateGrampusNPUDebugTask,
  36. ActionCreateGrampusNPUTrainTask,
  37. ActionCreateGrampusGCUDebugTask,
  38. ActionCreateGrampusGPUTrainTask:
  39. return TaskCreateCloudbrainTask
  40. case ActionCreateRepo:
  41. return TaskCreatePublicRepo
  42. case ActionCreatePullRequest:
  43. return TaskCreatePullRequest
  44. case ActionCommentIssue:
  45. return TaskCommentIssue
  46. case ActionUploadAttachment:
  47. return TaskUploadAttachment
  48. case ActionCreateNewModelTask:
  49. return TaskCreateNewModelTask
  50. case ActionBindWechat:
  51. return TaskBindWechat
  52. case ActionDatasetRecommended:
  53. return TaskDatasetRecommended
  54. case ActionImageRecommend:
  55. return TaskImageRecommend
  56. case ActionCreateImage:
  57. return TaskCreateImage
  58. case ActionChangeUserAvatar:
  59. return TaskChangeUserAvatar
  60. case ActionCommitRepo,
  61. ActionDeleteBranch,
  62. ActionPushTag,
  63. ActionDeleteTag:
  64. return TaskPushCommits
  65. case ActionCreateIssue:
  66. return TaskCreateIssue
  67. }
  68. return ""
  69. }
  70. //PointTaskConfig Only add and delete are allowed, edit is not allowed
  71. //so if you want to edit config for some task code,please delete first and add new one
  72. type TaskConfig struct {
  73. ID int64 `xorm:"pk autoincr"`
  74. TaskCode string `xorm:"NOT NULL"`
  75. Title string
  76. AwardType string `xorm:"NOT NULL"`
  77. AwardAmount int64 `xorm:"NOT NULL"`
  78. CreatorId int64 `xorm:"NOT NULL"`
  79. CreatorName string
  80. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  81. DeletedAt timeutil.TimeStamp `xorm:"deleted"`
  82. DeleterId int64
  83. DeleterName string
  84. }
  85. type TaskConfigWithLimit struct {
  86. ID int64
  87. TaskCode string
  88. Title string
  89. AwardType string
  90. AwardAmount int64
  91. Creator string
  92. IsDeleted bool
  93. CreatedUnix timeutil.TimeStamp
  94. DeleteAt timeutil.TimeStamp
  95. Limiters []*LimitConfigVO
  96. }
  97. type TaskConfigWithLimitResponse struct {
  98. Records []*TaskConfigWithSingleLimit
  99. Total int64
  100. PageSize int
  101. Page int
  102. }
  103. type TaskConfigWithSingleLimit struct {
  104. ID int64
  105. TaskCode string
  106. AwardType string
  107. AwardAmount int64
  108. Creator string
  109. IsDeleted bool
  110. CreatedUnix timeutil.TimeStamp
  111. DeleteAt timeutil.TimeStamp
  112. RefreshRate string
  113. LimitNum int64
  114. }
  115. type TaskAndLimiterConfig struct {
  116. TaskConfig TaskConfig `xorm:"extends"`
  117. LimitConfig LimitConfig `xorm:"extends"`
  118. }
  119. type PointRule struct {
  120. UserDailyLimit int64
  121. TaskRules []TaskRule
  122. }
  123. type TaskRule struct {
  124. TaskCode string
  125. AwardType string
  126. AwardAmount int64
  127. RefreshRate string
  128. LimitNum int64
  129. }
  130. func (TaskAndLimiterConfig) TableName() string {
  131. return "task_config"
  132. }
  133. type BatchLimitConfigVO struct {
  134. ConfigList []TaskConfigWithLimit
  135. }
  136. func getTaskConfig(t *TaskConfig) (*TaskConfig, error) {
  137. has, err := x.Get(t)
  138. if err != nil {
  139. return nil, err
  140. } else if !has {
  141. return nil, ErrRecordNotExist{}
  142. }
  143. return t, nil
  144. }
  145. func GetTaskConfigByTaskCode(taskCode string) (*TaskConfig, error) {
  146. t := &TaskConfig{
  147. TaskCode: taskCode,
  148. }
  149. return getTaskConfig(t)
  150. }
  151. func GetTaskConfigByID(id int64) (*TaskConfig, error) {
  152. t := &TaskConfig{
  153. ID: id,
  154. }
  155. return getTaskConfig(t)
  156. }
  157. func GetTaskConfigList() ([]*TaskConfig, error) {
  158. r := make([]*TaskConfig, 0)
  159. err := x.Find(&r)
  160. if err != nil {
  161. return nil, err
  162. }
  163. if len(r) == 0 {
  164. return nil, ErrRecordNotExist{}
  165. }
  166. return r, nil
  167. }
  168. type GetTaskConfigOpts struct {
  169. ListOptions
  170. Status int //1 normal 2 deleted
  171. TaskType string
  172. }
  173. func GetTaskConfigPageWithDeleted(opt GetTaskConfigOpts) ([]*TaskAndLimiterConfig, int64, error) {
  174. if opt.Page <= 0 {
  175. opt.Page = 1
  176. }
  177. cond := builder.NewCond()
  178. if opt.TaskType != "" {
  179. cond = cond.And(builder.Eq{"task_code": opt.TaskType})
  180. }
  181. var count int64
  182. var err error
  183. if opt.Status == 1 {
  184. subCond := builder.NewCond()
  185. subCond = subCond.Or(builder.IsNull{"task_config.deleted_at"})
  186. subCond = subCond.Or(builder.Eq{"task_config.deleted_at": 0})
  187. cond = cond.And(subCond)
  188. } else if opt.Status == 2 {
  189. cond = cond.And(builder.Gt{"task_config.deleted_at": 0})
  190. }
  191. count, err = x.Unscoped().Where(cond).Count(&TaskConfig{})
  192. if err != nil {
  193. return nil, 0, err
  194. }
  195. r := make([]*TaskAndLimiterConfig, 0)
  196. err = x.Join("LEFT", "limit_config", "task_config.id = limit_config.related_id").
  197. Unscoped().Where(cond).Limit(opt.PageSize, (opt.Page-1)*opt.PageSize).
  198. OrderBy("task_config.deleted_at desc,task_config.id desc").Find(&r)
  199. if len(r) == 0 {
  200. return nil, 0, ErrRecordNotExist{}
  201. }
  202. return r, count, nil
  203. }
  204. func EditTaskConfig(config TaskConfigWithLimit, doer *User) error {
  205. sess := x.NewSession()
  206. defer sess.Close()
  207. //delete old task config
  208. p := &TaskConfig{
  209. ID: config.ID,
  210. }
  211. _, err := sess.Delete(p)
  212. if err != nil {
  213. sess.Rollback()
  214. return err
  215. }
  216. //update deleter
  217. p.DeleterId = doer.ID
  218. p.DeleterName = doer.Name
  219. sess.Where("id = ?", config.ID).Unscoped().Update(p)
  220. //add new config
  221. t := &TaskConfig{
  222. TaskCode: config.TaskCode,
  223. Title: config.Title,
  224. AwardType: config.AwardType,
  225. AwardAmount: config.AwardAmount,
  226. CreatorId: doer.ID,
  227. CreatorName: doer.Name,
  228. }
  229. _, err = sess.InsertOne(t)
  230. if err != nil {
  231. sess.Rollback()
  232. return err
  233. }
  234. //delete old limiter config
  235. lp := &LimitConfig{
  236. RelatedId: config.ID,
  237. }
  238. _, err = sess.Delete(lp)
  239. if err != nil {
  240. sess.Rollback()
  241. return err
  242. }
  243. lp.DeleterName = doer.Name
  244. lp.DeleterId = doer.ID
  245. //update deleter
  246. sess.Where("related_id = ?", config.ID).Unscoped().Update(lp)
  247. //add new limiter config
  248. if config.Limiters != nil && len(config.Limiters) > 0 {
  249. for _, v := range config.Limiters {
  250. //add new config
  251. l := &LimitConfig{
  252. Title: v.Title,
  253. RefreshRate: v.RefreshRate,
  254. Scope: v.Scope,
  255. LimitNum: v.LimitNum,
  256. LimitCode: config.TaskCode,
  257. LimitType: LimitTypeTask.Name(),
  258. CreatorId: doer.ID,
  259. CreatorName: doer.Name,
  260. RelatedId: t.ID,
  261. }
  262. _, err = sess.Insert(l)
  263. if err != nil {
  264. sess.Rollback()
  265. return err
  266. }
  267. }
  268. }
  269. sess.Commit()
  270. return nil
  271. }
  272. func NewTaskConfig(config TaskConfigWithLimit, doer *User) error {
  273. sess := x.NewSession()
  274. defer sess.Close()
  275. //add new config
  276. t := &TaskConfig{
  277. TaskCode: config.TaskCode,
  278. Title: config.Title,
  279. AwardType: config.AwardType,
  280. AwardAmount: config.AwardAmount,
  281. CreatorId: doer.ID,
  282. CreatorName: doer.Name,
  283. }
  284. _, err := sess.InsertOne(t)
  285. if err != nil {
  286. sess.Rollback()
  287. return err
  288. }
  289. //add new limiter config
  290. if config.Limiters != nil && len(config.Limiters) > 0 {
  291. for _, v := range config.Limiters {
  292. //add new config
  293. l := &LimitConfig{
  294. RelatedId: t.ID,
  295. Title: v.Title,
  296. RefreshRate: v.RefreshRate,
  297. Scope: v.Scope,
  298. LimitNum: v.LimitNum,
  299. LimitCode: config.TaskCode,
  300. LimitType: LimitTypeTask.Name(),
  301. CreatorId: doer.ID,
  302. CreatorName: doer.Name,
  303. }
  304. _, err = sess.Insert(l)
  305. if err != nil {
  306. sess.Rollback()
  307. return err
  308. }
  309. }
  310. }
  311. sess.Commit()
  312. return nil
  313. }
  314. func DelTaskConfig(id int64, doer *User) error {
  315. sess := x.NewSession()
  316. defer sess.Close()
  317. //delete old task config
  318. p := &TaskConfig{
  319. ID: id,
  320. }
  321. _, err := sess.Delete(p)
  322. if err != nil {
  323. sess.Rollback()
  324. return err
  325. }
  326. //update deleter
  327. p.DeleterId = doer.ID
  328. p.DeleterName = doer.Name
  329. sess.Where("id = ?", id).Unscoped().Update(p)
  330. //delete old limiter config
  331. lp := &LimitConfig{
  332. RelatedId: id,
  333. }
  334. _, err = sess.Delete(lp)
  335. if err != nil {
  336. sess.Rollback()
  337. return err
  338. }
  339. lp.DeleterName = doer.Name
  340. lp.DeleterId = doer.ID
  341. //update deleter
  342. sess.Where("related_id = ?", id).Unscoped().Update(lp)
  343. sess.Commit()
  344. return nil
  345. }