Ejemplo n.º 1
0
def validate_model_version(version: int) -> None:
    if not isinstance(version, int):
        raise SubmarineException(
            f"Model version must be an integer, got {type(version)} type.")
    elif version < 1:
        raise SubmarineException(
            f"Model version must bigger than 0, but got {version}")
Ejemplo n.º 2
0
def validate_description(description: Optional[str]) -> None:
    if not isinstance(description, str) and description is not None:
        raise SubmarineException(
            f"Description must be String or None, but got {type(description)}")
    if isinstance(description, str) and len(description) > 5000:
        raise SubmarineException(
            f"Description must less than 5000 words, but got {len(description)}"
        )
Ejemplo n.º 3
0
def validate_tag(tag: str) -> None:
    """Check that `tag` is a valid tag value and raise an exception if it isn't."""
    # Reuse param & metric check.
    if tag is None or tag == "":
        raise SubmarineException("Tag cannot be empty.")
    if not _VALID_PARAM_AND_METRIC_NAMES.match(tag):
        raise SubmarineException("Invalid tag name: '%s'. %s" %
                                 (tag, _BAD_CHARACTERS_MESSAGE))
Ejemplo n.º 4
0
def _validate_metric_name(name):
    """Check that `name` is a valid metric name and raise an exception if it isn't."""
    if not _VALID_PARAM_AND_METRIC_NAMES.match(name):
        raise SubmarineException(
            "Invalid metric name: '%s'. %s" %
            (name, _BAD_CHARACTERS_MESSAGE), )

    if path_not_unique(name):
        raise SubmarineException("Invalid metric name: '%s'. %s" %
                                 (name, bad_path_message(name)))
Ejemplo n.º 5
0
def verify_rest_response(response, endpoint):
    """Verify the return code and raise exception if the request was not successful."""
    if response.status_code != 200:
        if _can_parse_as_json(response.text):
            raise RestException(json.loads(response.text))
        else:
            base_msg = "API request to endpoint %s failed with error code " \
                       "%s != 200" % (endpoint, response.status_code)
            raise SubmarineException("%s. Response body: '%s'" %
                                     (base_msg, response.text))
    return response
Ejemplo n.º 6
0
def validate_metric(key, value, timestamp, step):
    """
    Check that a param with the specified key, value, timestamp is valid and raise an exception if
    it isn't.
    """
    _validate_metric_name(key)
    if not isinstance(value, numbers.Number):
        raise SubmarineException(
            "Got invalid value %s for metric '%s' (timestamp=%s). Please specify value as a valid "
            "double (64-bit floating point)" % (value, key, timestamp), )

    if not isinstance(timestamp, numbers.Number) or timestamp < 0:
        raise SubmarineException(
            "Got invalid timestamp %s for metric '%s' (value=%s). Timestamp must be a nonnegative "
            "long (64-bit integer) " % (timestamp, key, value), )

    if not isinstance(step, numbers.Number):
        raise SubmarineException(
            "Got invalid step %s for metric '%s' (value=%s). Step must be a valid long "
            "(64-bit integer)." % (step, key, value), )
Ejemplo n.º 7
0
 def make_managed_session():
     """Provide a transactional scope around a series of operations."""
     session = SessionMaker()
     try:
         yield session
         session.commit()
     except SubmarineException:
         session.rollback()
         raise
     except Exception as e:
         session.rollback()
         raise SubmarineException(message=e)
     finally:
         session.close()
Ejemplo n.º 8
0
def extract_db_type_from_uri(db_uri):
    """
    Parse the specified DB URI to extract the database type. Confirm the database type is
    supported. If a driver is specified, confirm it passes a plausible regex.
    """
    scheme = urllib.parse.urlparse(db_uri).scheme
    scheme_plus_count = scheme.count('+')

    if scheme_plus_count == 0:
        db_type = scheme
    elif scheme_plus_count == 1:
        db_type, _ = scheme.split('+')
    else:
        error_msg = "Invalid database URI: '%s'. %s" % (db_uri, 'INVALID_DB_URI_MSG')
        raise SubmarineException(error_msg)

    return db_type
Ejemplo n.º 9
0
def _validate_length_limit(entity_name, limit, value):
    if len(value) > limit:
        raise SubmarineException(
            "%s '%s' had length %s, which exceeded length limit of %s" %
            (entity_name, value[:250], len(value), limit))
Ejemplo n.º 10
0
def _validate_db_type_string(db_type):
    """validates db_type parsed from DB URI is supported"""
    if db_type not in DATABASE_ENGINES:
        error_msg = "Invalid database engine: '%s'. '%s'" % (
            db_type, _UNSUPPORTED_DB_TYPE_MSG)
        raise SubmarineException(error_msg)
Ejemplo n.º 11
0
def get_canonical_stage(stage: str) -> str:
    key = stage.lower()
    if key not in _CANONICAL_MAPPING:
        raise SubmarineException(f"Invalid Model Version stage {stage}.")
    return _CANONICAL_MAPPING[key]
Ejemplo n.º 12
0
def _validate_param_name(name):
    """Check that `name` is a valid parameter name and raise an exception if it isn't."""
    if not _VALID_PARAM_AND_METRIC_NAMES.match(name):
        raise SubmarineException(
            "Invalid parameter name: '%s'. %s" %
            (name, _BAD_CHARACTERS_MESSAGE), )
Ejemplo n.º 13
0
def _validate_db_type_string(db_type):
    """validates db_type parsed from DB URI is supported"""
    if db_type not in DATABASE_ENGINES:
        raise SubmarineException(
            f"Invalid database engine: '{db_type}'. '{_UNSUPPORTED_DB_TYPE_MSG}'"
        )
Ejemplo n.º 14
0
def validate_model_name(name: str) -> None:
    if name is None or name == "":
        raise SubmarineException("Model name cannot be empty.")
Ejemplo n.º 15
0
def validate_tags(tags: Optional[List[str]]) -> None:
    if tags is not None and not isinstance(tags, list):
        raise SubmarineException("parameter tags must be list or None.")
    for tag in tags or []:
        validate_tag(tag)