def testNEQ(self): item1 = models.House(id=1, deploymentId = 1, address = "10 Test Address", startDate = NOW, endDate = NOW, ) item2 = models.House(id=1, deploymentId = 1, address = "10 Test Address", startDate = NOW, endDate = NOW, ) self.assertEqual(item1, item2) #AGAIN, Id and Deployment Id do not mater so much item2.address = "FOO" self.assertNotEqual(item1, item2) self.assertReallyNotEqual(item1, item2) item2.address = item1.address item2.startDate = datetime.datetime.now() self.assertNotEqual(item1, item2) self.assertReallyNotEqual(item1, item2)
def testCmp(self): #Test Comparison item1 = models.House(id=1, deploymentId = 1, address = "10 Test Address", startDate = NOW, endDate = NOW, ) item2 = models.House(id=1, deploymentId = 1, address = "10 Test Address", startDate = NOW, endDate = NOW, ) self.assertEqual(item1, item2) #Order On Name item2.address = "1 Test" self.assertGreater(item1, item2) item2.address = "100 Test" self.assertLess(item1, item2) item2.address = item1.address item2.startDate = datetime.datetime.now() self.assertLess(item1, item2) item1.startDate = datetime.datetime.now() self.assertGreater(item1, item2) #Order on end-date item2.address = item1.address item2.endDate = datetime.datetime.now() self.assertLess(item2, item1) self.assertGreater(item1, item2) #Order on start Date item2.endDate = item1.endDate item2.startDate = datetime.datetime.now() self.assertLess(item1, item2) self.assertGreater(item2, item1)
def _serialobj(self): """Helper Method to provde an object to serialise""" # rType = models.RoomType(id=1, # name="Test Type") theItem = models.House(id=1, deploymentId = 1, address = "10 Test Address", startDate = NOW, endDate = NOW, ) return theItem
def testEq(self): #"""Test for Equality""" item1 = models.House(id=1, deploymentId = 1, address = "10 Test Address", startDate = NOW, endDate = NOW, ) item2 = models.House(id=1, deploymentId = 1, address = "10 Test Address", startDate = NOW, endDate = NOW, ) self.assertEqual(item1, item2) self.assertReallyEqual(item1, item2) #Id should not make any difference item2.id = 5 self.assertEqual(item1, item2) self.assertReallyEqual(item1, item2)
def populatedata(): """Populate our testing database with an example deployment etc""" LOG = logging.getLogger(__name__) LOG.setLevel(logging.DEBUG) LOG.debug("Populating Testing Data") #The Deployment session = meta.Session() #now = datetime.datetime.now() now = datetime.datetime(2013, 01, 01, 00, 00, 00) thedeployment = models.Deployment(id=1, name="testing", description="a testing deployment", startDate=now) session.merge(thedeployment) session.flush() #We also add a testing house thehouse = models.House(id=1, address="testing house", deploymentId=thedeployment.id, startDate=now) session.merge(thehouse) session.flush() #Lets add two locations thebedroom = models.Location(id=1, houseId=thehouse.id, roomId=1) thebathroom = models.Location(id=2, houseId=thehouse.id, roomId=6) session.merge(thebedroom) session.merge(thebathroom) session.flush() #update the nodes so they have the correct locations thenode = session.query(models.Node).filter_by(id=837).first() if thenode is None: thenode = models.Node(id=837) session.add(thenode) thenode.locationId = 1 thenode = session.query(models.Node).filter_by(id=838).first() if thenode is None: thenode = models.Node(id=838) session.add(thenode) thenode.locationId = 2 session.flush() transaction.commit() #Now we want to add a load of readings thetime = now # - datetime.timedelta(days = 10) endtime = now + datetime.timedelta(days=10) #print "START TIME {0}".format(starttime) transaction.commit() #To test the Yields I also want an incomplte database thehouse = models.House(id=2, address="Poor House", deploymentId=thedeployment.id, startDate=now) session.merge(thehouse) #Add a couple of locations #Lets add two locations thebedroom = models.Location(id=3, houseId=thehouse.id, roomId=1) session.merge(thebedroom) thebathroom = models.Location(id=4, houseId=thehouse.id, roomId=6) session.merge(thebathroom) #And More Nodes thenode = session.query(models.Node).filter_by(id=1061).first() if thenode is None: thenode = models.Node(id=1061) session.add(thenode) thenode.locationId = 3 thenode = session.query(models.Node).filter_by(id=1063).first() if thenode is None: thenode = models.Node(id=1063) session.add(thenode) thenode.locationId = 4 session.flush() transaction.commit() #Add Nodestates thecount = 0.0 seqnum = -1 while thetime < endtime: #Increment and roll over the sequence number seqnum += 1 if seqnum > 255: seqnum = seqnum - 255 for nid in [837, 838, 1061, 1063]: locationid = 1 if nid == 838: locationid = 2 elif nid == 1061: locationid = 3 #Sample very 10 minutes (50% Yield) if thetime.minute % 10 == 0: continue elif nid == 1063: locationid = 4 #And remove every 3rd sample if thetime.minute % 15 == 0: continue ns = models.NodeState(nodeId=nid, parent=1, time=thetime, localtime=seqnum, seq_num=seqnum) session.add(ns) reading = models.Reading( nodeId=nid, typeId=0, time=thetime, locationId=locationid, value=18.0 + (2.0 * math.sin(thecount)), ) session.add(reading) #Increment the time thetime = thetime + datetime.timedelta(minutes=5) thecount = thecount + (3.14 / 144) transaction.commit()
def populatedata(session): """Populate with some testing data""" now = datetime.datetime.utcnow() startdate = now - datetime.timedelta(days=14) cutdate = now - datetime.timedelta(days=7) enddate = now - datetime.timedelta(hours=1) #Add A Deployment thedeployment = models.Deployment(id=1, name="Testing Deployments") session.add(thedeployment) session.flush() #And three servers (However only two are actually deployed) theserver = models.Server(id=1, hostname="server1", baseid=41) session.add(theserver) theserver = models.Server(id=2, hostname="server2", baseid=42) session.add(theserver) theserver = models.Server(id=3, hostname="server3", baseid=44) session.add(theserver) session.flush() #Add some houses thehouse = models.House(id=1, address="address1", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=2, address="address2", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=3, address="address3", deploymentId=1, serverid=1) session.add(thehouse) thehouse = models.House(id=4, address="address4", deploymentId=1, serverid=2) session.add(thehouse) #House that is out of date thehouse = models.House(id=5, address="address5", deploymentId=1, serverid=2) session.add(thehouse) #House missing one node thehouse = models.House(id=6, address="address6", deploymentId=1) session.add(thehouse) #Next nodes and locations (two for each house) locidx = 1 allnodes = [] for house in range(5): for room in range(2): thisloc = models.Location(id=locidx, houseId=house + 1, roomId=room + 1) session.add(thisloc) nid = house * 10 + room thisnode = models.Node(id=nid, locationId=locidx) session.add(thisnode) allnodes.append(nid) session.flush() locidx += 1 nidx = 1000 for x in range(2): thisnode = models.Node(id=nidx) session.add(thisnode) nidx += 1 session.flush() session.commit() #STUFF FOR PUSH STATUS currenttime = startdate while currenttime <= cutdate: for server in ["server1", "server2", "server3"]: pstat = models.PushStatus(time=currenttime, hostname=server) session.add(pstat) currenttime += datetime.timedelta(hours=2) session.flush() session.commit() while currenttime <= enddate: for server in ["server1", "server2"]: pstat = models.PushStatus(time=currenttime, hostname=server) session.add(pstat) currenttime += datetime.timedelta(hours=2) #Add readings / nodestates every two hours for two weeks #But stop 3 days ago currenttime = startdate seq_num = 1 while currenttime <= cutdate: for nid in allnodes: thereading = models.Reading(time=currenttime, nodeId=nid, typeId=0, value=0) thestate = models.NodeState(time=currenttime, nodeId=nid, seq_num=seq_num) session.add(thereading) session.add(thestate) session.flush() currenttime = currenttime + datetime.timedelta(hours=2) seq_num += 1 session.flush() session.commit() logging.debug("ALL NODES {0}".format(allnodes)) #And then for all but the nodes assocated with house 1 while currenttime <= enddate: for nid in allnodes[3:]: thereading = models.Reading(time=currenttime, nodeId=nid, typeId=0, value=0) thestate = models.NodeState(time=currenttime, nodeId=nid, seq_num=seq_num) session.add(thereading) session.add(thestate) session.flush() currenttime = currenttime + datetime.timedelta(hours=2) seq_num += 1 session.flush() session.commit() #STUFF FOR PUSLE COUNT currenttime = startdate pcount = 0 qry = session.query(models.SensorType).filter_by(name="Gas Pulse Count") gas_sensor = qry.first() while currenttime <= cutdate: #Let node 10, 11 work thereading = models.Reading(time=currenttime, nodeId=40, typeId=gas_sensor.id, value=pcount) session.add(thereading) thereading = models.Reading(time=currenttime, nodeId=41, typeId=gas_sensor.id, value=pcount) session.add(thereading) pcount += 1 currenttime += datetime.timedelta(hours=2) while currenttime <= enddate: thereading = models.Reading(time=currenttime, nodeId=40, typeId=gas_sensor.id, value=pcount) session.add(thereading) thereading = models.Reading(time=currenttime, nodeId=41, typeId=gas_sensor.id, value=100) session.add(thereading) pcount += 1 currenttime += datetime.timedelta(hours=2) session.flush() session.commit()
def housedetails(request): """Status screen for an individual house""" log = logging.getLogger(__name__) outdict = {} outdict["headLinks"] = homepage.genHeadUrls(request) outdict["sideLinks"] = homepage.genSideUrls(request) theUser = homepage.getUser(request) outdict["user"] = theUser.username outdict["nodeDropdowns"] = homepage.getNodeDropdowns() #Empty Dictionary outdict["pgTitle"] = "House Status" outdict["thehouse"] = "No Such House" outdict["servername"] = None outdict["lastpush"] = None outdict["skew"] = None theId = request.matchdict.get("id", None) log.debug("----- Status for House Id {0} -----".format(theId)) qry = DBSession.query(models.House).filter_by(id=theId) thishouse = qry.first() if thishouse: outdict["pgTitle"] = "Status of House {0}".format(thishouse.address) outdict["thehouse"] = thishouse else: outdict["thehouse"] = models.House(address="No Such House") return outdict #So First find the server that is associated #qry = DBSession.query(models.Server).filter_by(houseid = theId) theserver = thishouse.server if theserver: outdict["servername"] = theserver.hostname #And Last Push qry = DBSession.query(models.PushStatus) qry = qry.filter_by(hostname=theserver.hostname) qry = qry.order_by(models.PushStatus.time) lastpush = qry.first() if lastpush: outdict["lastpush"] = lastpush.time outdict["skew"] = lastpush.time - lastpush.localtime else: outdict["server"] = None outdict["lastpush"] = None outdict["skew"] = None #Details for individual Nodes #locationIds = [x.id for x in thishouse.locations] #log.debug("Locations for House {0}".format(locationIds)) nodedetails = [] checkednodes = [] nodepairs = [] #Pair of node / locations now = datetime.datetime.utcnow() yieldtime = now - datetime.timedelta(days=7) for location in thishouse.locations: log.debug("--> Getting Details for Location {0}".format(location)) nodeqry = DBSession.query(models.Node).filter_by(locationId=location.id) for node in nodeqry: log.debug("--> --> Getting Details for Node {0}".format(node)) itemdict = {"id": node.id, "location": "{0} ({1})".format(location.room.name, location.id), "lastreading":None, "laststate":None, "yield":None, "compression":None, "resets":None, "voltage":0.0, } #Work out the last reading qry = DBSession.query(models.Reading) qry = qry.filter_by(nodeId=node.id, locationId=location.id, typeId=6) qry = qry.order_by(models.Reading.time.desc()) lastreading = qry.first() if lastreading is None: qry = DBSession.query(models.Reading) qry = qry.filter_by(nodeId=node.id, locationId=location.id, typeId=0) qry = qry.order_by(models.Reading.time.desc()) lastreading = qry.first() if lastreading: itemdict["lastreading"] = lastreading.time else: itemdict["lastreading"] = lastreading.time log.debug("VOLTAGE:".format(lastreading.value)) itemdict["voltage"] = lastreading.value #Last Nodestate qry = DBSession.query(models.NodeState).filter_by(nodeId=node.id) qry = qry.order_by(models.NodeState.time.desc()) laststate = qry.first() if laststate: itemdict["laststate"] = laststate.time #Finally Yield values = yields.calcyieldNew(node.id, yieldtime) itemdict["yield"] = values[1] itemdict["compression"] = values[2] itemdict["resets"] = values[3] checkednodes.append(node.id) nodedetails.append(itemdict) nodepairs.append([node.id, location.id]) outdict["nodecount"] = len(checkednodes) outdict["nodedetails"] = nodedetails outdict["nodepairs"] = json.dumps(nodepairs) return outdict