def testSaveEvents(self): name = 'SR-All-20140326' params = { "desc": "SR daily SCR setpoint: SR and IS PS, RF", "system": "SR", "status": "inactive", "version": 20140326, } newid = saveconfig(self.conn, self.collection, name, **params) new = retrieveconfig(self.conn, self.collection, name=name) self.assertEqual(len(new), 1, "Should find only one entry instead of %s" % len(new)) configidx = new[0]["configidx"] with self.assertRaises(RuntimeError) as context: eventid = saveevent(self.conn, self.collection, configidx=configidx, comment="good snapshot", approval=True, masar_data=None, username="******") self.assertEqual(context.exception.message, "Data set can not be empty.") with self.assertRaises(RuntimeError) as context: eventid = saveevent(self.conn, self.collection, configidx=None, comment="good snapshot", approval=True, masar_data=["element"], username="******") self.assertEqual(context.exception.message, "Cannot identify configuration index number.") with self.assertRaises(ValueError) as context: eventid = saveevent(self.conn, self.collection, configidx=-1, comment="good snapshot", approval=True, masar_data=["element"], username="******") self.assertEqual(context.exception.message, "Unknown configuration index number (%s)" % str(-1)) eventid = saveevent(self.conn, self.collection, configidx=configidx, comment="good snapshot", approval=True, masar_data=["element"], username="******") self.assertNotEqual(eventid, None)
def testSaveEvents(self): name = 'SR-All-20140326' params = {"desc": "SR daily SCR setpoint: SR and IS PS, RF", "system": "SR", "status": "inactive", "version": 20140326, } newid = saveconfig(self.conn, self.collection, name, **params) new = retrieveconfig(self.conn, self.collection, name=name) self.assertEqual(len(new), 1, "Should find only one entry instead of %s"%len(new)) configidx = new[0]["configidx"] with self.assertRaises(RuntimeError) as context: eventid = saveevent(self.conn, self.collection, configidx=configidx, comment="good snapshot", approval=True, masar_data=None, username="******") self.assertEqual(context.exception.message, "Data set can not be empty.") with self.assertRaises(RuntimeError) as context: eventid = saveevent(self.conn, self.collection, configidx=None, comment="good snapshot", approval=True, masar_data=["element"], username="******") self.assertEqual(context.exception.message, "Cannot identify configuration index number.") with self.assertRaises(ValueError) as context: eventid = saveevent(self.conn, self.collection, configidx=-1, comment="good snapshot", approval=True, masar_data=["element"], username="******") self.assertEqual(context.exception.message, "Unknown configuration index number (%s)" % str(-1)) eventid = saveevent(self.conn, self.collection, configidx=configidx, comment="good snapshot", approval=True, masar_data=["element"], username="******") self.assertNotEqual(eventid, None)
def testRetrieveSnapshot(self): name = 'SR-All-20140326' test_comment = "test" test_approval = True test_username = "******" test_masar_data = [0] params = { "desc": "SR daily SCR setpoint: SR and IS PS, RF", "system": "SR", "status": "inactive", "version": 20140326, } newid = saveconfig(self.conn, self.collection, name, **params) new = retrieveconfig(self.conn, self.collection, name=name) self.assertEqual(len(new), 1, "Should find only one entry instead of %s" % len(new)) configidx = new[0]["configidx"] eventid = saveevent(self.conn, self.collection, configidx=configidx, comment=test_comment, approval=test_approval, masar_data=test_masar_data, username=test_username) self.assertNotEqual(eventid, None) result = retrievesnapshot(self.conn, self.collection, eventidx=eventid) self.assertEqual(result["eventidx"], eventid) self.assertEqual(result["configidx"], configidx) self.assertEqual(result["comment"], test_comment) self.assertEqual(result["approval"], test_approval) self.assertEqual(result["username"], test_username) self.assertEqual(result["masar_data"], test_masar_data)
def testRetrieveSnapshot(self): name = 'SR-All-20140326' test_comment = "test" test_approval = True test_username = "******" test_masar_data = [0] params = {"desc": "SR daily SCR setpoint: SR and IS PS, RF", "system": "SR", "status": "inactive", "version": 20140326, } newid = saveconfig(self.conn, self.collection, name, **params) new = retrieveconfig(self.conn, self.collection, name=name) self.assertEqual(len(new), 1, "Should find only one entry instead of %s" % len(new)) configidx = new[0]["configidx"] eventid = saveevent(self.conn, self.collection, configidx=configidx, comment=test_comment, approval=test_approval, masar_data=test_masar_data, username=test_username) self.assertNotEqual(eventid, None) result = retrievesnapshot(self.conn, self.collection, eventidx=eventid) self.assertEqual(result["eventidx"], eventid) self.assertEqual(result["configidx"], configidx) self.assertEqual(result["comment"], test_comment) self.assertEqual(result["approval"], test_approval) self.assertEqual(result["username"], test_username) self.assertEqual(result["masar_data"], test_masar_data)
def testUpdateEvents(self): name = 'SR-All-20140326' test_comment = "test" updated_comment = "updated" test_approval = True updated_approval = False test_username = "******" updated_username = "******" test_masar_data = [0] params = { "desc": "SR daily SCR setpoint: SR and IS PS, RF", "system": "SR", "status": "inactive", "version": 20140326, } newid = saveconfig(self.conn, self.collection, name, **params) new = retrieveconfig(self.conn, self.collection, name=name) self.assertEqual(len(new), 1, "Should find only one entry instead of %s" % len(new)) configidx = new[0]["configidx"] eventid = saveevent(self.conn, self.collection, configidx=configidx, comment=test_comment, approval=test_approval, masar_data=test_masar_data, username=test_username) self.assertNotEqual(eventid, None) with self.assertRaises(RuntimeError) as context: result = updateevent(self.conn, self.collection, configidx=None, comment=updated_comment, approval=updated_approval, username=updated_username) self.assertEqual(context.exception.message, "Unknown MASAR event to update.") with self.assertRaises(RuntimeError) as context: result = updateevent(self.conn, self.collection, eventidx=eventid) self.assertEqual(context.exception.message, "No fields to update.") result = updateevent(self.conn, self.collection, eventidx=eventid, comment=updated_comment, approval=updated_approval, username=updated_username) self.assertTrue(result)
def testUpdateEvents(self): name = 'SR-All-20140326' test_comment = "test" updated_comment = "updated" test_approval = True updated_approval = False test_username = "******" updated_username = "******" test_masar_data = [0] params = {"desc": "SR daily SCR setpoint: SR and IS PS, RF", "system": "SR", "status": "inactive", "version": 20140326, } newid = saveconfig(self.conn, self.collection, name, **params) new = retrieveconfig(self.conn, self.collection, name=name) self.assertEqual(len(new), 1, "Should find only one entry instead of %s" % len(new)) configidx = new[0]["configidx"] eventid = saveevent(self.conn, self.collection, configidx=configidx, comment=test_comment, approval=test_approval, masar_data=test_masar_data, username=test_username) self.assertNotEqual(eventid, None) with self.assertRaises(RuntimeError) as context: result = updateevent(self.conn, self.collection, configidx=None, comment=updated_comment, approval=updated_approval, username=updated_username) self.assertEqual(context.exception.message, "Unknown MASAR event to update.") with self.assertRaises(RuntimeError) as context: result = updateevent(self.conn, self.collection, eventidx=eventid) self.assertEqual(context.exception.message, "No fields to update.") result = updateevent(self.conn, self.collection, eventidx=eventid, comment=updated_comment, approval=updated_approval, username=updated_username) self.assertTrue(result)
def saveSnapshot(self, params): """Save event with data. :param params: a dictionary to carry query condition with structure like: :: [[(channel name,), (string value,),(double value,),(long value,),(dbr type),(is connected), (second past epoch,),(nano seconds,),(time stamp tag,), (alarm severity,),(alarm status,),(alarm message,), (is_array), (array_value) ], {"configname": , # configuration name which the new data set belongs to "comment": [optional], # comment description for this new data set "approval": [optional], # approval status, False is not provided "username": [optional], # user name who commands this action } ] :returns: list with tuple with header description for each field. Structure like: :: [event_id] or [-1] if fault :raises: ValueError """ key = ['configname', 'comment', 'approval', 'username'] config, comment, approval, username = self._parseParams(params[1], key) if config is None: raise ValueError("Unknown configuration when saving a new snapshot event.") if approval is None: approval = False else: approval = bool(json.loads(str(approval).lower())) result = NTMultiChannel(params[0]) dataLen = result.getNumberChannel() if dataLen == 0: raise RuntimeError("No available snapshot data.") # values format: the value is raw data from IOC # [(channel name,), (value,), (dbr type), (is connected), # (second past epoch,), (nano seconds,), (time stamp tag,), # (alarm severity,), (alarm status,), (alarm message,)] pvnames = result.getChannelName() values = result.getValue() dbrtype = result.getDbrType() isconnected = result.getIsConnected() severity = result.getSeverity() status = result.getStatus() message = result.getMessage() sec = result.getSecondsPastEpoch() nanosec = result.getNanoseconds() usertag = result.getUserTag() # data format: the data is prepared to save into rdb # rawdata format # [('channel name', 'string value', 'double value', 'long value', 'dbr type', 'is connected', # 'seconds past epoch', 'nano seconds', 'time stamp tag', 'alarm severity', 'alarm status', # 'alarm message', 'is_array', 'array_value'), # ... # ] datas = [] # get IOC raw data for i in range(dataLen): tmp = [] if isinstance(values[i], (list, tuple)): tmp = [pvnames[i], "", None, None, dbrtype[i], isconnected[i], sec[i], nanosec[i], usertag[i], severity[i], status[i], message[i], 1, values[i]] else: if dbrtype[i] in self.epicsString: tmp = [pvnames[i], values[i], None, None, dbrtype[i], isconnected[i], sec[i], nanosec[i], usertag[i], severity[i], status[i], message[i], 0, None] else: tmp = [pvnames[i], str(values[i]), values[i], values[i], dbrtype[i], isconnected[i], sec[i], nanosec[i], usertag[i], severity[i], status[i], message[i], 0, None] datas.append(tmp) # save into database try: mongoconn, collection = utils.conn(host=os.environ["MASAR_MONGO_HOST"], port=os.environ["MASAR_MONGO_PORT"], db=os.environ["MASAR_MONGO_DB"]) configs = pymasar.retrieveconfig(mongoconn, collection, name=config) if len(configs) != 1: raise RuntimeError("Cannot find a unique configuration.") eid = pymasar.saveevent(mongoconn, collection, configidx=configs[0]["configidx"], comment=comment, approval=approval, username=username, masar_data=datas) utils.close(mongoconn) return [eid, ] except: # keep the same format with a normal operation return [-1]