Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #837 from glogiotatidis/978694
Browse files Browse the repository at this point in the history
[fix bug 978694] Count hits to API Resources.
  • Loading branch information
glogiotatidis committed Mar 4, 2014
2 parents 11cdf7a + dcb618f commit 56fceef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
23 changes: 21 additions & 2 deletions mozillians/api/resources.py
@@ -1,7 +1,7 @@
# Implement HTTP Caching
# code from http://django-tastypie.readthedocs.org/en/latest/caching.html
from django.utils.cache import patch_cache_control

from django_statsd.clients import statsd


class ClientCacheResourceMixIn(object):
"""
Expand All @@ -11,6 +11,7 @@ class ClientCacheResourceMixIn(object):
TODO: To be removed when we upgrade to django-tastypie >= 0.9.12.
Code from http://django-tastypie.readthedocs.org/en/latest/caching.html
"""

def create_response(self, request, data, **response_kwargs):
Expand Down Expand Up @@ -39,3 +40,21 @@ def apply_sorting(self, obj_list, options=None):
sort_list = self.Meta.default_order

return obj_list.order_by(*sort_list)


class GraphiteMixIn(object):
"""
MixIn to post to graphite server every hit of API resource.
"""

def wrap_view(self, view):
real_wrapper = super(GraphiteMixIn, self).wrap_view(view)

def wrapper(request, *args, **kwargs):
callback = getattr(self, view)
counter_name = 'api.resources.{klass}.{func}'.format(
klass=callback.im_class.__name__,
func=callback.im_func.__name__)
statsd.incr(counter_name)
return real_wrapper(request, *args, **kwargs)
return wrapper
5 changes: 3 additions & 2 deletions mozillians/groups/api.py
Expand Up @@ -9,13 +9,14 @@
from mozillians.api.authenticators import AppAuthentication
from mozillians.api.authorisers import MozillaOfficialAuthorization
from mozillians.api.resources import (AdvancedSortingResourceMixIn,
ClientCacheResourceMixIn)
ClientCacheResourceMixIn,
GraphiteMixIn)
from mozillians.api.paginator import Paginator
from mozillians.groups.models import Group, Skill


class GroupBaseResource(AdvancedSortingResourceMixIn, ClientCacheResourceMixIn,
ModelResource):
GraphiteMixIn, ModelResource):
number_of_members = fields.IntegerField(attribute='number_of_members',
readonly=True)

Expand Down
8 changes: 5 additions & 3 deletions mozillians/users/api.py
Expand Up @@ -19,7 +19,8 @@
from mozillians.api.authorisers import MozillaOfficialAuthorization
from mozillians.api.paginator import Paginator
from mozillians.api.resources import (AdvancedSortingResourceMixIn,
ClientCacheResourceMixIn)
ClientCacheResourceMixIn,
GraphiteMixIn)
from mozillians.users.models import COUNTRIES, UserProfile


Expand Down Expand Up @@ -74,7 +75,8 @@ def __getitem__(self, key):


class LocationCustomResource(AdvancedSortingResourceMixIn,
ClientCacheResourceMixIn, Resource):
ClientCacheResourceMixIn, GraphiteMixIn,
Resource):

class Meta:
authentication = AppAuthentication()
Expand Down Expand Up @@ -235,7 +237,7 @@ def dehydrate_url(self, bundle):
return utils.absolutify(url)


class UserResource(ClientCacheResourceMixIn, ModelResource):
class UserResource(ClientCacheResourceMixIn, GraphiteMixIn, ModelResource):
"""User Resource."""
email = fields.CharField(attribute='user__email', null=True, readonly=True)
username = fields.CharField(attribute='user__username', null=True, readonly=True)
Expand Down

0 comments on commit 56fceef

Please sign in to comment.