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.

ai_model_manage.go 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package models
  2. import (
  3. "fmt"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/setting"
  6. "code.gitea.io/gitea/modules/timeutil"
  7. "xorm.io/builder"
  8. "xorm.io/xorm"
  9. )
  10. type AiModelManage struct {
  11. ID string `xorm:"pk"`
  12. Name string `xorm:"NOT NULL"`
  13. Version string `xorm:"NOT NULL"`
  14. VersionCount int `xorm:"NOT NULL DEFAULT 0"`
  15. New int `xorm:"NOT NULL"`
  16. Type int `xorm:"NOT NULL"`
  17. Size int64 `xorm:"NOT NULL"`
  18. Description string `xorm:"varchar(2000)"`
  19. Label string `xorm:"varchar(1000)"`
  20. Path string `xorm:"varchar(400) NOT NULL"`
  21. DownloadCount int `xorm:"NOT NULL DEFAULT 0"`
  22. Engine int64 `xorm:"NOT NULL DEFAULT 0"`
  23. Status int `xorm:"NOT NULL DEFAULT 0"`
  24. Accuracy string `xorm:"varchar(1000)"`
  25. AttachmentId string `xorm:"NULL"`
  26. RepoId int64 `xorm:"NULL"`
  27. CodeBranch string `xorm:"varchar(400) NULL"`
  28. CodeCommitID string `xorm:"NULL"`
  29. UserId int64 `xorm:"NOT NULL"`
  30. UserName string `xorm:"NULL"`
  31. UserRelAvatarLink string `xorm:"NULL"`
  32. TrainTaskInfo string `xorm:"text NULL"`
  33. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  34. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  35. }
  36. type AiModelQueryOptions struct {
  37. ListOptions
  38. RepoID int64 // include all repos if empty
  39. UserID int64
  40. ModelID string
  41. SortType string
  42. New int
  43. // JobStatus CloudbrainStatus
  44. Type int
  45. }
  46. func SaveModelToDb(model *AiModelManage) error {
  47. sess := x.NewSession()
  48. defer sess.Close()
  49. re, err := sess.Insert(model)
  50. if err != nil {
  51. log.Info("insert error." + err.Error())
  52. return err
  53. }
  54. log.Info("success to save db.re=" + fmt.Sprint((re)))
  55. return nil
  56. }
  57. func QueryModelById(id string) (*AiModelManage, error) {
  58. sess := x.NewSession()
  59. defer sess.Close()
  60. sess.Select("*").Table("ai_model_manage").
  61. Where("id='" + id + "'")
  62. aiModelManageList := make([]*AiModelManage, 0)
  63. err := sess.Find(&aiModelManageList)
  64. if err == nil {
  65. if len(aiModelManageList) == 1 {
  66. return aiModelManageList[0], nil
  67. }
  68. }
  69. return nil, err
  70. }
  71. func DeleteModelById(id string) error {
  72. sess := x.NewSession()
  73. defer sess.Close()
  74. re, err := sess.Delete(&AiModelManage{
  75. ID: id,
  76. })
  77. if err != nil {
  78. return err
  79. }
  80. log.Info("success to delete from db.re=" + fmt.Sprint((re)))
  81. return nil
  82. }
  83. func ModifyModelDescription(id string, description string) error {
  84. var sess *xorm.Session
  85. sess = x.ID(id)
  86. defer sess.Close()
  87. re, err := sess.Cols("description").Update(&AiModelManage{
  88. Description: description,
  89. })
  90. if err != nil {
  91. return err
  92. }
  93. log.Info("success to update description from db.re=" + fmt.Sprint((re)))
  94. return nil
  95. }
  96. func ModifyModelNewProperty(id string, new int, versioncount int) error {
  97. var sess *xorm.Session
  98. sess = x.ID(id)
  99. defer sess.Close()
  100. re, err := sess.Cols("new", "version_count").Update(&AiModelManage{
  101. New: new,
  102. VersionCount: versioncount,
  103. })
  104. if err != nil {
  105. return err
  106. }
  107. log.Info("success to update new property from db.re=" + fmt.Sprint((re)))
  108. return nil
  109. }
  110. func ModifyModelDownloadCount(id string) error {
  111. sess := x.NewSession()
  112. defer sess.Close()
  113. if _, err := sess.Exec("UPDATE `ai_model_manage` SET download_count = download_count + 1 WHERE id = ?", id); err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. func QueryModelByName(name string, repoId int64) []*AiModelManage {
  119. sess := x.NewSession()
  120. defer sess.Close()
  121. sess.Select("*").Table("ai_model_manage").
  122. Where("name='" + name + "' and repo_id=" + fmt.Sprint(repoId)).OrderBy("version desc")
  123. aiModelManageList := make([]*AiModelManage, 0)
  124. sess.Find(&aiModelManageList)
  125. return aiModelManageList
  126. }
  127. func QueryModel(opts *AiModelQueryOptions) ([]*AiModelManage, int64, error) {
  128. sess := x.NewSession()
  129. defer sess.Close()
  130. var cond = builder.NewCond()
  131. if opts.RepoID > 0 {
  132. cond = cond.And(
  133. builder.Eq{"ai_model_manage.repo_id": opts.RepoID},
  134. )
  135. }
  136. if opts.UserID > 0 {
  137. cond = cond.And(
  138. builder.Eq{"ai_model_manage.user_id": opts.UserID},
  139. )
  140. }
  141. if opts.New >= 0 {
  142. cond = cond.And(
  143. builder.Eq{"ai_model_manage.new": opts.New},
  144. )
  145. }
  146. if len(opts.ModelID) > 0 {
  147. cond = cond.And(
  148. builder.Eq{"ai_model_manage.id": opts.ModelID},
  149. )
  150. }
  151. if (opts.Type) >= 0 {
  152. cond = cond.And(
  153. builder.Eq{"ai_model_manage.type": opts.Type},
  154. )
  155. }
  156. count, err := sess.Where(cond).Count(new(AiModelManage))
  157. if err != nil {
  158. return nil, 0, fmt.Errorf("Count: %v", err)
  159. }
  160. if opts.Page >= 0 && opts.PageSize > 0 {
  161. var start int
  162. if opts.Page == 0 {
  163. start = 0
  164. } else {
  165. start = (opts.Page - 1) * opts.PageSize
  166. }
  167. sess.Limit(opts.PageSize, start)
  168. }
  169. sess.OrderBy("ai_model_manage.created_unix DESC")
  170. aiModelManages := make([]*AiModelManage, 0, setting.UI.IssuePagingNum)
  171. if err := sess.Table("ai_model_manage").Where(cond).
  172. Find(&aiModelManages); err != nil {
  173. return nil, 0, fmt.Errorf("Find: %v", err)
  174. }
  175. sess.Close()
  176. return aiModelManages, count, nil
  177. }