mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-10 03:57:39 +08:00
feat: change history data field
This commit is contained in:
9
db/db.go
9
db/db.go
@@ -382,11 +382,11 @@ func (c *Client) SetDefaultStorageByName(name string) error {
|
|||||||
|
|
||||||
func (c *Client) SaveHistoryRecord(h ent.History) (*ent.History, error) {
|
func (c *Client) SaveHistoryRecord(h ent.History) (*ent.History, error) {
|
||||||
return c.ent.History.Create().SetSeriesID(h.SeriesID).SetEpisodeID(h.EpisodeID).SetDate(time.Now()).
|
return c.ent.History.Create().SetSeriesID(h.SeriesID).SetEpisodeID(h.EpisodeID).SetDate(time.Now()).
|
||||||
SetCompleted(h.Completed).SetTargetDir(h.TargetDir).SetSourceTitle(h.SourceTitle).SetSaved(h.Saved).Save(context.TODO())
|
SetStatus(h.Status).SetTargetDir(h.TargetDir).SetSourceTitle(h.SourceTitle).SetSaved(h.Saved).Save(context.TODO())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetHistoryComplete(id int) error {
|
func (c *Client) SetHistoryStatus(id int, status history.Status) error {
|
||||||
return c.ent.History.Update().Where(history.ID(id)).SetCompleted(true).Exec(context.TODO())
|
return c.ent.History.Update().Where(history.ID(id)).SetStatus(status).Exec(context.TODO())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetHistories() ent.Histories {
|
func (c *Client) GetHistories() ent.Histories {
|
||||||
@@ -398,7 +398,8 @@ func (c *Client) GetHistories() ent.Histories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetRunningHistories() ent.Histories {
|
func (c *Client) GetRunningHistories() ent.Histories {
|
||||||
h, err := c.ent.History.Query().Where(history.Completed(false)).All(context.TODO())
|
h, err := c.ent.History.Query().Where(history.Or(history.StatusEQ(history.StatusRunning),
|
||||||
|
history.StatusEQ(history.StatusUploading))).All(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ type History struct {
|
|||||||
Date time.Time `json:"date,omitempty"`
|
Date time.Time `json:"date,omitempty"`
|
||||||
// TargetDir holds the value of the "target_dir" field.
|
// TargetDir holds the value of the "target_dir" field.
|
||||||
TargetDir string `json:"target_dir,omitempty"`
|
TargetDir string `json:"target_dir,omitempty"`
|
||||||
// Completed holds the value of the "completed" field.
|
// Status holds the value of the "status" field.
|
||||||
Completed bool `json:"completed"`
|
Status history.Status `json:"status,omitempty"`
|
||||||
// Saved holds the value of the "saved" field.
|
// Saved holds the value of the "saved" field.
|
||||||
Saved string `json:"saved,omitempty"`
|
Saved string `json:"saved,omitempty"`
|
||||||
selectValues sql.SelectValues
|
selectValues sql.SelectValues
|
||||||
@@ -39,11 +39,9 @@ func (*History) scanValues(columns []string) ([]any, error) {
|
|||||||
values := make([]any, len(columns))
|
values := make([]any, len(columns))
|
||||||
for i := range columns {
|
for i := range columns {
|
||||||
switch columns[i] {
|
switch columns[i] {
|
||||||
case history.FieldCompleted:
|
|
||||||
values[i] = new(sql.NullBool)
|
|
||||||
case history.FieldID, history.FieldSeriesID, history.FieldEpisodeID:
|
case history.FieldID, history.FieldSeriesID, history.FieldEpisodeID:
|
||||||
values[i] = new(sql.NullInt64)
|
values[i] = new(sql.NullInt64)
|
||||||
case history.FieldSourceTitle, history.FieldTargetDir, history.FieldSaved:
|
case history.FieldSourceTitle, history.FieldTargetDir, history.FieldStatus, history.FieldSaved:
|
||||||
values[i] = new(sql.NullString)
|
values[i] = new(sql.NullString)
|
||||||
case history.FieldDate:
|
case history.FieldDate:
|
||||||
values[i] = new(sql.NullTime)
|
values[i] = new(sql.NullTime)
|
||||||
@@ -98,11 +96,11 @@ func (h *History) assignValues(columns []string, values []any) error {
|
|||||||
} else if value.Valid {
|
} else if value.Valid {
|
||||||
h.TargetDir = value.String
|
h.TargetDir = value.String
|
||||||
}
|
}
|
||||||
case history.FieldCompleted:
|
case history.FieldStatus:
|
||||||
if value, ok := values[i].(*sql.NullBool); !ok {
|
if value, ok := values[i].(*sql.NullString); !ok {
|
||||||
return fmt.Errorf("unexpected type %T for field completed", values[i])
|
return fmt.Errorf("unexpected type %T for field status", values[i])
|
||||||
} else if value.Valid {
|
} else if value.Valid {
|
||||||
h.Completed = value.Bool
|
h.Status = history.Status(value.String)
|
||||||
}
|
}
|
||||||
case history.FieldSaved:
|
case history.FieldSaved:
|
||||||
if value, ok := values[i].(*sql.NullString); !ok {
|
if value, ok := values[i].(*sql.NullString); !ok {
|
||||||
@@ -161,8 +159,8 @@ func (h *History) String() string {
|
|||||||
builder.WriteString("target_dir=")
|
builder.WriteString("target_dir=")
|
||||||
builder.WriteString(h.TargetDir)
|
builder.WriteString(h.TargetDir)
|
||||||
builder.WriteString(", ")
|
builder.WriteString(", ")
|
||||||
builder.WriteString("completed=")
|
builder.WriteString("status=")
|
||||||
builder.WriteString(fmt.Sprintf("%v", h.Completed))
|
builder.WriteString(fmt.Sprintf("%v", h.Status))
|
||||||
builder.WriteString(", ")
|
builder.WriteString(", ")
|
||||||
builder.WriteString("saved=")
|
builder.WriteString("saved=")
|
||||||
builder.WriteString(h.Saved)
|
builder.WriteString(h.Saved)
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
package history
|
package history
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"entgo.io/ent/dialect/sql"
|
"entgo.io/ent/dialect/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,8 +23,8 @@ const (
|
|||||||
FieldDate = "date"
|
FieldDate = "date"
|
||||||
// FieldTargetDir holds the string denoting the target_dir field in the database.
|
// FieldTargetDir holds the string denoting the target_dir field in the database.
|
||||||
FieldTargetDir = "target_dir"
|
FieldTargetDir = "target_dir"
|
||||||
// FieldCompleted holds the string denoting the completed field in the database.
|
// FieldStatus holds the string denoting the status field in the database.
|
||||||
FieldCompleted = "completed"
|
FieldStatus = "status"
|
||||||
// FieldSaved holds the string denoting the saved field in the database.
|
// FieldSaved holds the string denoting the saved field in the database.
|
||||||
FieldSaved = "saved"
|
FieldSaved = "saved"
|
||||||
// Table holds the table name of the history in the database.
|
// Table holds the table name of the history in the database.
|
||||||
@@ -37,7 +39,7 @@ var Columns = []string{
|
|||||||
FieldSourceTitle,
|
FieldSourceTitle,
|
||||||
FieldDate,
|
FieldDate,
|
||||||
FieldTargetDir,
|
FieldTargetDir,
|
||||||
FieldCompleted,
|
FieldStatus,
|
||||||
FieldSaved,
|
FieldSaved,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,11 +53,31 @@ func ValidColumn(column string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
// Status defines the type for the "status" enum field.
|
||||||
// DefaultCompleted holds the default value on creation for the "completed" field.
|
type Status string
|
||||||
DefaultCompleted bool
|
|
||||||
|
// Status values.
|
||||||
|
const (
|
||||||
|
StatusRunning Status = "running"
|
||||||
|
StatusSuccess Status = "success"
|
||||||
|
StatusFail Status = "fail"
|
||||||
|
StatusUploading Status = "uploading"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s Status) String() string {
|
||||||
|
return string(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StatusValidator is a validator for the "status" field enum values. It is called by the builders before save.
|
||||||
|
func StatusValidator(s Status) error {
|
||||||
|
switch s {
|
||||||
|
case StatusRunning, StatusSuccess, StatusFail, StatusUploading:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("history: invalid enum value for status field: %q", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// OrderOption defines the ordering options for the History queries.
|
// OrderOption defines the ordering options for the History queries.
|
||||||
type OrderOption func(*sql.Selector)
|
type OrderOption func(*sql.Selector)
|
||||||
|
|
||||||
@@ -89,9 +111,9 @@ func ByTargetDir(opts ...sql.OrderTermOption) OrderOption {
|
|||||||
return sql.OrderByField(FieldTargetDir, opts...).ToFunc()
|
return sql.OrderByField(FieldTargetDir, opts...).ToFunc()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ByCompleted orders the results by the completed field.
|
// ByStatus orders the results by the status field.
|
||||||
func ByCompleted(opts ...sql.OrderTermOption) OrderOption {
|
func ByStatus(opts ...sql.OrderTermOption) OrderOption {
|
||||||
return sql.OrderByField(FieldCompleted, opts...).ToFunc()
|
return sql.OrderByField(FieldStatus, opts...).ToFunc()
|
||||||
}
|
}
|
||||||
|
|
||||||
// BySaved orders the results by the saved field.
|
// BySaved orders the results by the saved field.
|
||||||
|
|||||||
@@ -79,11 +79,6 @@ func TargetDir(v string) predicate.History {
|
|||||||
return predicate.History(sql.FieldEQ(FieldTargetDir, v))
|
return predicate.History(sql.FieldEQ(FieldTargetDir, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Completed applies equality check predicate on the "completed" field. It's identical to CompletedEQ.
|
|
||||||
func Completed(v bool) predicate.History {
|
|
||||||
return predicate.History(sql.FieldEQ(FieldCompleted, v))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Saved applies equality check predicate on the "saved" field. It's identical to SavedEQ.
|
// Saved applies equality check predicate on the "saved" field. It's identical to SavedEQ.
|
||||||
func Saved(v string) predicate.History {
|
func Saved(v string) predicate.History {
|
||||||
return predicate.History(sql.FieldEQ(FieldSaved, v))
|
return predicate.History(sql.FieldEQ(FieldSaved, v))
|
||||||
@@ -339,14 +334,24 @@ func TargetDirContainsFold(v string) predicate.History {
|
|||||||
return predicate.History(sql.FieldContainsFold(FieldTargetDir, v))
|
return predicate.History(sql.FieldContainsFold(FieldTargetDir, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompletedEQ applies the EQ predicate on the "completed" field.
|
// StatusEQ applies the EQ predicate on the "status" field.
|
||||||
func CompletedEQ(v bool) predicate.History {
|
func StatusEQ(v Status) predicate.History {
|
||||||
return predicate.History(sql.FieldEQ(FieldCompleted, v))
|
return predicate.History(sql.FieldEQ(FieldStatus, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompletedNEQ applies the NEQ predicate on the "completed" field.
|
// StatusNEQ applies the NEQ predicate on the "status" field.
|
||||||
func CompletedNEQ(v bool) predicate.History {
|
func StatusNEQ(v Status) predicate.History {
|
||||||
return predicate.History(sql.FieldNEQ(FieldCompleted, v))
|
return predicate.History(sql.FieldNEQ(FieldStatus, v))
|
||||||
|
}
|
||||||
|
|
||||||
|
// StatusIn applies the In predicate on the "status" field.
|
||||||
|
func StatusIn(vs ...Status) predicate.History {
|
||||||
|
return predicate.History(sql.FieldIn(FieldStatus, vs...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// StatusNotIn applies the NotIn predicate on the "status" field.
|
||||||
|
func StatusNotIn(vs ...Status) predicate.History {
|
||||||
|
return predicate.History(sql.FieldNotIn(FieldStatus, vs...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SavedEQ applies the EQ predicate on the "saved" field.
|
// SavedEQ applies the EQ predicate on the "saved" field.
|
||||||
|
|||||||
@@ -50,17 +50,9 @@ func (hc *HistoryCreate) SetTargetDir(s string) *HistoryCreate {
|
|||||||
return hc
|
return hc
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCompleted sets the "completed" field.
|
// SetStatus sets the "status" field.
|
||||||
func (hc *HistoryCreate) SetCompleted(b bool) *HistoryCreate {
|
func (hc *HistoryCreate) SetStatus(h history.Status) *HistoryCreate {
|
||||||
hc.mutation.SetCompleted(b)
|
hc.mutation.SetStatus(h)
|
||||||
return hc
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetNillableCompleted sets the "completed" field if the given value is not nil.
|
|
||||||
func (hc *HistoryCreate) SetNillableCompleted(b *bool) *HistoryCreate {
|
|
||||||
if b != nil {
|
|
||||||
hc.SetCompleted(*b)
|
|
||||||
}
|
|
||||||
return hc
|
return hc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +77,6 @@ func (hc *HistoryCreate) Mutation() *HistoryMutation {
|
|||||||
|
|
||||||
// Save creates the History in the database.
|
// Save creates the History in the database.
|
||||||
func (hc *HistoryCreate) Save(ctx context.Context) (*History, error) {
|
func (hc *HistoryCreate) Save(ctx context.Context) (*History, error) {
|
||||||
hc.defaults()
|
|
||||||
return withHooks(ctx, hc.sqlSave, hc.mutation, hc.hooks)
|
return withHooks(ctx, hc.sqlSave, hc.mutation, hc.hooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,14 +102,6 @@ func (hc *HistoryCreate) ExecX(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaults sets the default values of the builder before save.
|
|
||||||
func (hc *HistoryCreate) defaults() {
|
|
||||||
if _, ok := hc.mutation.Completed(); !ok {
|
|
||||||
v := history.DefaultCompleted
|
|
||||||
hc.mutation.SetCompleted(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check runs all checks and user-defined validators on the builder.
|
// check runs all checks and user-defined validators on the builder.
|
||||||
func (hc *HistoryCreate) check() error {
|
func (hc *HistoryCreate) check() error {
|
||||||
if _, ok := hc.mutation.SeriesID(); !ok {
|
if _, ok := hc.mutation.SeriesID(); !ok {
|
||||||
@@ -136,8 +119,13 @@ func (hc *HistoryCreate) check() error {
|
|||||||
if _, ok := hc.mutation.TargetDir(); !ok {
|
if _, ok := hc.mutation.TargetDir(); !ok {
|
||||||
return &ValidationError{Name: "target_dir", err: errors.New(`ent: missing required field "History.target_dir"`)}
|
return &ValidationError{Name: "target_dir", err: errors.New(`ent: missing required field "History.target_dir"`)}
|
||||||
}
|
}
|
||||||
if _, ok := hc.mutation.Completed(); !ok {
|
if _, ok := hc.mutation.Status(); !ok {
|
||||||
return &ValidationError{Name: "completed", err: errors.New(`ent: missing required field "History.completed"`)}
|
return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "History.status"`)}
|
||||||
|
}
|
||||||
|
if v, ok := hc.mutation.Status(); ok {
|
||||||
|
if err := history.StatusValidator(v); err != nil {
|
||||||
|
return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "History.status": %w`, err)}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -185,9 +173,9 @@ func (hc *HistoryCreate) createSpec() (*History, *sqlgraph.CreateSpec) {
|
|||||||
_spec.SetField(history.FieldTargetDir, field.TypeString, value)
|
_spec.SetField(history.FieldTargetDir, field.TypeString, value)
|
||||||
_node.TargetDir = value
|
_node.TargetDir = value
|
||||||
}
|
}
|
||||||
if value, ok := hc.mutation.Completed(); ok {
|
if value, ok := hc.mutation.Status(); ok {
|
||||||
_spec.SetField(history.FieldCompleted, field.TypeBool, value)
|
_spec.SetField(history.FieldStatus, field.TypeEnum, value)
|
||||||
_node.Completed = value
|
_node.Status = value
|
||||||
}
|
}
|
||||||
if value, ok := hc.mutation.Saved(); ok {
|
if value, ok := hc.mutation.Saved(); ok {
|
||||||
_spec.SetField(history.FieldSaved, field.TypeString, value)
|
_spec.SetField(history.FieldSaved, field.TypeString, value)
|
||||||
@@ -214,7 +202,6 @@ func (hcb *HistoryCreateBulk) Save(ctx context.Context) ([]*History, error) {
|
|||||||
for i := range hcb.builders {
|
for i := range hcb.builders {
|
||||||
func(i int, root context.Context) {
|
func(i int, root context.Context) {
|
||||||
builder := hcb.builders[i]
|
builder := hcb.builders[i]
|
||||||
builder.defaults()
|
|
||||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||||
mutation, ok := m.(*HistoryMutation)
|
mutation, ok := m.(*HistoryMutation)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -112,16 +112,16 @@ func (hu *HistoryUpdate) SetNillableTargetDir(s *string) *HistoryUpdate {
|
|||||||
return hu
|
return hu
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCompleted sets the "completed" field.
|
// SetStatus sets the "status" field.
|
||||||
func (hu *HistoryUpdate) SetCompleted(b bool) *HistoryUpdate {
|
func (hu *HistoryUpdate) SetStatus(h history.Status) *HistoryUpdate {
|
||||||
hu.mutation.SetCompleted(b)
|
hu.mutation.SetStatus(h)
|
||||||
return hu
|
return hu
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetNillableCompleted sets the "completed" field if the given value is not nil.
|
// SetNillableStatus sets the "status" field if the given value is not nil.
|
||||||
func (hu *HistoryUpdate) SetNillableCompleted(b *bool) *HistoryUpdate {
|
func (hu *HistoryUpdate) SetNillableStatus(h *history.Status) *HistoryUpdate {
|
||||||
if b != nil {
|
if h != nil {
|
||||||
hu.SetCompleted(*b)
|
hu.SetStatus(*h)
|
||||||
}
|
}
|
||||||
return hu
|
return hu
|
||||||
}
|
}
|
||||||
@@ -178,7 +178,20 @@ func (hu *HistoryUpdate) ExecX(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check runs all checks and user-defined validators on the builder.
|
||||||
|
func (hu *HistoryUpdate) check() error {
|
||||||
|
if v, ok := hu.mutation.Status(); ok {
|
||||||
|
if err := history.StatusValidator(v); err != nil {
|
||||||
|
return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "History.status": %w`, err)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (hu *HistoryUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
func (hu *HistoryUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||||
|
if err := hu.check(); err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
_spec := sqlgraph.NewUpdateSpec(history.Table, history.Columns, sqlgraph.NewFieldSpec(history.FieldID, field.TypeInt))
|
_spec := sqlgraph.NewUpdateSpec(history.Table, history.Columns, sqlgraph.NewFieldSpec(history.FieldID, field.TypeInt))
|
||||||
if ps := hu.mutation.predicates; len(ps) > 0 {
|
if ps := hu.mutation.predicates; len(ps) > 0 {
|
||||||
_spec.Predicate = func(selector *sql.Selector) {
|
_spec.Predicate = func(selector *sql.Selector) {
|
||||||
@@ -208,8 +221,8 @@ func (hu *HistoryUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|||||||
if value, ok := hu.mutation.TargetDir(); ok {
|
if value, ok := hu.mutation.TargetDir(); ok {
|
||||||
_spec.SetField(history.FieldTargetDir, field.TypeString, value)
|
_spec.SetField(history.FieldTargetDir, field.TypeString, value)
|
||||||
}
|
}
|
||||||
if value, ok := hu.mutation.Completed(); ok {
|
if value, ok := hu.mutation.Status(); ok {
|
||||||
_spec.SetField(history.FieldCompleted, field.TypeBool, value)
|
_spec.SetField(history.FieldStatus, field.TypeEnum, value)
|
||||||
}
|
}
|
||||||
if value, ok := hu.mutation.Saved(); ok {
|
if value, ok := hu.mutation.Saved(); ok {
|
||||||
_spec.SetField(history.FieldSaved, field.TypeString, value)
|
_spec.SetField(history.FieldSaved, field.TypeString, value)
|
||||||
@@ -321,16 +334,16 @@ func (huo *HistoryUpdateOne) SetNillableTargetDir(s *string) *HistoryUpdateOne {
|
|||||||
return huo
|
return huo
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCompleted sets the "completed" field.
|
// SetStatus sets the "status" field.
|
||||||
func (huo *HistoryUpdateOne) SetCompleted(b bool) *HistoryUpdateOne {
|
func (huo *HistoryUpdateOne) SetStatus(h history.Status) *HistoryUpdateOne {
|
||||||
huo.mutation.SetCompleted(b)
|
huo.mutation.SetStatus(h)
|
||||||
return huo
|
return huo
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetNillableCompleted sets the "completed" field if the given value is not nil.
|
// SetNillableStatus sets the "status" field if the given value is not nil.
|
||||||
func (huo *HistoryUpdateOne) SetNillableCompleted(b *bool) *HistoryUpdateOne {
|
func (huo *HistoryUpdateOne) SetNillableStatus(h *history.Status) *HistoryUpdateOne {
|
||||||
if b != nil {
|
if h != nil {
|
||||||
huo.SetCompleted(*b)
|
huo.SetStatus(*h)
|
||||||
}
|
}
|
||||||
return huo
|
return huo
|
||||||
}
|
}
|
||||||
@@ -400,7 +413,20 @@ func (huo *HistoryUpdateOne) ExecX(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check runs all checks and user-defined validators on the builder.
|
||||||
|
func (huo *HistoryUpdateOne) check() error {
|
||||||
|
if v, ok := huo.mutation.Status(); ok {
|
||||||
|
if err := history.StatusValidator(v); err != nil {
|
||||||
|
return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "History.status": %w`, err)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (huo *HistoryUpdateOne) sqlSave(ctx context.Context) (_node *History, err error) {
|
func (huo *HistoryUpdateOne) sqlSave(ctx context.Context) (_node *History, err error) {
|
||||||
|
if err := huo.check(); err != nil {
|
||||||
|
return _node, err
|
||||||
|
}
|
||||||
_spec := sqlgraph.NewUpdateSpec(history.Table, history.Columns, sqlgraph.NewFieldSpec(history.FieldID, field.TypeInt))
|
_spec := sqlgraph.NewUpdateSpec(history.Table, history.Columns, sqlgraph.NewFieldSpec(history.FieldID, field.TypeInt))
|
||||||
id, ok := huo.mutation.ID()
|
id, ok := huo.mutation.ID()
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -447,8 +473,8 @@ func (huo *HistoryUpdateOne) sqlSave(ctx context.Context) (_node *History, err e
|
|||||||
if value, ok := huo.mutation.TargetDir(); ok {
|
if value, ok := huo.mutation.TargetDir(); ok {
|
||||||
_spec.SetField(history.FieldTargetDir, field.TypeString, value)
|
_spec.SetField(history.FieldTargetDir, field.TypeString, value)
|
||||||
}
|
}
|
||||||
if value, ok := huo.mutation.Completed(); ok {
|
if value, ok := huo.mutation.Status(); ok {
|
||||||
_spec.SetField(history.FieldCompleted, field.TypeBool, value)
|
_spec.SetField(history.FieldStatus, field.TypeEnum, value)
|
||||||
}
|
}
|
||||||
if value, ok := huo.mutation.Saved(); ok {
|
if value, ok := huo.mutation.Saved(); ok {
|
||||||
_spec.SetField(history.FieldSaved, field.TypeString, value)
|
_spec.SetField(history.FieldSaved, field.TypeString, value)
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ var (
|
|||||||
{Name: "source_title", Type: field.TypeString},
|
{Name: "source_title", Type: field.TypeString},
|
||||||
{Name: "date", Type: field.TypeTime},
|
{Name: "date", Type: field.TypeTime},
|
||||||
{Name: "target_dir", Type: field.TypeString},
|
{Name: "target_dir", Type: field.TypeString},
|
||||||
{Name: "completed", Type: field.TypeBool, Default: false},
|
{Name: "status", Type: field.TypeEnum, Enums: []string{"running", "success", "fail", "uploading"}},
|
||||||
{Name: "saved", Type: field.TypeString, Nullable: true},
|
{Name: "saved", Type: field.TypeString, Nullable: true},
|
||||||
}
|
}
|
||||||
// HistoriesTable holds the schema information for the "histories" table.
|
// HistoriesTable holds the schema information for the "histories" table.
|
||||||
|
|||||||
@@ -1732,7 +1732,7 @@ type HistoryMutation struct {
|
|||||||
source_title *string
|
source_title *string
|
||||||
date *time.Time
|
date *time.Time
|
||||||
target_dir *string
|
target_dir *string
|
||||||
completed *bool
|
status *history.Status
|
||||||
saved *string
|
saved *string
|
||||||
clearedFields map[string]struct{}
|
clearedFields map[string]struct{}
|
||||||
done bool
|
done bool
|
||||||
@@ -2058,40 +2058,40 @@ func (m *HistoryMutation) ResetTargetDir() {
|
|||||||
m.target_dir = nil
|
m.target_dir = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCompleted sets the "completed" field.
|
// SetStatus sets the "status" field.
|
||||||
func (m *HistoryMutation) SetCompleted(b bool) {
|
func (m *HistoryMutation) SetStatus(h history.Status) {
|
||||||
m.completed = &b
|
m.status = &h
|
||||||
}
|
}
|
||||||
|
|
||||||
// Completed returns the value of the "completed" field in the mutation.
|
// Status returns the value of the "status" field in the mutation.
|
||||||
func (m *HistoryMutation) Completed() (r bool, exists bool) {
|
func (m *HistoryMutation) Status() (r history.Status, exists bool) {
|
||||||
v := m.completed
|
v := m.status
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return *v, true
|
return *v, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// OldCompleted returns the old "completed" field's value of the History entity.
|
// OldStatus returns the old "status" field's value of the History entity.
|
||||||
// If the History object wasn't provided to the builder, the object is fetched from the database.
|
// If the History object wasn't provided to the builder, the object is fetched from the database.
|
||||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||||
func (m *HistoryMutation) OldCompleted(ctx context.Context) (v bool, err error) {
|
func (m *HistoryMutation) OldStatus(ctx context.Context) (v history.Status, err error) {
|
||||||
if !m.op.Is(OpUpdateOne) {
|
if !m.op.Is(OpUpdateOne) {
|
||||||
return v, errors.New("OldCompleted is only allowed on UpdateOne operations")
|
return v, errors.New("OldStatus is only allowed on UpdateOne operations")
|
||||||
}
|
}
|
||||||
if m.id == nil || m.oldValue == nil {
|
if m.id == nil || m.oldValue == nil {
|
||||||
return v, errors.New("OldCompleted requires an ID field in the mutation")
|
return v, errors.New("OldStatus requires an ID field in the mutation")
|
||||||
}
|
}
|
||||||
oldValue, err := m.oldValue(ctx)
|
oldValue, err := m.oldValue(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return v, fmt.Errorf("querying old value for OldCompleted: %w", err)
|
return v, fmt.Errorf("querying old value for OldStatus: %w", err)
|
||||||
}
|
}
|
||||||
return oldValue.Completed, nil
|
return oldValue.Status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetCompleted resets all changes to the "completed" field.
|
// ResetStatus resets all changes to the "status" field.
|
||||||
func (m *HistoryMutation) ResetCompleted() {
|
func (m *HistoryMutation) ResetStatus() {
|
||||||
m.completed = nil
|
m.status = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSaved sets the "saved" field.
|
// SetSaved sets the "saved" field.
|
||||||
@@ -2193,8 +2193,8 @@ func (m *HistoryMutation) Fields() []string {
|
|||||||
if m.target_dir != nil {
|
if m.target_dir != nil {
|
||||||
fields = append(fields, history.FieldTargetDir)
|
fields = append(fields, history.FieldTargetDir)
|
||||||
}
|
}
|
||||||
if m.completed != nil {
|
if m.status != nil {
|
||||||
fields = append(fields, history.FieldCompleted)
|
fields = append(fields, history.FieldStatus)
|
||||||
}
|
}
|
||||||
if m.saved != nil {
|
if m.saved != nil {
|
||||||
fields = append(fields, history.FieldSaved)
|
fields = append(fields, history.FieldSaved)
|
||||||
@@ -2217,8 +2217,8 @@ func (m *HistoryMutation) Field(name string) (ent.Value, bool) {
|
|||||||
return m.Date()
|
return m.Date()
|
||||||
case history.FieldTargetDir:
|
case history.FieldTargetDir:
|
||||||
return m.TargetDir()
|
return m.TargetDir()
|
||||||
case history.FieldCompleted:
|
case history.FieldStatus:
|
||||||
return m.Completed()
|
return m.Status()
|
||||||
case history.FieldSaved:
|
case history.FieldSaved:
|
||||||
return m.Saved()
|
return m.Saved()
|
||||||
}
|
}
|
||||||
@@ -2240,8 +2240,8 @@ func (m *HistoryMutation) OldField(ctx context.Context, name string) (ent.Value,
|
|||||||
return m.OldDate(ctx)
|
return m.OldDate(ctx)
|
||||||
case history.FieldTargetDir:
|
case history.FieldTargetDir:
|
||||||
return m.OldTargetDir(ctx)
|
return m.OldTargetDir(ctx)
|
||||||
case history.FieldCompleted:
|
case history.FieldStatus:
|
||||||
return m.OldCompleted(ctx)
|
return m.OldStatus(ctx)
|
||||||
case history.FieldSaved:
|
case history.FieldSaved:
|
||||||
return m.OldSaved(ctx)
|
return m.OldSaved(ctx)
|
||||||
}
|
}
|
||||||
@@ -2288,12 +2288,12 @@ func (m *HistoryMutation) SetField(name string, value ent.Value) error {
|
|||||||
}
|
}
|
||||||
m.SetTargetDir(v)
|
m.SetTargetDir(v)
|
||||||
return nil
|
return nil
|
||||||
case history.FieldCompleted:
|
case history.FieldStatus:
|
||||||
v, ok := value.(bool)
|
v, ok := value.(history.Status)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("unexpected type %T for field %s", value, name)
|
return fmt.Errorf("unexpected type %T for field %s", value, name)
|
||||||
}
|
}
|
||||||
m.SetCompleted(v)
|
m.SetStatus(v)
|
||||||
return nil
|
return nil
|
||||||
case history.FieldSaved:
|
case history.FieldSaved:
|
||||||
v, ok := value.(string)
|
v, ok := value.(string)
|
||||||
@@ -2402,8 +2402,8 @@ func (m *HistoryMutation) ResetField(name string) error {
|
|||||||
case history.FieldTargetDir:
|
case history.FieldTargetDir:
|
||||||
m.ResetTargetDir()
|
m.ResetTargetDir()
|
||||||
return nil
|
return nil
|
||||||
case history.FieldCompleted:
|
case history.FieldStatus:
|
||||||
m.ResetCompleted()
|
m.ResetStatus()
|
||||||
return nil
|
return nil
|
||||||
case history.FieldSaved:
|
case history.FieldSaved:
|
||||||
m.ResetSaved()
|
m.ResetSaved()
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package ent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"polaris/ent/downloadclients"
|
"polaris/ent/downloadclients"
|
||||||
"polaris/ent/history"
|
|
||||||
"polaris/ent/indexers"
|
"polaris/ent/indexers"
|
||||||
"polaris/ent/schema"
|
"polaris/ent/schema"
|
||||||
"polaris/ent/series"
|
"polaris/ent/series"
|
||||||
@@ -46,12 +45,6 @@ func init() {
|
|||||||
downloadclientsDescTags := downloadclientsFields[10].Descriptor()
|
downloadclientsDescTags := downloadclientsFields[10].Descriptor()
|
||||||
// downloadclients.DefaultTags holds the default value on creation for the tags field.
|
// downloadclients.DefaultTags holds the default value on creation for the tags field.
|
||||||
downloadclients.DefaultTags = downloadclientsDescTags.Default.(string)
|
downloadclients.DefaultTags = downloadclientsDescTags.Default.(string)
|
||||||
historyFields := schema.History{}.Fields()
|
|
||||||
_ = historyFields
|
|
||||||
// historyDescCompleted is the schema descriptor for completed field.
|
|
||||||
historyDescCompleted := historyFields[5].Descriptor()
|
|
||||||
// history.DefaultCompleted holds the default value on creation for the completed field.
|
|
||||||
history.DefaultCompleted = historyDescCompleted.Default.(bool)
|
|
||||||
indexersFields := schema.Indexers{}.Fields()
|
indexersFields := schema.Indexers{}.Fields()
|
||||||
_ = indexersFields
|
_ = indexersFields
|
||||||
// indexersDescEnableRss is the schema descriptor for enable_rss field.
|
// indexersDescEnableRss is the schema descriptor for enable_rss field.
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func (History) Fields() []ent.Field {
|
|||||||
field.String("source_title"),
|
field.String("source_title"),
|
||||||
field.Time("date"),
|
field.Time("date"),
|
||||||
field.String("target_dir"),
|
field.String("target_dir"),
|
||||||
field.Bool("completed").Default(false).StructTag("json:\"completed\""),
|
field.Enum("status").Values("running", "success", "fail", "uploading"),
|
||||||
field.String("saved").Optional(),
|
field.String("saved").Optional(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
type Activity struct {
|
type Activity struct {
|
||||||
*ent.History
|
*ent.History
|
||||||
InBackgroud bool `json:"in_backgroud"`
|
|
||||||
Progress int `json:"progress"`
|
Progress int `json:"progress"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,12 +25,6 @@ func (s *Server) GetAllActivities(c *gin.Context) (interface{}, error) {
|
|||||||
if h.ID == id && task.Exists() {
|
if h.ID == id && task.Exists() {
|
||||||
a.Progress = task.Progress()
|
a.Progress = task.Progress()
|
||||||
}
|
}
|
||||||
if h.ID == id && task.Processing {
|
|
||||||
a.InBackgroud = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if a.Completed {
|
|
||||||
a.Progress = 100
|
|
||||||
}
|
}
|
||||||
activities = append(activities, a)
|
activities = append(activities, a)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"polaris/db"
|
"polaris/db"
|
||||||
"polaris/ent"
|
"polaris/ent"
|
||||||
|
"polaris/ent/history"
|
||||||
"polaris/log"
|
"polaris/log"
|
||||||
"polaris/pkg/torznab"
|
"polaris/pkg/torznab"
|
||||||
"polaris/pkg/transmission"
|
"polaris/pkg/transmission"
|
||||||
@@ -119,7 +120,7 @@ func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string
|
|||||||
EpisodeID: ep.ID,
|
EpisodeID: ep.ID,
|
||||||
SourceTitle: r1.Name,
|
SourceTitle: r1.Name,
|
||||||
TargetDir: dir,
|
TargetDir: dir,
|
||||||
Completed: false,
|
Status: history.StatusRunning,
|
||||||
Saved: torrent.Save(),
|
Saved: torrent.Save(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package server
|
|||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"polaris/ent"
|
"polaris/ent"
|
||||||
|
"polaris/ent/history"
|
||||||
storage1 "polaris/ent/storage"
|
storage1 "polaris/ent/storage"
|
||||||
"polaris/log"
|
"polaris/log"
|
||||||
"polaris/pkg"
|
"polaris/pkg"
|
||||||
@@ -34,14 +35,9 @@ func (s *Server) checkTasks() {
|
|||||||
log.Infof("task no longer exists: %v", id)
|
log.Infof("task no longer exists: %v", id)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if t.Processing {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("task (%s) percentage done: %d%%", t.Name(), t.Progress())
|
log.Infof("task (%s) percentage done: %d%%", t.Name(), t.Progress())
|
||||||
if t.Progress() == 100 {
|
if t.Progress() == 100 {
|
||||||
log.Infof("task is done: %v", t.Name())
|
log.Infof("task is done: %v", t.Name())
|
||||||
t.Processing = true
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := s.moveCompletedTask(id); err != nil {
|
if err := s.moveCompletedTask(id); err != nil {
|
||||||
log.Infof("post tasks for id %v fail: %v", id, err)
|
log.Infof("post tasks for id %v fail: %v", id, err)
|
||||||
@@ -51,9 +47,22 @@ func (s *Server) checkTasks() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) moveCompletedTask(id int) error {
|
func (s *Server) moveCompletedTask(id int) (err error) {
|
||||||
torrent := s.tasks[id]
|
torrent := s.tasks[id]
|
||||||
r := s.db.GetHistory(id)
|
r := s.db.GetHistory(id)
|
||||||
|
if r.Status == history.StatusUploading {
|
||||||
|
log.Infof("task %d is laready uploading, skip", id)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
s.db.SetHistoryStatus(r.ID, history.StatusUploading)
|
||||||
|
|
||||||
|
defer func () {
|
||||||
|
if err != nil {
|
||||||
|
s.db.SetHistoryStatus(r.ID, history.StatusFail)
|
||||||
|
} else {
|
||||||
|
s.db.SetHistoryStatus(r.ID, history.StatusSuccess)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
series := s.db.GetSeriesDetails(r.SeriesID)
|
series := s.db.GetSeriesDetails(r.SeriesID)
|
||||||
if series == nil {
|
if series == nil {
|
||||||
@@ -86,7 +95,7 @@ func (s *Server) moveCompletedTask(id int) error {
|
|||||||
log.Infof("move downloaded files to target dir success, file: %v, target dir: %v", torrent.Name(), r.TargetDir)
|
log.Infof("move downloaded files to target dir success, file: %v, target dir: %v", torrent.Name(), r.TargetDir)
|
||||||
torrent.Remove()
|
torrent.Remove()
|
||||||
delete(s.tasks, r.ID)
|
delete(s.tasks, r.ID)
|
||||||
s.db.SetHistoryComplete(r.ID)
|
s.db.SetHistoryStatus(r.ID, history.StatusSuccess)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,6 +177,6 @@ func (s *Server) checkFileExists(series *ent.Series) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
Processing bool
|
//Processing bool
|
||||||
pkg.Torrent
|
pkg.Torrent
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,21 +30,25 @@ class ActivityPage extends ConsumerWidget {
|
|||||||
DataCell(Text("${activity.sourceTitle}")),
|
DataCell(Text("${activity.sourceTitle}")),
|
||||||
DataCell(Text("${activity.date!.toLocal()}")),
|
DataCell(Text("${activity.date!.toLocal()}")),
|
||||||
DataCell(() {
|
DataCell(() {
|
||||||
if (activity.inBackgroud == true) {
|
if (activity.status == "uploading") {
|
||||||
return const MyProgressIndicator(
|
return const MyProgressIndicator(
|
||||||
size: 20,
|
size: 20,
|
||||||
);
|
);
|
||||||
}
|
}else if (activity.status == "fail") {
|
||||||
|
|
||||||
if (activity.completed != true && activity.progress == 0) {
|
|
||||||
return const Icon(
|
return const Icon(
|
||||||
Icons.close,
|
Icons.close,
|
||||||
color: Colors.red,
|
color: Colors.red,
|
||||||
);
|
);
|
||||||
|
} else if (activity.status == "success") {
|
||||||
|
return const Icon(
|
||||||
|
Icons.check,
|
||||||
|
color: Colors.green,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double p = activity.progress == null ? 0:activity.progress!.toDouble() / 100;
|
||||||
return MyProgressIndicator(
|
return MyProgressIndicator(
|
||||||
value: activity.progress!.toDouble() / 100,
|
value: p,
|
||||||
size: 20,
|
size: 20,
|
||||||
);
|
);
|
||||||
}()),
|
}()),
|
||||||
|
|||||||
@@ -43,9 +43,8 @@ class Activity {
|
|||||||
required this.sourceTitle,
|
required this.sourceTitle,
|
||||||
required this.date,
|
required this.date,
|
||||||
required this.targetDir,
|
required this.targetDir,
|
||||||
required this.completed,
|
required this.status,
|
||||||
required this.saved,
|
required this.saved,
|
||||||
required this.inBackgroud,
|
|
||||||
required this.progress
|
required this.progress
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -55,9 +54,8 @@ class Activity {
|
|||||||
final String? sourceTitle;
|
final String? sourceTitle;
|
||||||
final DateTime? date;
|
final DateTime? date;
|
||||||
final String? targetDir;
|
final String? targetDir;
|
||||||
final bool? completed;
|
final String? status;
|
||||||
final String? saved;
|
final String? saved;
|
||||||
final bool? inBackgroud;
|
|
||||||
final int? progress;
|
final int? progress;
|
||||||
|
|
||||||
factory Activity.fromJson(Map<String, dynamic> json) {
|
factory Activity.fromJson(Map<String, dynamic> json) {
|
||||||
@@ -68,9 +66,8 @@ class Activity {
|
|||||||
sourceTitle: json["source_title"],
|
sourceTitle: json["source_title"],
|
||||||
date: DateTime.tryParse(json["date"] ?? ""),
|
date: DateTime.tryParse(json["date"] ?? ""),
|
||||||
targetDir: json["target_dir"],
|
targetDir: json["target_dir"],
|
||||||
completed: json["completed"],
|
status: json["status"],
|
||||||
saved: json["saved"],
|
saved: json["saved"],
|
||||||
inBackgroud: json["in_backgroud"],
|
|
||||||
progress: json["progress"]
|
progress: json["progress"]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user