Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions app/controllers/socials.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ def _calculate_time_decay_score(date_field, decay_days=TRENDING_DECAY_DAYS):
Calculate a time decay score for trending calculations.
Returns a value between 0 and 1, where newer items score higher.
"""
now = datetime.utcnow()
decay_start = now - timedelta(days=decay_days)

return case(
(date_field >= decay_start,
1.0 - (cast(func.extract('epoch', now - date_field), Float) /
(decay_days * 86400.0))
),
else_=0.1 # Minimum score for old items
)
now = func.now()
total_decay_seconds = decay_days * 86400.0

# Calculate age in seconds and convert to decay score
age_seconds = func.extract('epoch', now) - \
func.extract('epoch', date_field)
decay_score = func.greatest(
0.1, 1.0 - (age_seconds / total_decay_seconds))

return decay_score

@staticmethod
def _get_user_interests(user):
Expand Down Expand Up @@ -174,14 +174,15 @@ def get_projects_data(current_user, search=None, filter_type=None, page=1, per_p
# Apply ordering based on filter type
if filter_type == 'trending':
# Advanced trending: combines follower count with time decay
follower_count = func.count(func.distinct(ProjectFollowers.id))
time_decay = SocialService._calculate_time_decay_score(
Project.updated_at)
follower_count = func.count(func.distinct(ProjectFollowers.id))

trending_score = (
(follower_count * SocialService.POPULARITY_WEIGHT) +
(time_decay * SocialService.RECENCY_WEIGHT)
)
).label('trending_score')

query = query.order_by(desc(trending_score),
desc(Project.date_created))

Expand Down