Skip to content

Commit

Permalink
GITC-210: Record visit/join actions inside a celery task instead of c…
Browse files Browse the repository at this point in the history
…ontext.py (#9295)

* GITC-210: Record visit/join actions inside a celery task instead of context.py

* GITC-210: Removes duplicated User imports

* GITC-210: Drops try...except wrapper from profile_dict.delay(profile.pk)
  • Loading branch information
gdixon authored Jul 15, 2021
1 parent 8f48554 commit d41e9d0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 36 deletions.
50 changes: 18 additions & 32 deletions app/app/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from app.utils import get_location_from_ip
from cacheops import cached_as
from dashboard.models import Activity, Tip, UserAction
from dashboard.tasks import record_join, record_visit
from dashboard.utils import _get_utm_from_cookie
from kudos.models import KudosTransfer
from marketing.utils import handle_marketing_callback
Expand Down Expand Up @@ -80,41 +81,26 @@ def preprocess(request):
profile = request.user.profile if user_is_authenticated and hasattr(request.user, 'profile') else None
if user_is_authenticated and profile and profile.pk:
# what actions to take?
record_join = not profile.last_visit
record_visit = not profile.last_visit or profile.last_visit < (
should_record_join = not profile.last_visit
should_record_visit = not profile.last_visit or profile.last_visit < (
timezone.now() - timezone.timedelta(seconds=RECORD_VISIT_EVERY_N_SECONDS)
)
if record_visit:
try:
profile.last_visit = timezone.now()
profile.save()
except Exception as e:
logger.exception(e)
try:
from dashboard.tasks import profile_dict
profile_dict.delay(profile.pk)
except Exception as e:
logger.exception(e)
metadata = {
'visitorId': request.COOKIES.get("visitorId", None),
'useragent': request.META['HTTP_USER_AGENT'],
'referrer': request.META.get('HTTP_REFERER', None),
'path': request.META.get('PATH_INFO', None),
'session_key': request.session._session_key,
}

if should_record_visit:
# values to pass to celery from the request
ip_address = get_ip(request)
UserAction.objects.create(
user=request.user,
profile=profile,
action='Visit',
location_data=get_location_from_ip(ip_address),
ip_address=ip_address,
utm=_get_utm_from_cookie(request),
metadata=metadata,
)

if record_join:
Activity.objects.create(profile=profile, activity_type='joined')
visitorId = request.COOKIES.get("visitorId", None)
useragent = request.META['HTTP_USER_AGENT']
referrer = request.META.get('HTTP_REFERER', None)
path = request.META.get('PATH_INFO', None)
session_key = request.session._session_key
utm = _get_utm_from_cookie(request)
# record the visit as a celery task
record_visit.delay(request.user.pk, profile.pk, ip_address, visitorId, useragent, referrer, path, session_key, utm)

if should_record_join:
# record the joined action as a celery task
record_join.delay(profile.pk)

ptoken = PersonalToken.objects.filter(token_owner_profile=profile).first()

Expand Down
6 changes: 4 additions & 2 deletions app/assets/v2/js/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ $(document).ready(function() {
});
};

if (document.visitorId) {
Cookies.set('visitorId', document.visitorId);
var visitorId = (document.visitorId || Cookies.get('_vid'));

if (visitorId) {
Cookies.set('visitorId', visitorId);
}
record_campaign_to_cookie();

Expand Down
70 changes: 68 additions & 2 deletions app/dashboard/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.http import HttpRequest
from django.utils import timezone

from app.services import RedisService
from app.utils import get_location_from_ip
from celery import app, group
from celery.utils.log import get_task_logger
from dashboard.models import Activity, Bounty, ObjectView, Profile
from dashboard.models import Activity, Bounty, ObjectView, Profile, UserAction
from marketing.mails import func_name, grant_update_email, send_mail
from proxy.views import proxy_view
from retail.emails import render_share_bounty
Expand Down Expand Up @@ -319,7 +321,6 @@ def increment_view_count(self, pks, content_type, user_id, view_type, retry: boo
@app.shared_task(bind=True, max_retries=1)
def sync_profile(self, handle, user_pk, hide_profile, retry: bool = True) -> None:
from app.utils import actually_sync_profile
from django.contrib.auth.models import User
user = User.objects.filter(pk=user_pk).first() if user_pk else None
actually_sync_profile(handle, user=user, hide_profile=hide_profile)

Expand All @@ -330,3 +331,68 @@ def recalculate_earning(self, pk, retry: bool = True) -> None:
earning = Earning.objects.get(pk=pk)
src = earning.source
src.save()


@app.shared_task(bind=True, max_retries=1)
def record_visit(self, user_pk, profile_pk, ip_address, visitorId, useragent, referrer, path, session_key, utm, retry: bool = True) -> None:
"""
:param self: Self
:param user_pk: user primary
:param profile_pk: profile primary
:param ip_address: get_ip(request)
:param visitorId: request.COOKIES.get("visitorId", None)
:param useragent: request.META['HTTP_USER_AGENT']
:param referrer: request.META.get('HTTP_REFERER', None)
:param path: request.META.get('PATH_INFO', None)
:param session_key: request.session._session_key
:param utm: _get_utm_from_cookie(request)
:return: None
"""

user = User.objects.filter(pk=user_pk).first() if user_pk else None
profile = Profile.objects.filter(pk=profile_pk).first() if profile_pk else None
if user and profile:
try:
profile.last_visit = timezone.now()
profile.save()
except Exception as e:
logger.exception(e)

# enqueue profile_dict recalc
profile_dict.delay(profile.pk)

try:
metadata = {
'visitorId': visitorId,
'useragent': useragent,
'referrer': referrer,
'path': path,
'session_key': session_key,
}
UserAction.objects.create(
user=user,
profile=profile,
action='Visit',
location_data=get_location_from_ip(ip_address),
ip_address=ip_address,
utm=utm,
metadata=metadata,
)
except Exception as e:
logger.exception(e)


@app.shared_task(bind=True, max_retries=1)
def record_join(self, profile_pk, retry: bool = True) -> None:
"""
:param self: Self
:param profile_pk: profile primary
:return: None
"""

profile = Profile.objects.filter(pk=profile_pk).first() if profile_pk else None
if profile:
try:
Activity.objects.create(profile=profile, activity_type='joined')
except Exception as e:
logger.exception(e)

0 comments on commit d41e9d0

Please sign in to comment.