<commit_before>from numpy import uint16
from numpy import bool_


from query_result_list import QueryResultList
from data_record import DataRecord
from has_actions import HasActions


class Query(DataRecord, HasActions):
  def __init__(self, query_id, topic = None, user = None, condition = None, autocomplete = None, query_text = None):
    DataRecord.__init__( self, uint16(query_id) )
    self.topic = topic
    self.user = user
    self.condition = condition
    self.autocomplete = bool_(autocomplete)
    self.query_text = query_text
    self.result_list = QueryResultList(self)

  def add_to_result_list( self, rank, document ):
    self.result_list.add( rank, document )

  def results_up_to_rank( self, rank ):
    if int(rank) < 1 or int(rank) > self.result_list.length():
        raise RuntimeError("Attempted to fetch results up to rank %s for query %s, which is impossible." % (rank, self.record_id))
    return self.result_list.results_up_to_rank( rank )<commit_msg>Add formulation time calculation methods to query model class<commit_after>from numpy import uint16
from numpy import bool_


from query_result_list import QueryResultList
from data_record import DataRecord
from has_actions import HasActions
from session import Session
from filterable import Filterable


class Query(DataRecord, HasActions, Filterable):
  def __init__(self, query_id, topic = None, user = None, condition = None, autocomplete = None, query_text = None):
    DataRecord.__init__( self, uint16(query_id) )
    self.topic = topic
    self.user = user
    self.condition = condition
    self.autocomplete = bool_(autocomplete)
    self.query_text = query_text
    self.result_list = QueryResultList(self)

  def add_to_result_list( self, rank, document ):
    self.result_list.add( rank, document )

  def results_up_to_rank( self, rank ):
    if int(rank) < 1 or int(rank) > self.result_list.length():
        raise RuntimeError("Attempted to fetch results up to rank %s for query %s, which is impossible." % (rank, self.record_id))
    return self.result_list.results_up_to_rank( rank )

  def formulation_time_in_seconds(self):
    (idx, query_start_action) = self.actions_by_type( 'QUERY_FOCUS' )[0]
    return self.action_duration_in_seconds_for( idx, query_start_action, 'QUERY_ISSUED' )

  @classmethod
  def average_formulation_time_in_seconds(cls, filter_func = lambda query: True):
    queries = filter( filter_func, cls.get_store().values() )
    return reduce( lambda acc, query: acc + query.formulation_time_in_seconds(), queries, 0 ) / len(queries)
{'fraction_non_alphanumeric': 0.08685446009389672, 'fraction_numerical': 0.004694835680751174, 'mean_word_length': 4.644591611479028, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1281396', 'n_tokens_mistral': 793, 'n_tokens_neox': 766, 'n_words': 228}