src/dirbs/reports/csv_reports.py
def write_condition_imei_overlaps(conn: callable, config: callable, month: int, year: int,
                                  country_name: str, output_dir: str, cond_names: list) -> callable:
    """
    Helper function to write out IMEIs that are seen on multiple operators that have been classified.

    Arguments:
        conn: DIRBS PostgreSQL connection object
        config: DIRBS config object
        month: month of the report
        year: year of the report
        country_name: name of the country
        output_dir: output directory path
        cond_names: list of condition names
    Returns:
        Reports metadata
    """
    with contextlib.ExitStack() as stack:
        # Push files into exit stack so that they will all be closed.
        filename_cond_map = {'{0}_{1:d}_{2:d}_condition_imei_overlap_{3}.csv'.format(country_name, month, year, c): c
                             for c in cond_names}
        condname_file_map = {c: stack.enter_context(open(os.path.join(output_dir, fn), 'w', encoding='utf8'))
                             for fn, c in filename_cond_map.items()}
        # Create a map from condition name to csv writer
        condname_csvwriter_map = {c: csv.writer(condname_file_map[c]) for c in cond_names}
        # Write the header to each csvwriter
        for _, writer in condname_csvwriter_map.items():
            writer.writerow(['IMEI', 'Operators'])
        # Runa query to find all the classified IMEIs seen on multiple operators
        with conn.cursor(name='imeis_overlap') as cursor:
            cursor.execute("""SELECT imei_norm, cond_name, string_agg(DISTINCT operator_id, '|') AS operators
                                FROM classification_state
                                JOIN monthly_network_triplets_per_mno_no_null_imeis
                               USING (imei_norm)
                               WHERE triplet_month = %s
                                 AND triplet_year = %s
                                 AND end_date IS NULL
                            GROUP BY imei_norm, cond_name
                                     HAVING COUNT(DISTINCT operator_id) > 1""",
                           [month, year])
            for res in cursor:
                condname_csvwriter_map[res.cond_name].writerow([res.imei_norm, res.operators])

    return _gen_metadata_for_reports(list(filename_cond_map.keys()), output_dir)

    Helper function to write out IMEIs that are seen on multiple operators that have been classified.

    Arguments:
        conn: DIRBS PostgreSQL connection object
        config: DIRBS config object
        month: month of the report
        year: year of the report
        country_name: name of the country
        output_dir: output directory path
        cond_names: list of condition names
    Returns:
        Reports metadata
    
Helper function to write out IMEIs that are seen on multiple operators that have been classified.
Helper function to write out IMEIs that are seen on multiple operators that have been classified .
def write_condition_imei_overlaps(conn: callable, config: callable, month: int, year: int,
                                  country_name: str, output_dir: str, cond_names: list) -> callable:
    with contextlib.ExitStack() as stack:
        filename_cond_map = {'{0}_{1:d}_{2:d}_condition_imei_overlap_{3}.csv'.format(country_name, month, year, c): c
                             for c in cond_names}
        condname_file_map = {c: stack.enter_context(open(os.path.join(output_dir, fn), 'w', encoding='utf8'))
                             for fn, c in filename_cond_map.items()}
        condname_csvwriter_map = {c: csv.writer(condname_file_map[c]) for c in cond_names}
        for _, writer in condname_csvwriter_map.items():
            writer.writerow(['IMEI', 'Operators'])
        with conn.cursor(name='imeis_overlap') as cursor:
            cursor.execute("""SELECT imei_norm, cond_name, string_agg(DISTINCT operator_id, '|') AS operators
                                FROM classification_state
                                JOIN monthly_network_triplets_per_mno_no_null_imeis
                               USING (imei_norm)
                               WHERE triplet_month = %s
                                 AND triplet_year = %s
                                 AND end_date IS NULL
                            GROUP BY imei_norm, cond_name
                                     HAVING COUNT(DISTINCT operator_id) > 1""",
                           [month, year])
            for res in cursor:
                condname_csvwriter_map[res.cond_name].writerow([res.imei_norm, res.operators])
    return _gen_metadata_for_reports(list(filename_cond_map.keys()), output_dir)
def write_condition_imei_overlaps ( conn : callable , config : callable , month : int , year : int , country_name : str , output_dir : str , cond_names : list ) -> callable : with contextlib . ExitStack ( ) as stack : filename_cond_map = { '{0}_{1:d}_{2:d}_condition_imei_overlap_{3}.csv' . format ( country_name , month , year , c ) : c for c in cond_names } condname_file_map = { c : stack . enter_context ( open ( os . path . join ( output_dir , fn ) , 'w' , encoding = 'utf8' ) ) for fn , c in filename_cond_map . items ( ) } condname_csvwriter_map = { c : csv . writer ( condname_file_map [ c ] ) for c in cond_names } for _ , writer in condname_csvwriter_map . items ( ) : writer . writerow ( [ 'IMEI' , 'Operators' ] ) with conn . cursor ( name = 'imeis_overlap' ) as cursor : cursor . execute ( """SELECT imei_norm, cond_name, string_agg(DISTINCT operator_id, '|') AS operators
                                FROM classification_state
                                JOIN monthly_network_triplets_per_mno_no_null_imeis
                               USING (imei_norm)
                               WHERE triplet_month = %s
                                 AND triplet_year = %s
                                 AND end_date IS NULL
                            GROUP BY imei_norm, cond_name
                                     HAVING COUNT(DISTINCT operator_id) > 1""" , [ month , year ] ) for res in cursor : condname_csvwriter_map [ res . cond_name ] . writerow ( [ res . imei_norm , res . operators ] ) return _gen_metadata_for_reports ( list ( filename_cond_map . keys ( ) ) , output_dir )
Helper function to write out IMEIs that are seen on multiple operators that have been classified.
Helper function to write out IMEIs that are seen on multiple operators that have been classified .
"""
    Helper function to write out IMEIs that are seen on multiple operators that have been classified.

    Arguments:
        conn: DIRBS PostgreSQL connection object
        config: DIRBS config object
        month: month of the report
        year: year of the report
        country_name: name of the country
        output_dir: output directory path
        cond_names: list of condition names
    Returns:
        Reports metadata
    """ # Push files into exit stack so that they will all be closed. # Create a map from condition name to csv writer # Write the header to each csvwriter # Runa query to find all the classified IMEIs seen on multiple operators
None
conn
callable
DIRBS PostgreSQL connection object
DIRBS PostgreSQL connection object
config
callable
DIRBS config object
DIRBS config object
month
int
month of the report
month of the report
year
int
year of the report
year of the report
country_name
str
name of the country
name of the country
output_dir
str
output directory path
output directory path
cond_names
list
list of condition names
list of condition names