How to use the grok.app.repository function in grok

To help you get started, we’ve selected a few grok examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github numenta / numenta-apps / grok / grok / app / adapters / datasource / cloudwatch / __init__.py View on Github external
    @repository.retryOnTransientErrors
    def startMonitoringWithRetries():
      """ :returns: metricId """
      with self.connectionFactory() as conn:
        with conn.begin():
          repository.lockOperationExclusive(conn,
                                            repository.OperationLock.METRICS)

          # Check if the metric is already monitored
          matchingMetrics = repository.getCloudwatchMetricsForNameAndServer(
            conn,
            nameColumnValue,
            canonicalResourceName,
            fields=[schema.metric.c.uid, schema.metric.c.parameters])

          for m in matchingMetrics:
            parameters = htmengine.utils.jsonDecode(m.parameters)
github numenta / numenta-apps / grok / grok / app / adapters / datasource / autostack / __init__.py View on Github external
:returns: datasource-specific unique model identifier

    :raises grok.app.exceptions.ObjectNotFoundError: if referenced autostack
      doesn't exist

    :raises grok.app.exceptions.MetricNotSupportedError: if requested metric
      isn't supported

    :raises grok.app.exceptions.MetricAlreadyMonitored: if the metric is already
      being monitored
    """
    metricSpec = modelSpec["metricSpec"]
    autostackId = metricSpec["autostackId"]
    with self.connectionFactory() as conn:
      autostack = repository.getAutostack(conn, autostackId)

    slaveDatasource = metricSpec["slaveDatasource"]
    slaveMetric = metricSpec["slaveMetric"]

    canonicalResourceName = self.getInstanceNameForModelSpec(modelSpec)

    metricAdapter = AutostackMetricAdapterBase.getMetricAdapter(slaveDatasource)
    nameColumnValue = metricAdapter.getMetricName(slaveMetric)
    metricDescription = metricAdapter.getMetricDescription(slaveMetric,
                                                           autostack)
    queryParams = metricAdapter.getQueryParams(nameColumnValue)

    defaultMin = queryParams["min"]
    defaultMax = queryParams["max"]
    defaultPeriod = queryParams["period"]
github numenta / numenta-apps / grok / grok / app / webservices / models_api.py View on Github external
Get model data stats

    ::

        GET /_models/data/stats

    Returns:

    ::

        {
            "processing_time_remaining": 37
        }
    """
    with repository.engineFactory().connect() as conn:
      unprocessedDataCount = repository.getUnprocessedModelDataCount(conn)
    processingTimeRemaining = int(math.ceil(
        unprocessedDataCount * _PROCESSING_TIME_PER_RECORD))

    self.addStandardHeaders()
    return utils.jsonEncode({
        "processing_time_remaining": processingTimeRemaining,
    })
github numenta / numenta-apps / grok / grok / app / runtime / notification_service.py View on Github external
============ ===========
        Key          Description
        ============ ===========
        notification Notification instance
        data         MetricData row that triggered notification
        date         Formatted date (%A, %B %d, %Y)
        time         Formatted time (%I:%M %p (%Z))
        unit         Canonical unit for metric value
        ============ ===========
    """

    subject = grok.app.config.get("notifications", "subject")

    bodyType = "default"
    with engine.connect() as conn:
      metricObj = repository.getMetric(conn, notificationObj.metric)
    if metricObj.datasource == "custom":
      bodyType = "custom"

    body = open(resource_filename(grok.__name__, os.path.join("../conf",
      grok.app.config.get("notifications", "body_" + bodyType)))).read()
    body = body.replace("\n", "\r\n") # Ensure windows newlines

    # Template variable storage (to be expanded in call to str.format())
    templated = dict(notification=notificationObj)

    # Metric
    templated["metric"] = metricObj

    # Instance
    templated["instance"] = metricObj.tag_name or metricObj.server
github numenta / numenta-apps / grok / grok / app / webservices / autostacks_api.py View on Github external
::

          DELETE /_autostacks/{autostackId}/metrics/{metricId}
    """
    try:
      # The if statement makes sure that the metric belongs to the autostack
      with web.ctx.connFactory() as conn:
        autostackObj = repository.getAutostackFromMetric(conn, metricId)

      if autostackId != autostackObj.uid:
        raise InvalidRequestResponse(
          {"result": "Metric=%s does not belong to autostack=%s"
           % (metricId, autostackId)})

      with web.ctx.connFactory() as conn:
        repository.deleteMetric(conn, metricId)

      model_swapper_utils.deleteHTMModel(metricId)
      raise web.HTTPError(status="204 No Content")
    except ObjectNotFoundError:
      raise web.notfound(("Autostack or metric not found: autostack=%s, "
                          "metric=%s") % (autostackId, metricId))
    except web.HTTPError as ex:
      if bool(re.match(r"([45][0-9][0-9])\s?", web.ctx.status)):
        # Log 400-599 status codes as errors, ignoring 200-399
        log.error(str(ex) or repr(ex))
      raise
    except Exception as ex:
      log.exception("DELETE Failed")
      raise web.internalerror(str(ex) or repr(ex))
