from document import Document


class Action:

  DOCUMENT_EVENT_PARAMS = [ 'document_id2', 'document_id', 'document_id3', 'user_relevance_score', 'rank' ]
  QUERY_EVENT_PARAMS = [ 'query_id', 'query_text' ]

  PARAMS = {
    'QUERY_ISSUED': QUERY_EVENT_PARAMS,
    'VIEW_SEARCH_RESULTS_PAGE': [ 'result_page' ],
    'DOCUMENT_HOVER_IN': DOCUMENT_EVENT_PARAMS,
    'DOCUMENT_HOVER_OUT': DOCUMENT_EVENT_PARAMS,
    'DOC_MARKED_VIEWED': DOCUMENT_EVENT_PARAMS,
    'DOC_MARKED_RELEVANT': DOCUMENT_EVENT_PARAMS,
    'QUERY_FOCUS': [],
    'NEXT_QUERY_ISSUED': QUERY_EVENT_PARAMS,
    'NEXT_QUERY_FOCUS': [],
    'INTERACTION_COMPLETE': [],
    'EXPERIMENT_TIMEOUT': [],
    'SEARCH_TASK_COMPLETED': []
  }

  type_dict = {}

  @classmethod
  def index_by_type( cls, action ):
    if not action.action_type in cls.type_dict:
      cls.type_dict[ action.action_type ] = []
    cls.type_dict[ action.action_type ].append( action )

  @classmethod
  def by_type( cls, type ):
    return cls.type_dict.get( type )

  @classmethod
  def filter_by_type( cls, type, filter_func ):
    actions_by_type = cls.by_type( type )
    return filter( filter_func, actions_by_type )

  def __init__(self, timestamp, user, condition, topic, action_type, action_parameters):
    self.timestamp = timestamp
    self.user = user
    self.condition = condition
    self.topic = topic
    self.action_type = action_type
    self.bare_action_parameters = action_parameters
    self.__parse_action_parameters()
    self.__index()

  def __parse_action_parameters(self):
    params_list = str(self.bare_action_parameters).split(' ')
    params_order = Action.PARAMS[ self.action_type ]
    for param_name, param_value in zip(params_order, params_list):
      setattr(self, param_name, param_value)
    self.__init_action_parameter_objects()

  def __init_action_parameter_objects(self):
    if hasattr(self, 'document_id'):
      self.document = Document.create_or_update( self.document_id )

  def __index( self ):
    self.__class__.index_by_type( self )

@@ -40,16 +40,651 @@
 ction:%0A%0A
+  highly_relevant_filter = lambda action: action.document_is_highly_relevant()%0A  moderately_relevant_filter = lambda action: action.document_is_moderately_relevant()%0A  relevant_filter = lambda action: action.document_is_relevant()%0A%0A  no_delays_filter = lambda action: action.condition.record_id == 6%0A  query_delay_filter = lambda action: action.condition.record_id == 7%0A  document_delay_filter = lambda action: action.condition.record_id == 8%0A  combined_delay_filter = lambda action: action.condition.record_id == 9%0A%0A  @staticmethod%0A  def combine_filters( *filters ):%0A    return lambda action: all(%5Bfil( action ) for fil in filters%5D)%0A%0A
   DOCUME
@@ -783,16 +783,16 @@
 rank' %5D%0A
-
   QUERY_
@@ -1369,16 +1369,18 @@
 d%0A  def 
+__
 index_by
@@ -2633,16 +2633,18 @@
 class__.
+__
 index_by
@@ -2657,8 +2657,338 @@
  self )%0A
+%0A  def document_is_highly_relevant( self ):%0A    return self.document.is_highly_relevant_for_topic( self.topic )%0A%0A  def document_is_moderately_relevant( self ):%0A    return self.document.is_moderately_relevant_for_topic( self.topic )%0A%0A  def document_is_relevant( self ):%0A    return self.document.is_relevant_for_topic( self.topic )%0A

Move action filtering lambda functions to action class