<s> """<STR_LIT>""" <EOL> import numpy <EOL> import numpy . random <EOL> import json <EOL> import time <EOL> import traceback <EOL> from next . resource_client . ResourceClient import ResourceClient <EOL> import next . utils as utils <EOL> from next . apps . AppPrototype import AppPrototype <EOL> from next . apps . PoolBasedTripletMDS . dashboard . Dashboard import PoolBasedTripletMDSDashboard <EOL> class PoolBasedTripletMDS ( AppPrototype ) : <EOL> def __init__ ( self ) : <EOL> self . app_id = '<STR_LIT>' <EOL> def daemonProcess ( self , exp_uid , args_json , db , ell ) : <EOL> try : <EOL> app_id = self . app_id <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : args_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> try : <EOL> args_dict = json . loads ( args_json ) <EOL> except : <EOL> error = "<STR_LIT>" % self . app_id <EOL> return '<STR_LIT:{}>' , False , error <EOL> necessary_fields = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for field in necessary_fields : <EOL> try : <EOL> args_dict [ field ] <EOL> except KeyError : <EOL> error = "<STR_LIT>" % ( self . app_id , str ( field ) ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> alg_daemon_args = args_dict [ '<STR_LIT>' ] <EOL> alg_uid = args_dict [ '<STR_LIT>' ] <EOL> alg_id , didSucceed , message = db . get ( app_id + '<STR_LIT>' , alg_uid , '<STR_LIT>' ) <EOL> rc = ResourceClient ( app_id , exp_uid , alg_uid , db ) <EOL> alg = utils . get_app_alg ( self . app_id , alg_id ) <EOL> didSucceed , dt = utils . timeit ( alg . daemonProcess ) ( resource = rc , daemon_args_dict = alg_daemon_args ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : alg_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : dt , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> log_entry_durations = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : alg_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : dt } <EOL> log_entry_durations . update ( rc . getDurations ( ) ) <EOL> meta = { '<STR_LIT>' : log_entry_durations } <EOL> daemon_message = { } <EOL> args_out = { '<STR_LIT:args>' : daemon_message , '<STR_LIT>' : meta } <EOL> response_json = json . dumps ( args_out ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : response_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return response_json , True , '<STR_LIT>' <EOL> except Exception , err : <EOL> error = traceback . format_exc ( ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:error>' : error , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> def initExp ( self , exp_uid , args_json , db , ell ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> app_id = self . app_id <EOL> didSucceed , message = db . delete_docs_with_filter ( '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = db . delete_docs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = db . delete_docs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = db . delete_docs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = db . delete_docs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = ell . delete_logs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = ell . delete_logs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = ell . delete_logs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = ell . delete_logs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = ell . delete_logs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : exp_uid } ) <EOL> didSucceed , message = db . ensure_index ( '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = db . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> didSucceed , message = ell . ensure_index ( app_id + '<STR_LIT>' , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> import next . constants <EOL> git_hash = next . constants . GIT_HASH <EOL> db . set ( '<STR_LIT>' , exp_uid , '<STR_LIT>' , exp_uid ) <EOL> db . set ( '<STR_LIT>' , exp_uid , '<STR_LIT>' , app_id ) <EOL> db . set ( '<STR_LIT>' , exp_uid , '<STR_LIT>' , utils . datetime2str ( utils . datetimeNow ( ) ) ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : args_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> try : <EOL> args_dict = json . loads ( args_json ) <EOL> except : <EOL> error = "<STR_LIT>" % self . app_id <EOL> return '<STR_LIT:{}>' , False , error <EOL> necessary_fields = [ '<STR_LIT:n>' , '<STR_LIT:d>' , '<STR_LIT>' ] <EOL> for field in necessary_fields : <EOL> try : <EOL> args_dict [ field ] <EOL> except KeyError : <EOL> error = "<STR_LIT>" % ( self . app_id , str ( field ) ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> n = args_dict [ '<STR_LIT:n>' ] <EOL> d = args_dict [ '<STR_LIT:d>' ] <EOL> delta = args_dict [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in args_dict : <EOL> alg_list = args_dict [ '<STR_LIT>' ] <EOL> supportedAlgs = utils . get_app_supported_algs ( self . app_id ) <EOL> for algorithm in alg_list : <EOL> if algorithm [ '<STR_LIT>' ] not in supportedAlgs : <EOL> error = "<STR_LIT>" % ( self . app_id , algorithm [ '<STR_LIT>' ] ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> else : <EOL> alg_list = utils . get_app_default_alg_list ( self . app_id ) <EOL> if '<STR_LIT>' not in args_dict : <EOL> instructions = utils . get_app_default_instructions ( app_id ) <EOL> else : <EOL> instructions = args_dict [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' not in args_dict : <EOL> debrief = utils . get_app_default_instructions ( app_id ) <EOL> else : <EOL> debrief = args_dict [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' not in args_dict : <EOL> num_tries = utils . get_app_default_num_tries ( app_id ) <EOL> else : <EOL> num_tries = args_dict [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' not in args_dict : <EOL> params = { } <EOL> params [ '<STR_LIT>' ] = [ ] <EOL> for algorithm in alg_list : <EOL> params [ '<STR_LIT>' ] . append ( { '<STR_LIT>' : algorithm [ '<STR_LIT>' ] , '<STR_LIT>' : <NUM_LIT:1.> / len ( alg_list ) } ) <EOL> algorithm_management_settings = { } <EOL> algorithm_management_settings [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> algorithm_management_settings [ '<STR_LIT>' ] = params <EOL> else : <EOL> algorithm_management_settings = args_dict [ '<STR_LIT>' ] <EOL> try : <EOL> mode = algorithm_management_settings [ '<STR_LIT>' ] <EOL> params = algorithm_management_settings [ '<STR_LIT>' ] <EOL> except : <EOL> error = "<STR_LIT>" % ( self . app_id ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> if mode == '<STR_LIT>' : <EOL> try : <EOL> algorithm_proportions_list = params [ '<STR_LIT>' ] <EOL> except : <EOL> error = "<STR_LIT>" % ( self . app_id ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> for proportion_item in algorithm_proportions_list : <EOL> proportion = proportion_item [ '<STR_LIT>' ] <EOL> target_alg_label = proportion_item [ '<STR_LIT>' ] <EOL> target_alg_label_in_alg_list = False <EOL> for algorithm in alg_list : <EOL> if algorithm [ '<STR_LIT>' ] == target_alg_label : <EOL> target_alg_label_in_alg_list = True <EOL> if not target_alg_label_in_alg_list : <EOL> error = "<STR_LIT>" % ( self . app_id , target_alg_label ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> elif mode == '<STR_LIT>' : <EOL> error = "<STR_LIT>" % ( self . app_id , mode ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> elif mode == '<STR_LIT>' : <EOL> error = "<STR_LIT>" % ( self . app_id , mode ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> else : <EOL> error = "<STR_LIT>" % ( self . app_id , algorithm_management_mode ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> if '<STR_LIT>' not in args_dict : <EOL> participant_to_algorithm_management = '<STR_LIT>' <EOL> else : <EOL> participant_to_algorithm_management = args_dict [ '<STR_LIT>' ] <EOL> if participant_to_algorithm_management not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> error = "<STR_LIT>" % ( self . app_id , participant_to_algorithm_management ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> for algorithm in alg_list : <EOL> alg_id = algorithm [ '<STR_LIT>' ] <EOL> alg_uid = utils . getNewUID ( ) <EOL> algorithm [ '<STR_LIT>' ] = alg_uid <EOL> db . set ( app_id + '<STR_LIT>' , alg_uid , '<STR_LIT>' , alg_id ) <EOL> db . set ( app_id + '<STR_LIT>' , alg_uid , '<STR_LIT>' , alg_uid ) <EOL> db . set ( app_id + '<STR_LIT>' , alg_uid , '<STR_LIT>' , exp_uid ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , exp_uid ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , app_id ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT:n>' , n ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT:d>' , d ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , delta ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , alg_list ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , algorithm_management_settings ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , participant_to_algorithm_management ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , instructions ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , debrief ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , num_tries ) <EOL> db . set ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' , git_hash ) <EOL> for algorithm in alg_list : <EOL> alg_id = algorithm [ '<STR_LIT>' ] <EOL> alg_uid = algorithm [ '<STR_LIT>' ] <EOL> params = algorithm . get ( '<STR_LIT>' , None ) <EOL> rc = ResourceClient ( app_id , exp_uid , alg_uid , db ) <EOL> alg = utils . get_app_alg ( self . app_id , alg_id ) <EOL> didSucceed , dt = utils . timeit ( alg . initExp ) ( resource = rc , n = n , d = d , failure_probability = delta , params = params ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : alg_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : dt , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> response_json = '<STR_LIT:{}>' <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : response_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return response_json , True , '<STR_LIT>' <EOL> except Exception , err : <EOL> error = traceback . format_exc ( ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:error>' : error , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> print log_entry <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> def getQuery ( self , exp_uid , args_json , db , ell ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> app_id = self . app_id <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : args_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> try : <EOL> args_dict = json . loads ( args_json ) <EOL> except : <EOL> error = "<STR_LIT>" % self . app_id <EOL> return '<STR_LIT:{}>' , False , error <EOL> alg_list , didSucceed , message = db . get ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' ) <EOL> alg_label_to_alg_id = { } <EOL> alg_label_to_alg_uid = { } <EOL> for algorithm in alg_list : <EOL> alg_label_to_alg_id [ algorithm [ '<STR_LIT>' ] ] = algorithm [ '<STR_LIT>' ] <EOL> alg_label_to_alg_uid [ algorithm [ '<STR_LIT>' ] ] = algorithm [ '<STR_LIT>' ] <EOL> algorithm_management_settings , didSucceed , message = db . get ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' ) <EOL> if '<STR_LIT>' in args_dict : <EOL> participant_uid = args_dict [ '<STR_LIT>' ] <EOL> else : <EOL> participant_uid = exp_uid <EOL> participant_doc_exists , didSucceed , message = db . exists ( app_id + '<STR_LIT>' , participant_uid , '<STR_LIT>' ) <EOL> first_participant_query = not participant_doc_exists <EOL> if first_participant_query : <EOL> db . set ( app_id + '<STR_LIT>' , participant_uid , '<STR_LIT>' , participant_uid ) <EOL> db . set ( app_id + '<STR_LIT>' , participant_uid , '<STR_LIT>' , exp_uid ) <EOL> participant_to_algorithm_management , didSucceed , message = db . get ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' ) <EOL> if ( participant_uid == exp_uid ) or ( participant_to_algorithm_management == '<STR_LIT>' ) or ( first_participant_query ) : <EOL> if algorithm_management_settings [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> proportions_list = algorithm_management_settings [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> prop = [ prop_item [ '<STR_LIT>' ] for prop_item in proportions_list ] <EOL> prop_item = numpy . random . choice ( alg_list , p = prop ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + algorithm_management_settings [ '<STR_LIT>' ] + '<STR_LIT>' ) <EOL> alg_id = alg_label_to_alg_id [ prop_item [ '<STR_LIT>' ] ] <EOL> alg_uid = alg_label_to_alg_uid [ prop_item [ '<STR_LIT>' ] ] <EOL> alg_label = prop_item [ '<STR_LIT>' ] <EOL> if ( first_participant_query ) and ( participant_to_algorithm_management == '<STR_LIT>' ) : <EOL> db . set ( app_id + '<STR_LIT>' , participant_uid , '<STR_LIT>' , alg_id ) <EOL> db . set ( app_id + '<STR_LIT>' , participant_uid , '<STR_LIT>' , alg_uid ) <EOL> elif ( participant_to_algorithm_management == '<STR_LIT>' ) : <EOL> alg_id , didSucceed , message = db . get ( app_id + '<STR_LIT>' , participant_uid , '<STR_LIT>' ) <EOL> alg_uid , didSucceed , message = db . get ( app_id + '<STR_LIT>' , participant_uid , '<STR_LIT>' ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + participant_to_algorithm_management + '<STR_LIT>' ) <EOL> rc = ResourceClient ( app_id , exp_uid , alg_uid , db ) <EOL> alg = utils . get_app_alg ( self . app_id , alg_id ) <EOL> index_center , index_left , index_right , dt = utils . timeit ( alg . getQuery ) ( resource = rc ) <EOL> log_entry_durations = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : alg_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : dt } <EOL> log_entry_durations . update ( rc . getDurations ( ) ) <EOL> meta = { '<STR_LIT>' : log_entry_durations } <EOL> timestamp = str ( utils . datetimeNow ( ) ) <EOL> query_uid = utils . getNewUID ( ) <EOL> query = { } <EOL> query [ '<STR_LIT>' ] = query_uid <EOL> query [ '<STR_LIT>' ] = [ { '<STR_LIT:index>' : index_center , '<STR_LIT:label>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } , { '<STR_LIT:index>' : index_left , '<STR_LIT:label>' : '<STR_LIT:left>' , '<STR_LIT>' : <NUM_LIT:0> } , { '<STR_LIT:index>' : index_right , '<STR_LIT:label>' : '<STR_LIT:right>' , '<STR_LIT>' : <NUM_LIT:0> } ] <EOL> query_doc = { } <EOL> query_doc . update ( query ) <EOL> query_doc [ '<STR_LIT>' ] = participant_uid <EOL> query_doc [ '<STR_LIT>' ] = alg_uid <EOL> query_doc [ '<STR_LIT>' ] = exp_uid <EOL> query_doc [ '<STR_LIT>' ] = alg_label <EOL> query_doc [ '<STR_LIT>' ] = timestamp <EOL> for field in query_doc : <EOL> db . set ( app_id + '<STR_LIT>' , query_uid , field , query_doc [ field ] ) <EOL> args_out = { '<STR_LIT:args>' : query , '<STR_LIT>' : meta } <EOL> response_json = json . dumps ( args_out ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : response_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return response_json , True , '<STR_LIT>' <EOL> except Exception , err : <EOL> error = traceback . format_exc ( ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:error>' : error , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> def processAnswer ( self , exp_uid , args_json , db , ell ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> app_id = self . app_id <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : args_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> try : <EOL> args_dict = json . loads ( args_json ) <EOL> except : <EOL> error = "<STR_LIT>" % self . app_id <EOL> return '<STR_LIT:{}>' , False , error <EOL> necessary_fields = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for field in necessary_fields : <EOL> try : <EOL> args_dict [ field ] <EOL> except KeyError : <EOL> error = "<STR_LIT>" % ( self . app_id , str ( field ) ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> alg_list , didSucceed , message = db . get ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' ) <EOL> query_uid = args_dict [ '<STR_LIT>' ] <EOL> alg_uid , didSucceed , message = db . get ( app_id + '<STR_LIT>' , query_uid , '<STR_LIT>' ) <EOL> if not didSucceed : <EOL> raise Exception ( "<STR_LIT>" + query_uid ) <EOL> for algorithm in alg_list : <EOL> if alg_uid == algorithm [ '<STR_LIT>' ] : <EOL> alg_id = algorithm [ '<STR_LIT>' ] <EOL> alg_label = algorithm [ '<STR_LIT>' ] <EOL> test_alg_label = algorithm [ '<STR_LIT>' ] <EOL> num_reported_answers , didSucceed , message = db . increment ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' + alg_uid ) <EOL> rc = ResourceClient ( app_id , exp_uid , alg_uid , db ) <EOL> alg = utils . get_app_alg ( self . app_id , alg_id ) <EOL> targets , didSucceed , message = db . get ( app_id + '<STR_LIT>' , query_uid , '<STR_LIT>' ) <EOL> for target in targets : <EOL> if target [ '<STR_LIT:label>' ] == '<STR_LIT>' : <EOL> index_center = target [ '<STR_LIT:index>' ] <EOL> elif target [ '<STR_LIT:label>' ] == '<STR_LIT:left>' : <EOL> index_left = target [ '<STR_LIT:index>' ] <EOL> elif target [ '<STR_LIT:label>' ] == '<STR_LIT:right>' : <EOL> index_right = target [ '<STR_LIT:index>' ] <EOL> index_winner = args_dict [ '<STR_LIT>' ] <EOL> timestamp_query_generated , didSucceed , message = db . get ( app_id + '<STR_LIT>' , query_uid , '<STR_LIT>' ) <EOL> datetime_query_generated = utils . str2datetime ( timestamp_query_generated ) <EOL> timestamp_answer_received = args_dict . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , None ) <EOL> if timestamp_answer_received == None : <EOL> datetime_answer_received = datetime_query_generated <EOL> else : <EOL> datetime_answer_received = utils . str2datetime ( timestamp_answer_received ) <EOL> delta_datetime = datetime_answer_received - datetime_query_generated <EOL> round_trip_time = delta_datetime . seconds + delta_datetime . microseconds / <NUM_LIT> <EOL> response_time = float ( args_dict . get ( '<STR_LIT>' , <NUM_LIT:0.> ) ) <EOL> db . set ( app_id + '<STR_LIT>' , query_uid , '<STR_LIT>' , response_time ) <EOL> db . set ( app_id + '<STR_LIT>' , query_uid , '<STR_LIT>' , round_trip_time - response_time ) <EOL> db . set ( app_id + '<STR_LIT>' , query_uid , '<STR_LIT>' , index_winner ) <EOL> q = [ index_left , index_right , index_center ] <EOL> if index_winner == index_right : <EOL> q = [ index_right , index_left , index_center ] <EOL> db . set ( app_id + '<STR_LIT>' , query_uid , '<STR_LIT:q>' , q ) <EOL> didSucceed , dt = utils . timeit ( alg . processAnswer ) ( resource = rc , index_center = index_center , index_left = index_left , index_right = index_right , index_winner = index_winner ) <EOL> log_entry_durations = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : alg_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : dt } <EOL> log_entry_durations . update ( rc . getDurations ( ) ) <EOL> meta = { '<STR_LIT>' : log_entry_durations } <EOL> n , didSucceed , message = db . get ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT:n>' ) <EOL> if num_reported_answers % ( ( n + <NUM_LIT:4> ) / <NUM_LIT:4> ) == <NUM_LIT:0> : <EOL> predict_id = '<STR_LIT>' <EOL> params = { '<STR_LIT>' : alg_label } <EOL> predict_args_dict = { '<STR_LIT>' : predict_id , '<STR_LIT>' : params } <EOL> predict_args_json = json . dumps ( predict_args_dict ) <EOL> db . submit_job ( app_id , exp_uid , '<STR_LIT>' , predict_args_json , ignore_result = True ) <EOL> response_args_dict = { } <EOL> args_out = { '<STR_LIT:args>' : response_args_dict , '<STR_LIT>' : meta } <EOL> response_json = json . dumps ( args_out ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : response_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return response_json , True , "<STR_LIT>" <EOL> except Exception , err : <EOL> error = traceback . format_exc ( ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:error>' : error , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> def predict ( self , exp_uid , args_json , db , ell ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> app_id = self . app_id <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : args_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> try : <EOL> args_dict = json . loads ( args_json ) <EOL> except : <EOL> error = "<STR_LIT>" % self . app_id <EOL> return '<STR_LIT:{}>' , False , error <EOL> necessary_fields = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for field in necessary_fields : <EOL> try : <EOL> args_dict [ field ] <EOL> except KeyError : <EOL> error = "<STR_LIT>" % ( self . app_id , str ( field ) ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> predict_id = args_dict [ '<STR_LIT>' ] <EOL> params = args_dict [ '<STR_LIT>' ] <EOL> alg_label = params [ '<STR_LIT>' ] <EOL> alg_list , didSucceed , message = db . get ( app_id + '<STR_LIT>' , exp_uid , '<STR_LIT>' ) <EOL> for algorithm in alg_list : <EOL> if alg_label == algorithm [ '<STR_LIT>' ] : <EOL> alg_id = algorithm [ '<STR_LIT>' ] <EOL> alg_uid = algorithm [ '<STR_LIT>' ] <EOL> meta = { } <EOL> if predict_id == '<STR_LIT>' : <EOL> rc = ResourceClient ( app_id , exp_uid , alg_uid , db ) <EOL> alg = utils . get_app_alg ( self . app_id , alg_id ) <EOL> Xd , num_reported_answers , dt = utils . timeit ( alg . predict ) ( rc ) <EOL> log_entry_durations = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : alg_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : dt } <EOL> log_entry_durations . update ( rc . getDurations ( ) ) <EOL> meta = { '<STR_LIT>' : log_entry_durations } <EOL> params [ '<STR_LIT>' ] = Xd <EOL> params [ '<STR_LIT>' ] = num_reported_answers <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : alg_uid , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> log_entry . update ( params ) <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> params [ '<STR_LIT>' ] = str ( log_entry [ '<STR_LIT>' ] ) <EOL> response_args_dict = params <EOL> elif predict_id == '<STR_LIT>' : <EOL> queries , didSucceed , message = db . get_docs_with_filter ( app_id + '<STR_LIT>' , { '<STR_LIT>' : alg_uid } ) <EOL> S = [ ] <EOL> for query in queries : <EOL> if '<STR_LIT:q>' in query . keys ( ) : <EOL> q = query [ '<STR_LIT:q>' ] <EOL> S . append ( q ) <EOL> params [ '<STR_LIT>' ] = S <EOL> params [ '<STR_LIT>' ] = len ( S ) <EOL> response_args_dict = params <EOL> args_out = { '<STR_LIT:args>' : response_args_dict , '<STR_LIT>' : meta } <EOL> predict_json = json . dumps ( args_out ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : predict_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return predict_json , True , '<STR_LIT>' <EOL> except Exception , err : <EOL> error = traceback . format_exc ( ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:error>' : str ( error ) , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> didSucceed , message = ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> def getStats ( self , exp_uid , args_json , db , ell ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> app_id = self . app_id <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : args_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> try : <EOL> args_dict = json . loads ( args_json ) <EOL> except : <EOL> error = "<STR_LIT>" % self . app_id <EOL> return '<STR_LIT:{}>' , False , error <EOL> necessary_fields = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for field in necessary_fields : <EOL> try : <EOL> args_dict [ field ] <EOL> except KeyError : <EOL> error = "<STR_LIT>" % ( self . app_id , str ( field ) ) <EOL> return '<STR_LIT:{}>' , False , error <EOL> stat_id = args_dict [ '<STR_LIT>' ] <EOL> params = args_dict [ '<STR_LIT>' ] <EOL> dashboard = PoolBasedTripletMDSDashboard ( db , ell ) <EOL> if stat_id == "<STR_LIT>" : <EOL> task = params [ '<STR_LIT>' ] <EOL> activity_stats = dashboard . api_activity_histogram ( self . app_id , exp_uid , task ) <EOL> stats = activity_stats <EOL> elif stat_id == "<STR_LIT>" : <EOL> activity_stats = dashboard . api_processAnswer_activity_stacked_histogram ( self . app_id , exp_uid ) <EOL> stats = activity_stats <EOL> elif stat_id == "<STR_LIT>" : <EOL> task = params [ '<STR_LIT>' ] <EOL> compute_stats = dashboard . compute_duration_multiline_plot ( self . app_id , exp_uid , task ) <EOL> stats = compute_stats <EOL> elif stat_id == "<STR_LIT>" : <EOL> task = params [ '<STR_LIT>' ] <EOL> alg_label = params [ '<STR_LIT>' ] <EOL> compute_detailed_stats = dashboard . compute_duration_detailed_stacked_area_plot ( self . app_id , exp_uid , task , alg_label ) <EOL> stats = compute_detailed_stats <EOL> elif stat_id == "<STR_LIT>" : <EOL> alg_label = params [ '<STR_LIT>' ] <EOL> response_time_stats = dashboard . response_time_histogram ( self . app_id , exp_uid , alg_label ) <EOL> stats = response_time_stats <EOL> elif stat_id == "<STR_LIT>" : <EOL> alg_label = params [ '<STR_LIT>' ] <EOL> network_delay_stats = dashboard . network_delay_histogram ( self . app_id , exp_uid , alg_label ) <EOL> stats = network_delay_stats <EOL> elif stat_id == "<STR_LIT>" : <EOL> stats = dashboard . test_error_multiline_plot ( self . app_id , exp_uid ) <EOL> elif stat_id == "<STR_LIT>" : <EOL> alg_label = params [ '<STR_LIT>' ] <EOL> stats = dashboard . most_current_embedding ( self . app_id , exp_uid , alg_label ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> response_json = json . dumps ( stats ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : response_json , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return response_json , True , '<STR_LIT>' <EOL> except Exception , err : <EOL> error = traceback . format_exc ( ) <EOL> log_entry = { '<STR_LIT>' : exp_uid , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:error>' : error , '<STR_LIT>' : utils . datetimeNow ( ) } <EOL> ell . log ( app_id + '<STR_LIT>' , log_entry ) <EOL> return '<STR_LIT:{}>' , False , error </s>
