예제 #1
0
def model(user):
    # now = str(int(time.time()))
    # sc = SparkContext(appName="model")
    # # Load and parse the data
    # data = sc.textFile("data/mllib/kmeans_data.txt")
    # parseddata = data.map(lambda line: array([float(x) for x in line.split(' ')]))
    # # Build the model (cluster the data)
    # clusters = KMeans.train(parseddata, 2, maxIterations=10, initializationMode="random")
    # clusters.save(sc, "files/" + id + '/model/' + now)
    if request.method == 'POST':
        now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        data = request.form
        if data['comment'] == '':
            comment = 'null'
        else:
            comment = '"' + data['comment'] + '"'
        value = '("' + user + '", "' + data["modelName"] + '","' + data[
            "dataName"] + '",' + comment + ',"' + now + '",0 , 0)'
        sql = 'insert into model (user, modelname, dataname, comment, subtime, status, category) values ' + value
        try:
            con = get_con()
            cur = con.cursor()
            cur.execute(sql)
            con.commit()
            sql2 = 'select id from model where user = '******' and subtime = "' + now + '"'
            cur.execute(sql2)
            row = cur.fetchone()
            con.close()
            modelid = str(row[0])
            # conn.rpush(user, modelid)
            temp = r.rpush('model', modelid)
            return jsonify({'status': 1})
        except:
            return jsonify({'status': -1})
    else:
        con = get_con()
        cur = con.cursor()
        cur.execute('select * from model where user = '******'size': len(rows)}
        content = []
        for row in rows:
            tmp = dict()
            tmp['comment'] = row[4]
            tmp['modelName'] = row[2]
            tmp['trainData'] = row[3]
            tmp['submitTime'] = row[5]
            tmp['endTime'] = row[6]
            tmp['status'] = row[7]
            tmp['modelCategory'] = row[8]
            tmp['id'] = row[0]
            content.append(tmp)
        result['content'] = content
        return jsonify(result)
예제 #2
0
def dotaskspark(taskid):
    flag = 0
    sql = 'select user,modelname,testfile, modelfile from task where id = ' + taskid
    con = get_con()
    cur = con.cursor()
    cur.execute(sql)
    task = cur.fetchone()
    if task is None:
        return
    else:
        pass
    if task[1] == 'KMeans':
        try:
            modelfile = dotaskkmeans(task[0], task[2], taskid)
        except:
            flag = 1
    else:
        pass
    now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    if flag == 0:
        cur.execute('update task set status = ' + '1' + ', endtime = ' + '"' +
                    now + '", resultfile = "' + modelfile + '" where id = ' +
                    taskid)
    else:
        cur.execute('update task set status = ' + '2' + ', endtime = ' + '"' +
                    now + '" where id = ' + taskid)
    con.commit()
    con.close()
    return 0
예제 #3
0
def domodelspark(modelid):
    sql = 'select user,modelname,dataname from model where id = ' + modelid
    con = get_con()
    cur = con.cursor()
    cur.execute(sql)
    model = cur.fetchone()
    if model is None:
        return
    else:
        pass
    flag = 0
    if model[1] == 'KMeans':
        try:
            modelfile = dokmeans(model[0], model[2], modelid)
        except:
            flag = 1
    else:
        pass
    now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    if flag == 0:
        cur.execute('update model set status = ' + '1' + ', endtime = ' + '"' +
                    now + '", modelfile = "' + modelfile + '" where id = ' +
                    modelid)
    else:
        cur.execute('update model set status = ' + '2' + ', endtime = ' + '"' +
                    now + '" where id = ' + modelid)
    con.commit()
    con.close()
    return 0
def testfile(id):
    con = get_con()
    cur = con.cursor()
    cur.execute("select * from testfile where user = " + id)
    rows = cur.fetchall()
    con.close()
    namelist = []
    for row in rows:
        namelist.append(row[1])
    return jsonify({'fileNameList': namelist})
예제 #5
0
def td(taskid):
    if request.method == "POST":
        try:
            con = get_con()
            cur = con.cursor()
            sql = 'delete from task where id = ' + taskid
            cur.execute(sql)
            con.commit()
            con.close()
            return jsonify({'status': 1})
        except:
            return jsonify({'status': -1})
