Example #1
0
async def new_model_handler(r: ModelUploadRequest, uid: str = Depends(get_user())) -> JSONResponse:
    """API handler used to retrieve model by
    model ID for a given user

    Returns:
        JSONResponse: [description]
    """

    LOGGER.debug('received request to upload new model for user %s', uid)
    # insert into postgres database and retrieve model ID
    try:
        meta, bytes_data = parse_base64_file(r.model_content)
        # try to parse uploaded content to tensorflow model
        expected_shapes = validate_upload_content(bytes_data, r.model_schema)
        bytes_data.seek(0)
    except Exception:
        LOGGER.exception('unable to parse file')
        return json_response_with_message(status.HTTP_400_BAD_REQUEST, 'Invalid model data')

    model_id = insert_user_model(PG_CREDENTIALS,
                                 uid,
                                 r.model_name,
                                 r.model_description,
                                 r.model_schema,
                                 meta.file_size,
                                 expected_shapes.input_shape,
                                 expected_shapes.output_shape)
    # upload data to s3 bucket
    upload_s3_file(bytes_data, '/tensor-trigger/' + str(model_id))
    return json_response_with_message(status.HTTP_201_CREATED, 'Successfully created model')
Example #2
0
async def get_model_content_handler(
    job_id: UUID, uid: str = Depends(get_user())) -> JSONResponse:
    """API handler used to retrieve models
    for a given user

    Returns:
        JSONResponse: [description]
    """

    LOGGER.debug('retrieving models for user %s', uid)
    # get all models from postgres database and convert to dict
    job = get_user_job(PG_CREDENTIALS, uid, job_id)
    if job is None:
        LOGGER.error('unable to find job %s for user %s', job_id, uid)
        return json_response_with_message(status.HTTP_404_NOT_FOUND,
                                          'Cannot find specified job')

    s3_data = retrieve_s3_file('/tensor-trigger/input-data' + str(job.job_id))
    # generate metadata for file (including mime type) and convert to
    # base64 encoded format
    meta = Base64FileMetadata(file_size=0, mime_type='text/plain')
    content = {
        'http_code': status.HTTP_200_OK,
        'job': generate_base64_file(s3_data, meta)
    }
    return JSONResponse(status_code=status.HTTP_200_OK, content=je(content))
Example #3
0
 def get(self):
     """
     Get all notifications for current user.
     """
     args = uid_parser.parse_args()
     uid = args['uid']
     user = utils.get_user(uid)
     return {'notifications': user.serialize_notifications()}
Example #4
0
 def put(self):
     """
     User read all notifications.
     """
     args = uid_parser.parse_args()
     uid = args['uid']
     user = utils.get_user(uid)
     user.read_notifications()
     return {'result': 'SUCCESS'}
Example #5
0
  def get(self):

    # Get the user, sign in url, and sign out url
    user, sign_in_url, sign_out_url = utils.get_user();

    # If the user exists (is logged in), render the page
    if user:
      self.render("pages/home.html", page_title="Home", sign_out_url=sign_out_url)

    # If the user is not logged in, redirect to the sign in url
    else:
      self.redirect(sign_in_url)
Example #6
0
 def test_read_notifications(self, mock_update):
     uid = uuid4()
     user = User(uid=uid).save()
     user.notifications = [
         InvitationNotification(inviter_id='i' + str(i),
                                task_id='t' + str(i)).save()
         for i in range(3)
     ]
     user.read_notifications()
     user = utils.get_user(uid)
     for notification in user.notifications:
         assert user.read[notification.nid]
Example #7
0
async def get_models_handler(uid: str = Depends(get_user())) -> JSONResponse:
    """API handler used to retrieve models
    for a given user

    Returns:
        JSONResponse: [description]
    """

    LOGGER.debug('retrieving models for user %s', uid)
    # get all models from postgres database and convert to dict
    jobs = [j._asdict() for j in get_user_jobs(PG_CREDENTIALS, uid)]
    content = {'http_code': status.HTTP_200_OK, 'jobs': jobs}
    return JSONResponse(status_code=status.HTTP_200_OK, content=je(content))
