task.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. relayInfo.ChannelMeta.ChannelType == constant.ChannelTypeVertexAi {
  158. privateData.Key = relayInfo.ChannelMeta.ApiKey
  159. }
  160. if relayInfo.UpstreamModelName != "" {
  161. properties.UpstreamModelName = relayInfo.UpstreamModelName
  162. }
  163. if relayInfo.OriginModelName != "" {
  164. properties.OriginModelName = relayInfo.OriginModelName
  165. }
  166. }
  167. // 使用预生成的公开 ID(如果有),否则新生成
  168. taskID := ""
  169. if relayInfo.TaskRelayInfo != nil && relayInfo.TaskRelayInfo.PublicTaskID != "" {
  170. taskID = relayInfo.TaskRelayInfo.PublicTaskID
  171. } else {
  172. taskID = GenerateTaskID()
  173. }
  174. t := &Task{
  175. TaskID: taskID,
  176. UserId: relayInfo.UserId,
  177. Group: relayInfo.UsingGroup,
  178. SubmitTime: time.Now().Unix(),
  179. Status: TaskStatusNotStart,
  180. Progress: "0%",
  181. ChannelId: relayInfo.ChannelId,
  182. Platform: platform,
  183. Properties: properties,
  184. PrivateData: privateData,
  185. }
  186. return t
  187. }
  188. func TaskGetAllUserTask(userId int, startIdx int, num int, queryParams SyncTaskQueryParams) []*Task {
  189. var tasks []*Task
  190. var err error
  191. // 初始化查询构建器
  192. query := DB.Where("user_id = ?", userId)
  193. if queryParams.TaskID != "" {
  194. query = query.Where("task_id = ?", queryParams.TaskID)
  195. }
  196. if queryParams.Action != "" {
  197. query = query.Where("action = ?", queryParams.Action)
  198. }
  199. if queryParams.Status != "" {
  200. query = query.Where("status = ?", queryParams.Status)
  201. }
  202. if queryParams.Platform != "" {
  203. query = query.Where("platform = ?", queryParams.Platform)
  204. }
  205. if queryParams.StartTimestamp != 0 {
  206. // 假设您已将前端传来的时间戳转换为数据库所需的时间格式,并处理了时间戳的验证和解析
  207. query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
  208. }
  209. if queryParams.EndTimestamp != 0 {
  210. query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
  211. }
  212. // 获取数据
  213. err = query.Omit("channel_id").Order("id desc").Limit(num).Offset(startIdx).Find(&tasks).Error
  214. if err != nil {
  215. return nil
  216. }
  217. return tasks
  218. }
  219. func TaskGetAllTasks(startIdx int, num int, queryParams SyncTaskQueryParams) []*Task {
  220. var tasks []*Task
  221. var err error
  222. // 初始化查询构建器
  223. query := DB
  224. // 添加过滤条件
  225. if queryParams.ChannelID != "" {
  226. query = query.Where("channel_id = ?", queryParams.ChannelID)
  227. }
  228. if queryParams.Platform != "" {
  229. query = query.Where("platform = ?", queryParams.Platform)
  230. }
  231. if queryParams.UserID != "" {
  232. query = query.Where("user_id = ?", queryParams.UserID)
  233. }
  234. if len(queryParams.UserIDs) != 0 {
  235. query = query.Where("user_id in (?)", queryParams.UserIDs)
  236. }
  237. if queryParams.TaskID != "" {
  238. query = query.Where("task_id = ?", queryParams.TaskID)
  239. }
  240. if queryParams.Action != "" {
  241. query = query.Where("action = ?", queryParams.Action)
  242. }
  243. if queryParams.Status != "" {
  244. query = query.Where("status = ?", queryParams.Status)
  245. }
  246. if queryParams.StartTimestamp != 0 {
  247. query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
  248. }
  249. if queryParams.EndTimestamp != 0 {
  250. query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
  251. }
  252. // 获取数据
  253. err = query.Order("id desc").Limit(num).Offset(startIdx).Find(&tasks).Error
  254. if err != nil {
  255. return nil
  256. }
  257. return tasks
  258. }
  259. func GetTimedOutUnfinishedTasks(cutoffUnix int64, limit int) []*Task {
  260. var tasks []*Task
  261. err := DB.Where("progress != ?", "100%").
  262. Where("status NOT IN ?", []string{TaskStatusFailure, TaskStatusSuccess}).
  263. Where("submit_time < ?", cutoffUnix).
  264. Order("submit_time").
  265. Limit(limit).
  266. Find(&tasks).Error
  267. if err != nil {
  268. return nil
  269. }
  270. return tasks
  271. }
  272. func GetAllUnFinishSyncTasks(limit int) []*Task {
  273. var tasks []*Task
  274. var err error
  275. // get all tasks progress is not 100%
  276. err = DB.Where("progress != ?", "100%").Where("status != ?", TaskStatusFailure).Where("status != ?", TaskStatusSuccess).Limit(limit).Order("id").Find(&tasks).Error
  277. if err != nil {
  278. return nil
  279. }
  280. return tasks
  281. }
  282. func GetByOnlyTaskId(taskId string) (*Task, bool, error) {
  283. if taskId == "" {
  284. return nil, false, nil
  285. }
  286. var task *Task
  287. var err error
  288. err = DB.Where("task_id = ?", taskId).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 GetByTaskId(userId int, taskId string) (*Task, bool, error) {
  296. if taskId == "" {
  297. return nil, false, nil
  298. }
  299. var task *Task
  300. var err error
  301. err = DB.Where("user_id = ? and task_id = ?", userId, taskId).
  302. First(&task).Error
  303. exist, err := RecordExist(err)
  304. if err != nil {
  305. return nil, false, err
  306. }
  307. return task, exist, err
  308. }
  309. func GetByTaskIds(userId int, taskIds []any) ([]*Task, error) {
  310. if len(taskIds) == 0 {
  311. return nil, nil
  312. }
  313. var task []*Task
  314. var err error
  315. err = DB.Where("user_id = ? and task_id in (?)", userId, taskIds).
  316. Find(&task).Error
  317. if err != nil {
  318. return nil, err
  319. }
  320. return task, nil
  321. }
  322. func (Task *Task) Insert() error {
  323. var err error
  324. err = DB.Create(Task).Error
  325. return err
  326. }
  327. type taskSnapshot struct {
  328. Status TaskStatus
  329. Progress string
  330. StartTime int64
  331. FinishTime int64
  332. FailReason string
  333. ResultURL string
  334. Data json.RawMessage
  335. }
  336. func (s taskSnapshot) Equal(other taskSnapshot) bool {
  337. return s.Status == other.Status &&
  338. s.Progress == other.Progress &&
  339. s.StartTime == other.StartTime &&
  340. s.FinishTime == other.FinishTime &&
  341. s.FailReason == other.FailReason &&
  342. s.ResultURL == other.ResultURL &&
  343. bytes.Equal(s.Data, other.Data)
  344. }
  345. func (t *Task) Snapshot() taskSnapshot {
  346. return taskSnapshot{
  347. Status: t.Status,
  348. Progress: t.Progress,
  349. StartTime: t.StartTime,
  350. FinishTime: t.FinishTime,
  351. FailReason: t.FailReason,
  352. ResultURL: t.PrivateData.ResultURL,
  353. Data: t.Data,
  354. }
  355. }
  356. func (Task *Task) Update() error {
  357. var err error
  358. err = DB.Save(Task).Error
  359. return err
  360. }
  361. // UpdateWithStatus performs a conditional UPDATE guarded by fromStatus (CAS).
  362. // Returns (true, nil) if this caller won the update, (false, nil) if
  363. // another process already moved the task out of fromStatus.
  364. //
  365. // Uses Model().Select("*").Updates() instead of Save() because GORM's Save
  366. // falls back to INSERT ON CONFLICT when the WHERE-guarded UPDATE matches
  367. // zero rows, which silently bypasses the CAS guard.
  368. func (t *Task) UpdateWithStatus(fromStatus TaskStatus) (bool, error) {
  369. result := DB.Model(t).Where("status = ?", fromStatus).Select("*").Updates(t)
  370. if result.Error != nil {
  371. return false, result.Error
  372. }
  373. return result.RowsAffected > 0, nil
  374. }
  375. // TaskBulkUpdateByID performs an unconditional bulk UPDATE by primary key IDs.
  376. // WARNING: This function has NO CAS (Compare-And-Swap) guard — it will overwrite
  377. // any concurrent status changes. DO NOT use in billing/quota lifecycle flows
  378. // (e.g., timeout, success, failure transitions that trigger refunds or settlements).
  379. // For status transitions that involve billing, use Task.UpdateWithStatus() instead.
  380. func TaskBulkUpdateByID(ids []int64, params map[string]any) error {
  381. if len(ids) == 0 {
  382. return nil
  383. }
  384. return DB.Model(&Task{}).
  385. Where("id in (?)", ids).
  386. Updates(params).Error
  387. }
  388. type TaskQuotaUsage struct {
  389. Mode string `json:"mode"`
  390. Count float64 `json:"count"`
  391. }
  392. // TaskCountAllTasks returns total tasks that match the given query params (admin usage)
  393. func TaskCountAllTasks(queryParams SyncTaskQueryParams) int64 {
  394. var total int64
  395. query := DB.Model(&Task{})
  396. if queryParams.ChannelID != "" {
  397. query = query.Where("channel_id = ?", queryParams.ChannelID)
  398. }
  399. if queryParams.Platform != "" {
  400. query = query.Where("platform = ?", queryParams.Platform)
  401. }
  402. if queryParams.UserID != "" {
  403. query = query.Where("user_id = ?", queryParams.UserID)
  404. }
  405. if len(queryParams.UserIDs) != 0 {
  406. query = query.Where("user_id in (?)", queryParams.UserIDs)
  407. }
  408. if queryParams.TaskID != "" {
  409. query = query.Where("task_id = ?", queryParams.TaskID)
  410. }
  411. if queryParams.Action != "" {
  412. query = query.Where("action = ?", queryParams.Action)
  413. }
  414. if queryParams.Status != "" {
  415. query = query.Where("status = ?", queryParams.Status)
  416. }
  417. if queryParams.StartTimestamp != 0 {
  418. query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
  419. }
  420. if queryParams.EndTimestamp != 0 {
  421. query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
  422. }
  423. _ = query.Count(&total).Error
  424. return total
  425. }
  426. // TaskCountAllUserTask returns total tasks for given user
  427. func TaskCountAllUserTask(userId int, queryParams SyncTaskQueryParams) int64 {
  428. var total int64
  429. query := DB.Model(&Task{}).Where("user_id = ?", userId)
  430. if queryParams.TaskID != "" {
  431. query = query.Where("task_id = ?", queryParams.TaskID)
  432. }
  433. if queryParams.Action != "" {
  434. query = query.Where("action = ?", queryParams.Action)
  435. }
  436. if queryParams.Status != "" {
  437. query = query.Where("status = ?", queryParams.Status)
  438. }
  439. if queryParams.Platform != "" {
  440. query = query.Where("platform = ?", queryParams.Platform)
  441. }
  442. if queryParams.StartTimestamp != 0 {
  443. query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
  444. }
  445. if queryParams.EndTimestamp != 0 {
  446. query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
  447. }
  448. _ = query.Count(&total).Error
  449. return total
  450. }
  451. func (t *Task) ToOpenAIVideo() *dto.OpenAIVideo {
  452. openAIVideo := dto.NewOpenAIVideo()
  453. openAIVideo.ID = t.TaskID
  454. openAIVideo.Status = t.Status.ToVideoStatus()
  455. openAIVideo.Model = t.Properties.OriginModelName
  456. openAIVideo.SetProgressStr(t.Progress)
  457. openAIVideo.CreatedAt = t.CreatedAt
  458. openAIVideo.CompletedAt = t.UpdatedAt
  459. openAIVideo.SetMetadata("url", t.GetResultURL())
  460. return openAIVideo
  461. }