Exemple #1
0
def post():
    """
    Route for uploading "big data" to database

    Request should be json, contain a simple dict with entries for any data to be uploaded,
    matching options from 'data_types'

    :return: status of request, if true contains how many were inserted
    """

    status = {
        'success': True,
    }
    if not request.is_json:
        status['success'] = False
        return status
    database = db.get_db().get_database('big_data')
    inserted = 0
    for data_type in data_types:
        if data_type in request.json:
            ins = {'datetime': datetime.now(), 'data': request.json[data_type]}
            database.get_collection(data_type).insert_one(ins)
            inserted += 1
    status['inserted'] = inserted
    return status
Exemple #2
0
def add_to_db(collection, content):
    """
    Add one item to a collection in the database

    :param collection: the collection to add to
    :param content: the item to insert
    """

    db = get_db().get_database('home_automation')
    db.get_collection(collection).insert_one(content)
Exemple #3
0
def get_from_db_raw(collection, filter=None):
    """
    Get all items in a certain collection in the database, without labels

    :param collection: the collection to pull from
    :param filter: the filter to use, default None
    :return: filtered items in collection, in a mongoDB cursor reference
    """

    db = get_db().get_database('home_automation')
    return db.get_collection(collection).find(filter)
Exemple #4
0
def home():
    """
    Simple data browser route

    Renders a template that formats all the data into a nice list, using the user's timezone.
    It's only the user's timezone as long as they live in CST.

    :return: Rendered template
    """
    database = db.get_db().get_database('big_data')
    data_dict = {}
    for data_type in data_types:
        data_dict[data_type] = []
        for item in database.get_collection(data_type).find().sort(
                'datetime', pymongo.DESCENDING):
            data_dict[data_type].append(item)
    return render_template('big_data/big_data_home.html',
                           datatypes=data_types,
                           datadict=data_dict,
                           user_timezone=timezone(timedelta(hours=-6)))
Exemple #5
0
def remove_from_db(collection, item_id):
    db = get_db().get_database('home_automation')
    db.get_collection(collection).delete_one({'_id': ObjectId(item_id)})