<s> from biicode . common . utils . bii_logging import logger <EOL> from biicode . server . rest . bottle_plugins . util import get_user_ip <EOL> from biicode . server . background . model . user_trace import UserTracedAction <EOL> from biicode . server . conf import ( BII_MIXPANEL_TOKEN , BII_API_MIXPANEL_EVENT_NAME , <EOL> BII_GA_API_KEY , BII_AES_SECRET_KEY ) <EOL> from bottle import request <EOL> from biicode . server . utils . encryption import AESEncryption <EOL> from biicode . common . model . brl . brl_block import BRLBlock <EOL> from biicode . common . model . symbolic . block_version import BlockVersion <EOL> from biicode . common . publish . publish_request import PublishRequest <EOL> from biicode . server . background . enqueuer import ( register_publish , <EOL> register_get_version_delta_info , <EOL> register_user_action ) <EOL> class BiiUserTraceBottlePlugin ( object ) : <EOL> '''<STR_LIT>''' <EOL> api = <NUM_LIT:2> <EOL> async_process = True <EOL> def apply ( self , callback , context ) : <EOL> '''<STR_LIT>''' <EOL> def wrapper ( * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> action_name = str ( callback . __name__ ) <EOL> description = "<STR_LIT>" <EOL> if action_name != "<STR_LIT>" : <EOL> client_user = request . headers . get ( '<STR_LIT>' , None ) <EOL> auth_user = kwargs . get ( "<STR_LIT>" , None ) <EOL> client_token = request . headers . get ( '<STR_LIT>' , None ) <EOL> _trace_mixpanel ( action_name , auth_user , client_user , client_token , kwargs ) <EOL> description = _get_action_label ( action_name , kwargs ) <EOL> _trace_user_action ( action_name , auth_user , client_user , client_token , description , <EOL> self . async_process ) <EOL> _trace_ga ( action_name , auth_user , client_user , client_token , description ) <EOL> rv = callback ( * args , ** kwargs ) <EOL> if action_name != "<STR_LIT>" : <EOL> _trace_achievement_action ( action_name , description , auth_user or client_user , <EOL> self . async_process ) <EOL> return rv <EOL> return wrapper <EOL> def _trace_achievement_action ( action_name , description , username , async_process ) : <EOL> try : <EOL> if username : <EOL> if action_name == "<STR_LIT>" : <EOL> register_publish ( username , BlockVersion . loads ( description ) , <EOL> async_process = async_process ) <EOL> elif action_name == "<STR_LIT>" : <EOL> register_get_version_delta_info ( username , async_process = async_process ) <EOL> except Exception as e : <EOL> logger . warning ( "<STR_LIT>" % e ) <EOL> def _trace_user_action ( action_name , auth_user , client_user , client_token , description , async_process ) : <EOL> ip_address = get_user_ip ( ) <EOL> try : <EOL> auth_user = auth_user or client_user or "<STR_LIT>" <EOL> action = UserTracedAction ( "<STR_LIT>" , action_name , ip_address , auth_user , <EOL> anonymous_user_token = client_token , <EOL> description = description ) <EOL> register_user_action ( action , async_process = async_process ) <EOL> except Exception as e : <EOL> logger . warning ( "<STR_LIT>" % e ) <EOL> def _trace_mixpanel ( action_name , auth_user , client_user , client_token , kwargs ) : <EOL> from mixpanel import Mixpanel <EOL> from mixpanel_async import AsyncBufferedConsumer <EOL> some_user_id = auth_user or client_user or client_token <EOL> try : <EOL> if action_name in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> mp = Mixpanel ( BII_MIXPANEL_TOKEN , consumer = AsyncBufferedConsumer ( ) ) <EOL> properties = { '<STR_LIT:action>' : action_name , '<STR_LIT>' : ( some_user_id == client_token ) } <EOL> if action_name == "<STR_LIT>" : <EOL> properties [ "<STR_LIT>" ] = kwargs [ "<STR_LIT>" ] [ "<STR_LIT:data>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] <EOL> properties [ "<STR_LIT>" ] = kwargs [ "<STR_LIT>" ] [ "<STR_LIT:data>" ] [ <NUM_LIT:1> ] <EOL> mp . track ( some_user_id , BII_API_MIXPANEL_EVENT_NAME , properties ) <EOL> except Exception as e : <EOL> logger . warning ( "<STR_LIT>" % e ) <EOL> def _trace_ga ( action_name , auth_user , client_user , client_token , label ) : <EOL> from google_measurement_protocol import Event , report <EOL> try : <EOL> username = auth_user or client_user or client_token <EOL> logger . debug ( "<STR_LIT>" % ( auth_user , client_user , client_token ) ) <EOL> logger . debug ( "<STR_LIT>" % ( action_name , username , label ) ) <EOL> aes_manager = AESEncryption ( BII_AES_SECRET_KEY ) <EOL> client_id = aes_manager . encrypt ( username ) <EOL> logger . debug ( "<STR_LIT>" % str ( client_id ) ) <EOL> event = Event ( '<STR_LIT>' , action_name , label ) <EOL> ret = report ( BII_GA_API_KEY , client_id , event ) <EOL> if ret [ <NUM_LIT:0> ] . status_code == <NUM_LIT:200> : <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( action_name , username , client_id ) ) <EOL> else : <EOL> logger . debug ( "<STR_LIT>" % ( ret [ <NUM_LIT:0> ] . reason ) ) <EOL> except Exception as exc : <EOL> logger . warning ( "<STR_LIT>" % ( exc ) ) <EOL> def _get_action_label ( action_name , kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> block_version_from_kwargs = lambda kwargs : str ( BlockVersion ( <EOL> BRLBlock ( "<STR_LIT>" % ( kwargs [ "<STR_LIT>" ] , <EOL> kwargs [ "<STR_LIT>" ] , <EOL> kwargs [ "<STR_LIT>" ] ) ) , <EOL> kwargs [ "<STR_LIT:version>" ] ) ) <EOL> if action_name == "<STR_LIT>" : <EOL> return str ( BRLBlock ( "<STR_LIT>" % ( kwargs [ "<STR_LIT>" ] , <EOL> kwargs [ "<STR_LIT>" ] , <EOL> kwargs [ "<STR_LIT>" ] ) ) ) <EOL> elif action_name == "<STR_LIT>" : <EOL> return block_version_from_kwargs ( kwargs ) <EOL> elif action_name == "<STR_LIT>" : <EOL> return str ( BlockVersion . deserialize ( kwargs [ "<STR_LIT>" ] [ "<STR_LIT:data>" ] ) ) <EOL> elif action_name == "<STR_LIT>" : <EOL> return block_version_from_kwargs ( kwargs ) <EOL> elif action_name == "<STR_LIT>" : <EOL> return "<STR_LIT:U+002C>" . join ( [ str ( BlockVersion . deserialize ( elem [ <NUM_LIT:0> ] ) ) <EOL> for elem in kwargs [ "<STR_LIT>" ] [ "<STR_LIT:data>" ] ] ) <EOL> elif action_name == "<STR_LIT>" : <EOL> return str ( BlockVersion . deserialize ( kwargs [ "<STR_LIT>" ] [ "<STR_LIT:data>" ] <EOL> [ PublishRequest . SERIAL_TRACKED_KEY ] ) ) <EOL> elif action_name == "<STR_LIT>" : <EOL> return "<STR_LIT:U+002CU+0020>" . join ( [ str ( BlockVersion . deserialize ( elem ) ) <EOL> for elem in kwargs [ "<STR_LIT>" ] [ "<STR_LIT:data>" ] ] ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> except Exception as e : <EOL> logger . error ( "<STR_LIT>" % str ( e ) ) <EOL> return "<STR_LIT>" </s>
