diff --git a/pkg/config/config.go b/pkg/config/config.go index 1e6abe7..0848600 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -151,6 +151,7 @@ type SchedulsRule struct { Query string `yaml:"query,omitempty" json:"query,omitempty" desc:"The semantic query to get the feeds. NOTE it is optional"` Threshold float32 `yaml:"threshold,omitempty" json:"threshold,omitempty" desc:"The threshold to filter the query result by relevance (with 'query') score. It does not work when query is not set. Default is 0.6."` LabelFilters []string `yaml:"label_filters,omitempty" json:"label_filters,omitempty" desc:"The label filters (equal or not equal) to match the feeds. e.g. [category=tech, source!=github]"` + Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty" desc:"The labels to attach to the feeds."` EveryDay string `yaml:"every_day,omitempty" json:"every_day,omitempty" desc:"The query range at the end time of every day. Format: start~end, e.g. 00:00~23:59, or -22:00~7:00 (yesterday 22:00 to today 07:00)."` WatchInterval timeutil.Duration `yaml:"watch_interval,omitempty" json:"watch_interval,omitempty" desc:"The run and query interval to watch the rule. Default is 10m. It can not be set with every_day at same time."` } diff --git a/pkg/model/model.go b/pkg/model/model.go index a57ff4f..32a3079 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -83,6 +83,7 @@ func (ls *Labels) FromMap(m map[string]string) { for k, v := range m { *ls = append(*ls, Label{Key: k, Value: v}) } + ls.EnsureSorted() } func (ls Labels) Map() map[string]string { diff --git a/pkg/schedule/rule/periodic.go b/pkg/schedule/rule/periodic.go index e596bf8..71a5a39 100644 --- a/pkg/schedule/rule/periodic.go +++ b/pkg/schedule/rule/periodic.go @@ -119,6 +119,12 @@ func (r *periodic) execute(ctx context.Context, now time.Time) error { return nil } + // Attach labels to feeds. + for _, feed := range feeds { + feed.Labels = append(feed.Labels, config.labels...) + feed.Labels.EnsureSorted() + } + // Notify. r.Dependencies().Out <- &Result{ Rule: config.Name, diff --git a/pkg/schedule/rule/rule.go b/pkg/schedule/rule/rule.go index d2f3b0a..5e7b69e 100644 --- a/pkg/schedule/rule/rule.go +++ b/pkg/schedule/rule/rule.go @@ -22,6 +22,7 @@ import ( "github.com/pkg/errors" "github.com/glidea/zenfeed/pkg/component" + "github.com/glidea/zenfeed/pkg/model" "github.com/glidea/zenfeed/pkg/storage/feed" "github.com/glidea/zenfeed/pkg/storage/feed/block" ) @@ -37,6 +38,8 @@ type Config struct { Query string Threshold float32 LabelFilters []string + Labels map[string]string + labels model.Labels // Periodic type. EveryDay string // e.g. "00:00~23:59", or "-22:00~7:00" (yesterday 22:00 to today 07:00) @@ -63,6 +66,9 @@ func (c *Config) Validate() error { //nolint:cyclop,gocognit if c.Threshold < 0 || c.Threshold > 1 { return errors.New("threshold must be between 0 and 1") } + if len(c.Labels) > 0 { + c.labels.FromMap(c.Labels) + } if c.EveryDay != "" && c.WatchInterval != 0 { return errors.New("every_day and watch_interval cannot both be set") } diff --git a/pkg/schedule/rule/watch.go b/pkg/schedule/rule/watch.go index 7ae2932..bb4ea0e 100644 --- a/pkg/schedule/rule/watch.go +++ b/pkg/schedule/rule/watch.go @@ -101,6 +101,12 @@ func (r *watch) execute(ctx context.Context, start, end time.Time) error { return nil } + // Attach labels to feeds. + for _, feed := range feeds { + feed.Labels = append(feed.Labels, config.labels...) + feed.Labels.EnsureSorted() + } + // Split feeds by start time. feedsByStart := make(map[time.Time][]*block.FeedVO) // Start time -> feeds. for _, feed := range feeds { diff --git a/pkg/schedule/schedule.go b/pkg/schedule/schedule.go index 9f2be2b..46a566d 100644 --- a/pkg/schedule/schedule.go +++ b/pkg/schedule/schedule.go @@ -58,6 +58,7 @@ func (c *Config) From(app *config.App) *Config { Query: r.Query, Threshold: r.Threshold, LabelFilters: r.LabelFilters, + Labels: r.Labels, EveryDay: r.EveryDay, WatchInterval: time.Duration(r.WatchInterval), }