task.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. package model
  2. import (
  3. "bytes"
  4. "database/sql/driver"
  5. "encoding/json"
  6. "time"
  7. "github.com/QuantumNous/new-api/common"
  8. "github.com/QuantumNous/new-api/constant"
  9. "github.com/QuantumNous/new-api/dto"
  10. commonRelay "github.com/QuantumNous/new-api/relay/common"
  11. )
  12. type TaskStatus string
  13. func (t TaskStatus) ToVideoStatus() string {
  14. var status string
  15. switch t {
  16. case TaskStatusQueued, TaskStatusSubmitted:
  17. status = dto.VideoStatusQueued
  18. case TaskStatusInProgress:
  19. status = dto.VideoStatusInProgress
  20. case TaskStatusSuccess:
  21. status = dto.VideoStatusCompleted
  22. case TaskStatusFailure:
  23. status = dto.VideoStatusFailed
  24. default:
  25. status = dto.VideoStatusUnknown // Default fallback
  26. }
  27. return status
  28. }
  29. const (
  30. TaskStatusNotStart TaskStatus = "NOT_START"
  31. TaskStatusSubmitted = "SUBMITTED"
  32. TaskStatusQueued = "QUEUED"
  33. TaskStatusInProgress = "IN_PROGRESS"
  34. TaskStatusFailure = "FAILURE"
  35. TaskStatusSuccess = "SUCCESS"
  36. TaskStatusUnknown = "UNKNOWN"
  37. )
  38. type Task struct {
  39. ID int64 `json:"id" gorm:"primary_key;AUTO_INCREMENT"`
  40. CreatedAt int64 `json:"created_at" gorm:"index"`
  41. UpdatedAt int64 `json:"updated_at"`
  42. TaskID string `json:"task_id" gorm:"type:varchar(191);index"` // 第三方id,不一定有/ song id\ Task id
  43. Platform constant.TaskPlatform `json:"platform" gorm:"type:varchar(30);index"` // 平台
  44. UserId int `json:"user_id" gorm:"index"`
  45. Group string `json:"group" gorm:"type:varchar(50)"` // 修正计费用
  46. ChannelId int `json:"channel_id" gorm:"index"`
  47. Quota int `json:"quota"`
  48. Action string `json:"action" gorm:"type:varchar(40);index"` // 任务类型, song, lyrics, description-mode
  49. Status TaskStatus `json:"status" gorm:"type:varchar(20);index"` // 任务状态
  50. FailReason string `json:"fail_reason"`
  51. SubmitTime int64 `json:"submit_time" gorm:"index"`
  52. StartTime int64 `json:"start_time" gorm:"index"`
  53. FinishTime int64 `json:"finish_time" gorm:"index"`
  54. Progress string `json:"progress" gorm:"type:varchar(20);index"`
  55. Properties Properties `json:"properties" gorm:"type:json"`
  56. Username string `json:"username,omitempty" gorm:"-"`
  57. // 禁止返回给用户,内部可能包含key等隐私信息
  58. PrivateData TaskPrivateData `json:"-" gorm:"column:private_data;type:json"`
  59. Data json.RawMessage `json:"data" gorm:"type:json"`
  60. }
  61. func (t *Task) SetData(data any) {
  62. b, _ := common.Marshal(data)
  63. t.Data = json.RawMessage(b)
  64. }
  65. func (t *Task) GetData(v any) error {
  66. return common.Unmarshal(t.Data, &v)
  67. }
  68. type Properties struct {
  69. Input string `json:"input"`
  70. UpstreamModelName string `json:"upstream_model_name,omitempty"`
  71. OriginModelName string `json:"origin_model_name,omitempty"`
  72. }
  73. func (m *Properties) Scan(val interface{}) error {
  74. bytesValue, _ := val.([]byte)
  75. if len(bytesValue) == 0 {
  76. *m = Properties{}
  77. return nil
  78. }
  79. return common.Unmarshal(bytesValue, m)
  80. }
  81. func (m Properties) Value() (driver.Value, error) {
  82. if m == (Properties{}) {
  83. return nil, nil
  84. }
  85. return common.Marshal(m)
  86. }
  87. type TaskPrivateData struct {
  88. Key string `json:"key,omitempty"`
  89. UpstreamTaskID string `json:"upstream_task_id,omitempty"` // 上游真实 task ID
  90. ResultURL string `json:"result_url,omitempty"` // 任务成功后的结果 URL(视频地址等)
  91. // 计费上下文:用于异步退款/差额结算(轮询阶段读取)
  92. BillingSource string `json:"billing_source,omitempty"` // "wallet" 或 "subscription"
  93. SubscriptionId int `json:"subscription_id,omitempty"` // 订阅 ID,用于订阅退款
  94. TokenId int `json:"token_id,omitempty"` // 令牌 ID,用于令牌额度退款
  95. BillingContext *TaskBillingContext `json:"billing_context,omitempty"` // 计费参数快照(用于轮询阶段重新计算)
  96. }
  97. // TaskBillingContext 记录任务提交时的计费参数,以便轮询阶段可以重新计算额度。
  98. type TaskBillingContext struct {
  99. ModelPrice float64 `json:"model_price,omitempty"` // 模型单价
  100. GroupRatio float64 `json:"group_ratio,omitempty"` // 分组倍率
  101. ModelRatio float64 `json:"model_ratio,omitempty"` // 模型倍率
  102. OtherRatios map[string]float64 `json:"other_ratios,omitempty"` // 附加倍率(时长、分辨率等)
  103. OriginModelName string `json:"origin_model_name,omitempty"` // 模型名称,必须为OriginModelName
  104. PerCallBilling bool `json:"per_call_billing,omitempty"` // 按次计费:跳过轮询阶段的差额结算
  105. }
  106. // GetUpstreamTaskID 获取上游真实 task ID(用于与 provider 通信)
  107. // 旧数据没有 UpstreamTaskID 时,TaskID 本身就是上游 ID
  108. func (t *Task) GetUpstreamTaskID() string {
  109. if t.PrivateData.UpstreamTaskID != "" {
  110. return t.PrivateData.UpstreamTaskID
  111. }
  112. return t.TaskID
  113. }
  114. // GetResultURL 获取任务结果 URL(视频地址等)
  115. // 新数据存在 PrivateData.ResultURL 中;旧数据回退到 FailReason(历史兼容)
  116. func (t *Task) GetResultURL() string {
  117. if t.PrivateData.ResultURL != "" {
  118. return t.PrivateData.ResultURL
  119. }
  120. return t.FailReason
  121. }
  122. // GenerateTaskID 生成对外暴露的 task_xxxx 格式 ID
  123. func GenerateTaskID() string {
  124. key, _ := common.GenerateRandomCharsKey(32)
  125. return "task_" + key
  126. }
  127. func (p *TaskPrivateData) Scan(val interface{}) error {
  128. bytesValue, _ := val.([]byte)
  129. if len(bytesValue) == 0 {
  130. return nil
  131. }
  132. return common.Unmarshal(bytesValue, p)
  133. }
  134. func (p TaskPrivateData) Value() (driver.Value, error) {
  135. if (p == TaskPrivateData{}) {
  136. return nil, nil
  137. }
  138. return common.Marshal(p)
  139. }
  140. // SyncTaskQueryParams 用于包含所有搜索条件的结构体,可以根据需求添加更多字段
  141. type SyncTaskQueryParams struct {
  142. Platform constant.TaskPlatform
  143. ChannelID string
  144. TaskID string
  145. UserID string
  146. Action string
  147. Status string
  148. StartTimestamp int64
  149. EndTimestamp int64
  150. UserIDs []int
  151. }
  152. func InitTask(platform constant.TaskPlatform, relayInfo *commonRelay.RelayInfo) *Task {
  153. properties := Properties{}
  154. privateData := TaskPrivateData{}
  155. if relayInfo != nil && relayInfo.ChannelMeta != nil {
  156. if relayInfo.ChannelMeta.ChannelType == constant.ChannelTypeGemini {
  157. privateData.Key = relayInfo.ChannelMeta.ApiKey
  158. }
  159. if relayInfo.UpstreamModelName != "" {
  160. properties.UpstreamModelName = relayInfo.UpstreamModelName
  161. }
  162. if relayInfo.OriginModelName != "" {
  163. properties.OriginModelName = relayInfo.OriginModelName
  164. }
  165. }
  166. // 使用预生成的公开 ID(如果有),否则新生成
  167. taskID := ""
  168. if relayInfo.TaskRelayInfo != nil && relayInfo.TaskRelayInfo.PublicTaskID != "" {
  169. taskID = relayInfo.TaskRelayInfo.PublicTaskID
  170. } else {
  171. taskID = GenerateTaskID()
  172. }
  173. t := &Task{
  174. TaskID: taskID,
  175. UserId: relayInfo.UserId,
  176. Group: relayInfo.UsingGroup,
  177. SubmitTime: time.Now().Unix(),
  178. Status: TaskStatusNotStart,
  179. Progress: "0%",
  180. ChannelId: relayInfo.ChannelId,
  181. Platform: platform,
  182. Properties: properties,
  183. PrivateData: privateData,
  184. }
  185. return t
  186. }
  187. func TaskGetAllUserTask(userId int, startIdx int, num int, queryParams SyncTaskQueryParams) []*Task {
  188. var tasks []*Task
  189. var err error
  190. // 初始化查询构建器
  191. query := DB.Where("user_id = ?", userId)
  192. if queryParams.TaskID != "" {
  193. query = query.Where("task_id = ?", queryParams.TaskID)
  194. }
  195. if queryParams.Action != "" {
  196. query = query.Where("action = ?", queryParams.Action)
  197. }
  198. if queryParams.Status != "" {
  199. query = query.Where("status = ?", queryParams.Status)
  200. }
  201. if queryParams.Platform != "" {
  202. query = query.Where("platform = ?", queryParams.Platform)
  203. }
  204. if queryParams.StartTimestamp != 0 {
  205. // 假设您已将前端传来的时间戳转换为数据库所需的时间格式,并处理了时间戳的验证和解析
  206. query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
  207. }
  208. if queryParams.EndTimestamp != 0 {
  209. query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
  210. }
  211. // 获取数据
  212. err = query.Omit("channel_id").Order("id desc").Limit(num).Offset(startIdx).Find(&tasks).Error
  213. if err != nil {
  214. return nil
  215. }
  216. return tasks
  217. }
  218. func TaskGetAllTasks(startIdx int, num int, queryParams SyncTaskQueryParams) []*Task {
  219. var tasks []*Task
  220. var err error
  221. // 初始化查询构建器
  222. query := DB
  223. // 添加过滤条件
  224. if queryParams.ChannelID != "" {
  225. query = query.Where("channel_id = ?", queryParams.ChannelID)
  226. }
  227. if queryParams.Platform != "" {
  228. query = query.Where("platform = ?", queryParams.Platform)
  229. }
  230. if queryParams.UserID != "" {
  231. query = query.Where("user_id = ?", queryParams.UserID)
  232. }
  233. if len(queryParams.UserIDs) != 0 {
  234. query = query.Where("user_id in (?)", queryParams.UserIDs)
  235. }
  236. if queryParams.TaskID != "" {
  237. query = query.Where("task_id = ?", queryParams.TaskID)
  238. }
  239. if queryParams.Action != "" {
  240. query = query.Where("action = ?", queryParams.Action)
  241. }
  242. if queryParams.Status != "" {
  243. query = query.Where("status = ?", queryParams.Status)
  244. }
  245. if queryParams.StartTimestamp != 0 {
  246. query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
  247. }
  248. if queryParams.EndTimestamp != 0 {
  249. query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
  250. }
  251. // 获取数据
  252. err = query.Order("id desc").Limit(num).Offset(startIdx).Find(&tasks).Error
  253. if err != nil {
  254. return nil
  255. }
  256. return tasks
  257. }
  258. func GetAllUnFinishSyncTasks(limit int) []*Task {
  259. var tasks []*Task
  260. var err error
  261. // get all tasks progress is not 100%
  262. err = DB.Where("progress != ?", "100%").Where("status != ?", TaskStatusFailure).Where("status != ?", TaskStatusSuccess).Limit(limit).Order("id").Find(&tasks).Error
  263. if err != nil {
  264. return nil
  265. }
  266. return tasks
  267. }
  268. func GetByOnlyTaskId(taskId string) (*Task, bool, error) {
  269. if taskId == "" {
  270. return nil, false, nil
  271. }
  272. var task *Task
  273. var err error
  274. err = DB.Where("task_id = ?", taskId).First(&task).Error
  275. exist, err := RecordExist(err)
  276. if err != nil {
  277. return nil, false, err
  278. }
  279. return task, exist, err
  280. }
  281. func GetByTaskId(userId int, taskId string) (*Task, bool, error) {
  282. if taskId == "" {
  283. return nil, false, nil
  284. }
  285. var task *Task
  286. var err error
  287. err = DB.Where("user_id = ? and task_id = ?", userId, taskId).
  288. First(&task).Error
  289. exist, err := RecordExist(err)
  290. if err != nil {
  291. return nil, false, err
  292. }
  293. return task, exist, err
  294. }
  295. func GetByTaskIds(userId int, taskIds []any) ([]*Task, error) {
  296. if len(taskIds) == 0 {
  297. return nil, nil
  298. }
  299. var task []*Task
  300. var err error
  301. err = DB.Where("user_id = ? and task_id in (?)", userId, taskIds).
  302. Find(&task).Error
  303. if err != nil {
  304. return nil, err
  305. }
  306. return task, nil
  307. }
  308. func (Task *Task) Insert() error {
  309. var err error
  310. err = DB.Create(Task).Error
  311. return err
  312. }
  313. type taskSnapshot struct {
  314. Status TaskStatus
  315. Progress string
  316. StartTime int64
  317. FinishTime int64
  318. FailReason string
  319. ResultURL string
  320. Data json.RawMessage
  321. }
  322. func (s taskSnapshot) Equal(other taskSnapshot) bool {
  323. return s.Status == other.Status &&
  324. s.Progress == other.Progress &&
  325. s.StartTime == other.StartTime &&
  326. s.FinishTime == other.FinishTime &&
  327. s.FailReason == other.FailReason &&
  328. s.ResultURL == other.ResultURL &&
  329. bytes.Equal(s.Data, other.Data)
  330. }
  331. func (t *Task) Snapshot() taskSnapshot {
  332. return taskSnapshot{
  333. Status: t.Status,
  334. Progress: t.Progress,
  335. StartTime: t.StartTime,
  336. FinishTime: t.FinishTime,
  337. FailReason: t.FailReason,
  338. ResultURL: t.PrivateData.ResultURL,
  339. Data: t.Data,
  340. }
  341. }
  342. func (Task *Task) Update() error {
  343. var err error
  344. err = DB.Save(Task).Error
  345. return err
  346. }
  347. // UpdateWithStatus performs a conditional UPDATE guarded by fromStatus (CAS).
  348. // Returns (true, nil) if this caller won the update, (false, nil) if
  349. // another process already moved the task out of fromStatus.
  350. //
  351. // Uses Model().Select("*").Updates() instead of Save() because GORM's Save
  352. // falls back to INSERT ON CONFLICT when the WHERE-guarded UPDATE matches
  353. // zero rows, which silently bypasses the CAS guard.
  354. func (t *Task) UpdateWithStatus(fromStatus TaskStatus) (bool, error) {
  355. result := DB.Model(t).Where("status = ?", fromStatus).Select("*").Updates(t)
  356. if result.Error != nil {
  357. return false, result.Error
  358. }
  359. return result.RowsAffected > 0, nil
  360. }
  361. func TaskBulkUpdateByID(ids []int64, params map[string]any) error {
  362. if len(ids) == 0 {
  363. return nil
  364. }
  365. return DB.Model(&Task{}).
  366. Where("id in (?)", ids).
  367. Updates(params).Error
  368. }
  369. type TaskQuotaUsage struct {
  370. Mode string `json:"mode"`
  371. Count float64 `json:"count"`
  372. }
  373. // TaskCountAllTasks returns total tasks that match the given query params (admin usage)
  374. func TaskCountAllTasks(queryParams SyncTaskQueryParams) int64 {
  375. var total int64
  376. query := DB.Model(&Task{})
  377. if queryParams.ChannelID != "" {
  378. query = query.Where("channel_id = ?", queryParams.ChannelID)
  379. }
  380. if queryParams.Platform != "" {
  381. query = query.Where("platform = ?", queryParams.Platform)
  382. }
  383. if queryParams.UserID != "" {
  384. query = query.Where("user_id = ?", queryParams.UserID)
  385. }
  386. if len(queryParams.UserIDs) != 0 {
  387. query = query.Where("user_id in (?)", queryParams.UserIDs)
  388. }
  389. if queryParams.TaskID != "" {
  390. query = query.Where("task_id = ?", queryParams.TaskID)
  391. }
  392. if queryParams.Action != "" {
  393. query = query.Where("action = ?", queryParams.Action)
  394. }
  395. if queryParams.Status != "" {
  396. query = query.Where("status = ?", queryParams.Status)
  397. }
  398. if queryParams.StartTimestamp != 0 {
  399. query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
  400. }
  401. if queryParams.EndTimestamp != 0 {
  402. query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
  403. }
  404. _ = query.Count(&total).Error
  405. return total
  406. }
  407. // TaskCountAllUserTask returns total tasks for given user
  408. func TaskCountAllUserTask(userId int, queryParams SyncTaskQueryParams) int64 {
  409. var total int64
  410. query := DB.Model(&Task{}).Where("user_id = ?", userId)
  411. if queryParams.TaskID != "" {
  412. query = query.Where("task_id = ?", queryParams.TaskID)
  413. }
  414. if queryParams.Action != "" {
  415. query = query.Where("action = ?", queryParams.Action)
  416. }
  417. if queryParams.Status != "" {
  418. query = query.Where("status = ?", queryParams.Status)
  419. }
  420. if queryParams.Platform != "" {
  421. query = query.Where("platform = ?", queryParams.Platform)
  422. }
  423. if queryParams.StartTimestamp != 0 {
  424. query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
  425. }
  426. if queryParams.EndTimestamp != 0 {
  427. query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
  428. }
  429. _ = query.Count(&total).Error
  430. return total
  431. }
  432. func (t *Task) ToOpenAIVideo() *dto.OpenAIVideo {
  433. openAIVideo := dto.NewOpenAIVideo()
  434. openAIVideo.ID = t.TaskID
  435. openAIVideo.Status = t.Status.ToVideoStatus()
  436. openAIVideo.Model = t.Properties.OriginModelName
  437. openAIVideo.SetProgressStr(t.Progress)
  438. openAIVideo.CreatedAt = t.CreatedAt
  439. openAIVideo.CompletedAt = t.UpdatedAt
  440. openAIVideo.SetMetadata("url", t.GetResultURL())
  441. return openAIVideo
  442. }