def getModel(metricId):
   try:
     with web.ctx.connFactory() as conn:
       metric = repository.getMetric(conn,
                                     metricId,
                                     getMetricDisplayFields(conn))
     return metric
   except app_exceptions.ObjectNotFoundError:
     raise web.notfound("ObjectNotFoundError Metric not found: Metric ID: %s"
                        % metricId)
 def getModel(metricId):
     try:
         with web.ctx.connFactory() as conn:
             metric = repository.getMetric(conn, metricId,
                                           getMetricDisplayFields(conn))
         return metric
     except app_exceptions.ObjectNotFoundError:
         raise web.notfound(
             "ObjectNotFoundError Metric not found: Metric ID: %s" %
             metricId)
    def GET(self, metricId=None):
        """ Returns a dict sufficient for importing a new model from scratch """
        try:
            if metricId is not None:
                try:
                    with web.ctx.connFactory() as conn:
                        metricRow = (repository.getMetric(
                            conn,
                            metricId,
                            fields=[
                                schema.metric.c.uid, schema.metric.c.datasource
                            ]))
                    nativeMetrics = [self._exportNativeMetric(metricRow)]
                except app_exceptions.ObjectNotFoundError:
                    raise web.notfound("ObjectNotFoundError Metric not found: "
                                       "Metric ID: %s" % metricId)
            else:
                with web.ctx.connFactory() as conn:
                    metricRowList = repository.getAllModels(conn)
                if metricRowList:
                    nativeMetrics = [
                        self._exportNativeMetric(metricRow)
                        for metricRow in metricRowList
                    ]
                else:
                    nativeMetrics = []

            self.addStandardHeaders()

            web.header("Content-Description", "Taurus Export")
            web.header("Expires", "0")
            web.header("Cache-Control",
                       "must-revalidate, post-check=0, pre-check=0")

            data = web.input(filename=None)

            if data.filename:
                web.header("Content-Disposition",
                           "attachment;filename=%s" % (data.filename))

            returned = utils.jsonEncode(nativeMetrics)

            web.header("Content-length", len(returned))
            return returned
        except web.HTTPError as ex:
            log.info(str(ex) or repr(ex))
            raise ex
        except Exception as ex:
            log.exception("GET Failed")
            raise web.internalerror(str(ex) or repr(ex))
  def GET(self, metricId=None):
    """ Returns a dict sufficient for importing a new model from scratch """
    try:
      if metricId is not None:
        try:
          with web.ctx.connFactory() as conn:
            metricRow = (
              repository.getMetric(conn,
                                   metricId,
                                   fields=[schema.metric.c.uid,
                                           schema.metric.c.datasource]))
          nativeMetrics = [self._exportNativeMetric(metricRow)]
        except app_exceptions.ObjectNotFoundError:
          raise web.notfound("ObjectNotFoundError Metric not found: "
                             "Metric ID: %s" % metricId)
      else:
        with web.ctx.connFactory() as conn:
          metricRowList = repository.getAllModels(conn)
        if metricRowList:
          nativeMetrics = [self._exportNativeMetric(metricRow)
                           for metricRow in metricRowList]
        else:
          nativeMetrics = []

      self.addStandardHeaders()

      web.header("Content-Description", "Taurus Export")
      web.header("Expires", "0")
      web.header("Cache-Control", "must-revalidate, post-check=0, pre-check=0")

      data = web.input(filename=None)

      if data.filename:
        web.header("Content-Disposition", "attachment;filename=%s" % (
          data.filename))

      returned = utils.jsonEncode(nativeMetrics)

      web.header("Content-length", len(returned))
      return returned
    except web.HTTPError as ex:
      log.info(str(ex) or repr(ex))
      raise ex
    except Exception as ex:
      log.exception("GET Failed")
      raise web.internalerror(str(ex) or repr(ex))
  def deleteModel(metricId):
    try:
      with web.ctx.connFactory() as conn:
        metricRow = repository.getMetric(conn, metricId)
    except app_exceptions.ObjectNotFoundError:
      raise web.notfound("ObjectNotFoundError Metric not found: Metric ID: %s"
                         % metricId)

    log.debug("Deleting model for %s metric=%s", metricRow.datasource,
              metricId)
    # NOTE: this is the new way using datasource adapters
    try:
      createDatasourceAdapter(metricRow.datasource).unmonitorMetric(metricId)
    except app_exceptions.ObjectNotFoundError:
      raise web.notfound(
        "ObjectNotFoundError Metric not found: Metric ID: %s" % (metricId,))

    return utils.jsonEncode({'result': 'success'})
    def createModel(cls, modelSpec=None):
        """
    NOTE MER-3479: this code path is presently incorrectly used for two
      purposes:
        * Importing of all types of metrics (not desirable; there should be a
          separate endpoint or an import-specific flag in this endpoint for
          importing that facilitates slightly different behavior, such as
          suppressing certain errors to allow for re-import in case of tranisent
          error part way through the prior import)
    """

        if not modelSpec:
            # Metric data is missing
            log.error(
                "Data is missing in request, raising BadRequest exception")
            raise InvalidRequestResponse({"result": "Metric data is missing"})

        # TODO MER-3479: import using import-specific endpoint
        # NOTE: pending MER-3479, this is presently a hack for exercising
        #   the adapter import API
        importing = False

        if modelSpec.get("datasource") == "custom":
            if "data" in modelSpec:
                importing = True

        try:
            adapter = createDatasourceAdapter(modelSpec["datasource"])
            try:
                # TODO: Maybe initialize transaction and commit here
                if importing:
                    # TODO MER-3479: import using import-specific endpoint
                    # NOTE: pending MER-3479, this is presently a hack for exercising
                    #   the adapter import API
                    metricId = adapter.importModel(modelSpec)
                else:
                    metricId = adapter.monitorMetric(modelSpec)
            except app_exceptions.MetricAlreadyMonitored as e:
                metricId = e.uid

            with web.ctx.connFactory() as conn:
                return repository.getMetric(conn, metricId)
        except (ValueError, app_exceptions.MetricNotSupportedError) as e:
            raise InvalidRequestResponse({"result": repr(e)})
  def createModel(cls, modelSpec=None):
    """
    NOTE MER-3479: this code path is presently incorrectly used for two
      purposes:
        * Importing of all types of metrics (not desirable; there should be a
          separate endpoint or an import-specific flag in this endpoint for
          importing that facilitates slightly different behavior, such as
          suppressing certain errors to allow for re-import in case of tranisent
          error part way through the prior import)
    """

    if not modelSpec:
      # Metric data is missing
      log.error("Data is missing in request, raising BadRequest exception")
      raise InvalidRequestResponse({"result": "Metric data is missing"})

    # TODO MER-3479: import using import-specific endpoint
    # NOTE: pending MER-3479, this is presently a hack for exercising
    #   the adapter import API
    importing = False

    if modelSpec.get("datasource") == "custom":
      if "data" in modelSpec:
        importing = True

    try:
      adapter = createDatasourceAdapter(modelSpec["datasource"])
      try:
        # TODO: Maybe initialize transaction and commit here
        if importing:
          # TODO MER-3479: import using import-specific endpoint
          # NOTE: pending MER-3479, this is presently a hack for exercising
          #   the adapter import API
          metricId = adapter.importModel(modelSpec)
        else:
          metricId = adapter.monitorMetric(modelSpec)
      except app_exceptions.MetricAlreadyMonitored as e:
        metricId = e.uid

      with web.ctx.connFactory() as conn:
        return repository.getMetric(conn, metricId)
    except (ValueError, app_exceptions.MetricNotSupportedError) as e:
      raise InvalidRequestResponse({"result": repr(e)})
Exemple #8
0
 def _getMetric():
     return repository.getMetric(engine, uid)