diff --git a/db/db.go b/db/db.go index 34c6321..2a183eb 100644 --- a/db/db.go +++ b/db/db.go @@ -247,19 +247,23 @@ type TorznabSetting struct { ApiKey string `json:"api_key"` } -func (c *Client) SaveTorznabInfo(name string, setting TorznabSetting) error { - data, err := json.Marshal(setting) - if err != nil { - return errors.Wrap(err, "marshal json") + +func (c *Client) SaveIndexer(in *ent.Indexers) error { + + if in.ID != 0 { + //update setting + return c.ent.Indexers.Update().Where(indexers.ID(in.ID)).SetName(in.Name).SetImplementation(in.Implementation). + SetPriority(in.Priority).SetSettings(in.Settings).SetSeedRatio(in.SeedRatio).SetDisabled(in.Disabled).Exec(context.Background()) } - count := c.ent.Indexers.Query().Where(indexers.Name(name)).CountX(context.TODO()) + //create new one + count := c.ent.Indexers.Query().Where(indexers.Name(in.Name)).CountX(context.TODO()) if count > 0 { - c.ent.Indexers.Update().Where(indexers.Name(name)).SetSettings(string(data)).Save(context.TODO()) - return err + return fmt.Errorf("name already esxits: %v", in.Name) } - _, err = c.ent.Indexers.Create(). - SetName(name).SetImplementation(IndexerTorznabImpl).SetPriority(1).SetSettings(string(data)).Save(context.TODO()) + _, err := c.ent.Indexers.Create(). + SetName(in.Name).SetImplementation(in.Implementation).SetPriority(in.Priority).SetSettings(in.Settings).SetSeedRatio(in.SeedRatio). + SetDisabled(in.Disabled).Save(context.TODO()) if err != nil { return errors.Wrap(err, "save db") } @@ -272,8 +276,7 @@ func (c *Client) DeleteTorznab(id int) { } type TorznabInfo struct { - ID int `json:"id"` - Name string `json:"name"` + *ent.Indexers TorznabSetting } @@ -289,8 +292,7 @@ func (c *Client) GetAllTorznabInfo() []*TorznabInfo { continue } l = append(l, &TorznabInfo{ - ID: r.ID, - Name: r.Name, + Indexers: r, TorznabSetting: ss, }) } diff --git a/ent/indexers.go b/ent/indexers.go index 28b985e..9590606 100644 --- a/ent/indexers.go +++ b/ent/indexers.go @@ -25,7 +25,11 @@ type Indexers struct { // EnableRss holds the value of the "enable_rss" field. EnableRss bool `json:"enable_rss,omitempty"` // Priority holds the value of the "priority" field. - Priority int `json:"priority,omitempty"` + Priority int `json:"priority,omitempty"` + // minimal seed ratio requied, before removing torrent + SeedRatio float32 `json:"seed_ratio,omitempty"` + // Disabled holds the value of the "disabled" field. + Disabled bool `json:"disabled,omitempty"` selectValues sql.SelectValues } @@ -34,8 +38,10 @@ func (*Indexers) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case indexers.FieldEnableRss: + case indexers.FieldEnableRss, indexers.FieldDisabled: values[i] = new(sql.NullBool) + case indexers.FieldSeedRatio: + values[i] = new(sql.NullFloat64) case indexers.FieldID, indexers.FieldPriority: values[i] = new(sql.NullInt64) case indexers.FieldName, indexers.FieldImplementation, indexers.FieldSettings: @@ -91,6 +97,18 @@ func (i *Indexers) assignValues(columns []string, values []any) error { } else if value.Valid { i.Priority = int(value.Int64) } + case indexers.FieldSeedRatio: + if value, ok := values[j].(*sql.NullFloat64); !ok { + return fmt.Errorf("unexpected type %T for field seed_ratio", values[j]) + } else if value.Valid { + i.SeedRatio = float32(value.Float64) + } + case indexers.FieldDisabled: + if value, ok := values[j].(*sql.NullBool); !ok { + return fmt.Errorf("unexpected type %T for field disabled", values[j]) + } else if value.Valid { + i.Disabled = value.Bool + } default: i.selectValues.Set(columns[j], values[j]) } @@ -141,6 +159,12 @@ func (i *Indexers) String() string { builder.WriteString(", ") builder.WriteString("priority=") builder.WriteString(fmt.Sprintf("%v", i.Priority)) + builder.WriteString(", ") + builder.WriteString("seed_ratio=") + builder.WriteString(fmt.Sprintf("%v", i.SeedRatio)) + builder.WriteString(", ") + builder.WriteString("disabled=") + builder.WriteString(fmt.Sprintf("%v", i.Disabled)) builder.WriteByte(')') return builder.String() } diff --git a/ent/indexers/indexers.go b/ent/indexers/indexers.go index 9794efb..e1d24c3 100644 --- a/ent/indexers/indexers.go +++ b/ent/indexers/indexers.go @@ -21,6 +21,10 @@ const ( FieldEnableRss = "enable_rss" // FieldPriority holds the string denoting the priority field in the database. FieldPriority = "priority" + // FieldSeedRatio holds the string denoting the seed_ratio field in the database. + FieldSeedRatio = "seed_ratio" + // FieldDisabled holds the string denoting the disabled field in the database. + FieldDisabled = "disabled" // Table holds the table name of the indexers in the database. Table = "indexers" ) @@ -33,6 +37,8 @@ var Columns = []string{ FieldSettings, FieldEnableRss, FieldPriority, + FieldSeedRatio, + FieldDisabled, } // ValidColumn reports if the column name is valid (part of the table columns). @@ -48,6 +54,12 @@ func ValidColumn(column string) bool { var ( // DefaultEnableRss holds the default value on creation for the "enable_rss" field. DefaultEnableRss bool + // DefaultPriority holds the default value on creation for the "priority" field. + DefaultPriority int + // DefaultSeedRatio holds the default value on creation for the "seed_ratio" field. + DefaultSeedRatio float32 + // DefaultDisabled holds the default value on creation for the "disabled" field. + DefaultDisabled bool ) // OrderOption defines the ordering options for the Indexers queries. @@ -82,3 +94,13 @@ func ByEnableRss(opts ...sql.OrderTermOption) OrderOption { func ByPriority(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldPriority, opts...).ToFunc() } + +// BySeedRatio orders the results by the seed_ratio field. +func BySeedRatio(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldSeedRatio, opts...).ToFunc() +} + +// ByDisabled orders the results by the disabled field. +func ByDisabled(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldDisabled, opts...).ToFunc() +} diff --git a/ent/indexers/where.go b/ent/indexers/where.go index 9e4f189..732d2d4 100644 --- a/ent/indexers/where.go +++ b/ent/indexers/where.go @@ -78,6 +78,16 @@ func Priority(v int) predicate.Indexers { return predicate.Indexers(sql.FieldEQ(FieldPriority, v)) } +// SeedRatio applies equality check predicate on the "seed_ratio" field. It's identical to SeedRatioEQ. +func SeedRatio(v float32) predicate.Indexers { + return predicate.Indexers(sql.FieldEQ(FieldSeedRatio, v)) +} + +// Disabled applies equality check predicate on the "disabled" field. It's identical to DisabledEQ. +func Disabled(v bool) predicate.Indexers { + return predicate.Indexers(sql.FieldEQ(FieldDisabled, v)) +} + // NameEQ applies the EQ predicate on the "name" field. func NameEQ(v string) predicate.Indexers { return predicate.Indexers(sql.FieldEQ(FieldName, v)) @@ -323,6 +333,76 @@ func PriorityLTE(v int) predicate.Indexers { return predicate.Indexers(sql.FieldLTE(FieldPriority, v)) } +// SeedRatioEQ applies the EQ predicate on the "seed_ratio" field. +func SeedRatioEQ(v float32) predicate.Indexers { + return predicate.Indexers(sql.FieldEQ(FieldSeedRatio, v)) +} + +// SeedRatioNEQ applies the NEQ predicate on the "seed_ratio" field. +func SeedRatioNEQ(v float32) predicate.Indexers { + return predicate.Indexers(sql.FieldNEQ(FieldSeedRatio, v)) +} + +// SeedRatioIn applies the In predicate on the "seed_ratio" field. +func SeedRatioIn(vs ...float32) predicate.Indexers { + return predicate.Indexers(sql.FieldIn(FieldSeedRatio, vs...)) +} + +// SeedRatioNotIn applies the NotIn predicate on the "seed_ratio" field. +func SeedRatioNotIn(vs ...float32) predicate.Indexers { + return predicate.Indexers(sql.FieldNotIn(FieldSeedRatio, vs...)) +} + +// SeedRatioGT applies the GT predicate on the "seed_ratio" field. +func SeedRatioGT(v float32) predicate.Indexers { + return predicate.Indexers(sql.FieldGT(FieldSeedRatio, v)) +} + +// SeedRatioGTE applies the GTE predicate on the "seed_ratio" field. +func SeedRatioGTE(v float32) predicate.Indexers { + return predicate.Indexers(sql.FieldGTE(FieldSeedRatio, v)) +} + +// SeedRatioLT applies the LT predicate on the "seed_ratio" field. +func SeedRatioLT(v float32) predicate.Indexers { + return predicate.Indexers(sql.FieldLT(FieldSeedRatio, v)) +} + +// SeedRatioLTE applies the LTE predicate on the "seed_ratio" field. +func SeedRatioLTE(v float32) predicate.Indexers { + return predicate.Indexers(sql.FieldLTE(FieldSeedRatio, v)) +} + +// SeedRatioIsNil applies the IsNil predicate on the "seed_ratio" field. +func SeedRatioIsNil() predicate.Indexers { + return predicate.Indexers(sql.FieldIsNull(FieldSeedRatio)) +} + +// SeedRatioNotNil applies the NotNil predicate on the "seed_ratio" field. +func SeedRatioNotNil() predicate.Indexers { + return predicate.Indexers(sql.FieldNotNull(FieldSeedRatio)) +} + +// DisabledEQ applies the EQ predicate on the "disabled" field. +func DisabledEQ(v bool) predicate.Indexers { + return predicate.Indexers(sql.FieldEQ(FieldDisabled, v)) +} + +// DisabledNEQ applies the NEQ predicate on the "disabled" field. +func DisabledNEQ(v bool) predicate.Indexers { + return predicate.Indexers(sql.FieldNEQ(FieldDisabled, v)) +} + +// DisabledIsNil applies the IsNil predicate on the "disabled" field. +func DisabledIsNil() predicate.Indexers { + return predicate.Indexers(sql.FieldIsNull(FieldDisabled)) +} + +// DisabledNotNil applies the NotNil predicate on the "disabled" field. +func DisabledNotNil() predicate.Indexers { + return predicate.Indexers(sql.FieldNotNull(FieldDisabled)) +} + // And groups predicates with the AND operator between them. func And(predicates ...predicate.Indexers) predicate.Indexers { return predicate.Indexers(sql.AndPredicates(predicates...)) diff --git a/ent/indexers_create.go b/ent/indexers_create.go index 6b99f8b..f355e21 100644 --- a/ent/indexers_create.go +++ b/ent/indexers_create.go @@ -57,6 +57,42 @@ func (ic *IndexersCreate) SetPriority(i int) *IndexersCreate { return ic } +// SetNillablePriority sets the "priority" field if the given value is not nil. +func (ic *IndexersCreate) SetNillablePriority(i *int) *IndexersCreate { + if i != nil { + ic.SetPriority(*i) + } + return ic +} + +// SetSeedRatio sets the "seed_ratio" field. +func (ic *IndexersCreate) SetSeedRatio(f float32) *IndexersCreate { + ic.mutation.SetSeedRatio(f) + return ic +} + +// SetNillableSeedRatio sets the "seed_ratio" field if the given value is not nil. +func (ic *IndexersCreate) SetNillableSeedRatio(f *float32) *IndexersCreate { + if f != nil { + ic.SetSeedRatio(*f) + } + return ic +} + +// SetDisabled sets the "disabled" field. +func (ic *IndexersCreate) SetDisabled(b bool) *IndexersCreate { + ic.mutation.SetDisabled(b) + return ic +} + +// SetNillableDisabled sets the "disabled" field if the given value is not nil. +func (ic *IndexersCreate) SetNillableDisabled(b *bool) *IndexersCreate { + if b != nil { + ic.SetDisabled(*b) + } + return ic +} + // Mutation returns the IndexersMutation object of the builder. func (ic *IndexersCreate) Mutation() *IndexersMutation { return ic.mutation @@ -96,6 +132,18 @@ func (ic *IndexersCreate) defaults() { v := indexers.DefaultEnableRss ic.mutation.SetEnableRss(v) } + if _, ok := ic.mutation.Priority(); !ok { + v := indexers.DefaultPriority + ic.mutation.SetPriority(v) + } + if _, ok := ic.mutation.SeedRatio(); !ok { + v := indexers.DefaultSeedRatio + ic.mutation.SetSeedRatio(v) + } + if _, ok := ic.mutation.Disabled(); !ok { + v := indexers.DefaultDisabled + ic.mutation.SetDisabled(v) + } } // check runs all checks and user-defined validators on the builder. @@ -161,6 +209,14 @@ func (ic *IndexersCreate) createSpec() (*Indexers, *sqlgraph.CreateSpec) { _spec.SetField(indexers.FieldPriority, field.TypeInt, value) _node.Priority = value } + if value, ok := ic.mutation.SeedRatio(); ok { + _spec.SetField(indexers.FieldSeedRatio, field.TypeFloat32, value) + _node.SeedRatio = value + } + if value, ok := ic.mutation.Disabled(); ok { + _spec.SetField(indexers.FieldDisabled, field.TypeBool, value) + _node.Disabled = value + } return _node, _spec } diff --git a/ent/indexers_update.go b/ent/indexers_update.go index c0dbb34..154ae4e 100644 --- a/ent/indexers_update.go +++ b/ent/indexers_update.go @@ -104,6 +104,53 @@ func (iu *IndexersUpdate) AddPriority(i int) *IndexersUpdate { return iu } +// SetSeedRatio sets the "seed_ratio" field. +func (iu *IndexersUpdate) SetSeedRatio(f float32) *IndexersUpdate { + iu.mutation.ResetSeedRatio() + iu.mutation.SetSeedRatio(f) + return iu +} + +// SetNillableSeedRatio sets the "seed_ratio" field if the given value is not nil. +func (iu *IndexersUpdate) SetNillableSeedRatio(f *float32) *IndexersUpdate { + if f != nil { + iu.SetSeedRatio(*f) + } + return iu +} + +// AddSeedRatio adds f to the "seed_ratio" field. +func (iu *IndexersUpdate) AddSeedRatio(f float32) *IndexersUpdate { + iu.mutation.AddSeedRatio(f) + return iu +} + +// ClearSeedRatio clears the value of the "seed_ratio" field. +func (iu *IndexersUpdate) ClearSeedRatio() *IndexersUpdate { + iu.mutation.ClearSeedRatio() + return iu +} + +// SetDisabled sets the "disabled" field. +func (iu *IndexersUpdate) SetDisabled(b bool) *IndexersUpdate { + iu.mutation.SetDisabled(b) + return iu +} + +// SetNillableDisabled sets the "disabled" field if the given value is not nil. +func (iu *IndexersUpdate) SetNillableDisabled(b *bool) *IndexersUpdate { + if b != nil { + iu.SetDisabled(*b) + } + return iu +} + +// ClearDisabled clears the value of the "disabled" field. +func (iu *IndexersUpdate) ClearDisabled() *IndexersUpdate { + iu.mutation.ClearDisabled() + return iu +} + // Mutation returns the IndexersMutation object of the builder. func (iu *IndexersUpdate) Mutation() *IndexersMutation { return iu.mutation @@ -163,6 +210,21 @@ func (iu *IndexersUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := iu.mutation.AddedPriority(); ok { _spec.AddField(indexers.FieldPriority, field.TypeInt, value) } + if value, ok := iu.mutation.SeedRatio(); ok { + _spec.SetField(indexers.FieldSeedRatio, field.TypeFloat32, value) + } + if value, ok := iu.mutation.AddedSeedRatio(); ok { + _spec.AddField(indexers.FieldSeedRatio, field.TypeFloat32, value) + } + if iu.mutation.SeedRatioCleared() { + _spec.ClearField(indexers.FieldSeedRatio, field.TypeFloat32) + } + if value, ok := iu.mutation.Disabled(); ok { + _spec.SetField(indexers.FieldDisabled, field.TypeBool, value) + } + if iu.mutation.DisabledCleared() { + _spec.ClearField(indexers.FieldDisabled, field.TypeBool) + } if n, err = sqlgraph.UpdateNodes(ctx, iu.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{indexers.Label} @@ -260,6 +322,53 @@ func (iuo *IndexersUpdateOne) AddPriority(i int) *IndexersUpdateOne { return iuo } +// SetSeedRatio sets the "seed_ratio" field. +func (iuo *IndexersUpdateOne) SetSeedRatio(f float32) *IndexersUpdateOne { + iuo.mutation.ResetSeedRatio() + iuo.mutation.SetSeedRatio(f) + return iuo +} + +// SetNillableSeedRatio sets the "seed_ratio" field if the given value is not nil. +func (iuo *IndexersUpdateOne) SetNillableSeedRatio(f *float32) *IndexersUpdateOne { + if f != nil { + iuo.SetSeedRatio(*f) + } + return iuo +} + +// AddSeedRatio adds f to the "seed_ratio" field. +func (iuo *IndexersUpdateOne) AddSeedRatio(f float32) *IndexersUpdateOne { + iuo.mutation.AddSeedRatio(f) + return iuo +} + +// ClearSeedRatio clears the value of the "seed_ratio" field. +func (iuo *IndexersUpdateOne) ClearSeedRatio() *IndexersUpdateOne { + iuo.mutation.ClearSeedRatio() + return iuo +} + +// SetDisabled sets the "disabled" field. +func (iuo *IndexersUpdateOne) SetDisabled(b bool) *IndexersUpdateOne { + iuo.mutation.SetDisabled(b) + return iuo +} + +// SetNillableDisabled sets the "disabled" field if the given value is not nil. +func (iuo *IndexersUpdateOne) SetNillableDisabled(b *bool) *IndexersUpdateOne { + if b != nil { + iuo.SetDisabled(*b) + } + return iuo +} + +// ClearDisabled clears the value of the "disabled" field. +func (iuo *IndexersUpdateOne) ClearDisabled() *IndexersUpdateOne { + iuo.mutation.ClearDisabled() + return iuo +} + // Mutation returns the IndexersMutation object of the builder. func (iuo *IndexersUpdateOne) Mutation() *IndexersMutation { return iuo.mutation @@ -349,6 +458,21 @@ func (iuo *IndexersUpdateOne) sqlSave(ctx context.Context) (_node *Indexers, err if value, ok := iuo.mutation.AddedPriority(); ok { _spec.AddField(indexers.FieldPriority, field.TypeInt, value) } + if value, ok := iuo.mutation.SeedRatio(); ok { + _spec.SetField(indexers.FieldSeedRatio, field.TypeFloat32, value) + } + if value, ok := iuo.mutation.AddedSeedRatio(); ok { + _spec.AddField(indexers.FieldSeedRatio, field.TypeFloat32, value) + } + if iuo.mutation.SeedRatioCleared() { + _spec.ClearField(indexers.FieldSeedRatio, field.TypeFloat32) + } + if value, ok := iuo.mutation.Disabled(); ok { + _spec.SetField(indexers.FieldDisabled, field.TypeBool, value) + } + if iuo.mutation.DisabledCleared() { + _spec.ClearField(indexers.FieldDisabled, field.TypeBool) + } _node = &Indexers{config: iuo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues diff --git a/ent/migrate/schema.go b/ent/migrate/schema.go index bc97693..4857628 100644 --- a/ent/migrate/schema.go +++ b/ent/migrate/schema.go @@ -80,7 +80,9 @@ var ( {Name: "implementation", Type: field.TypeString}, {Name: "settings", Type: field.TypeString}, {Name: "enable_rss", Type: field.TypeBool, Default: true}, - {Name: "priority", Type: field.TypeInt}, + {Name: "priority", Type: field.TypeInt, Default: 50}, + {Name: "seed_ratio", Type: field.TypeFloat32, Nullable: true, Default: 0}, + {Name: "disabled", Type: field.TypeBool, Nullable: true, Default: false}, } // IndexersTable holds the schema information for the "indexers" table. IndexersTable = &schema.Table{ diff --git a/ent/mutation.go b/ent/mutation.go index e1182ea..e7f31c6 100644 --- a/ent/mutation.go +++ b/ent/mutation.go @@ -2669,6 +2669,9 @@ type IndexersMutation struct { enable_rss *bool priority *int addpriority *int + seed_ratio *float32 + addseed_ratio *float32 + disabled *bool clearedFields map[string]struct{} done bool oldValue func(context.Context) (*Indexers, error) @@ -2973,6 +2976,125 @@ func (m *IndexersMutation) ResetPriority() { m.addpriority = nil } +// SetSeedRatio sets the "seed_ratio" field. +func (m *IndexersMutation) SetSeedRatio(f float32) { + m.seed_ratio = &f + m.addseed_ratio = nil +} + +// SeedRatio returns the value of the "seed_ratio" field in the mutation. +func (m *IndexersMutation) SeedRatio() (r float32, exists bool) { + v := m.seed_ratio + if v == nil { + return + } + return *v, true +} + +// OldSeedRatio returns the old "seed_ratio" field's value of the Indexers entity. +// If the Indexers 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. +func (m *IndexersMutation) OldSeedRatio(ctx context.Context) (v float32, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldSeedRatio is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldSeedRatio requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldSeedRatio: %w", err) + } + return oldValue.SeedRatio, nil +} + +// AddSeedRatio adds f to the "seed_ratio" field. +func (m *IndexersMutation) AddSeedRatio(f float32) { + if m.addseed_ratio != nil { + *m.addseed_ratio += f + } else { + m.addseed_ratio = &f + } +} + +// AddedSeedRatio returns the value that was added to the "seed_ratio" field in this mutation. +func (m *IndexersMutation) AddedSeedRatio() (r float32, exists bool) { + v := m.addseed_ratio + if v == nil { + return + } + return *v, true +} + +// ClearSeedRatio clears the value of the "seed_ratio" field. +func (m *IndexersMutation) ClearSeedRatio() { + m.seed_ratio = nil + m.addseed_ratio = nil + m.clearedFields[indexers.FieldSeedRatio] = struct{}{} +} + +// SeedRatioCleared returns if the "seed_ratio" field was cleared in this mutation. +func (m *IndexersMutation) SeedRatioCleared() bool { + _, ok := m.clearedFields[indexers.FieldSeedRatio] + return ok +} + +// ResetSeedRatio resets all changes to the "seed_ratio" field. +func (m *IndexersMutation) ResetSeedRatio() { + m.seed_ratio = nil + m.addseed_ratio = nil + delete(m.clearedFields, indexers.FieldSeedRatio) +} + +// SetDisabled sets the "disabled" field. +func (m *IndexersMutation) SetDisabled(b bool) { + m.disabled = &b +} + +// Disabled returns the value of the "disabled" field in the mutation. +func (m *IndexersMutation) Disabled() (r bool, exists bool) { + v := m.disabled + if v == nil { + return + } + return *v, true +} + +// OldDisabled returns the old "disabled" field's value of the Indexers entity. +// If the Indexers 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. +func (m *IndexersMutation) OldDisabled(ctx context.Context) (v bool, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldDisabled is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldDisabled requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldDisabled: %w", err) + } + return oldValue.Disabled, nil +} + +// ClearDisabled clears the value of the "disabled" field. +func (m *IndexersMutation) ClearDisabled() { + m.disabled = nil + m.clearedFields[indexers.FieldDisabled] = struct{}{} +} + +// DisabledCleared returns if the "disabled" field was cleared in this mutation. +func (m *IndexersMutation) DisabledCleared() bool { + _, ok := m.clearedFields[indexers.FieldDisabled] + return ok +} + +// ResetDisabled resets all changes to the "disabled" field. +func (m *IndexersMutation) ResetDisabled() { + m.disabled = nil + delete(m.clearedFields, indexers.FieldDisabled) +} + // Where appends a list predicates to the IndexersMutation builder. func (m *IndexersMutation) Where(ps ...predicate.Indexers) { m.predicates = append(m.predicates, ps...) @@ -3007,7 +3129,7 @@ func (m *IndexersMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *IndexersMutation) Fields() []string { - fields := make([]string, 0, 5) + fields := make([]string, 0, 7) if m.name != nil { fields = append(fields, indexers.FieldName) } @@ -3023,6 +3145,12 @@ func (m *IndexersMutation) Fields() []string { if m.priority != nil { fields = append(fields, indexers.FieldPriority) } + if m.seed_ratio != nil { + fields = append(fields, indexers.FieldSeedRatio) + } + if m.disabled != nil { + fields = append(fields, indexers.FieldDisabled) + } return fields } @@ -3041,6 +3169,10 @@ func (m *IndexersMutation) Field(name string) (ent.Value, bool) { return m.EnableRss() case indexers.FieldPriority: return m.Priority() + case indexers.FieldSeedRatio: + return m.SeedRatio() + case indexers.FieldDisabled: + return m.Disabled() } return nil, false } @@ -3060,6 +3192,10 @@ func (m *IndexersMutation) OldField(ctx context.Context, name string) (ent.Value return m.OldEnableRss(ctx) case indexers.FieldPriority: return m.OldPriority(ctx) + case indexers.FieldSeedRatio: + return m.OldSeedRatio(ctx) + case indexers.FieldDisabled: + return m.OldDisabled(ctx) } return nil, fmt.Errorf("unknown Indexers field %s", name) } @@ -3104,6 +3240,20 @@ func (m *IndexersMutation) SetField(name string, value ent.Value) error { } m.SetPriority(v) return nil + case indexers.FieldSeedRatio: + v, ok := value.(float32) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetSeedRatio(v) + return nil + case indexers.FieldDisabled: + v, ok := value.(bool) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetDisabled(v) + return nil } return fmt.Errorf("unknown Indexers field %s", name) } @@ -3115,6 +3265,9 @@ func (m *IndexersMutation) AddedFields() []string { if m.addpriority != nil { fields = append(fields, indexers.FieldPriority) } + if m.addseed_ratio != nil { + fields = append(fields, indexers.FieldSeedRatio) + } return fields } @@ -3125,6 +3278,8 @@ func (m *IndexersMutation) AddedField(name string) (ent.Value, bool) { switch name { case indexers.FieldPriority: return m.AddedPriority() + case indexers.FieldSeedRatio: + return m.AddedSeedRatio() } return nil, false } @@ -3141,6 +3296,13 @@ func (m *IndexersMutation) AddField(name string, value ent.Value) error { } m.AddPriority(v) return nil + case indexers.FieldSeedRatio: + v, ok := value.(float32) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddSeedRatio(v) + return nil } return fmt.Errorf("unknown Indexers numeric field %s", name) } @@ -3148,7 +3310,14 @@ func (m *IndexersMutation) AddField(name string, value ent.Value) error { // ClearedFields returns all nullable fields that were cleared during this // mutation. func (m *IndexersMutation) ClearedFields() []string { - return nil + var fields []string + if m.FieldCleared(indexers.FieldSeedRatio) { + fields = append(fields, indexers.FieldSeedRatio) + } + if m.FieldCleared(indexers.FieldDisabled) { + fields = append(fields, indexers.FieldDisabled) + } + return fields } // FieldCleared returns a boolean indicating if a field with the given name was @@ -3161,6 +3330,14 @@ func (m *IndexersMutation) FieldCleared(name string) bool { // ClearField clears the value of the field with the given name. It returns an // error if the field is not defined in the schema. func (m *IndexersMutation) ClearField(name string) error { + switch name { + case indexers.FieldSeedRatio: + m.ClearSeedRatio() + return nil + case indexers.FieldDisabled: + m.ClearDisabled() + return nil + } return fmt.Errorf("unknown Indexers nullable field %s", name) } @@ -3183,6 +3360,12 @@ func (m *IndexersMutation) ResetField(name string) error { case indexers.FieldPriority: m.ResetPriority() return nil + case indexers.FieldSeedRatio: + m.ResetSeedRatio() + return nil + case indexers.FieldDisabled: + m.ResetDisabled() + return nil } return fmt.Errorf("unknown Indexers field %s", name) } diff --git a/ent/runtime.go b/ent/runtime.go index d91c3f0..1f5d7ce 100644 --- a/ent/runtime.go +++ b/ent/runtime.go @@ -61,6 +61,18 @@ func init() { indexersDescEnableRss := indexersFields[3].Descriptor() // indexers.DefaultEnableRss holds the default value on creation for the enable_rss field. indexers.DefaultEnableRss = indexersDescEnableRss.Default.(bool) + // indexersDescPriority is the schema descriptor for priority field. + indexersDescPriority := indexersFields[4].Descriptor() + // indexers.DefaultPriority holds the default value on creation for the priority field. + indexers.DefaultPriority = indexersDescPriority.Default.(int) + // indexersDescSeedRatio is the schema descriptor for seed_ratio field. + indexersDescSeedRatio := indexersFields[5].Descriptor() + // indexers.DefaultSeedRatio holds the default value on creation for the seed_ratio field. + indexers.DefaultSeedRatio = indexersDescSeedRatio.Default.(float32) + // indexersDescDisabled is the schema descriptor for disabled field. + indexersDescDisabled := indexersFields[6].Descriptor() + // indexers.DefaultDisabled holds the default value on creation for the disabled field. + indexers.DefaultDisabled = indexersDescDisabled.Default.(bool) mediaFields := schema.Media{}.Fields() _ = mediaFields // mediaDescCreatedAt is the schema descriptor for created_at field. diff --git a/ent/schema/indexers.go b/ent/schema/indexers.go index 826851e..e551f90 100644 --- a/ent/schema/indexers.go +++ b/ent/schema/indexers.go @@ -17,7 +17,9 @@ func (Indexers) Fields() []ent.Field { field.String("implementation"), field.String("settings"), field.Bool("enable_rss").Default(true), - field.Int("priority"), + field.Int("priority").Default(50), + field.Float32("seed_ratio").Optional().Default(0).Comment("minimal seed ratio requied, before removing torrent"), + field.Bool("disabled").Optional().Default(false), } } diff --git a/server/setting.go b/server/setting.go index 6065b88..d284dce 100644 --- a/server/setting.go +++ b/server/setting.go @@ -1,6 +1,7 @@ package server import ( + "encoding/json" "fmt" "net/http" "net/url" @@ -92,9 +93,13 @@ func (s *Server) GetSetting(c *gin.Context) (interface{}, error) { } type addTorznabIn struct { - Name string `json:"name" binding:"required"` - URL string `json:"url" binding:"required"` - ApiKey string `json:"api_key" binding:"required"` + ID int `json:"id"` + Name string `json:"name" binding:"required"` + URL string `json:"url" binding:"required"` + ApiKey string `json:"api_key" binding:"required"` + Disabled bool `json:"disabled"` + Priority int `json:"priority"` + SeedRatio float32 `json:"seed_ratio"` } func (s *Server) AddTorznabInfo(c *gin.Context) (interface{}, error) { @@ -102,10 +107,31 @@ func (s *Server) AddTorznabInfo(c *gin.Context) (interface{}, error) { if err := c.ShouldBindJSON(&in); err != nil { return nil, errors.Wrap(err, "bind json") } - err := s.db.SaveTorznabInfo(in.Name, db.TorznabSetting{ + + log.Infof("add indexer settings: %+v", in) + setting := db.TorznabSetting{ URL: in.URL, ApiKey: in.ApiKey, - }) + } + + data, err := json.Marshal(setting) + if err != nil { + return nil, errors.Wrap(err, "marshal json") + } + if in.Priority > 128 { + in.Priority = 128 + } + + indexer := ent.Indexers{ + ID: in.ID, + Name: in.Name, + Implementation: "torznab", + Settings: string(data), + Priority: in.Priority, + Disabled: in.Disabled, + SeedRatio: in.SeedRatio, + } + err = s.db.SaveIndexer(&indexer) if err != nil { return nil, errors.Wrap(err, "add ") } diff --git a/ui/lib/providers/settings.dart b/ui/lib/providers/settings.dart index df25df9..c79816e 100644 --- a/ui/lib/providers/settings.dart +++ b/ui/lib/providers/settings.dart @@ -130,20 +130,30 @@ class Indexer { String? url; String? apiKey; int? id; + int? priority; + double? seedRatio; + bool? disabled; - Indexer({this.name, this.url, this.apiKey}); + Indexer({this.name, this.url, this.apiKey, this.id, this.priority=50, this.seedRatio=0, this.disabled}); Indexer.fromJson(Map json) { name = json['name']; url = json['url']; apiKey = json['api_key']; id = json["id"]; + priority = json["priority"]; + seedRatio = json["seed_ratio"]??0; + disabled = json["disabled"] ?? false; } Map toJson() { final Map data = {}; data['name'] = name; data['url'] = url; data['api_key'] = apiKey; + data["id"] = id; + data["priority"] = priority; + data["seed_ratio"] = seedRatio; + data["disabled"] = disabled; return data; } } diff --git a/ui/lib/settings/dialog.dart b/ui/lib/settings/dialog.dart index 947aae7..c982385 100644 --- a/ui/lib/settings/dialog.dart +++ b/ui/lib/settings/dialog.dart @@ -16,7 +16,7 @@ Future showSettingDialog( title: Text(title), content: SingleChildScrollView( child: SizedBox( - width: 300, + width: 400, child: body, ), ), diff --git a/ui/lib/settings/indexer.dart b/ui/lib/settings/indexer.dart index 10ca768..54d3d18 100644 --- a/ui/lib/settings/indexer.dart +++ b/ui/lib/settings/indexer.dart @@ -8,7 +8,6 @@ import 'package:ui/widgets/progress_indicator.dart'; import 'package:ui/widgets/widgets.dart'; class IndexerSettings extends ConsumerStatefulWidget { - const IndexerSettings({super.key}); @override ConsumerState createState() { @@ -47,7 +46,10 @@ class _IndexerState extends ConsumerState { "name": indexer.name, "url": indexer.url, "api_key": indexer.apiKey, - "impl": "torznab" + "impl": "torznab", + "priority": indexer.priority.toString(), + "seed_ratio": indexer.seedRatio.toString(), + "disabled": indexer.disabled }, child: Column( children: [ @@ -76,6 +78,26 @@ class _IndexerState extends ConsumerState { autovalidateMode: AutovalidateMode.onUserInteraction, validator: FormBuilderValidators.required(), ), + FormBuilderTextField( + name: "priority", + decoration: const InputDecoration( + labelText: "索引优先级", + helperText: "取值范围1-128, 数值越大,优先级越高", + ), + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: FormBuilderValidators.positiveNumber(), + ), + FormBuilderTextField( + name: "seed_ratio", + decoration: const InputDecoration( + labelText: "做种率", + helperText: "种子的做种率,达到此做种率后,种子才会被删除, 0表示不做种", + hintText: "1.0", + ), + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: FormBuilderValidators.numeric(), + ), + FormBuilderSwitch(name: "disabled", title: const Text("禁用此索引器")) ], ), ); @@ -87,9 +109,13 @@ class _IndexerState extends ConsumerState { if (_formKey.currentState!.saveAndValidate()) { var values = _formKey.currentState!.value; return ref.read(indexersProvider.notifier).addIndexer(Indexer( + id: indexer.id, name: values["name"], url: values["url"], - apiKey: values["api_key"])); + apiKey: values["api_key"], + priority: int.parse(values["priority"]), + seedRatio: double.parse(values["seed_ratio"]), + disabled: values["disabled"])); } else { throw "validation_error"; }