def update_available(): content = request.json date_format = "%Y-%m-%dT%H:%M:%S.%f" try: client_time = float(content['timestamp']) except Exception as e: raise APIError("Malformed JSON POST data", status_code=400) last_update = float(flask_redis.get("last-update")) user_behind = client_time < last_update return jsonify(status=str(user_behind))
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 = [] 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 "": num_used += 1 except Exception: pass unsorted_cells.append(cell) cells = unsorted_cells rows.append(cells) num_free = num_machines - num_used reserved = flask_redis.smembers('reserved-machines') 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") return { "room" : room, "rows" : rows, "reserved" : reserved, "num_free" : num_free, "num_machines" : num_machines, "low_availability" : low_availability, "last_update" : last_update }
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 }