Private
Public Access
FilterRule had no indexes beyond the default primary/foreign-key ones, and apply_filter_rules_for_feed issued 2 queries per subscriber (its enabled rules, its subscription tags) inside a loop over every subscriber of the feed — both cost grew with subscriber/rule count on every single feed poll. Adds a composite index on (user, is_enabled, priority), matching the exact filter+order shape every caller already uses (this task, apps.rules.tasks' backlog re-apply). apply_filter_rules_for_feed now fetches rules and subscription tags once for the whole feed's subscriber set, grouped by user/subscription in Python, instead of once per subscriber. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
92 lines
3.4 KiB
Python
92 lines
3.4 KiB
Python
from django.conf import settings
|
|
from django.db import models
|
|
|
|
from apps.articles.models import Tag
|
|
from apps.feeds.models import Feed
|
|
|
|
|
|
class FilterRule(models.Model):
|
|
"""A single condition -> action rule, evaluated at ingest time against
|
|
new/updated articles for every subscriber of the matching feed(s).
|
|
|
|
v1 supports one condition per rule; users combine effects by stacking
|
|
multiple rules (evaluated in ``priority`` order, ascending — lower
|
|
numbers run first). Compound AND/OR condition groups are deferred.
|
|
"""
|
|
|
|
FIELD_TITLE = "title"
|
|
FIELD_CONTENT = "content"
|
|
FIELD_AUTHOR = "author"
|
|
FIELD_FEED_TITLE = "feed_title"
|
|
FIELD_URL = "url"
|
|
MATCH_FIELD_CHOICES = [
|
|
(FIELD_TITLE, "Title"),
|
|
(FIELD_CONTENT, "Content"),
|
|
(FIELD_AUTHOR, "Author"),
|
|
(FIELD_FEED_TITLE, "Feed"),
|
|
(FIELD_URL, "URL"),
|
|
]
|
|
|
|
MATCH_CONTAINS = "contains"
|
|
MATCH_NOT_CONTAINS = "not_contains"
|
|
MATCH_REGEX = "regex"
|
|
MATCH_EXACT = "exact"
|
|
MATCH_TYPE_CHOICES = [
|
|
(MATCH_CONTAINS, "Contains"),
|
|
(MATCH_NOT_CONTAINS, "Does not contain"),
|
|
(MATCH_REGEX, "Regex"),
|
|
(MATCH_EXACT, "Exact match"),
|
|
]
|
|
|
|
ACTION_TAG = "tag"
|
|
ACTION_STAR = "star"
|
|
ACTION_HIDE = "hide"
|
|
ACTION_MARK_READ = "mark_read"
|
|
ACTION_CHOICES = [
|
|
(ACTION_TAG, "Add tag"),
|
|
(ACTION_STAR, "Star"),
|
|
(ACTION_HIDE, "Hide"),
|
|
(ACTION_MARK_READ, "Mark read"),
|
|
]
|
|
|
|
user = models.ForeignKey(
|
|
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="filter_rules"
|
|
)
|
|
name = models.CharField(max_length=255, blank=True)
|
|
|
|
# Scope: null means "every subscription". A tag-scoped rule applies to
|
|
# any feed carrying that tag (via SubscriptionTag) — a feed can carry
|
|
# more than one tag, unlike the single-folder scoping this replaced.
|
|
feed = models.ForeignKey(Feed, null=True, blank=True, on_delete=models.CASCADE)
|
|
tag = models.ForeignKey(Tag, null=True, blank=True, on_delete=models.CASCADE)
|
|
|
|
match_field = models.CharField(max_length=20, choices=MATCH_FIELD_CHOICES)
|
|
match_type = models.CharField(max_length=20, choices=MATCH_TYPE_CHOICES)
|
|
match_value = models.CharField(max_length=500)
|
|
case_sensitive = models.BooleanField(default=False)
|
|
|
|
action = models.CharField(max_length=20, choices=ACTION_CHOICES)
|
|
action_value = models.CharField(
|
|
max_length=100, blank=True, help_text="Tag name, only used when action is 'Add tag'."
|
|
)
|
|
|
|
priority = models.PositiveIntegerField(default=0, help_text="Lower numbers run first.")
|
|
is_enabled = models.BooleanField(default=True)
|
|
|
|
class Meta:
|
|
ordering = ["priority", "id"]
|
|
indexes = [
|
|
# Matches the ingest-time query shape exactly (C9): every
|
|
# caller — apps.articles.tasks.apply_filter_rules_for_feed,
|
|
# apps.rules.tasks.reapply_rules_for_user_backlog — filters by
|
|
# (user, is_enabled) and orders by priority. `user` alone
|
|
# already has an index from the ForeignKey, but it doesn't
|
|
# cover is_enabled or let the database satisfy the ORDER BY
|
|
# from the index itself.
|
|
models.Index(fields=["user", "is_enabled", "priority"]),
|
|
]
|
|
|
|
def __str__(self):
|
|
label = self.name or f"{self.get_match_field_display()} {self.get_match_type_display()}"
|
|
return f"{label} -> {self.get_action_display()}"
|