示例#1
0
    def GET(self, deviceId):
        """
      Get notification settings for device.

      ::

          GET /_notifications/{deviceId}/settings

      Returns:

      ::

          {
            "email_addr": "*****@*****.**",
            "windowsize": 3600,
            "sensitivity": 0.99999,
            "last_timestamp": "2014-02-06 00:00:00",
            "uid": "9a90eaf2-6374-4230-aa96-0830c0a737fe"
          }

      :param email_addr: Target email address associated with device
      :type email_addr: string
      :param windowsize: Notification window in seconds during which no other
        notifications for a given instance should be sent to a given device
      :type windowsize: int
      :param sensitivity: Anomaly score threshold that should trigger a
        notification
      :type sensitivity: float
      :param last_timestamp: Last updated timestamp
      :type last_timestamp: timestamp
      :param uid: Notification ID
      :type uid: str
    """
        if deviceId:
            try:
                with web.ctx.connFactory() as conn:
                    settingsRow = repository.getDeviceNotificationSettings(
                        conn, deviceId)

                settingsDict = dict([(col.name, settingsRow[col.name])
                                     for col in schema.notification_settings.c
                                     ])

                self.addStandardHeaders()
                return utils.jsonEncode(settingsDict)
            except ObjectNotFoundError:
                return web.notfound("Notification Settings not found: %s" %
                                    deviceId)
        else:
            log.error("Missing device ID, raising BadRequest exception")
            raise web.badrequest("Missing device ID")
  def GET(self, deviceId):
    """
      Get notification settings for device.

      ::

          GET /_notifications/{deviceId}/settings

      Returns:

      ::

          {
            "email_addr": "*****@*****.**",
            "windowsize": 3600,
            "sensitivity": 0.99999,
            "last_timestamp": "2014-02-06 00:00:00",
            "uid": "9a90eaf2-6374-4230-aa96-0830c0a737fe"
          }

      :param email_addr: Target email address associated with device
      :type email_addr: string
      :param windowsize: Notification window in seconds during which no other
        notifications for a given instance should be sent to a given device
      :type windowsize: int
      :param sensitivity: Anomaly score threshold that should trigger a
        notification
      :type sensitivity: float
      :param last_timestamp: Last updated timestamp
      :type last_timestamp: timestamp
      :param uid: Notification ID
      :type uid: str
    """
    if deviceId:
      try:
        with web.ctx.connFactory() as conn:
          settingsRow = repository.getDeviceNotificationSettings(conn, deviceId)

        settingsDict = dict([(col.name, settingsRow[col.name])
                            for col in schema.notification_settings.c])

        self.addStandardHeaders()
        return utils.jsonEncode(settingsDict)
      except ObjectNotFoundError:
        return web.notfound("Notification Settings not found: %s" % deviceId)
    else:
      log.error("Missing device ID, raising BadRequest exception")
      raise web.badrequest("Missing device ID")
  def PUT(self, deviceId):
    """
      Create, or update notification settings for device.

      ::

          PUT /_notifications/{deviceId}/settings

          {
            "email_addr": "*****@*****.**",
            "windowsize": 3600,
            "sensitivity": 0.99999
          }

      :param email_addr: Target email address associated with device
      :type email_addr: string
      :param windowsize: Notification window in seconds during which no other
        notifications for a given instance should be sent to a given device
      :type windowsize: int
      :param sensitivity: Anomaly score threshold that should trigger a
        notification
      :type sensitivity: float
    """
    data = web.data()

    if data:
      data = utils.jsonDecode(data) if isinstance(data, basestring) else data

      try:
        with web.ctx.connFactory() as conn:
          settingsRow = repository.getDeviceNotificationSettings(conn, deviceId)

        settingsDict = dict([(col.name, settingsRow[col.name])
                            for col in schema.notification_settings.c])
      except ObjectNotFoundError:
        settingsDict = None

      if settingsDict:
        # Update existing
        changes = dict()

        if "windowsize" in data:
          changes["windowsize"] = data["windowsize"]

        if "sensitivity" in data:
          changes["sensitivity"] = data["sensitivity"]

        if "email_addr" in data:
          changes["email_addr"] = data["email_addr"]

        if changes:
          log.info("Notification settings updated for email=%s, "
                   "deviceid=%s, %r",
                   anonymizeEmail(settingsDict["email_addr"]),
                   deviceId,
                   changes.keys())
          with web.ctx.connFactory() as conn:
            repository.updateDeviceNotificationSettings(conn, deviceId, changes)

        self.addStandardHeaders()
        for (header, value) in web.ctx.headers:
          if header == "Content-Type":
            web.ctx.headers.remove((header, value))
        raise web.HTTPError(status="204 No Content")

      else:
        # Create new settings

        if "windowsize" in data:
          windowsize = data["windowsize"]
        else:
          windowsize = 60*60 # TODO: Configurable default

        if "sensitivity" in data:
          sensitivity = data["sensitivity"]
        else:
          sensitivity = 0.99999 # TODO: Configurable default

        if "email_addr" in data:
          email_addr = data["email_addr"]
        else:
          email_addr = None

        with web.ctx.connFactory() as conn:
          repository.addDeviceNotificationSettings(conn,
                                                   deviceId,
                                                   windowsize,
                                                   sensitivity,
                                                   email_addr)
        log.info("Notification settings created for deviceid=%s", deviceId)
        self.addStandardHeaders()
        raise web.created("")

    else:
      # Metric data is missing
      log.error("Data is missing in request, raising BadRequest exception")
      raise web.badrequest("Metric data is missing")