def get_videos(session_id, pair_number): '''Gets the pair id for the round of the given user (session id)''' result = DB.select(SESSION_DB_NAME, where='session_id=%d' % session_id, what='round_rbn') round_num = result[0]['round_rbn'] result = DB.select(PAIRS_DB_NAME, where='pair_num=%d AND round_rbn=%d' % (pair_number, round_num), what='video_id1,video_id2') row = result[0] return row['video_id1'], row['video_id2']
def num_pairs(session_id): ''' Simply counts the number of rows in the pairs for the round of the given user. ''' result = DB.select(SESSION_DB_NAME, where='session_id=%d' % session_id, what='round_rbn') round_num = result[0]['round_rbn'] count = 0 for _ in DB.select(PAIRS_DB_NAME, where='round_rbn=%d' % round_num): count += 1 return count
def load_machine(name): results = list(DB.select('machine', what='*', where='name = $name', vars=dict(name=name))) if not results: raise web.NotFound() return results[0]
def get_number_evaluated(session_id): '''Get's number of pairs evaluted for a given session''' result = DB.select(SESSION_DB_NAME, where='session_id=%d' % session_id, \ what='num_eval') return result[0]['num_eval']
def get_curr_pair(session_id): '''Gets the current pair being evaluated''' result = DB.select(SESSION_DB_NAME, where='session_id=%d' % session_id, what='curr_pair') return result[0]['curr_pair']
def GET(self): rows = list(DB.select('machine', what='*')) if not rows: raise web.NotFound() for row in rows: row.locked_since = row.locked_since.isoformat() web.header('Content-type', 'text/json') return json.dumps(rows)
def update_session(session_id): '''Increments the evaluation pair for a session''' result = DB.select(SESSION_DB_NAME, where='session_id=%d' % session_id, \ what='num_eval') num_eval = result[0]['num_eval'] + 1 DB.update(SESSION_DB_NAME, where='session_id=%d' % session_id, num_eval=num_eval) return num_eval
def has_user_id(session_id): '''Checks if the user demographic database already has this session id''' try: result = DB.select(USER_DB_NAME, where='session_id=%d' % session_id, \ what='session_id') result[0]['session_id'] return True except IndexError: return False
def has_id(session_id): '''Checks if the session database already has this session id''' try: result = DB.select(SESSION_DB_NAME, where='session_id=%d' % session_id, what='num_eval') result[0]['num_eval'] return True except IndexError: return False
def get_evaluated(session_id): '''Gets videos which were evaluated to be evaluated''' results = DB.select(EVAL_DB_NAME, where='session_id=%d' % session_id, what='pair_num') evaluated = set() for result in results: evaluated.add(result['pair_num']) return evaluated
def add_id(session_id): '''Adds new id to the session database''' round_select = DB.select(ROUND_ROBIN_DB_NAME)[0] curr_round = round_select['current_round'] total_rounds = round_select['total_rounds'] DB.insert(SESSION_DB_NAME, session_id=session_id, round_rbn=curr_round, num_eval=0) next_round = (curr_round + 1) % total_rounds return DB.update(ROUND_ROBIN_DB_NAME, where='current_round=%d' % curr_round, current_round=next_round)
def POST(self): user = web.input('user')['user'] num = int(web.input('num')['num']) if num < 1: raise web.BadRequest() tries = 0 while True: try: # transaction will be rolled back if an exception is raised with DB.transaction(): results = list(DB.select('machine', what='name, sshpubkey', where='locked = false AND up = true', limit=num)) if len(results) < num: raise web.HTTPError(status='503 Service Unavailable') name_keys = {} for row in results: name_keys[row.name] = row.sshpubkey where_cond = web.db.sqlors('name = ', name_keys.keys()) \ + ' AND locked = false AND up = true' num_locked = DB.update('machine', where=where_cond, locked=True, locked_by=user, locked_since=web.db.SQLLiteral('NOW()')) assert num_locked == num, 'Failed to lock machines' except: tries += 1 if tries < 10: continue raise else: break web.header('Content-type', 'text/json') return json.dumps(name_keys)
def POST(self): user = web.input('user')['user'] desc = web.input(desc=None)['desc'] num = int(web.input('num')['num']) machinetype = dict(machinetype=(web.input(machinetype='plana')['machinetype'])) if num < 1: raise web.BadRequest() tries = 0 check_existing = True while True: try: # transaction will be rolled back if an exception is raised with DB.transaction(): if desc is not None and check_existing: # if a description is provided, treat it as a # key for locking in case the same run locked # machines in the db successfully before, but # the web server reported failure to it # because the request took too long. Only try # this once per request. check_existing = False results = list(DB.select('machine', machinetype, desc, user, what='name, sshpubkey', where='locked = true AND up = true AND type = $machinetype AND description = $desc AND locked_by = $user', limit=num)) if len(results) == num: name_keys = {} for row in results: name_keys[row.name] = row.sshpubkey print 'reusing machines', name_keys.keys() break results = list(DB.select('machine', machinetype, what='name, sshpubkey, type', where='locked = false AND up = true AND type = $machinetype', limit=num)) if len(results) < num: raise web.HTTPError(status='503 Service Unavailable') name_keys = {} for row in results: if row.type == 'vps': curkey = row.sshpubkey else: curkey, getstatus = get_sshkey(row.name) if getstatus != 0: curkey = row.sshpubkey if row.sshpubkey != curkey: newkey = curkey update_sshkey(row.name, curkey, row.type) else: newkey = row.sshpubkey name_keys[row.name] = newkey where_cond = web.db.sqlors('name = ', name_keys.keys()) \ + ' AND locked = false AND up = true' num_locked = DB.update('machine', where=where_cond, locked=True, locked_by=user, description=desc, locked_since=web.db.SQLLiteral('NOW()')) assert num_locked == num, 'Failed to lock machines' except Exception: log.exception("Saw exception") tries += 1 if tries < 10: continue raise else: break print user, 'locked', name_keys.keys(), 'desc', desc web.header('Content-type', 'text/json') return json.dumps(name_keys)
def read(): results = list(DB.select('galleries',order="last_update desc")) if len(results) == 0: return None return results
def POST(self): user = web.input('user')['user'] desc = web.input(desc=None)['desc'] num = int(web.input('num')['num']) machinetype = dict(machinetype=(web.input( machinetype='plana')['machinetype'])) if num < 1: raise web.BadRequest() tries = 0 check_existing = True while True: try: # transaction will be rolled back if an exception is raised with DB.transaction(): if desc is not None and check_existing: # if a description is provided, treat it as a # key for locking in case the same run locked # machines in the db successfully before, but # the web server reported failure to it # because the request took too long. Only try # this once per request. check_existing = False results = list( DB.select( 'machine', machinetype, desc, user, what='name, sshpubkey', where= 'locked = true AND up = true AND type = $machinetype AND description = $desc AND locked_by = $user', limit=num)) if len(results) == num: name_keys = {} for row in results: name_keys[row.name] = row.sshpubkey print 'reusing machines', name_keys.keys() break results = list( DB.select( 'machine', machinetype, what='name, sshpubkey, type', where= 'locked = false AND up = true AND type = $machinetype', limit=num)) if len(results) < num: raise web.HTTPError(status='503 Service Unavailable') name_keys = {} for row in results: if row.type == 'vps': curkey = row.sshpubkey else: curkey, getstatus = get_sshkey(row.name) if getstatus != 0: curkey = row.sshpubkey if row.sshpubkey != curkey: newkey = curkey update_sshkey(row.name, curkey, row.type) else: newkey = row.sshpubkey name_keys[row.name] = newkey where_cond = web.db.sqlors('name = ', name_keys.keys()) \ + ' AND locked = false AND up = true' num_locked = DB.update( 'machine', where=where_cond, locked=True, locked_by=user, description=desc, locked_since=web.db.SQLLiteral('NOW()')) assert num_locked == num, 'Failed to lock machines' except Exception: log.exception("Saw exception") tries += 1 if tries < 10: continue raise else: break print user, 'locked', name_keys.keys(), 'desc', desc web.header('Content-type', 'text/json') return json.dumps(name_keys)