Beispiel #1
0
    def __init__(self,
                 mdb,
                 output_filename="-",
                 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._filename = output_filename
        self._format = formatter
        self._files = []
        self._limit = limit

        self._view = view
        if batchID is None:
            self._batchID = audit.getCurrentValidBatchID()
        else:
            self._batchID = batchID

        self._pro_account = audit.isProBatch(self._batchID)
Beispiel #2
0
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)
Beispiel #3
0
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")
        pass

    #@unittest.skip
    def test_incrementID(self):
        batchID = self._audit.incrementBatchID()
        curID = self._audit.getCurrentBatchID()

        self.assertEqual(batchID, curID)
        newID = self._audit.incrementBatchID()
        self.assertEqual(batchID + 1, newID)

    def test_getCurrentValidBatchID(self):
        batchID1 = self._audit.startBatch(doc={"test": "doc"}, trial=True)
        self._audit.endBatch(batchID1)
        #self.assertRaises( ValueError, self._audit.getCurrentValidBatchID )

        batchID2 = self._audit.startBatch(
            {
                "args": "arg list",
                "version": __programName__ + " " + __version__
            },
            trial=False,
            apikey=get_meetup_key())

        self._audit.endBatch(batchID2)
        self.assertEqual(batchID2, self._audit.getCurrentValidBatchID())

        batchID3 = self._audit.startBatch(doc={"test": "doc"}, trial=True)
        self._audit.endBatch(batchID3)
        self.assertEqual(batchID2, self._audit.getCurrentValidBatchID())

    def test_batch(self):

        batchIDs = [x for x in self._audit.getBatchIDs()]

        thisBatchID = self._audit.startBatch(doc={"test": "doc"}, trial=True)

        newBatchIDs = [x for x in self._audit.getBatchIDs()]

        self.assertEqual(len(batchIDs) + 1, len(newBatchIDs))

        self.assertTrue(thisBatchID in newBatchIDs)

        self._audit.endBatch(thisBatchID)

    #@unittest.skip
    def test_IDs(self):
        self.assertRaises(ValueError, self._audit.getCurrentBatchID)
        self.assertRaises(ValueError, self._audit.getLastBatchID)
        self.assertFalse(self._audit.inBatch())
        batchID = self._audit.startBatch({})
        self.assertTrue(self._audit.inBatch())
        self.assertEquals(1, self._audit.getCurrentBatchID())
        self._audit.endBatch(batchID)

        batch = self._audit.getBatch(batchID)
        self.assertTrue("start" in batch)
        self.assertTrue("end" in batch)
        self.assertTrue("info" in batch)
        self.assertTrue("batchID" in batch)
        self.assertFalse(self._audit.incomplete(batchID))

        batchID = self._audit.startBatch({})
        self.assertTrue(self._audit.inBatch())
        self.assertEquals(2, self._audit.getCurrentBatchID())
        self._audit.endBatch(batchID)
        self.assertFalse(self._audit.inBatch())

    #@unittest.skip
    def test_start_end_batch(self):

        batchID = self._audit.startBatch({})
        self.assertTrue(self._audit.incomplete(batchID))
        self._audit.endBatch(batchID)
        self.assertFalse(self._audit.incomplete(batchID))