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 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  5. "code.gitea.io/gitea/modules/log"
  6. "code.gitea.io/gitea/modules/setting"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. "xorm.io/builder"
  9. "xorm.io/xorm"
  10. )
  11. type AiModelManage struct {
  12. ID string `xorm:"pk"`
  13. Name string `xorm:"INDEX NOT NULL"`
  14. Version string `xorm:"NOT NULL"`
  15. VersionCount int `xorm:"NOT NULL DEFAULT 0"`
  16. New int `xorm:"NOT NULL"`
  17. Type int `xorm:"NOT NULL"`
  18. Size int64 `xorm:"NOT NULL"`
  19. Description string `xorm:"varchar(2000)"`
  20. Label string `xorm:"varchar(1000)"`
  21. Path string `xorm:"varchar(400) NOT NULL"`
  22. DownloadCount int `xorm:"NOT NULL DEFAULT 0"`
  23. Engine int64 `xorm:"NOT NULL DEFAULT 0"`
  24. Status int `xorm:"NOT NULL DEFAULT 0"`
  25. Accuracy string `xorm:"varchar(1000)"`
  26. AttachmentId string `xorm:"NULL"`
  27. RepoId int64 `xorm:"INDEX NULL"`
  28. CodeBranch string `xorm:"varchar(400) NULL"`
  29. CodeCommitID string `xorm:"NULL"`
  30. UserId int64 `xorm:"NOT NULL"`
  31. UserName string
  32. UserRelAvatarLink string
  33. TrainTaskInfo string `xorm:"text NULL"`
  34. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  35. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  36. IsCanOper bool
  37. IsCanDelete bool
  38. }
  39. type AiModelConvert struct {
  40. ID string `xorm:"pk"`
  41. Name string `xorm:"INDEX NOT NULL"`
  42. Status string `xorm:"NULL"`
  43. SrcEngine int `xorm:"NOT NULL DEFAULT 0"`
  44. RepoId int64 `xorm:"INDEX NULL"`
  45. ModelId string `xorm:"NOT NULL"`
  46. ModelName string `xorm:"NULL"`
  47. ModelVersion string `xorm:"NOT NULL"`
  48. ModelPath string `xorm:"NULL"`
  49. DestFormat int `xorm:"NOT NULL DEFAULT 0"`
  50. NetOutputFormat int `xorm:"NULL"`
  51. UserId int64 `xorm:"NOT NULL"`
  52. CloudBrainTaskId string `xorm:"NULL"`
  53. ContainerID string
  54. ContainerIp string
  55. RunTime int64 `xorm:"NULL"`
  56. TrainJobDuration string
  57. InputShape string `xorm:"varchar(2000)"`
  58. InputDataFormat string `xorm:"NOT NULL"`
  59. Description string `xorm:"varchar(2000)"`
  60. Path string `xorm:"varchar(400) NOT NULL"`
  61. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  62. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  63. StartTime timeutil.TimeStamp
  64. EndTime timeutil.TimeStamp
  65. UserName string
  66. UserRelAvatarLink string
  67. IsCanOper bool
  68. IsCanDelete bool
  69. }
  70. type AiModelQueryOptions struct {
  71. ListOptions
  72. RepoID int64 // include all repos if empty
  73. UserID int64
  74. ModelID string
  75. SortType string
  76. New int
  77. // JobStatus CloudbrainStatus
  78. Type int
  79. Status int
  80. }
  81. func ModelComputeAndSetDuration(task *AiModelConvert, result JobResultPayload) {
  82. if task.StartTime == 0 {
  83. task.StartTime = timeutil.TimeStamp(result.JobStatus.CreatedTime / 1000)
  84. }
  85. if task.EndTime == 0 {
  86. if result.JobStatus.CompletedTime > 0 {
  87. task.EndTime = timeutil.TimeStamp(result.JobStatus.CompletedTime / 1000)
  88. }
  89. }
  90. var d int64
  91. if task.StartTime == 0 {
  92. d = 0
  93. } else if task.EndTime == 0 {
  94. d = time.Now().Unix() - task.StartTime.AsTime().Unix()
  95. } else {
  96. d = task.EndTime.AsTime().Unix() - task.StartTime.AsTime().Unix()
  97. }
  98. if d < 0 {
  99. d = 0
  100. }
  101. task.RunTime = d
  102. task.TrainJobDuration = ConvertDurationToStr(d)
  103. }
  104. func UpdateModelConvertCBTI(id string, CloudBrainTaskId string) error {
  105. var sess *xorm.Session
  106. sess = x.ID(id)
  107. defer sess.Close()
  108. re, err := sess.Cols("cloud_brain_task_id").Update(&AiModelConvert{
  109. CloudBrainTaskId: CloudBrainTaskId,
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. log.Info("success to update cloud_brain_task_id from db.re=" + fmt.Sprint((re)))
  115. return nil
  116. }
  117. func UpdateModelConvert(job *AiModelConvert) error {
  118. return updateModelConvert(x, job)
  119. }
  120. func updateModelConvert(e Engine, job *AiModelConvert) error {
  121. var sess *xorm.Session
  122. sess = e.Where("id = ?", job.ID)
  123. _, err := sess.Cols("status", "train_job_duration", "run_time", "start_time", "end_time", "updated_unix").Update(job)
  124. return err
  125. }
  126. func SaveModelConvert(modelConvert *AiModelConvert) error {
  127. sess := x.NewSession()
  128. defer sess.Close()
  129. re, err := sess.Insert(modelConvert)
  130. if err != nil {
  131. log.Info("insert modelConvert error." + err.Error())
  132. return err
  133. }
  134. log.Info("success to save modelConvert db.re=" + fmt.Sprint((re)))
  135. return nil
  136. }
  137. func SaveModelToDb(model *AiModelManage) error {
  138. sess := x.NewSession()
  139. defer sess.Close()
  140. re, err := sess.Insert(model)
  141. if err != nil {
  142. log.Info("insert error." + err.Error())
  143. return err
  144. }
  145. log.Info("success to save db.re=" + fmt.Sprint((re)))
  146. return nil
  147. }
  148. func QueryModelConvertById(id string) (*AiModelConvert, error) {
  149. sess := x.NewSession()
  150. defer sess.Close()
  151. sess.Select("*").Table(new(AiModelConvert)).Where("id='" + id + "'")
  152. aiModelManageConvertList := make([]*AiModelConvert, 0)
  153. err := sess.Find(&aiModelManageConvertList)
  154. if err == nil {
  155. if len(aiModelManageConvertList) == 1 {
  156. return aiModelManageConvertList[0], nil
  157. }
  158. }
  159. return nil, err
  160. }
  161. func QueryModelById(id string) (*AiModelManage, error) {
  162. sess := x.NewSession()
  163. defer sess.Close()
  164. sess.Select("*").Table("ai_model_manage").
  165. Where("id='" + id + "'")
  166. aiModelManageList := make([]*AiModelManage, 0)
  167. err := sess.Find(&aiModelManageList)
  168. if err == nil {
  169. if len(aiModelManageList) == 1 {
  170. return aiModelManageList[0], nil
  171. }
  172. } else {
  173. log.Info("error=" + err.Error())
  174. }
  175. return nil, err
  176. }
  177. func DeleteModelConvertById(id string) error {
  178. sess := x.NewSession()
  179. defer sess.Close()
  180. re, err := sess.Delete(&AiModelConvert{
  181. ID: id,
  182. })
  183. if err != nil {
  184. return err
  185. }
  186. log.Info("success to delete AiModelManageConvert from db.re=" + fmt.Sprint((re)))
  187. return nil
  188. }
  189. func DeleteModelById(id string) error {
  190. sess := x.NewSession()
  191. defer sess.Close()
  192. re, err := sess.Delete(&AiModelManage{
  193. ID: id,
  194. })
  195. if err != nil {
  196. return err
  197. }
  198. log.Info("success to delete from db.re=" + fmt.Sprint((re)))
  199. return nil
  200. }
  201. func ModifyModelDescription(id string, description string) error {
  202. var sess *xorm.Session
  203. sess = x.ID(id)
  204. defer sess.Close()
  205. re, err := sess.Cols("description").Update(&AiModelManage{
  206. Description: description,
  207. })
  208. if err != nil {
  209. return err
  210. }
  211. log.Info("success to update description from db.re=" + fmt.Sprint((re)))
  212. return nil
  213. }
  214. func ModifyModelNewProperty(id string, new int, versioncount int) error {
  215. var sess *xorm.Session
  216. sess = x.ID(id)
  217. defer sess.Close()
  218. re, err := sess.Cols("new", "version_count").Update(&AiModelManage{
  219. New: new,
  220. VersionCount: versioncount,
  221. })
  222. if err != nil {
  223. return err
  224. }
  225. log.Info("success to update new property from db.re=" + fmt.Sprint((re)))
  226. return nil
  227. }
  228. func ModifyModelDownloadCount(id string) error {
  229. sess := x.NewSession()
  230. defer sess.Close()
  231. if _, err := sess.Exec("UPDATE `ai_model_manage` SET download_count = download_count + 1 WHERE id = ?", id); err != nil {
  232. return err
  233. }
  234. return nil
  235. }
  236. func QueryModelByName(name string, repoId int64) []*AiModelManage {
  237. sess := x.NewSession()
  238. defer sess.Close()
  239. sess.Select("*").Table("ai_model_manage").
  240. Where("name='" + name + "' and repo_id=" + fmt.Sprint(repoId)).OrderBy("created_unix desc")
  241. aiModelManageList := make([]*AiModelManage, 0)
  242. sess.Find(&aiModelManageList)
  243. return aiModelManageList
  244. }
  245. func QueryModel(opts *AiModelQueryOptions) ([]*AiModelManage, int64, error) {
  246. sess := x.NewSession()
  247. defer sess.Close()
  248. var cond = builder.NewCond()
  249. if opts.RepoID > 0 {
  250. cond = cond.And(
  251. builder.Eq{"ai_model_manage.repo_id": opts.RepoID},
  252. )
  253. }
  254. if opts.UserID > 0 {
  255. cond = cond.And(
  256. builder.Eq{"ai_model_manage.user_id": opts.UserID},
  257. )
  258. }
  259. if opts.New >= 0 {
  260. cond = cond.And(
  261. builder.Eq{"ai_model_manage.new": opts.New},
  262. )
  263. }
  264. if len(opts.ModelID) > 0 {
  265. cond = cond.And(
  266. builder.Eq{"ai_model_manage.id": opts.ModelID},
  267. )
  268. }
  269. if (opts.Type) >= 0 {
  270. cond = cond.And(
  271. builder.Eq{"ai_model_manage.type": opts.Type},
  272. )
  273. }
  274. count, err := sess.Where(cond).Count(new(AiModelManage))
  275. if err != nil {
  276. return nil, 0, fmt.Errorf("Count: %v", err)
  277. }
  278. if opts.Page >= 0 && opts.PageSize > 0 {
  279. var start int
  280. if opts.Page == 0 {
  281. start = 0
  282. } else {
  283. start = (opts.Page - 1) * opts.PageSize
  284. }
  285. sess.Limit(opts.PageSize, start)
  286. }
  287. sess.OrderBy("ai_model_manage.created_unix DESC")
  288. aiModelManages := make([]*AiModelManage, 0, setting.UI.IssuePagingNum)
  289. if err := sess.Table("ai_model_manage").Where(cond).
  290. Find(&aiModelManages); err != nil {
  291. return nil, 0, fmt.Errorf("Find: %v", err)
  292. }
  293. return aiModelManages, count, nil
  294. }
  295. func QueryModelConvert(opts *AiModelQueryOptions) ([]*AiModelConvert, int64, error) {
  296. sess := x.NewSession()
  297. defer sess.Close()
  298. var cond = builder.NewCond()
  299. if opts.RepoID > 0 {
  300. cond = cond.And(
  301. builder.Eq{"ai_model_convert.repo_id": opts.RepoID},
  302. )
  303. }
  304. if opts.UserID > 0 {
  305. cond = cond.And(
  306. builder.Eq{"ai_model_convert.user_id": opts.UserID},
  307. )
  308. }
  309. count, err := sess.Where(cond).Count(new(AiModelConvert))
  310. if err != nil {
  311. return nil, 0, fmt.Errorf("Count: %v", err)
  312. }
  313. if opts.Page >= 0 && opts.PageSize > 0 {
  314. var start int
  315. if opts.Page == 0 {
  316. start = 0
  317. } else {
  318. start = (opts.Page - 1) * opts.PageSize
  319. }
  320. sess.Limit(opts.PageSize, start)
  321. }
  322. sess.OrderBy("ai_model_convert.created_unix DESC")
  323. aiModelManageConvert := make([]*AiModelConvert, 0, setting.UI.IssuePagingNum)
  324. if err := sess.Table(new(AiModelConvert)).Where(cond).
  325. Find(&aiModelManageConvert); err != nil {
  326. return nil, 0, fmt.Errorf("Find: %v", err)
  327. }
  328. return aiModelManageConvert, count, nil
  329. }