Example #8
0
  def get(self):

    # Get all sheet IDs
    sheet_id = self.request.get_all('sheet_id');

    # Get the user, sign in url, and sign out url
    user = utils.get_user();

    # If the user exists (is logged in), return the requested data
    if user:
      self.response.out.write(utils.get_drive_data(sheet_id))

    # If the user is not logged in, return 401 Unauthorized error
    else:
      self.response.error(401)
Example #9
0
  def get(self):

    # Get the user, sign in url, and sign out url
    user, sign_in_url, sign_out_url = utils.get_user();

    # Get the sheet data from Clients spreadsheet
    sheet_data = utils.get_drive_data([])

    # If the user exists (is logged in), render the page
    if user:
      self.render("pages/clients.html", page_title="Clients", sign_out_url=sign_out_url, sheet_data=sheet_data)

    # If the user is not logged in, redirect to the sign in url
    else:
      self.redirect(sign_in_url)
Example #10
0
async def get_model_meta_handler(model_id: UUID, uid: str = Depends(get_user())) -> JSONResponse:
    """API handler used to retrieve model by
    model ID for a given user

    Returns:
        JSONResponse: [description]
    """

    LOGGER.debug('retrieving model %s for user %s', model_id, uid)
    model_meta = get_user_model(PG_CREDENTIALS, uid, model_id)
    if model_meta is None:
        return json_response_with_message(status.HTTP_404_NOT_FOUND, 'Cannot find specified model')

    content = {'http_code': status.HTTP_200_OK,
               'model': model_meta._asdict()}
    return JSONResponse(status_code=status.HTTP_200_OK, content=je(content))
Example #11
0
    def get(self):

        # Get the user, sign in url, and sign out url
        user, sign_in_url, sign_out_url = utils.get_user()

        # Get the sheet data from Clients spreadsheet
        sheet_data = utils.get_drive_data([])

        # If the user exists (is logged in), render the page
        if user:
            self.render("pages/clients.html",
                        page_title="Clients",
                        sign_out_url=sign_out_url,
                        sheet_data=sheet_data)

        # If the user is not logged in, redirect to the sign in url
        else:
            self.redirect(sign_in_url)
Example #12
0
async def get_model_meta_handler(
    job_id: UUID, uid: str = Depends(get_user())) -> JSONResponse:
    """API handler used to retrieve models
    for a given user

    Returns:
        JSONResponse: [description]
    """

    LOGGER.debug('retrieving models for user %s', uid)
    # get all models from postgres database and convert to dict
    job = get_user_job(PG_CREDENTIALS, uid, job_id)
    if job is None:
        LOGGER.error('unable to find job %s for user %s', job_id, uid)
        return json_response_with_message(status.HTTP_404_NOT_FOUND,
                                          'Cannot find specified job')

    content = {'http_code': status.HTTP_200_OK, 'job': job._asdict()}
    return JSONResponse(status_code=status.HTTP_200_OK, content=je(content))
Example #13
0
  def get(self):

    # Get the current year from the request
    year = self.request.get("year")

    # If no year in the request, set it equal to the current year
    if not year:
      year = utils.get_this_year();

    # Get the user, sign in url, and sign out url
    user, sign_in_url, sign_out_url = utils.get_user();

    # If the user exists (is logged in), render the page
    if user:
      self.render("pages/sales-goals.html", page_title="Sales Goals", sign_out_url=sign_out_url, year=year)

    # If the user is not logged in, redirect to the sign in url
    else:
      self.redirect(sign_in_url)