github numenta / numenta-apps / grok / grok / app / adapters / datasource / cloudwatch / __init__.py View on Github external
def startMonitoringWithRetries():
      """ :returns: metricId """
      with self.connectionFactory() as conn:
        with conn.begin():
          repository.lockOperationExclusive(conn,
                                            repository.OperationLock.METRICS)

          # Check if the metric is already monitored
          matchingMetrics = repository.getCloudwatchMetricsForNameAndServer(
            conn,
            nameColumnValue,
            canonicalResourceName,
            fields=[schema.metric.c.uid, schema.metric.c.parameters])

          for m in matchingMetrics:
            parameters = htmengine.utils.jsonDecode(m.parameters)
            if (parameters["metricSpec"]["dimensions"] ==
                metricSpec["dimensions"]):
              msg = ("monitorMetric: Cloudwatch modelId=%s is already "
                     "monitoring metric=%s on resource=%s; model=%r"
                     % (m.uid, nameColumnValue, canonicalResourceName, m))
github numenta / numenta-apps / grok / grok / app / webservices / instances_api.py View on Github external
if namespace not in supportedNamespaces:
      raise InvalidRequestResponse({"result": ("Not supported. Namespace '%s' "
                                               "was not found.") % namespace})

    try:
      # Attempt to validate instances list using validictory
      validate(instances, _INSTANCES_MODEL_CREATION_SCHEMA)
    except ValidationError as e:
      response = "InvalidArgumentsError: " + str(e)
      raise InvalidRequestResponse({"result": response})

    if instances:
      for instanceId in instances:
        server = "/".join([region, namespace, instanceId])
        with web.ctx.connFactory() as conn:
          numMetrics = repository.getMetricCountForServer(conn, server)
        if numMetrics > 0:
          # Metrics exist for instance id.
          pass

        else:
          try:
            resourceType = cloudwatch.NAMESPACE_TO_RESOURCE_TYPE[namespace]
          except KeyError:
            raise InvalidRequestResponse({"result": "Not supported."})

          modelSpecs = cwAdapter.getDefaultModelSpecs(
              resourceType, region, instanceId, dimension)

          for modelSpec in modelSpecs:
            ModelHandler.createModel(modelSpec)
github numenta / numenta-apps / grok / grok / app / webservices / annotations_api.py View on Github external
Request::

      DELETE /_annotations/{uid}

    :param uid: Annotation ID

    Response::

      HTTP 204 No Content

    """
    try:
      if uid:
        with web.ctx.connFactory() as conn:
          repository.deleteAnnotationById(conn, uid)
        self.addServerHeader()
        raise web.HTTPError(status="204 No Content")
    except app_exceptions.ObjectNotFoundError:
      self.addStandardHeaders()
      raise web.notfound("ObjectNotFoundError Annotation not found: ID: %s"
                         % uid)