예제 #1
0
 def testDeleteModelInvalid(self, getMetricMock, _engineMock):
   getMetricMock.side_effect = ObjectNotFoundError("Test")
   response = self.app.delete("/12232-jn939", headers=self.headers,
     status="*")
   assertions.assertNotFound(self, response)
   self.assertEqual("ObjectNotFoundError Metric not found:"
                    " Metric ID: 12232-jn939", response.body)
예제 #2
0
def deleteMetric(conn, metricId):
    """Delete metric

  :param conn: SQLAlchemy connection object
  :type conn: sqlalchemy.engine.base.Connection
  :param metricId: Metric uid
  :returns: Result of delete operation
  :rtype: sqlalchemy.engine.result.ResultProxy
  """
    result = None

    with conn.begin():

        # Make sure to clear the model. This call will also make sure data integrity
        # is kept by deleting any related data when necessary
        deleteModel(conn, metricId)

        # Delete metric
        result = (
            conn.execute(schema.metric.delete()  # pylint: disable=E1120
                         .where(schema.metric.c.uid == metricId)))

        if result.rowcount == 0:
            raise ObjectNotFoundError("Metric not found for uid=%s" % metricId)

    return result
예제 #3
0
def deleteModel(conn, metricId):
    """Delete the model by resetting model-specific attributes.
  This method will also make sure the data integrity is kept by removing any
  model-related data when necessary, either at the model level or at the
  server/instance level

  :param conn: SQLAlchemy connection object
  :type conn: sqlalchemy.engine.Connection
  :param metricId: Metric uid
  """
    # TODO MER-3781: Deleting custom model should also delete notifications

    with conn.begin():
        # Reset model and model data
        update = (
            schema.metric.update()  # pylint: disable=E1120
            .values(parameters=None,
                    model_params=None,
                    status=MetricStatus.UNMONITORED,
                    message=None).where(schema.metric.c.uid == metricId))

        result = conn.execute(update)

        if result.rowcount == 0:
            raise ObjectNotFoundError("Metric not found for uid=%s" % metricId)

        update = (
            schema.metric_data.update()  # pylint: disable=E1120
            .values(anomaly_score=None,
                    raw_anomaly_score=None,
                    display_value=None,
                    multi_step_best_predictions=None).where(
                        schema.metric_data.c.uid == metricId))

        conn.execute(update)
예제 #4
0
def _getMetricImpl(conn, metricId, fields, lockKind):
    """Get Metric given metric uid

  :param conn: SQLAlchemy;
  :type conn: sqlalchemy.engine.Connection

  :param metricId: Metric uid
  :type metricId: str

  :param fields: Sequence of columns to be returned by underlying query
  :returns: Metric

  :param lockKind: None for no lock or one of the _SelectLock constants to
    choose either "SELECT ... LOCK IN SHARE MODE" or "SELECT ... FOR UPDATE".

  :rtype: sqlalchemy.engine.RowProxy

  :raises ObjectNotFoundError: if a row with the requested metricId wasn't found
  """
    fields = fields or [schema.metric]

    sel = select(fields).where(schema.metric.c.uid == metricId)
    if lockKind is not None:
        sel = sel.with_for_update(read=lockKind)

    result = conn.execute(sel)
    metric = result.first()

    if metric is None:
        raise ObjectNotFoundError("Metric not found for uid=%s" % (metricId, ))

    return metric
예제 #5
0
def setMetricLastTimestamp(conn, metricId, value):
  """Set Timestamp for most recent update of metric

  :param conn: SQLAlchemy connection object
  :type conn: sqlalchemy.engine.base.Connection
  :param metricId: Metric uid
  :type metricId: str
  :param value: Timestamp value
  :type value: str
  :raises: htmengine.exceptions.ObjectNotFoundError if no match
  """
  update = schema.metric.update().where(schema.metric.c.uid == metricId)
  result = conn.execute(update.values(last_timestamp=value))

  if result.rowcount == 0:
    raise ObjectNotFoundError("Metric not found for uid=%s" % metricId)
예제 #6
0
def setMetricCollectorError(conn, metricId, value):
  """Set Metric given metric uid

  :param conn: SQLAlchemy connection object
  :type conn: sqlalchemy.engine.base.Connection
  :param metricId: Metric uid
  :type metricId: str
  :param value: Collector error value
  :type value: str
  :raises: htmengine.exceptions.ObjectNotFoundError if no match
  """
  update = schema.metric.update().where(schema.metric.c.uid == metricId)
  result = conn.execute(update.values(collector_error=value))

  if result.rowcount == 0:
    raise ObjectNotFoundError("Metric not found for uid=%s" % metricId)
예제 #7
0
def getCustomMetricByName(conn, name, fields=None):
    """Get Metric given metric name and datasource

  :param conn: SQLAlchemy connection object
  :type conn: sqlalchemy.engine.base.Connection
  :param name: Metric name
  :returns: Metric
  :rtype: sqlalchemy.engine.RowProxy
  """
    where = ((schema.metric.c.name == name) &
             (schema.metric.c.datasource == "custom"))

    result = _getMetrics(conn, fields=fields, where=where)

    metricObj = result.first()
    if metricObj is None:
        raise ObjectNotFoundError("Custom metric not found for name=%s" % name)

    return metricObj
예제 #8
0
def setMetricStatus(conn, metricId, status, message=None, refStatus=None):
    """Set metric status

  :param conn: SQLAlchemy connection object
  :type conn: sqlalchemy.engine.base.Connection

  :param metricId: Metric uid
  :type metricId: str

  :param status: metric status value to set
  :type status: one of MetricStatus values

  :param message: message to set; clears message field by default
  :type message: string or None; if None, the message field will be cleared

  :param refStatus: reference status; if None (default), the requested values
      will be set regardless of the current value of the row's status field;
      otherwise, the status will be updated only if the metric row's current
      status is refStatus (checked automically). If the current value was not
      refStatus, then upon return, the reloaded metric dao's `status`
      attribute will reflect the status value that was in the metric row at
      the time the update was attempted instead of the requested status value.
  :type refStatus: None or one of MetricStatus values

  :raises: htmengine.exceptions.ObjectNotFoundError if no match
  """
    updateValues = {"status": status, "message": message}

    update = (
        schema.metric.update()  # pylint: disable=E1120
        .where(schema.metric.c.uid == metricId))

    if refStatus is not None:
        # Add refStatus match to the predicate
        update = update.where(schema.metric.c.status == refStatus)

    result = conn.execute(update.values(updateValues))

    # "result.rowcount" returns the number of rows matching the where expression
    if result.rowcount == 0:
        raise ObjectNotFoundError("Metric not found for uid=%s" % metricId)