Ejemplo n.º 1
0
def save_results(session_id, like, share, pop, know, details):
    '''Saves results and increments video pair number'''
    
    with DB.transaction():
        pair_number = db.get_curr_pair(session_id)
        id1, id2 = db.get_videos(session_id, pair_number)

        db.save_choice(session_id, pair_number, id1, id2, like, share, pop, 
                know, details)
        return db.update_session(session_id)
Ejemplo n.º 2
0
def random_id():
    '''Generates a random ascii string composed of numbers'''
    
    chars = string.digits
    new_id = int(''.join(random.choice(chars) for x in xrange(IDLEN)))

    with DB.transaction():
        while has_id(new_id): #TODO: We can make IDs in sequence
            new_id = int(''.join(random.choice(chars) for x in xrange(IDLEN)))
        db.add_id(new_id)

    return new_id
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
def save_start_eval(session_id):
    '''Saves the timestamp of when a videopage is loaded'''

    with DB.transaction():
        pair_number = db.get_curr_pair(session_id)
        db.save_start_eval(session_id, pair_number)
Ejemplo n.º 6
0
def save_user_info(session_id, age, gender, country, view, share, share_all):
    '''Saves used info to the database'''

    with DB.transaction():
        db.save_user_info(session_id, age, gender, country, view, share, 
                share_all)
Ejemplo n.º 7
0
    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)