Skip to content

Commit

Permalink
record CountedUsage in a celery task
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Oct 26, 2022
1 parent 3c0fc59 commit f40eae2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
23 changes: 15 additions & 8 deletions api/metrics/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from api.base.serializers import BaseAPISerializer
from api.base.utils import absolute_reverse
from framework.celery_tasks.handlers import enqueue_task
from osf.metrics.counted_usage import CountedUsage
from osf.metrics.tasks import record_counted_usage
from website import settings as website_settings


Expand Down Expand Up @@ -60,14 +62,19 @@ def validate(self, data):
return data

def create(self, validated_data):
return CountedUsage.record(
platform_iri=website_settings.DOMAIN,
provider_id=validated_data.get('provider_id'),
item_guid=validated_data.get('item_guid'),
session_id=validated_data['session_id'], # must be provided by the view
action_labels=validated_data.get('action_labels'),
pageview_info=validated_data.get('pageview_info'),
)
record_kwargs = { # passed to CountedUsage.record()
'platform_iri': website_settings.DOMAIN,
'provider_id': validated_data.get('provider_id'),
'item_guid': validated_data.get('item_guid'),
'session_id': validated_data['session_id'], # must be provided by the view
'action_labels': validated_data.get('action_labels'),
'pageview_info': validated_data.get('pageview_info'),
}
if website_settings.USE_CELERY:
enqueue_task(record_counted_usage.s(record_kwargs))
else:
record_counted_usage(record_kwargs)
return {}


class ReportNameSerializer(ser.BaseSerializer):
Expand Down
10 changes: 10 additions & 0 deletions osf/metrics/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from framework.celery_tasks import app as celery_app
from osf.metrics.counted_usage import CountedUsage


@celery_app.task(bind=True, max_retries=5, default_retry_delay=60)
def record_counted_usage(self, record_kwargs):
try:
CountedUsage.record(**record_kwargs)
except Exception as exc:
self.retry(exc=exc)
1 change: 1 addition & 0 deletions website/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class CeleryConfig:
'osf.management.commands.populate_branched_from',
'osf.management.commands.cumulative_plos_metrics',
'osf.management.commands.daily_reporters_go',
'osf.metrics.tasks',
}

med_pri_modules = {
Expand Down

0 comments on commit f40eae2

Please sign in to comment.