def createHTMModel(modelId, params): """ Dispatch command to create HTM model :param modelId: unique identifier of the metric row :param modelParams: model params for creating a scalar model per ModelSwapper interface :param modelSwapper: htmengine.model_swapper.model_swapper_interface object """ with ModelSwapperInterface() as modelSwapper: modelSwapper.defineModel(modelID=modelId, args=params, commandID=createGuid())
def testPOST(self, dataMock, uploadTarfileMock, getMetricMock, getMetricDataMock, _engineFactoryMock): # Set up mocking uploadTarfileMock.return_value = "s3_key" metricId = utils.createGuid() metric = Mock(uid=metricId, description="My Metric Description", server="My Server", location="My Location", datasource="My Datasource", poll_interval=60, parameters="{}", last_timestamp=None, status=MetricStatus.ACTIVE, message=None, collector_error=None, tag_name=None, model_params=None, last_rowid=0) metric.name = "My Metric Name" # Mock the request POST data dataMock.return_value = '{"uid": "%s"}' % metricId # Return metric from mocked repository.getMetric() getMetricMock.return_value = metric getMetricDataMock.return_value = [] # Call the POST function response = logging_api.FeedbackHandler.POST() # Verify results dataMock.assert_called_once() uploadTarfileMock.assert_called_once() self.assertEqual(response, "s3_key")
def deleteHTMModel(modelId): with ModelSwapperInterface() as modelSwapper: modelSwapper.deleteModel(modelID=modelId, commandID=createGuid())
def POST(self): """ Create new Annotation Request:: POST /_annotations { "device", "1231AC32FE", "timestamp":"2013-08-27 16:45:00", "user":"******", "server":" AWS/EC2/i-12345678", "message": "The CPU Utilization was high ...", "data": { JSON Object } } :param device: Device ID if the annotation was created by the mobile app or Service UID if the annotation was created by a service :param timestamp: The date and time to be annotated :param user: User name who created the annotation if the annotation was created by the mobile app or service name if the annotation was created by a service :param server: Instance ID associated with the annotation :param message: Annotation message (Optional if data is provided) :param data: Service specific data associated with this annotation (Optional if message is provided) Response:: HTTP Status 201 Created { "uid": "2a123bb1dd4d46e7a806d62efc29cbb9", "device", "1231AC32FE", "created":"2013-08-27 16:46:51", "timestamp":"2013-08-27 16:45:00", "user":"******", "server":" AWS/EC2/i-12345678", "message": "The CPU Utilization was high ...", "data": {JSON Object } } """ self.addStandardHeaders() webdata = web.data() if webdata: try: if isinstance(webdata, basestring): webdata = utils.jsonDecode(webdata) except ValueError as e: raise web.badrequest("Invalid JSON in request: " + repr(e)) if "device" in webdata: device = webdata["device"] else: raise web.badrequest("Missing 'device' in request") if "timestamp" in webdata: timestamp = webdata["timestamp"] else: raise web.badrequest("Missing 'timestamp' in request") if "user" in webdata: user = webdata["user"] else: raise web.badrequest("Missing 'user' in request") if "server" in webdata: server = webdata["server"] else: raise web.badrequest("Missing 'server' in request") if "message" in webdata: message = webdata["message"] else: message = None if "data" in webdata: data = webdata["data"] else: data = None if data is None and message is None: raise web.badrequest("Annotation must contain either 'message' or 'data'") # lower timestamp resolution to seconds because the database rounds up # microsecond to the nearest second created = datetime.datetime.utcnow().replace(microsecond=0) uid = utils.createGuid() try: with web.ctx.connFactory() as conn: repository.addAnnotation( conn=conn, timestamp=timestamp, device=device, user=user, server=server, message=message, data=data, created=created, uid=uid, ) # Prepare response with generated "uid" and "created" fields filled response = utils.jsonEncode( { "uid": uid, "created": created, "device": device, "timestamp": timestamp, "user": user, "server": server, "message": message, "data": data, } ) raise web.created(response) except app_exceptions.ObjectNotFoundError as ex: raise web.badrequest(str(ex) or repr(ex))
def POST(self): """ Create new Annotation Request:: POST /_annotations { "device", "1231AC32FE", "timestamp":"2013-08-27 16:45:00", "user":"******", "server":" AWS/EC2/i-12345678", "message": "The CPU Utilization was high ...", "data": { JSON Object } } :param device: Device ID if the annotation was created by the mobile app or Service UID if the annotation was created by a service :param timestamp: The date and time to be annotated :param user: User name who created the annotation if the annotation was created by the mobile app or service name if the annotation was created by a service :param server: Instance ID associated with the annotation :param message: Annotation message (Optional if data is provided) :param data: Service specific data associated with this annotation (Optional if message is provided) Response:: HTTP Status 201 Created { "uid": "2a123bb1dd4d46e7a806d62efc29cbb9", "device", "1231AC32FE", "created":"2013-08-27 16:46:51", "timestamp":"2013-08-27 16:45:00", "user":"******", "server":" AWS/EC2/i-12345678", "message": "The CPU Utilization was high ...", "data": {JSON Object } } """ self.addStandardHeaders() webdata = web.data() if webdata: try: if isinstance(webdata, basestring): webdata = utils.jsonDecode(webdata) except ValueError as e: raise web.badrequest("Invalid JSON in request: " + repr(e)) if "device" in webdata: device = webdata["device"] else: raise web.badrequest("Missing 'device' in request") if "timestamp" in webdata: timestamp = webdata["timestamp"] else: raise web.badrequest("Missing 'timestamp' in request") if "user" in webdata: user = webdata["user"] else: raise web.badrequest("Missing 'user' in request") if "server" in webdata: server = webdata["server"] else: raise web.badrequest("Missing 'server' in request") if "message" in webdata: message = webdata["message"] else: message = None if "data" in webdata: data = webdata["data"] else: data = None if data is None and message is None: raise web.badrequest( "Annotation must contain either 'message' or 'data'") # lower timestamp resolution to seconds because the database rounds up # microsecond to the nearest second created = datetime.datetime.utcnow().replace(microsecond=0) uid = utils.createGuid() try: with web.ctx.connFactory() as conn: repository.addAnnotation(conn=conn, timestamp=timestamp, device=device, user=user, server=server, message=message, data=data, created=created, uid=uid) # Prepare response with generated "uid" and "created" fields filled response = utils.jsonEncode({ "uid": uid, "created": created, "device": device, "timestamp": timestamp, "user": user, "server": server, "message": message, "data": data, }) raise web.created(response) except app_exceptions.ObjectNotFoundError as ex: raise web.badrequest(str(ex) or repr(ex))