def getRunningCondorStatus(status_dict):
    """Return a dictionary of collectors containing running(claimed) slots
    Each element is a condorStatus

    :param status_dict: output of getCondorStatus
    :return: dictionary of collectors containing running(claimed) slots
    """
    out = {}
    for collector_name in status_dict:
        # Consider following slots
        # 1. Static - running slots
        # 2. Dynamic slots (They are always running)
        # 3. p-slot with one or more dynamic slots
        #    We get them here so we can use them easily in appendRealRunning()

        sq = condorMonitor.SubQuery(
                status_dict[collector_name],
                lambda el: (
                    ((el.get('State') == 'Claimed') and
                     (el.get('Activity') in ('Busy', 'Retiring'))
                    ) or
                    ((el.get('PartitionableSlot') == True) and
                     (el.get('TotalSlots', 1) > 1)
                    )
                )
        )
        sq.load()
        out[collector_name] = sq
    return out
def getRunningCondorQ(condorq_dict):
    out = {}
    for schedd_name in condorq_dict.keys():
        sq = condorMonitor.SubQuery(condorq_dict[schedd_name], lambda el:('JobStatus' in el and (el['JobStatus'] == 2)))
        sq.load()
        out[schedd_name] = sq
    return out
def getRunningJobsCondorStatus(status_dict):
    """Return a dictionary of collectors containing running(claimed) slots
    This includes Fixed slots and Dynamic slots (no partitionable slots)
    Each one is matched with a single job (gives number of running jobs)
    Each element is a condorStatus

    :param status_dict: output of getCondorStatus
    :return: dictionary of collectors containing running(claimed) slots
    """
    out = {}
    for collector_name in status_dict.keys():
        # This counts the running slots: fixed (static/not partitionable) or dynamic
        # It may give problems when matching with RemoteHost in the jobs
        # since dynamic slots report the parent partitionable slot in GLIDEIN_SiteWMS_Slot

        sq = condorMonitor.SubQuery(
            status_dict[collector_name],
            lambda el: (
                ((el.get('State') == 'Claimed') and
                 (el.get('Activity') in ('Busy', 'Retiring'))
                 )
            )
        )
        sq.load()
        out[collector_name] = sq
    return out
def getIdleCondorStatus(status_dict):
    out = {}
    for collector_name in status_dict.keys():

        # Exclude partitionable slots with no free memory/cpus
        # Minimum memory required by CMS is 2500 MB
        #
        # 1. (el.get('PartitionableSlot') != True)
        # Includes static slots irrespective of the free cpu/mem
        #
        # 2. (el.get('TotalSlots') == 1)
        # p-slots not yet partitioned
        #
        # 3. (el.get('Cpus', 0) > 0 and el.get('Memory', 2501) > 2500)
        # p-slots that have enough idle resources.

        sq = condorMonitor.SubQuery(
            status_dict[collector_name],
            lambda el: (
                (el.get('State') == 'Unclaimed') and
                (el.get('Activity') == 'Idle') and
                (
                    (el.get('PartitionableSlot') != True) or
                    (el.get('TotalSlots') == 1) or
                    (el.get('Cpus', 0) > 0 and el.get('Memory', 2501) > 2500)
                )
            )
        )
        sq.load()
        out[collector_name] = sq
    return out
def getIdleProxyCondorQ(condorq_dict):
    out={}
    for schedd_name in condorq_dict.keys():
        sq=condorMonitor.SubQuery(condorq_dict[schedd_name], lambda el:((el.get('JobStatus')==1) and ('x509userproxy' in el)))
        sq.load()
        out[schedd_name]=sq
    return out
def getOldCondorQ(condorq_dict, min_age):
    out = {}
    for schedd_name in condorq_dict.keys():
        sq = condorMonitor.SubQuery(condorq_dict[schedd_name], lambda el:('ServerTime' in el and 'EnteredCurrentStatus' in el and ((el['ServerTime'] - el['EnteredCurrentStatus']) >= min_age)))
        sq.load()
        out[schedd_name] = sq
    return out
def getClientCondorStatus(status_dict, frontend_name, group_name, request_name):
    client_name_old = "%s@%s.%s" % (request_name, frontend_name, group_name)
    client_name_new = "%s.%s" % (frontend_name, group_name)
    out = {}
    for collector_name in status_dict.keys():
        sq = condorMonitor.SubQuery(
                 status_dict[collector_name],
                 lambda el:('GLIDECLIENT_Name' in el and ((el['GLIDECLIENT_Name'] == client_name_old) or ((el['GLIDECLIENT_Name'] == client_name_new) and (("%s@%s@%s" % (el['GLIDEIN_Entry_Name'], el['GLIDEIN_Name'], el['GLIDEIN_Factory'])) == request_name)))))
        sq.load()
        out[collector_name] = sq
    return out
def getClientCondorStatusCredIdOnly(status_dict, cred_id):
    out = {}
    for collector_name, collector_status in status_dict.iteritems():
        sq = condorMonitor.SubQuery(
            collector_status,
            lambda el: (
                'GLIDEIN_CredentialIdentifier' in el and
                (el['GLIDEIN_CredentialIdentifier'] == cred_id)
            )
        )
        sq.load()
        out[collector_name] = sq
    return out
def getFailedCondorStatus(status_dict):
    out = {}
    for collector_name in status_dict.keys():
        sq = condorMonitor.SubQuery(
            status_dict[collector_name],
            lambda el: (
                (el.get('State') == "Drained") and
                (el.get('Activity') == "Retiring")
            )
        )
        sq.load()
        out[collector_name] = sq
    return out
def getCondorStatusNonDynamic(status_dict):
    """
    Return a dictionary of collectors containing static+partitionable slots
    and exclude any dynamic slots

    Each element is a condorStatus
    Use the output of getCondorStatus
    """
    out = {}
    for collector_name in status_dict.keys():
        # Exclude partitionable slots with no free memory/cpus
        sq = condorMonitor.SubQuery(
            status_dict[collector_name],
            lambda el: (
                (el.get('SlotType') != 'Dynamic')
            )
        )
        sq.load()
        out[collector_name] = sq
    return out
def getRunningPSlotCondorStatus(status_dict):
    """Return a dictionary of collectors containing running(claimed) partitionable slots
    Each element is a condorStatus

    :param status_dict: output of getCondorStatus
    :return: collectors containing running(claimed) partitionable slots
    """
    out = {}
    for collector_name in status_dict.keys():
        # Get p-slot where there is atleast one dynamic slot
        sq = condorMonitor.SubQuery(
                 status_dict[collector_name],
                 lambda el:(
                     (el.get('PartitionableSlot') == True) and
                     (el.get('TotalSlots', 1) > 1)
                 )
             )

        sq.load()
        out[collector_name] = sq
    return out