Ejemplo n.º 1
0
def test_db(app):
    """
    Test database connection
    """
    with app.app_context():
        database = get_database()
        assert database is not None
Ejemplo n.º 2
0
def instance(instance_id):
    """
    Creates a collection and inserts a document to host the app instance.
    :param instance_id: The unique identifier of the instance.
    :return: list of instances, including the new ones.
    """
    db = get_database()
    app_collection = db.get_collection(str(instance_id))
    # import pdb; pdb.set_trace()
    if request.method == 'POST' and request.data:
        document = util.create_document(request.get_json())
        # persist document
        post_id = app_collection.insert_one(document).inserted_id
        app_collection.create_index([("timestamp", ASCENDING)])

        return make_response(
            jsonify(document_id=str(post_id), instance_id=instance_id),
            status.HTTP_201_CREATED)

    app_collection = db.get_collection(str(instance_id))
    col_info_response = {
        "full_name": app_collection.full_name,
        "aprox_doc_count":
        app_collection.estimated_document_count(maxTimeMS=5000)
    }

    return make_response(jsonify(col_info_response), status.HTTP_200_OK)
Ejemplo n.º 3
0
def list_instances():
    """
    Lists all the ids of the instances in the database.
    :return: list of json documents that contains the instances in the database collection.
    """
    db = get_database()
    collection_list = db.list_collection_names()

    return make_response(dumps(collection_list), status.HTTP_200_OK)
Ejemplo n.º 4
0
def get_history_since(instance_id, number_of_documents):
    """
	Lists the first n documents, from oldest to newest.
	"""
    app_db = get_database()
    app_collection = app_db.get_collection(str(instance_id))
    history_documents = app_collection.find().sort(
        'timestamp', ASCENDING).limit(number_of_documents)
    return make_response(util.create_document(history_documents),
                         status.HTTP_200_OK)
Ejemplo n.º 5
0
def find_first(instance_id):
    """
    Gets the first document of the instance collection.
    :param instance_id: unique instance identification
    :return:
    """
    db = get_database()
    app_collection = db.get_collection(str(instance_id))
    first_document = app_collection.find().sort('timestamp',
                                                direction=ASCENDING).limit(1)

    return make_response(dumps(first_document), status.HTTP_200_OK)
Ejemplo n.º 6
0
def find_head(instance_id):
    """
    Gets the latest document of the instance collection
    :param instance_id: unique instance identification
    :return: the latest (most recent) document in the given instance.
    """
    db = get_database()
    app_collection = db.get_collection(str(instance_id))
    latest_document = app_collection.find().sort('timestamp',
                                                 DESCENDING).limit(1)

    return make_response(dumps(latest_document), status.HTTP_200_OK)
Ejemplo n.º 7
0
def get_first_documents(instance_id, number_of_documents):
    """
    Lists the first n documents, from oldest to newest.
    :param instance_id: unique instance identification
    :param number_of_documents:
    :return:
    """
    db = get_database()
    # This is done to protect the database against large requests for no reason.
    if number_of_documents > 1000:
        number_of_documents = 1000

    app_collection = db.get_collection(str(instance_id))
    history_documents = app_collection.find().sort(
        'timestamp', ASCENDING).limit(number_of_documents).limit(1)

    return make_response(dumps(history_documents), status.HTTP_200_OK)
Ejemplo n.º 8
0
def _memory_insert(collection: str, data: dict):
    """
    Inserts a new document object into the database
    :param collection: The collection that the document belongs and should be saved to.
    :param data: The data (dictionary) to save.
    :return: Document Object ID.
    """
    try:
        if sys.getsizeof(data) > MAX_BYTES:
            raise ValueError(
                "Document is too large. Use memory_file_insert if object is above "
                + str(MAX_BYTES))
        db = get_database()
        result = db[collection].insert_one(data)
        return result.inserted_id
    except ValueError as ve:
        print(ve)
Ejemplo n.º 9
0
def find_head(instance_id):
    """
    Finds and returns the entire data collection for that particular instance id.
    :param instance_id: UUID with the desired instance id.
    :return:
    """
    head_query = {"header.instanceId": str(instance_id)}
    db = get_database()
    event_memory = db['events'].find(head_query).sort('timestamp', DESCENDING)
    map_memory = db['maps'].find(head_query).sort('timestamp', DESCENDING)
    dataset_memory = db['dataset'].find(head_query).sort(
        'timestamp', DESCENDING)
    fork_memory = db['fork'].find(head_query).sort('timestamp', DESCENDING)

    data = event_memory, map_memory, dataset_memory, fork_memory
    result = dumps(data)

    return make_response(result, status.HTTP_200_OK)
Ejemplo n.º 10
0
def clone_instance(from_instance_id, to_instance_id):
    """
    Lists the first n documents, from oldest to newest.
    :param from_instance_id: unique collection identification to copy from
    :param to_instance_id: unique collection identification destination
    return: id of the new document inside the collection
    """
    db = get_database()
    source_collection = db.get_collection(str(from_instance_id))
    projection_fields = {'_id': False}

    # Find the first document of the "from_instance_id" collection. Projection does not bring old id.
    source_document = source_collection.find(
        projection=projection_fields).sort('timestamp', ASCENDING).limit(1)
    # Prepare a new collection, with the destination UUID.
    # Save the document. New document id will be assigned by database.
    destination_collection = db.get_collection(str(to_instance_id))
    destination_document = destination_collection.insert_one(
        source_document[0])

    return make_response(str(destination_document.inserted_id),
                         status.HTTP_201_CREATED)