Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Revert "Merge pull request #692 from OkunaOrg/feature/257-trending-co…
Browse files Browse the repository at this point in the history
…mmunities-n-posts-rehaul"

This reverts commit 1858ae7, reversing
changes made to 7cb94cf.
  • Loading branch information
lifenautjoe committed May 6, 2020
1 parent 1858ae7 commit 05ae2a4
Show file tree
Hide file tree
Showing 31 changed files with 205 additions and 1,637 deletions.
7 changes: 0 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ The change log for the API server for Okuna.

## Table of contents

- [Release 0.0.66](#release-0.0.66)
- [Release 0.0.63](#release-0.0.63)
- [Release 0.0.59](#release-0.0.59)


## Release 0.0.66

- Introduce activity score in posts and communities, add jobs for the same
- Refactor trending posts and trending communities to use activity score


## Release 0.0.63

- Improve performance for linked users API
Expand Down
37 changes: 2 additions & 35 deletions openbook/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from dotenv import load_dotenv, find_dotenv
from sentry_sdk.integrations.django import DjangoIntegration
from django_replicated.settings import *
from decimal import Decimal, getcontext

# Logging config
from sentry_sdk.integrations.rq import RqIntegration
Expand Down Expand Up @@ -178,8 +177,6 @@
REDIS_RQ_DEFAULT_JOBS_CACHE_LOCATION = '%(redis_location)s/%(db)d' % {'redis_location': REDIS_LOCATION, 'db': 1}
REDIS_RQ_HIGH_JOBS_CACHE_LOCATION = '%(redis_location)s/%(db)d' % {'redis_location': REDIS_LOCATION, 'db': 2}
REDIS_RQ_LOW_JOBS_CACHE_LOCATION = '%(redis_location)s/%(db)d' % {'redis_location': REDIS_LOCATION, 'db': 3}
REDIS_ACTIVITY_SCORES_JOBS_CACHE_LOCATION = '%(redis_location)s/%(db)d' % {
'redis_location': REDIS_LOCATION, 'db': 4}

CACHES = {
'default': {
Expand Down Expand Up @@ -214,14 +211,6 @@
},
"KEY_PREFIX": "ob-api-rq-low-job-"
},
'activity-score-jobs': {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_ACTIVITY_SCORES_JOBS_CACHE_LOCATION,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
},
"KEY_PREFIX": "ob-api-actvty-score-job-"
},
}

CACHEOPS_REDIS_DB = int(os.environ.get('CACHEOPS_REDIS_DB', '1'))
Expand All @@ -247,9 +236,6 @@
'low': {
'USE_REDIS_CACHE': 'rq-low-jobs',
},
'process-activity-score': {
'USE_REDIS_CACHE': 'activity-score-jobs',
},
}

if IS_BUILD:
Expand Down Expand Up @@ -541,26 +527,7 @@

MIN_UNIQUE_TOP_POST_REACTIONS_COUNT = int(os.environ.get('MIN_UNIQUE_TOP_POST_REACTIONS_COUNT', '5'))
MIN_UNIQUE_TOP_POST_COMMENTS_COUNT = int(os.environ.get('MIN_UNIQUE_TOP_POST_COMMENTS_COUNT', '5'))

# for activity score, set decimal precision to 10
getcontext().prec = 10

MIN_ACTIVITY_SCORE_FOR_POST_TRENDING = Decimal((os.environ.get('MIN_ACTIVITY_SCORE_FOR_POST_TRENDING', 0.002)))
MIN_ACTIVITY_SCORE_FOR_COMMUNITY_TRENDING = Decimal((os.environ.get('MIN_ACTIVITY_SCORE_FOR_COMMUNITY_TRENDING', 0.002)))
ACTIVITY_ATOMIC_WEIGHT = Decimal((os.environ.get('ACTIVITY_ATOMIC_WEIGHT', 0.001)))

ACTIVITY_UNIQUE_REACTION_MULTIPLIER = int(os.environ.get('ACTIVITY_UNIQUE_REACTION_MULTIPLIER', 1))
ACTIVITY_UNIQUE_COMMENT_MULTIPLIER = int(os.environ.get('ACTIVITY_UNIQUE_COMMENT_MULTIPLIER', 1))
ACTIVITY_COUNT_COMMENTS_MULTIPLIER = int(os.environ.get('ACTIVITY_COUNT_COMMENTS_MULTIPLIER', 1))
ACTIVITY_UNIQUE_POST_MULTIPLIER = int(os.environ.get('ACTIVITY_UNIQUE_POST_MULTIPLIER', 1))
ACTIVITY_COUNT_POSTS_MULTIPLIER = int(os.environ.get('ACTIVITY_COUNT_POSTS_MULTIPLIER', 1))

