def upload_logset():
    #created specifies if the new_logset is a pre-existing object or new
    if request.form['name'] == "":
        return jsonify(dict(status = 'Error', message = "Provide a name for the logset"))
    new_logset, created = LogsetMetadata.objects.get_or_create(\
                name = request.form['name']\
                , creator_name = current_user.name\
                , users_with_access = [current_user.name]\
            )
    uploaded_file = request.files['file']
    if uploaded_file:
        filename = secure_filename(uploaded_file.filename)
        uploaded_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        lp = LogParser()
        file_location = app.config['UPLOAD_FOLDER'] + filename
        collection_name = current_user.name + '_' + request.form['name']
        if lp.load_apache_log_file_into_DB(file_location, collection_name) is False:
            return jsonify(dict(status = 'Error', message='The data was not stored'))
        else:
            try:
                new_logset.save()
                return jsonify(dict(status = 'Success', message = 'Upload successful'))
            except db.NotUniqueError:
                return jsonify(dict(status = 'Error', message = 'Logset by this name already exists'))
    else:
        return jsonify(dict(status = 'Error', message = 'No file provided'))
from log_analyzer import LogAnalyzer, LogParser
# from json import json
if __name__ == '__main__':
    lp = LogParser()
    lp.load_apache_log_file_into_DB('access.2','access_log')
    # lp.load_apache_log_file_into_DB('access_log_2','access_log')
    # lp.load_apache_log_file_into_DB('access_log_3','access_log')
    # lp.load_apache_log_file_into_DB('access_log_4','access_log')
    # la = LogAnalyzer(db = 'test', collection = 'access_log')
    # print la.get_log_data(collection_name = 'access_log', page_number = 5)
    # print la.get_log_data('access_log')
    # print la.count_hits('access_log')
    
    # df = la.load_apache_logs_into_DataFrame()
    # data = la.group_by(df,['request_country'])
    # print json.dumps(la.daily_bandwidth_sums())
    # print la.sum(data, 'request_size')
    # print (la.count(data, ['request_country'])['request_country']) #group_by os and browser and return the count of referer based on that groups. notice the field selection after the count, otherwise all the fields will be printed
    # print la.count(data, 'referer')
    # data = la.group_by(df, 'referer')
    # data = la.count(data, 'referer')
    # print la.to_dict(data)
    # #print la.to_dict(data, 'referer', 'count')
    # data = la.group_by(df, 'os')
    # data = la.count(data, 'os')
    # print data
    # data = la.group_by(df, 'request_country')
    # data = la.count(data, 'request_country')
    # print data
    # data = la.group_by(df, 'browser')
    # data = la.count(data, 'browser')