Example #1
0
def iter_hosts_dict(page=None):
    clients_collection = mongo.get_collection("clients")
    server_collection = mongo.get_collection("servers")

    response = server_collection.aggregate(
        [
            {"$project": {"hosts": True, "organizations": True}},
            {"$unwind": "$hosts"},
            {"$unwind": "$organizations"},
            {"$group": {"_id": "$hosts", "organizations": {"$addToSet": "$organizations"}}},
        ]
    )

    orgs = set()
    host_orgs = collections.defaultdict(list)
    for doc in response:
        orgs = orgs.union(doc["organizations"])
        host_orgs[doc["_id"]] = doc["organizations"]

    org_user_count = organization.get_user_count(orgs)

    for hst in iter_hosts(page=page):
        users_online = len(clients_collection.distinct("user_id", {"host_id": hst.id, "type": CERT_CLIENT}))

        user_count = 0
        for org_id in host_orgs[hst.id]:
            user_count += org_user_count.get(org_id, 0)

        hst.user_count = user_count
        hst.users_online = users_online

        yield hst.dict()
Example #2
0
def iter_hosts_dict(page=None):
    server_collection = mongo.get_collection('servers')

    response = server_collection.aggregate([
        {'$project': {
            'host_id': '$instances.host_id',
            'client': '$instances.clients',
        }},
        {'$unwind': '$host_id'},
        {'$unwind': '$client'},
        {'$unwind': '$client'},
        {'$match': {
            'client.type': CERT_CLIENT,
        }},
        {'$group': {
            '_id': '$host_id',
            'clients': {'$addToSet': '$client.id'},
        }},
    ])

    hosts_clients = {}
    for doc in response:
        hosts_clients[doc['_id']] = doc['clients']

    response = server_collection.aggregate([
        {'$project': {
            'hosts': True,
            'organizations': True,
        }},
        {'$unwind': '$hosts'},
        {'$unwind': '$organizations'},
        {'$group': {
            '_id': '$hosts',
            'organizations': {'$addToSet': '$organizations'},
        }},
    ])

    orgs = set()
    host_orgs = collections.defaultdict(list)
    for doc in response:
        orgs = orgs.union(doc['organizations'])
        host_orgs[doc['_id']] = doc['organizations']

    org_user_count = organization.get_user_count(orgs)

    for hst in iter_hosts(page=page):
        users_online = len(hosts_clients.get(hst.id, []))

        user_count = 0
        for org_id in host_orgs[hst.id]:
            user_count += org_user_count.get(org_id, 0)

        hst.user_count = user_count
        hst.users_online = users_online

        yield hst.dict()
Example #3
0
def iter_servers_dict():
    server_collection = mongo.get_collection('servers')

    response = server_collection.aggregate([
        {'$project': {
            'host_id': '$instances.host_id',
            'client': '$instances.clients',
        }},
        {'$unwind': '$host_id'},
        {'$unwind': '$client'},
        {'$unwind': '$client'},
        {'$match': {
            'client.type': CERT_CLIENT,
        }},
        {'$group': {
            '_id': '$host_id',
            'clients': {'$addToSet': '$client.id'},
        }},
    ])['result']

    hosts_clients = {}
    for doc in response:
        hosts_clients[doc['_id']] = doc['clients']

    org_user_count = organization.get_user_count()

    response = server_collection.aggregate([
        {'$project': {
            'hosts': True,
            'organizations': True,
        }},
        {'$unwind': '$hosts'},
        {'$unwind': '$organizations'},
        {'$group': {
            '_id': '$hosts',
            'organizations': {'$addToSet': '$organizations'},
        }},
    ])['result']

    host_orgs = collections.defaultdict(list)
    for doc in response:
        host_orgs[doc['_id']] = doc['organizations']

    for doc in Host.collection.find().sort('name'):
        hst = Host(doc=doc)

        users_online = len(hosts_clients.get(hst.id, ''))

        user_count = 0
        for org_id in host_orgs[hst.id]:
            user_count += org_user_count.get(org_id, 0)

        hst.user_count = user_count
        hst.users_online = users_online

        yield hst.dict()
Example #4
0
def iter_servers_dict():
    server_collection = mongo.get_collection('servers')

    response = server_collection.aggregate([
        {'$project': {
            'host_id': '$instances.host_id',
            'client': '$instances.clients',
        }},
        {'$unwind': '$host_id'},
        {'$unwind': '$client'},
        {'$unwind': '$client'},
        {'$match': {
            'client.type': CERT_CLIENT,
        }},
        {'$group': {
            '_id': '$host_id',
            'clients': {'$addToSet': '$client.id'},
        }},
    ])['result']

    hosts_clients = {}
    for doc in response:
        hosts_clients[doc['_id']] = doc['clients']

    org_user_count = organization.get_user_count()

    response = server_collection.aggregate([
        {'$project': {
            'hosts': True,
            'organizations': True,
        }},
        {'$unwind': '$hosts'},
        {'$unwind': '$organizations'},
        {'$group': {
            '_id': '$hosts',
            'organizations': {'$addToSet': '$organizations'},
        }},
    ])['result']

    host_orgs = collections.defaultdict(list)
    for doc in response:
        host_orgs[doc['_id']] = doc['organizations']

    for doc in Host.collection.find().sort('name'):
        hst = Host(doc=doc)

        users_online = len(hosts_clients.get(hst.id, []))

        user_count = 0
        for org_id in host_orgs[hst.id]:
            user_count += org_user_count.get(org_id, 0)

        hst.user_count = user_count
        hst.users_online = users_online

        yield hst.dict()
Example #5
0
def iter_hosts_dict(page=None):
    server_collection = mongo.get_collection('servers')

    response = server_collection.aggregate([
        {'$project': {
            'host_id': '$instances.host_id',
            'client': '$instances.clients',
        }},
        {'$unwind': '$host_id'},
        {'$unwind': '$client'},
        {'$unwind': '$client'},
        {'$match': {
            'client.type': CERT_CLIENT,
        }},
        {'$group': {
            '_id': '$host_id',
            'clients': {'$addToSet': '$client.id'},
        }},
    ])

    hosts_clients = {}
    for doc in response:
        hosts_clients[doc['_id']] = doc['clients']

    response = server_collection.aggregate([
        {'$project': {
            'hosts': True,
            'organizations': True,
        }},
        {'$unwind': '$hosts'},
        {'$unwind': '$organizations'},
        {'$group': {
            '_id': '$hosts',
            'organizations': {'$addToSet': '$organizations'},
        }},
    ])

    orgs = set()
    host_orgs = collections.defaultdict(list)
    for doc in response:
        orgs = orgs.union(doc['organizations'])
        host_orgs[doc['_id']] = doc['organizations']

    org_user_count = organization.get_user_count(orgs)

    for hst in iter_hosts(page=page):
        users_online = len(hosts_clients.get(hst.id, []))

        user_count = 0
        for org_id in host_orgs[hst.id]:
            user_count += org_user_count.get(org_id, 0)

        hst.user_count = user_count
        hst.users_online = users_online

        yield hst.dict()