ACTIVITY_UNIQUE_REACTION_WEIGHT = Decimal((ACTIVITY_ATOMIC_WEIGHT * ACTIVITY_UNIQUE_REACTION_MULTIPLIER))
ACTIVITY_UNIQUE_COMMENT_WEIGHT = Decimal((ACTIVITY_ATOMIC_WEIGHT * ACTIVITY_UNIQUE_COMMENT_MULTIPLIER))
ACTIVITY_COUNT_COMMENTS_WEIGHT = Decimal((ACTIVITY_ATOMIC_WEIGHT * ACTIVITY_COUNT_COMMENTS_MULTIPLIER))
ACTIVITY_UNIQUE_POST_WEIGHT = Decimal((ACTIVITY_ATOMIC_WEIGHT * ACTIVITY_UNIQUE_POST_MULTIPLIER))
ACTIVITY_COUNT_POSTS_WEIGHT = Decimal((ACTIVITY_ATOMIC_WEIGHT * ACTIVITY_COUNT_POSTS_MULTIPLIER))
ACTIVITY_SCORE_EXPIRY_IN_HOURS = int(os.environ.get('ACTIVITY_SCORE_EXPIRY_IN_HOURS', 12))
MIN_UNIQUE_TRENDING_POST_REACTIONS_COUNT = int(os.environ.get('MIN_UNIQUE_TRENDING_POST_REACTIONS_COUNT', '5'))

# Email Config

Expand All @@ -583,7 +550,7 @@
OS_TRANSLATION_STRATEGY_NAME = 'testing'
MIN_UNIQUE_TOP_POST_REACTIONS_COUNT = 1
MIN_UNIQUE_TOP_POST_COMMENTS_COUNT = 1
MIN_ACTIVITY_SCORE_FOR_POST_TRENDING = 0.001
MIN_UNIQUE_TRENDING_POST_REACTIONS_COUNT = 1

if IS_PRODUCTION:
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
Expand Down
4 changes: 2 additions & 2 deletions openbook/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
from openbook_posts.views.post_media.views import PostMedia
from openbook_posts.views.post_reaction.views import PostReactionItem
from openbook_posts.views.post_reactions.views import PostReactions, PostReactionsEmojiCount, PostReactionEmojiGroups
from openbook_posts.views.posts.views import Posts, TrendingPosts, TopPosts, TrendingPostsLegacy, \
from openbook_posts.views.posts.views import Posts, TrendingPosts, TopPosts, TrendingPostsNew, \
ProfilePostsExcludedCommunities, SearchProfilePostsExcludedCommunities, TopPostsExcludedCommunities, \
SearchTopPostsExcludedCommunities, ProfilePostsExcludedCommunity, TopPostsExcludedCommunity
from openbook_importer.views import ImportItem
Expand Down Expand Up @@ -238,7 +238,7 @@
path('<uuid:post_uuid>/', include(post_patterns)),
path('', Posts.as_view(), name='posts'),
path('trending/', TrendingPosts.as_view(), name='trending-posts'),
path('trending/new/', TrendingPostsLegacy.as_view(), name='trending-posts-new'),
path('trending/new/', TrendingPostsNew.as_view(), name='trending-posts-new'),
path('emojis/groups/', PostReactionEmojiGroups.as_view(), name='posts-emoji-groups'),
path('profile/', include(posts_profile_patterns)),
path('top/', include(posts_top_patterns)),
Expand Down
11 changes: 3 additions & 8 deletions openbook_auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1987,18 +1987,13 @@ def get_trending_posts(self, max_id=None, min_id=None):
Post = get_post_model()
return Post.get_trending_posts_for_user_with_id(user_id=self.pk, max_id=max_id, min_id=min_id)

def get_trending_posts_legacy(self):
def get_trending_posts_old(self):
Post = get_post_model()
return Post.get_trending_posts_for_user_with_id_legacy(user_id=self.pk)
return Post.get_trending_posts_old_for_user_with_id(user_id=self.pk)

def get_trending_communities(self, category_name=None):
Community = get_community_model()
return Community.get_trending_communities_for_user_with_id(user_id=self.pk,
category_name=category_name)

def get_trending_communities_by_members(self, category_name=None):
Community = get_community_model()
return Community.get_trending_communities_by_members_for_user_with_id(user_id=self.pk, category_name=category_name)
return Community.get_trending_communities_for_user_with_id(user_id=self.pk, category_name=category_name)

