Ejemplo n.º 1
0
    def __init__(self, groupId, mongo, src, testsLibrary=None):
        # TODO consolidate src into testsLibrary?
        # TODO remove mongo and testsLibrary from __init__?
        self.mongo = mongo
        self.src = src
        self.testsLibrary = testsLibrary
        # A support.companies document, from Clienthub
        # TODO move towards support.salesforce documents
        self.company = None
        # A euphonia.groups document, from ProactiveDB ;)
        self.group = None

        # Use existing logger if it exists
        self.logger = logging.getLogger('logger')

        res = sfProjectIdToSFProjectName(groupId, mongo)
        if not res['ok']:
            raise Exception("Failed to get group name for %s" % groupId)
        groupName = res['payload']

        # Get group doc if it exists, otherwise create it
        try:
            query = {'_id': groupId, 'name': groupName}
            updoc = {'$set': query}
            self.group = self.mongo.euphonia.groups.find_and_modify(
                query=query, update=updoc, upsert=True, new=True)
        except pymongo.errors.PyMongoError as e:
            raise e

        # Get active tests for this src type
        try:
            match = {'active': True, 'src': self.src}
            curr_tests = self.mongo.euphonia.tests.find(match)
        except pymongo.errors.PyMongoError as e:
            raise e

        self.tests = {test['name']: test for test in curr_tests}
        # TODO move out of base class
        self.testPriorityScores = {'low': 2, 'medium': 4, 'high': 8}

        # Supplement with company information if it's available
        # TODO move to a separate clienthub/sfdc library
        try:
            match = {'sf_project_id': groupId}
            curr_companies = self.mongo.support.companies.find(match)
        except pymongo.errors.PyMongoError as e:
            raise e

        if curr_companies.count() != 1:
            self.logger.warning("Unable to uniquely identify company for group"
                                "%s", groupId)
            self.company = None
        else:
            self.company = curr_companies.next()
coll_lostgroups = mongo.euphonia.lostgroups
try:
    curr = coll_groups.find()
except pymongo.errors.PyMongoError as e:
    raise e
for doc in curr:
    mmsGroupName = doc.get("name")
    res = mmsGroupNameToSFProjectId(mmsGroupName, mongo)
    if not res["ok"]:
        logger.warning(res["payload"])
        coll_lostgroups.insert(doc)
    else:
        logger.info("%s -> %s", mmsGroupName, res["payload"])
        newdoc = {"_id": res["payload"]}

        res = sfProjectIdToSFProjectName(res["payload"], mongo)
        if not res["ok"]:
            logger.warning(res["payload"])
            continue
        newdoc["name"] = res["payload"]

        if "failedTests" in doc:
            newdoc["failedtests"] = copy.deepcopy(doc["failedTests"])
            del doc["failedTests"]
        if "score" in doc:
            newdoc["score"] = copy.deepcopy(doc["score"])
            del doc["score"]
        # the rest is MMS
        doc["id"] = copy.deepcopy(doc["_id"])
        del doc["_id"]