class Test_audit(unittest.TestCase): def setUp(self): self._mdb = MUGAlyserMongoDB( uri="mongodb://localhost/TEST_AUDIT" ) self._audit = Audit( self._mdb ) def tearDown(self): self._mdb.client().drop_database( "TEST_AUDIT" ) #@unittest.skip def test_get_current_batch_id(self): self.assertFalse( self._audit.in_batch()) batch_id = self._audit.start_batch( doc = { "test" : "doc"}) self.assertTrue( self._audit.in_batch()) self._audit.end_batch( batch_id ) self.assertTrue( self._audit.get_batch( batch_id )) self.assertFalse( self._audit.in_batch()) self.assertEqual( batch_id, self._audit.get_last_valid_batch_id()) def test_get_valid_batches(self): id1 = self._audit.start_batch( doc = { "test" : "doc"}) id2 = self._audit.start_batch( doc = { "test" : "doc"}) self.assertTrue( self._audit.in_batch()) self._audit.end_batch( id2 ) self.assertTrue( self._audit.in_batch()) self._audit.end_batch( id1 ) batch = self._audit.get_batch_end( id1 ) self.assertGreaterEqual( batch[ 'end'], parse( "1-Jun-2017", ) ) self.assertFalse( self._audit.in_batch()) idlist = list( self._audit.get_valid_batch_ids()) self.assertTrue( id1 in idlist ) self.assertTrue( id2 in idlist ) def test_get_last_batch_id(self): id1 = self._audit.start_batch( doc = { "test" : "doc"}) id2 = self._audit.start_batch( doc = { "test" : "doc"}) self.assertEqual( 101, self._audit.get_last_batch_id()) self._audit.end_batch( id2 ) self.assertEqual( 101, self._audit.get_last_batch_id()) self._audit.end_batch( id1 ) id1 = self._audit.start_batch( doc = { "test" : "doc"}) self.assertEqual( 102, self._audit.get_last_batch_id()) self._audit.end_batch( id1 ) def test_pro_batch_id(self):
def __init__(self, mdb, formatter="json", batchID=None, limit=None, view=None): self._mdb = mdb audit = Audit(mdb) self._sorter = None self._start_date = None self._end_date = None self._format = formatter self._files = [] self._limit = limit self._view = view if batchID is None: self._batchID = audit.get_last_valid_batch_id() else: self._batchID = batchID self._pro_account = audit.isProBatch(self._batchID)
print "Connecting to database..." mdb = MUGAlyserMongoDB(uri=uri) except Exception as e: print "Error", e print "URI isn't valid, trying to run on localhost now" mdb = MUGAlyserMongoDB() auditdb = Audit(mdb) an = MUG_Analytics(mdb) membersCollection = mdb.membersCollection() proMemCollection = mdb.proMembersCollection() groupCollection = mdb.groupsCollection() proGrpCollection = mdb.proGroupsCollection() eventsCollection = mdb.pastEventsCollection() currentBatch = auditdb.get_last_valid_batch_id() connection = mdb.client() db = connection.MUGS userColl = db.users resetColl = db.resets euList = an.get_group_names('EU') usList = an.get_group_names('US') otherList = euList + usList euIDList = an.get_group_ids('EU') usIDList = an.get_group_ids('US') otherIDList = euIDList + usIDList
class MUGData( object ): def __init__( self, mdb, collection_name ): self._mdb = mdb self._audit = Audit( mdb ) self._collection = mdb.make_collection( collection_name ) def collection(self): return self._collection @staticmethod def filter( cursor, selector, values ): for i in cursor: if i[ selector ] in values: yield i def find_one(self, query=None ): batch_query = { "batchID" : self._audit.get_last_valid_batch_id() } if query is not None: batch_query.update( query ) #pprint.pprint( batch_query ) return self._collection.find_one( batch_query ) def find(self, q=None, *args, **kwargs ): """ :rtype: """ query = { "batchID" : self._audit.get_last_valid_batch_id() } if q : query.update( q ) if args and kwargs : return self._collection.find( query, args, kwargs ) elif args : return self._collection.find( query, args ) elif kwargs: return self._collection.find( query, kwargs ) else: return self._collection.find( query ) def count(self, g ): count = 0 for _ in g: count = count + 1 return count def generator(self, cursor ): for i in cursor: yield i def summary(self, doc ): pass def one_line(self, doc ): pass def full(self, doc ): pass def doc_print(self, doc, format_type = None ): if format_type == "summary" : print( self.summary( doc )) elif format_type == "oneline" : print( self.one_line( doc )) else: pprint.pprint( doc ) def count_print( self, iterator, format_type=None ): count = 0 for i in iterator : count = count + 1 self.doc_print( i, format_type ) print( "Total: %i" % count )