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