import time



'''
This module performs the group attatchment query
Query returns a list of subscribers with what talk group they are currently registered on
Looks up the talk group ssi and matches to the talk group description
It then updates the MongoDB subscriber collection
'''



def attachment(CURSOR, DB):
    '''
    Retrieves group attachment for each subscriber
    '''
    # Execute MySQL query
    CURSOR.execute("SELECT \
        Ssi, \
        GroupSsi, \
        Selected \
        FROM `groupattachment` \
        WHERE Selected = 1;"
    )
    myresult = CURSOR.fetchall()

    # Get the list of talk group names
    groups_list = DB['tetra_subscribers'].find(
        {
            'type' : 'Group'
        }
    )
    # Convert to a dict with the ssi as the key
    groups = {group['ssi'] : group['description'] for group in groups_list}

    # Iterate over the subscribers
    for subscriber in myresult:
        try:
            # Get the group attachment
            attached_group = subscriber[1]
            # Look up the talk group description in the dictionary
            talkgroup = groups[attached_group]
            # Update the subscriber list with the latest group
            DB['tetra_subscribers'].find_one_and_update(
                {
                    'ssi' : subscriber[0],
                },
                {
                    '$set': {
                        'talkgroup' : talkgroup
                    }
                },
                upsert=True
            )
        except KeyError:
            pass
import time



'''
This module performs the group attatchment query
Query returns a list of subscribers with what talk group they are currently registered on
Looks up the talk group ssi and matches to the talk group description
It then updates the MongoDB subscriber collection
'''



def attachment(CURSOR, DB):
    '''
    Retrieves group attachment for each subscriber
    '''
    # Execute MySQL query
    CURSOR.execute("SELECT \
        Ssi, \
        GroupSsi, \
        Selected \
        FROM `groupattachment` \
        WHERE Selected = 1;"
    )
    myresult = CURSOR.fetchall()

    # Get the list of talk group names
    groups_list = DB['tetra_subscribers'].find(
        {
            'type' : 'Group'
        }
    )
    # Convert to a dict with the ssi as the key
    groups = {group['ssi'] : group['description'] for group in groups_list}

    # Iterate over the subscribers
    for subscriber in myresult:
        try:
            # Get the group attachment
            attached_group = subscriber[1]
            # Look up the talk group description in the dictionary
            talkgroup = groups[attached_group]
            # Update the subscriber list with the latest group
            DB['tetra_subscribers'].find_one_and_update(
                {
                    'ssi' : subscriber[0],
                },
                {
                    '$set': {
                        'talkgroup' : talkgroup
                    }
                },
                upsert=True
            )
        except KeyError:
            pass
This module performs the group attatchment query Query returns a list of subscribers with what talk group they are currently registered on Looks up the talk group ssi and matches to the talk group description It then updates the MongoDB subscriber collection Retrieves group attachment for each subscriber # Execute MySQL query # Get the list of talk group names # Convert to a dict with the ssi as the key # Iterate over the subscribers # Get the group attachment # Look up the talk group description in the dictionary # Update the subscriber list with the latest group