Skip to content

Commit

Permalink
Merge pull request #2176 from onaio/use_cache_submission_stats
Browse files Browse the repository at this point in the history
Use cache submission stats
  • Loading branch information
DavisRayM authored Jan 11, 2022
2 parents 6897249 + 0310f7d commit 07af4cd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions onadata/apps/api/tests/viewsets/test_charts_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.utils import timezone
from django.core.cache import cache
from django.test.utils import override_settings
from rest_framework.test import APIClient
from rest_framework.test import APIRequestFactory
from rest_framework.test import force_authenticate
Expand Down Expand Up @@ -397,6 +398,7 @@ def test_cascading_select(self):
]
self.assertEqual(expected, response.data['data'])

@override_settings(XFORM_CHARTS_CACHE_TIME=0)
def test_deleted_submission_not_in_chart_endpoint(self):
data = {'field_name': 'gender'}
request = self.factory.get('/charts', data)
Expand Down
16 changes: 14 additions & 2 deletions onadata/apps/api/viewsets/charts_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"""

from django.core.exceptions import ImproperlyConfigured
from django.core.cache import cache
from django.conf import settings

from rest_framework import viewsets
from rest_framework.exceptions import ParseError
from rest_framework.renderers import (BrowsableAPIRenderer, JSONRenderer,
Expand All @@ -23,6 +26,7 @@
from onadata.libs.serializers.chart_serializer import (ChartSerializer,
FieldsChartSerializer)
from onadata.libs.utils.chart_tools import get_chart_data_for_field
from onadata.libs.utils.cache_tools import XFORM_CHARTS


def get_form_field_chart_url(url, field):
Expand Down Expand Up @@ -104,8 +108,16 @@ def retrieve(self, request, *args, **kwargs):
return Response(serializer.data)

if field_name or field_xpath:
data = get_chart_data_for_field(field_name, xform, fmt, group_by,
field_xpath)
cache_key = '{}{}{}{}{}{}'.format(XFORM_CHARTS, xform.pk,
field_xpath, field_name,
group_by, fmt)

data = cache.get(cache_key)
if not data:
data = get_chart_data_for_field(field_name, xform, fmt,
group_by, field_xpath)

cache.set(cache_key, data, settings.XFORM_CHARTS_CACHE_TIME)

return Response(data, template_name='chart_detail.html')

Expand Down
16 changes: 16 additions & 0 deletions onadata/libs/serializers/stats_serializer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.utils.translation import ugettext as _
from django.core.cache import cache
from django.conf import settings

from rest_framework import exceptions
from rest_framework import serializers
from rest_framework.utils.serializer_helpers import ReturnList
Expand All @@ -10,6 +13,9 @@
from onadata.apps.logger.models.xform import XForm
from onadata.libs.data.query import get_form_submissions_grouped_by_field

from onadata.libs.utils.cache_tools import XFORM_SUBMISSION_STAT


SELECT_FIELDS = ['select one', 'select multiple']

STATS_FUNCTIONS = {
Expand Down Expand Up @@ -43,6 +49,13 @@ def to_representation(self, obj):
raise exceptions.ParseError(_(u"Expecting `group` and `name`"
u" query parameters."))

cache_key = '{}{}{}{}'.format(XFORM_SUBMISSION_STAT, obj.pk,
field, name)

data = cache.get(cache_key)
if data:
return data

try:
data = get_form_submissions_grouped_by_field(
obj, field, name)
Expand All @@ -57,6 +70,9 @@ def to_representation(self, obj):
label = obj.get_choice_label(element, record[name])
record[name] = label

cache.set(cache_key, data,
settings.XFORM_SUBMISSION_STAT_CACHE_TIME)

return data

@property
Expand Down
2 changes: 2 additions & 0 deletions onadata/libs/utils/cache_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
# Cache names used in XForm Model
XFORM_SUBMISSION_COUNT_FOR_DAY = "xfm-get_submission_count-"
XFORM_SUBMISSION_COUNT_FOR_DAY_DATE = "xfm-get_submission_count_date-"
XFORM_SUBMISSION_STAT = "xfm-get_form_submissions_grouped_by_field-"
XFORM_CHARTS = "xfm-get_form_charts-"


def safe_delete(key):
Expand Down
5 changes: 5 additions & 0 deletions onadata/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,8 @@ def configure_logging(logger, **kwargs):

# Project & XForm Visibility Settings
ALLOW_PUBLIC_DATASETS = True

# Cache xform submission stat by 10 min 10 * 60
XFORM_SUBMISSION_STAT_CACHE_TIME = 600

XFORM_CHARTS_CACHE_TIME = 600
1 change: 1 addition & 0 deletions onadata/settings/github_actions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@

ODK_TOKEN_FERNET_KEY = 'ROsB4T8s1rCJskAdgpTQEKfH2x2K_EX_YBi3UFyoYng=' # nosec
OPENID_CONNECT_PROVIDERS = {}

0 comments on commit 07af4cd

Please sign in to comment.