def invoke():
    """
    Loops over all users and:
    * Looks up an existing bldg for the current date
    * If not found, creates one, next to the previous day
    * Connects any data-pipes for this user to the created bldg
    """
    logging.info("Invoking lifecycle manager...")
    today = format_date(datetime.utcnow())
    db = get_db()
    managers = db.lifecycle_managers.find(
        {"type": DAILY_FEED_DISPATCHER_LIFEYCLE_MANAGER}
    )
    # TODO read & process in batches
    for manager in managers:
        create_daily_bldg(db, today, manager)
Beispiel #2
0
def load_data_pipes(criteria=None, limit=100):
    """
    Generator returning batches of data-pipe records
    :param limit: the size of each batch
    :return:
    """
    db = get_db()
    spec = {
        "status": STATUS_ACTIVE,
        "connectedBldg": {'$exists': True}
    }
    if criteria is not None:
        spec.update(criteria)
    skip = 0
    done = False
    while not done:
        results = db.data_pipes.find(spec, limit=limit, skip=skip)
        yield results
        results = list(results)
        skip += len(results)
        done = len(results) < limit
Beispiel #3
0
def create_buildings(content_type, keys, payloads, flr, position_hints=None):
    """
    Creates a batch of buildings.
    :param content_type: the content-type of the buildings
    :param keys: the list of keys for the buildings
    :param payloads: the list of payloads for the buildings
    :param flr: the target floor in which to create the buildings
    :param position_hints: dict of hints where to position
    the new buildings, such as:
    * near_x: x coordinate, near which the buildings will be created
    * near_y: y coordinate, near which the buildings will be created
    * next_free: if True, create the buildings in the next
    free place (sequentially)
    :return: the addresses of the created buildings.
    """
    def _create_batch_of_buildings():
        # TODO handle errors
        db.buildings.insert(buildings)
        return len(buildings)

    created_addresses = []
    db = get_db()
    batch_size = 10
    buildings = []
    count = 0
    for i, payload in enumerate(payloads):
        bldg = construct_bldg(flr, content_type, keys[i], payload,
                              position_hints, db)
        buildings.append(bldg)
        created_addresses.append(bldg["address"])
        if len(buildings) == batch_size:
            count += _create_batch_of_buildings()
            buildings = []
    if buildings:
        count += _create_batch_of_buildings()
    logging.info("Created {} buildings in {}".format(count, flr))
    return created_addresses
Beispiel #4
0
def update_data_pipe(data_pipe_id, change):
    db = get_db()
    db.data_pipes.update({"_id": data_pipe_id}, {"$set": change})
    logging.info("Updated data-pipe {}: {}".format(data_pipe_id, change))