<commit_before>import logging
from ckan.lib.base import BaseController, c, render
import dbutil

import urllib
from pprint import pprint
import logging
import ckan.logic as logic
import hashlib
import threading
from pylons import config

from webob.multidict import UnicodeMultiDict
from paste.util.multidict import MultiDict

from ckan.controllers.api import ApiController

log = logging.getLogger('ckanext.googleanalytics')

class GAController(BaseController):
    def view(self):
        # get package objects corresponding to popular GA content
        c.top_packages = dbutil.get_top_packages(limit=10)
        c.top_resources = dbutil.get_top_resources(limit=10)
        return render('summary.html')

class GAApiController(ApiController):
    # intercept API calls to record via google analytics
    def _post_analytics(self,user,request_obj_type,request_function,request_id):
        if (config.get('googleanalytics.id') != None):
            data = urllib.urlencode({
                "v":1,
                "tid":config.get('googleanalytics.id'),
                "cid":hashlib.md5(user).hexdigest(), #customer id should be obfuscated
                "t":"event",
                "dh":c.environ['HTTP_HOST'],
                "dp":c.environ['PATH_INFO'],
                "dr":c.environ.get('HTTP_REFERER',''),
                "ec":"CKAN API Request",
                "ea":request_obj_type+request_function,
                "el":request_id,
                })
            log.debug("Sending API event to Google Analytics: "+data)
            # send analytics asynchronously
            threading.Thread(target=urllib.urlopen,args=("http://www.google-analytics.com/collect", data)).start()


    def action(self, logic_function, ver=None):
        try:
                function = logic.get_action(logic_function)
        except Exception,e:
                    log.debug(e)
                    pass
        try:
           side_effect_free = getattr(function, 'side_effect_free', False)
           request_data = self._get_request_data(try_url_params=side_effect_free)
           if isinstance(request_data, dict):
                        id = request_data.get('id','')
                        if 'q' in request_data.keys():
                                id = request_data['q']
                        if 'query' in request_data.keys():
                                id = request_data['query']
                        self._post_analytics(c.user,logic_function,'', id)
        except Exception,e:
           print log.debug(e)
           pass

        return ApiController.action(self,logic_function, ver)

    def list(self, ver=None, register=None, subregister=None, id=None):
        self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"list",id)
        return ApiController.list(self,ver, register, subregister, id)
    def show(self, ver=None, register=None, subregister=None, id=None, id2=None):
        self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"show",id)
        return ApiController.show(self,ver, register, subregister, id,id2)
    def update(self, ver=None, register=None, subregister=None, id=None, id2=None):
        self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"update",id)
        return ApiController.update(self,ver, register, subregister, id,id2)
    def delete(self, ver=None, register=None, subregister=None, id=None, id2=None):
        self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"delete",id)
        return ApiController.delete(self,ver, register, subregister, id,id2)
    def search(self, ver=None, register=None):
        id = None
        try:
            params = MultiDict(self._get_search_params(request.params))
            if 'q' in params.keys():
                id = params['q']
            if 'query' in params.keys():
                id = params['query']
        except ValueError, e:
            print str(e)
            pass
        self._post_analytics(c.user,register,"search",id)<commit_msg>PEP8 fixes for Google Analytics Event Tracking for CKAN API<commit_after>import logging
from ckan.lib.base import BaseController, c, render, request
import dbutil

import urllib

import logging
import ckan.logic as logic
import hashlib
import threading
from pylons import config

from webob.multidict import UnicodeMultiDict
from paste.util.multidict import MultiDict

from ckan.controllers.api import ApiController

log = logging.getLogger('ckanext.googleanalytics')


class GAController(BaseController):
    def view(self):
        # get package objects corresponding to popular GA content
        c.top_packages = dbutil.get_top_packages(limit=10)
        c.top_resources = dbutil.get_top_resources(limit=10)
        return render('summary.html')


class GAApiController(ApiController):
    # intercept API calls to record via google analytics
    def _post_analytics(
            self, user, request_obj_type, request_function, request_id):
        if config.get('googleanalytics.id'):
            data_dict = {
                "v": 1,
                "tid": config.get('googleanalytics.id'),
                "cid": hashlib.md5(user).hexdigest(),
                # customer id should be obfuscated
                "t": "event",
                "dh": c.environ['HTTP_HOST'],
                "dp": c.environ['PATH_INFO'],
                "dr": c.environ.get('HTTP_REFERER', ''),
                "ec": "CKAN API Request",
                "ea": request_obj_type+request_function,
                "el": request_id,
            }
            data = urllib.urlencode(data_dict)
            log.debug("Sending API event to Google Analytics: "+data)
            # send analytics asynchronously
            threading.Thread(target=urllib.urlopen,
                             args=(
                                 "http://www.google-analytics.com/collect",
                                 data)).start()

    def action(self, logic_function, ver=None):
        try:
            function = logic.get_action(logic_function)
            side_effect_free = getattr(function, 'side_effect_free', False)
            request_data = self._get_request_data(
                try_url_params=side_effect_free)
            if isinstance(request_data, dict):
                id = request_data.get('id', '')
                if 'q' in request_data:
                    id = request_data['q']
                if 'query' in request_data:
                    id = request_data['query']
                self._post_analytics(c.user, logic_function, '', id)
        except Exception, e:
            log.debug(e)
            pass

        return ApiController.action(self, logic_function, ver)

    def list(self, ver=None, register=None,
             subregister=None, id=None):
        self._post_analytics(c.user,
                             register +
                             ("_"+str(subregister) if subregister else ""),
                             "list",
                             id)
        return ApiController.list(self, ver, register, subregister, id)

    def show(self, ver=None, register=None,
             subregister=None, id=None, id2=None):
        self._post_analytics(c.user,
                             register +
                             ("_"+str(subregister) if subregister else ""),
                             "show",
                             id)
        return ApiController.show(self, ver, register, subregister, id, id2)

    def update(self, ver=None, register=None,
               subregister=None, id=None, id2=None):
        self._post_analytics(c.user,
                             register +
                             ("_"+str(subregister) if subregister else ""),
                             "update",
                             id)
        return ApiController.update(self, ver, register, subregister, id, id2)

    def delete(self, ver=None, register=None,
               subregister=None, id=None, id2=None):
        self._post_analytics(c.user,
                             register +
                             ("_"+str(subregister) if subregister else ""),
                             "delete",
                             id)
        return ApiController.delete(self, ver, register, subregister, id, id2)

    def search(self, ver=None, register=None):
        id = None
        try:
            params = MultiDict(self._get_search_params(request.params))
            if 'q' in params.keys():
                id = params['q']
            if 'query' in params.keys():
                id = params['query']
        except ValueError, e:
            log.debug(str(e))
            pass
        self._post_analytics(c.user, register, "search", id)
{'fraction_non_alphanumeric': 0.10583276372750057, 'fraction_numerical': 0.0028480291638186377, 'mean_word_length': 2.033517622667588, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5756380', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2399, 'n_words': 537}