예제 #6
0
def md(modelid):
    if request.method == 'POST':
        try:
            con = get_con()
            cur = con.cursor()
            sql = 'delete from model where id = ' + modelid
            cur.execute(sql)
            sql = 'delete from task where modelid =' + modelid
            cur.execute(sql)
            con.commit()
            con.close()
            return jsonify({'status': 1})
        except:
            return jsonify({'status': -1})
예제 #7
0
def download(taskid):
    if request.method == "GET":
        con = get_con()
        cur = con.cursor()
        sql = 'select user from task where id = ' + taskid
        cur.execute(sql)
        row = cur.fetchone()
        con.close()
        user = row[0]
        if os.path.isfile(PROJECT_PATH + '/files/' + user + '/result/' +
                          taskid):
            return send_from_directory(PROJECT_PATH + '/files/' + user +
                                       '/result/',
                                       taskid,
                                       as_attachment=True)
    abort(404)
예제 #8
0
def task(modelid):
    if request.method == 'POST':
        now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        data = request.form
        if len(data) == 0:
            con = get_con()
            cur = con.cursor()
            sql = 'select user, modelname, dataname from model where id = ' + modelid
            cur.execute(sql)
            row = cur.fetchone()
            con.close()
            sql = 'insert task (modelid, subtime, status, testfile, modelname, category, user) values (' + modelid + ',"' + now + '",0,' + '"' + row[
                2] + '","' + row[1] + '",0,"' + row[0] + '")'
            # if row[3] is None:
            #     sql = 'insert task (modelid, subtime, status, testfile, modelname, category, user) values (' + modelid + ',"' + now + '",0,' + '"' + row[2] + '","' + row[1] + '",0,"' + row[0] + '")'
            # else:
            #     sql = 'insert task (modelid, subtime, status, testfile, comment, modelname, category, user) values (' + modelid + ',"' + now + '",0,' + '"' + row[2] + '","' + row[3] + '","' + row[1] + '",0,"' + row[0] + '")'
            try:
                con = get_con()
                cur = con.cursor()
                cur.execute(sql)
                con.commit()
                sql = 'select id from task where subtime = "' + now + '"'
                cur.execute(sql)
                row = cur.fetchone()
                con.close()
                p = Process(target=dotaskspark, args=(str(row[0]), ))
                p.start()
                return jsonify({'status': 1})
            except:
                return jsonify({'status': 0})
        else:
            con = get_con()
            cur = con.cursor()
            sql = 'select user, modelname, modelfile from model where id = ' + modelid
            cur.execute(sql)
            row = cur.fetchone()
            con.close()
            if data['comment'] == '':
                sql = 'insert task (modelid, subtime, status, testfile, modelname, category, user, modelfile) values (' + modelid + ',"' + now + '",0,' + '"' + data[
                    'dataName'] + '","' + row[1] + '",0,"' + row[
                        0] + '","' + row[2] + '")'
            else:
                sql = 'insert task (modelid, subtime, status, testfile, comment, modelname, category, user, modelfile) values (' + modelid + ',"' + now + '",0,' + '"' + data[
                    'dataName'] + '","' + data['comment'] + '","' + row[
                        1] + '",0,"' + row[0] + '","' + row[2] + '")'
            try:
                con = get_con()
                cur = con.cursor()
                cur.execute(sql)
                con.commit()
                sql = 'select id from task where subtime = "' + now + '"'
                cur.execute(sql)
                row = cur.fetchone()
                con.close()
                p = Process(target=dotaskspark, args=(str(row[0]), ))
                p.start()
                return jsonify({'status': 1})
            except:
                return jsonify({'status': 0})
    else:
        con = get_con()
        cur = con.cursor()
        cur.execute('select * from task where user = '******'size': len(rows)}
        content = []
        for row in rows:
            tmp = dict()
            tmp['comment'] = row[6]
            tmp['modelName'] = row[7]
            tmp['testData'] = None
            tmp['submitTime'] = row[2]
            tmp['endTime'] = row[3]
            tmp['status'] = row[4]
            if row[8] != 0:
                tmp['testData'] = row[5]
            tmp['id'] = row[0]
            content.append(tmp)
        result['content'] = content
        return jsonify(result)