support attach labels to rule result

This commit is contained in:
glidea
2025-06-07 15:37:04 +08:00
parent f4d3786496
commit ae90606263
6 changed files with 21 additions and 0 deletions

View File

@@ -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."`
}

View File

@@ -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 {

View File

@@ -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,

View File

@@ -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")
}

View File

@@ -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 {

View File

@@ -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),
}