Example #14
0
async def get_model_handler(model_id: UUID, uid: str = Depends(get_user())) -> JSONResponse:
    """API handler used to retrieve model by
    model ID for a given user

    Returns:
        JSONResponse: [description]
    """

    LOGGER.debug('retrieving model %s for user %s', model_id, uid)
    model_meta = get_user_model(PG_CREDENTIALS, uid, model_id)
    if model_meta is None:
        return json_response_with_message(status.HTTP_404_NOT_FOUND, 'Cannot find specified model')

    s3_data = retrieve_s3_file('/tensor-trigger/' + str(model_id))
    # generate metadata for file (including mime type) and convert to
    # base64 encoded format
    meta = Base64FileMetadata(file_size=0, mime_type='application/octet-stream')
    content = {'http_code': status.HTTP_200_OK,
               'model': generate_base64_file(s3_data, meta)}
    return JSONResponse(status_code=status.HTTP_200_OK, content=je(content))
Example #15
0
async def get_job_results_handler(
    job_id: UUID, uid: str = Depends(get_user())) -> JSONResponse:
    """API handler used to retrieve models
    for a given user

    Returns:
        JSONResponse: [description]
    """

    LOGGER.debug('retrieving models for user %s', uid)
    # get all models from postgres database and convert to dict
    job = get_user_job(PG_CREDENTIALS, uid, job_id)
    if job is None:
        LOGGER.error('unable to find job %s for user %s', job_id, uid)
        return json_response_with_message(status.HTTP_404_NOT_FOUND,
                                          'Cannot find specified job')

    if job.job_state != 2:
        LOGGER.error(
            'unable to retrieve job results for %s: invalid job state %s',
            job_id, job.job_state)
        return json_response_with_message(status.HTTP_400_BAD_REQUEST,
                                          'Invalid job state')

    meta = get_user_model(PG_CREDENTIALS, uid, job.model_id)
    schema = meta.model_schema

    s3_data = retrieve_s3_file('/tensor-trigger/output-data' + str(job.job_id))
    results = json.loads(s3_data.getvalue().decode())
    # generate metadata for file (including mime type) and convert to
    # base64 encoded format
    meta = Base64FileMetadata(file_size=0, mime_type='text/plain')
    content = {
        'http_code':
        status.HTTP_200_OK,
        'results':
        _format_output_vector(results.get('output', []),
                              schema.get('output_schema'))
    }
    return JSONResponse(status_code=status.HTTP_200_OK, content=je(content))
Example #16
0
    def get(self):

        # Get the current year from the request
        year = self.request.get("year")

        # If no year in the request, set it equal to the current year
        if not year:
            year = utils.get_this_year()

        # Get the user, sign in url, and sign out url
        user, sign_in_url, sign_out_url = utils.get_user()

        # If the user exists (is logged in), render the page
        if user:
            self.render("pages/sales-goals.html",
                        page_title="Sales Goals",
                        sign_out_url=sign_out_url,
                        year=year)

        # If the user is not logged in, redirect to the sign in url
        else:
            self.redirect(sign_in_url)
Example #17
0
async def delete_model_handler(model_id: UUID, uid: str = Depends(get_user())) -> JSONResponse:
    """API handler used to delete model

    Args:
        model_id (UUID): [description]
        uid (str, optional): [description]. Defaults to Depends(get_user()).

    Returns:
        JSONResponse: [description]
    """

    LOGGER.debug('deleting model %s for user %s', model_id, uid)
    model_meta = get_user_model(PG_CREDENTIALS, uid, model_id)
    if model_meta is None:
        return json_response_with_message(status.HTTP_404_NOT_FOUND, 'Cannot find specified model')

    # delete model from S3 bucket and from postgres database
    delete_s3_file('/tensor-trigger/' + str(model_id))
    delete_user_model(PG_CREDENTIALS, uid, model_id)

    content = {'http_code': status.HTTP_200_OK,
               'message': 'Successfully deleted model'}
    return JSONResponse(status_code=status.HTTP_200_OK, content=content)