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.6 kB

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