Skip to content

Commit

Permalink
chg: [controllers] Improved the processing of stats for a feed used i…
Browse files Browse the repository at this point in the history
…n the feed_view view.
  • Loading branch information
cedricbonhomme committed Jan 17, 2025
1 parent b0b9608 commit 8a6324e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 12 additions & 0 deletions newspipe/controllers/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,15 @@ def get_oldest(self, **filters):

def read_ordered(self, **filters):
return super().read(**filters).order_by(Article.date.desc())

def get_date_statistics(self, **filters):
return (
super()
.read(**filters)
.with_entities(
func.min(Article.date).label("min_date"),
func.max(Article.date).label("max_date"),
func.count(Article.id).label("total_articles"),
)
.first()
)
18 changes: 11 additions & 7 deletions newspipe/web/views/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ def feeds():


def feed_view(feed_id=None, user_id=None):
"""Display various information about a fedd."""
feed = FeedController(user_id).get(id=feed_id)
category = None
if feed.category_id:
category = CategoryController(user_id).get(id=feed.category_id)
filters = {}
filters["feed_id"] = feed_id
filters = {"feed_id": feed_id}
articles = ArticleController(user_id).read_light(**filters)

# Server-side pagination
Expand All @@ -72,16 +72,20 @@ def feed_view(feed_id=None, user_id=None):
per_page=per_page,
)

# Optimized date statistics calculation
try:
last_article = ArticleController(user_id).get_newest(**filters).date
first_article = ArticleController(user_id).get_oldest(**filters).date
stats = ArticleController(user_id).get_date_statistics(**filters)
first_article = stats.min_date or datetime.fromtimestamp(0)
last_article = stats.max_date or datetime.fromtimestamp(0)
total_articles = stats.total_articles or 0

delta = last_article - first_article
average = round(articles.count() / abs(delta.days), 2)
average = round(total_articles / abs(delta.days), 2) if delta.days > 0 else 0
except Exception:
last_article = datetime.fromtimestamp(0)
first_article = datetime.fromtimestamp(0)
first_article = last_article = datetime.fromtimestamp(0)
delta = last_article - first_article
average = 0

elapsed = datetime.now() - last_article

return render_template(
Expand Down

0 comments on commit 8a6324e

Please sign in to comment.