def addJoinDate(mdb, cursor): members = Members(mdb) for i in cursor: member = members.get_by_ID(i["info"]["attendee"]["member"]["id"]) i["join_date"] = member["join_time"] yield i
def get_totals(self, urls, countries=None): ''' Total number of members Total number of groups Total number of events Total number of RSVPs ''' members = Members(self._mdb) if countries is None: member_count = members.get_all_members().count() else: member_count = members.get_all_members({ "member.country": { "$in": countries } }).count() print("Total members: %i" % member_count) events = PastEvents(self._mdb) event_count = events.get_all_events({ "event.group.urlname": { "$in": urls } }).count() print("Total events: %i" % event_count)
def setUp(self): self._mdb = MUGAlyserMongoDB( uri="mongodb://localhost/TESTMUGS") self._members = Members( self._mdb ) self._pro_members = Pro_Members( self._mdb )
class Test_members(unittest.TestCase): def setUp(self): self._mdb = MUGAlyserMongoDB( uri="mongodb://localhost/TESTMUGS") self._members = Members( self._mdb ) self._pro_members = Pro_Members( self._mdb ) def tearDown(self): #self._mdb.client().drop_database( "TEST_MUGS" ) pass def test_get_all_members( self ): cursor = self._members.get_all_members() #print( cursor.count()) self.assertGreaterEqual( cursor.count(), 826 ) cursor = self._members.get_all_members() #print( cursor.count()) self.assertGreaterEqual( cursor.count(), 826 ) def test_pro_get_many_group_members(self ): members = self._pro_members.get_many_group_members( [ "DublinMUG", "London-MongoDB-User-Group"] ) self.assertGreaterEqual( self._members.count( members ), 2357 ) def test_get_group_members(self ): members = self._pro_members.get_group_members( "DublinMUG" ) self.assertGreaterEqual( len( list( members)), 830 ) #self._members.count_print( members ) def test_distinct(self): unique_members = self._pro_members.distinct_members() self.assertGreaterEqual( len( unique_members ), 46095 ) unique_members = self._members.distinct_members() self.assertGreaterEqual( len( unique_members ), 46095 ) #print( len( unique_members )) def test_get_by_name(self): jdrumgoole = self._pro_members.get_by_name( "Joe Drumgoole") self.assertNotEqual( jdrumgoole, None ) def test_get_by_date_range(self ): start = parse( "1-Jan-2009, 00:01" ) end = parse( "31-Dec-2009 11:59" ) members = self._pro_members.get_by_join_date( start , end ) all_members = list( members ) self.assertEqual( len( all_members), 58 ) def test_joined_by_year(self): joined = self._pro_members.joined_by_year() total = 0 for i in joined : total = total + i[ "total_registered"] members = self._pro_members.get_all_members() self.assertEqual( len( list( members )), total )
def main(argv=None): if argv: sys.argv.extend(argv) try: parser = ArgumentParser() parser.add_argument( "--host", default="mongodb://localhost:27017/MUGS", help="URI for connecting to MongoDB [default: %(default)s]") parser.add_argument("--hasgroup", nargs="+", default=[], help="Is this a MongoDB Group") parser.add_argument("-l", "--listgroups", action="store_true", default=False, help="print out all the groups") parser.add_argument("-v", "--version", action="store_true", default=False, help="print version") parser.add_argument("--members", nargs="+", default=[], help="list all members of a list of groups") parser.add_argument("--distinct", action="store_true", default=False, help="List all distinct members") parser.add_argument("-i", "--memberid", type=int, help="get info for member id") parser.add_argument("--membername", help="get info for member id") parser.add_argument("--upcomingevents", nargs="+", default=[], help="List upcoming events") parser.add_argument("--pastevents", nargs="+", default=[], help="List past events") parser.add_argument("--country", nargs="+", default=[], help="print groups by country") parser.add_argument( "--batches", action="store_true", default=False, help= "List all the batches in the audit database [default: %(default)s]" ) parser.add_argument("--curbatch", action="store_true", default=False, help="Report current batch ID") parser.add_argument("--joined", action="store_true", default=False, help="Report people who joined by year") parser.add_argument("--organizer", nargs="+", default=[], help="List organizers for a specific set of MUGS") parser.add_argument( "--start", help="Range used for fields in which ranges relevant") parser.add_argument( "--finish", help="Range used for fields in which ranges relevant") parser.add_argument("-f", "--format_type", choices=["oneline", "summary", "full"], default="oneline", help="type of output") # Process arguments args = parser.parse_args() mdb = MUGAlyserMongoDB(uri=args.host) members = Members(mdb) if args.version: print("%s muginfo %s" % (__programName__, __version__)) sys.exit(2) if args.curbatch: audit = Audit(mdb) curbatch = audit.getCurrentValidBatchID() print("current batch ID = {'batchID': %i}" % curbatch) if args.memberid: member = members.get_by_ID(args.memberid) if member: pprint(member) print(member["member_name"]) else: print("No such member: %s" % args.memberid) if args.membername: member = members.find_one({"member.member_name": args.membername}) if member: pprint(member) else: print("No such member: %s" % args.membername) for i in args.hasgroup: groups = Groups(mdb) if groups.get_group(i): print("{:40} :is a MongoDB MUG".format(i)) else: print("{:40} :is not a MongoDB MUG".format(i)) if args.listgroups: groups = Groups(mdb) count = 0 for g in groups.get_all_groups(): count = count + 1 print("{:40} (location: {})".format(g["group"]["urlname"], g["group"]["country"])) print("total: %i" % count) if args.country: count = 0 groups = Groups(mdb) country_groups = groups.find({"group.country": args.country}) for g in country_groups: count = count + 1 print("{:20} has MUG: {}".format(g["group"]["urlname"], args.country)) print("total : %i " % count) if args.batches: if not args.host: print("Need to specify --host for batchIDs") sys.exit(2) audit = Audit(mdb) batchIDs = audit.getBatchIDs() for b in batchIDs: print(b) count = 0 if args.members: print("args.members : %s" % args.members) q = Query() if args.start and not args.finish: q.add_range("member.join_time", parse(args.start), datetime.now()) elif not args.start and args.finish: q.add_range("member.join_time", datetime.now(), parse(args.finish)) elif args.start and args.finish: q.add_range("member.join_time", parse(args.start), parse(args.finish)) if "all" in args.members: it = members.get_all_members(q) else: it = members.get_many_group_members(args.members) for i in it: count = count + 1 # # sometimes country is not defined. # country = i["member"].pop("country", "Undefined") if "member_id" in i["member"]: # PRO API member format print(u"{:30}, {:20}, {:20}".format( i["member"]["member_name"], country, i["member"]["member_id"])) else: print(u"{:30}, {:20}, {:20}".format( i["member"]["name"], country, i["member"]["id"])) print("%i total" % count) if args.joined: members = Members(mdb) joined = members.joined_by_year() for i in joined: print(i) if args.distinct: members = Members(mdb) distinct = members.distinct_members() printCount(distinct) if args.upcomingevents: events = UpcomingEvents(mdb) events.count_print( events.get_all_group_events(args.upcomingevents), args.format_type) if args.pastevents: events = PastEvents(mdb) events.count_print(events.get_all_group_events(args.pastevents), args.format_type) if "all" in args.organizer: organizers = Organizers(mdb) members = organizers.get_organizers() organizers.count_print(members, args.format_type) else: organizers = Organizers(mdb) for i in args.organizer: print("Organizer: '%s'" % i) mugs = organizers.get_mugs(i) for m in mugs: print("\t%s" % m["urlname"]) except KeyboardInterrupt: print("Keyboard interrupt : Exiting...") sys.exit(2) except pymongo.errors.ServerSelectionTimeoutError, e: print("Failed to connect to MongoDB Server (server timeout): %s" % e) sys.exit(2)