コード例 #1
0
def get_friend_rooms():
    rooms = map(lambda name: flask_redis.hgetall(name),
                flask_redis.smembers("forresthill-rooms"))
    rooms.sort(key=lambda x: x['key'])

    friends = set()
    if current_user.is_authenticated:
        for room in rooms:
            room_machines = flask_redis.lrange(room['key'] + "-machines", 0,
                                               -1)
            for machineName in room_machines:
                machine = flask_redis.hgetall(machineName)
                if current_user.has_friend(machine['user']):
                    uun = current_user.get_friend(machine['user'])
                    friends.add((uun, room['key'], room['name']))
        friends = list(friends)

        # uun -> name
        names = ldap.get_names([f[0] for f in friends])
        for i in range(len(friends)):
            uun, b, c = friends[i]
            if uun in names:
                friends[i] = {
                    'uun': uun,
                    'name': names[uun],
                    'room_key': b,
                    'room_name': c
                }

        friends.sort(key=lambda x: x['name'])

    return friends
コード例 #2
0
ファイル: views.py プロジェクト: timmygill/mapp
def route_get_cascaders():
    if current_user.is_disabled:
        return jsonify([])

    cascaders = get_cascaders()

    rooms = map(lambda name: flask_redis.hgetall(name),
                flask_redis.smembers("forresthill-rooms"))
    result = []

    for room in rooms:
        room_machines = flask_redis.lrange(room['key'] + "-machines", 0, -1)
        for machineName in room_machines:
            machine = flask_redis.hgetall(machineName)
            if machine['user']:
                uun = find_cascader(cascaders, machine['user'])
                if uun:
                    result.append({
                        'uun': uun,
                        'room': room['name'],
                    })

    uuns = [f['uun'] for f in result]

    # uun -> name
    names = ldap.get_names(uuns)

    # uun -> tagline
    taglines = flask_redis.hmget("cascaders.taglines", uuns)

    for i in range(len(result)):
        uun = result[i]['uun']
        if uun in names:
            result[i]['name'] = names[uun]
        result[i]['tagline'] = taglines[i]

    return jsonify(result)
コード例 #3
0
def map_routine(which_room):
    room = flask_redis.hgetall(str(which_room))
    room_machines = flask_redis.lrange(room['key'] + "-machines", 0, -1)
    machines = {m: flask_redis.hgetall(m) for m in room_machines}
    num_rows = max([int(machines[m]['row']) for m in machines])
    num_cols = max([int(machines[m]['col']) for m in machines])

    num_machines = len(machines.keys())
    num_used = 0

    rows = []
    uuns = []
    for r in xrange(0, num_rows + 1):
        unsorted_cells = []
        for c in xrange(0, num_cols + 1):
            default_cell = {'hostname': None, 'col': c, 'row': r}
            cell = [
                v for (k, v) in machines.iteritems()
                if int(v['row']) == r and int(v['col']) == c
            ]
            if not cell:
                cell = default_cell
            else:
                cell = cell[0]

            try:
                if cell['user'] is not "" or cell['status'] == "offline":
                    num_used += 1
            except Exception:
                pass

            if 'user' in cell:
                if cell['user'] == "":
                    del cell['user']
                else:
                    uun = current_user.get_friend(cell['user'])
                    if uun:
                        uuns.append(uun)
                        cell["user"] = uun
                    else:
                        cell["user"] = "******"

            unsorted_cells.append(cell)

        cells = unsorted_cells
        rows.append(cells)

    uun_names = ldap.get_names(uuns)

    for y in xrange(len(rows)):
        for x in xrange(len(rows[y])):
            cell = rows[y][x]
            if "user" in cell:
                uun = cell["user"]
                if uun in uun_names:
                    rows[y][x]["friend"] = uun_names[uun]

    num_free = num_machines - num_used

    low_availability = num_free <= 0.3 * num_machines

    date_format = "%Y-%m-%dT%H:%M:%S.%f"
    last_update = datetime.strptime(flask_redis.get("last-update"),
                                    date_format)
    last_update = last_update.strftime("%Y-%m-%d %H:%M:%S")

    # Annotate friends with "here" if they are here
    room_key = room['key']
    friends = get_friend_rooms()
    friends_here, friends_elsewhere = (0, 0)
    for i in range(len(friends)):
        if friends[i]['room_key'] == room_key:
            friends[i]['here'] = True
            friends_here += 1
        else:
            friends_elsewhere += 1

    return {
        "friends": friends,
        "friends_here_count": friends_here,
        "friends_elsewhere_count": friends_elsewhere,
        "room": room,
        "rows": rows,
        "num_free": num_free,
        "num_machines": num_machines,
        "low_availability": low_availability,
        "last_update": last_update
    }