def search_communities_with_query(self, query, excluded_from_profile_posts):
Community = get_community_model()
Expand Down
2 changes: 2 additions & 0 deletions openbook_auth/tests/views/test_authenticated_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,8 @@ def test_cannot_set_invalid_language(self):
'language_id': 99999
}, **headers)

print(response)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
user.refresh_from_db()
self.assertTrue(user.language.id, language.id)
Expand Down
11 changes: 1 addition & 10 deletions openbook_common/tests/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest.mock import patch

from rest_framework.test import APITestCase, APITransactionTestCase
from rest_framework.test import APITestCase


class OpenbookAPITestCase(APITestCase):
Expand All @@ -10,12 +10,3 @@ def setUp(self):

def tearDown(self):
self.patcher.stop()


class OpenbookAPITransactionTestCase(APITransactionTestCase):
def setUp(self):
self.patcher = patch('openbook_notifications.helpers._send_notification_to_user')
self.mock_foo = self.patcher.start()

def tearDown(self):
self.patcher.stop()
22 changes: 0 additions & 22 deletions openbook_communities/migrations/0034_trendingcommunity.py

This file was deleted.

18 changes: 0 additions & 18 deletions openbook_communities/migrations/0035_community_activity_score.py

This file was deleted.

16 changes: 0 additions & 16 deletions openbook_communities/migrations/0036_delete_trendingcommunity.py

This file was deleted.

17 changes: 0 additions & 17 deletions openbook_communities/migrations/0037_auto_20200222_1344.py

This file was deleted.

18 changes: 0 additions & 18 deletions openbook_communities/migrations/0038_auto_20200224_1615.py

This file was deleted.

39 changes: 2 additions & 37 deletions openbook_communities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ class Community(models.Model):
_('is deleted'),
default=False,
)
activity_score = models.DecimalField(default=0.0, decimal_places=10, max_digits=10)

class Meta:
verbose_name_plural = 'communities'
indexes = [
models.Index(fields=['activity_score']),
]

@classmethod
def is_user_with_username_invited_to_community_with_name(cls, username, community_name):
Expand Down Expand Up @@ -151,12 +147,7 @@ def get_new_user_suggested_communities(cls):
def get_trending_communities_for_user_with_id(cls, user_id, category_name=None):
trending_communities_query = cls._make_trending_communities_query(category_name=category_name)
trending_communities_query.add(~Q(banned_users__id=user_id), Q.AND)

trending_communities = cls._get_trending_communities_with_query(query=trending_communities_query)
if trending_communities.count() == 0:
return cls.get_trending_communities_by_members_for_user_with_id(user_id, category_name=category_name)

return trending_communities
return cls._get_trending_communities_with_query(query=trending_communities_query)

@classmethod
def get_trending_communities(cls, category_name=None):
Expand All @@ -165,37 +156,11 @@ def get_trending_communities(cls, category_name=None):

@classmethod
def _get_trending_communities_with_query(cls, query):
return cls.objects.filter(query).order_by('-activity_score')

@classmethod
def _make_trending_communities_query(cls, category_name=None):
trending_communities_query = Q(type=Community.COMMUNITY_TYPE_PUBLIC, is_deleted=False)
trending_communities_query.add(Q(activity_score__gte=settings.MIN_ACTIVITY_SCORE_FOR_COMMUNITY_TRENDING), Q.AND)
trending_communities_query.add(~Q(moderated_object__status=ModeratedObject.STATUS_APPROVED), Q.AND)

if category_name:
trending_communities_query.add(Q(categories__name=category_name), Q.AND)

return trending_communities_query

@classmethod
def get_trending_communities_by_members_for_user_with_id(cls, user_id, category_name=None):
trending_communities_query = cls._make_trending_communities_by_members_query(category_name=category_name)
trending_communities_query.add(~Q(banned_users__id=user_id), Q.AND)
return cls._get_trending_communities_by_members_with_query(query=trending_communities_query)

@classmethod
def get_trending_communities_by_members(cls, category_name=None):
trending_communities_query = cls._make_trending_communities_by_members_query(category_name=category_name)
return cls._get_trending_communities_by_members_with_query(query=trending_communities_query)

@classmethod
def _get_trending_communities_by_members_with_query(cls, query):
return cls.objects.annotate(Count('memberships')).filter(query).order_by(
'-memberships__count', '-created')

@classmethod
def _make_trending_communities_by_members_query(cls, category_name=None):
def _make_trending_communities_query(cls, category_name=None):
trending_communities_query = Q(type=cls.COMMUNITY_TYPE_PUBLIC, is_deleted=False)

if category_name:
Expand Down
Loading

0 comments on commit 05ae2a4

Please sign in to comment.