Ejemplo n.º 1
0
    def task_status():
        while True:
            task = task_in_exec.AsyncResult(task_id)
            response = make_message(False, state=task.state)

            if task.state == 'WORKING':
                message = random_message()
                response['status'] = message
                yield 'event: message\ndata: {}\n\n'.format(
                    json.dumps(response))
                time.sleep(1.2)
            elif task.state != 'FAILURE':
                if 'status' in task.info:
                    response['status'] = task.info.get('status')
                if 'results' in task.info:
                    response['results'] = task.info.get('results')
                yield 'event: message\ndata: {0}\n\n'.format(
                    json.dumps(response))
                time.sleep(1.2)
            else:
                response['error'] = True
                response['status'] = str(task.info)
                yield 'event: message\ndata: {0}\n\n'.format(
                    json.dumps(response))
                time.sleep(1.2)
Ejemplo n.º 2
0
def task_status(task_name, task_id):
    task_in_exec = tasks_directory.get(task_name)
    if not task_in_exec:
        return jsonify(
            make_message(not task_in_exec,
                         message='No {} task availabale with id: {}'.format(
                             task_name, task_id)))

    def task_status():
        while True:
            task = task_in_exec.AsyncResult(task_id)
            response = make_message(False, state=task.state)

            if task.state == 'WORKING':
                message = random_message()
                response['status'] = message
                yield 'event: message\ndata: {}\n\n'.format(
                    json.dumps(response))
                time.sleep(1.2)
            elif task.state != 'FAILURE':
                if 'status' in task.info:
                    response['status'] = task.info.get('status')
                if 'results' in task.info:
                    response['results'] = task.info.get('results')
                yield 'event: message\ndata: {0}\n\n'.format(
                    json.dumps(response))
                time.sleep(1.2)
            else:
                response['error'] = True
                response['status'] = str(task.info)
                yield 'event: message\ndata: {0}\n\n'.format(
                    json.dumps(response))
                time.sleep(1.2)

    headers = Headers()

    headers.add('Content-Type', 'text/event-stream')
    headers.add('Cache-Control', 'no-cache')
    headers.add('Connection', 'keep-alive')

    return Response(stream_with_context(task_status()),
                    status=200,
                    headers=headers)
Ejemplo n.º 3
0
def train_file():
    ok, msg = check_properties(request,
                               form=None,
                               files=None,
                               content_type=expectedContentType)
    if not ok:
        return jsonify(make_message(True, message=msg))

    form = request.form.to_dict()
    files = request.files.to_dict()

    originalTrainFileName = files.get('trainFile').filename
    originalTestFileName = files.get('testFile').filename

    trainFileExt = os.path.splitext(originalTrainFileName)[1]
    testFileExt = os.path.splitext(originalTestFileName)[1]
    trainMimeType = files.get('trainFile').mimetype
    testMimeType = files.get('testFile').mimetype
    trainFileName = uuid.uuid1().hex + trainFileExt
    testFileName = uuid.uuid1().hex + testFileExt

    try:
        files.get('trainFile').save(os.path.join(uploadFolder, trainFileName))
        files.get('testFile').save(os.path.join(uploadFolder, testFileName))
    except IOError as ioe:
        saveFileErrorMessage = 'I/O error during write of files: {}'.format(
            repr(ioe))
        print(saveFileErrorMessage)
        return jsonify(make_message(True, message=saveFileErrorMessage))
    except Exception as e:
        exceptionErrorMessage = 'Exception during file processing: {}'.format(
            repr(e))
        print(exceptionErrorMessage)
        return jsonify(make_message(True, message=exceptionErrorMessage))

    trainStats = os.stat(os.path.join(uploadFolder, trainFileName))
    testStats = os.stat(os.path.join(uploadFolder, testFileName))

    trainFileCreated = datetime.utcfromtimestamp(trainStats.st_ctime)
    testFileCreated = datetime.utcfromtimestamp(testStats.st_ctime)

    trainFileSize = trainStats.st_size
    testFileSize = testStats.st_size

    try:
        algorithm = db.session.query(Algorithm).filter_by(
            name=form['algorithmName']).one()
    except Exception as e:
        exceptionErrorMessage = 'Exception during algorithm search: {}. On searching for: {}'.format(
            repr(e), form['algorithmName'])
        print(exceptionErrorMessage)
        return jsonify(make_message(True, message=exceptionErrorMessage))

    trainFile = DataFile(originalfilename=originalTrainFileName,
                         actualfilename=trainFileName,
                         actualfilepath=uploadFolder,
                         mimetype=trainMimeType,
                         filesize=trainFileSize,
                         created=datetime.utcnow(),
                         fileprototype="train")

    testFile = DataFile(originalfilename=originalTestFileName,
                        actualfilename=testFileName,
                        actualfilepath=uploadFolder,
                        mimetype=testMimeType,
                        filesize=testFileSize,
                        created=datetime.utcnow(),
                        fileprototype="test")

    dataset = DataSet(name=form.get('dataName'),
                      date=datetime.utcnow(),
                      datatype='training',
                      algorithm_id=algorithm.id)

    dataset.files = [trainFile, testFile]

    db.session.add(dataset)
    db.session.commit()

    task = train_task.apply_async(args=[
        os.path.join(uploadFolder, trainFileName),
        os.path.join(uploadFolder, testFileName), dataset.id,
        algorithm.script_name
    ])
    message = make_message(False,
                           taskstatusurl=url_for('api.task_status',
                                                 task_name='train_task',
                                                 task_id=task.id))

    return make_response(jsonify(message), 202)
Ejemplo n.º 4
0
    def test_app(self, name, expected):
        actual = make_message(name)

        assert actual == expected
Ejemplo n.º 5
0
def hello():
    """sample api"""
    print('START /hello')
    name = request.args.get('name')
    print('END /hello')
    return make_message(name)