src/dirbs/reports/csv_reports.py
def write_report(report: callable, month: int, year: int, output_dir: str, filename_prefix: str,
                 css_filename: str, js_filename: str, per_tac_compliance_data: dict) -> callable:
    """
    Helper function to write an individual report to disk.

    Arguments:
        report: Report type to write
        month: month of the generated report
        year: year of the generated report
        output_dir: path of the output dir
        filename_prefix: Prefix of report file name
        css_filename: name of the css file
        js_filename: name of the JS file
        per_tac_compliance_data: compliance data of tacs
    Returns:
        Reports metadata

    """
    # Generate the raw data
    logger = logging.getLogger('dirbs.report')
    data = report.gen_report_data()

    data_filename = '{0}_{1:d}_{2:d}.json'.format(filename_prefix, month, year)
    html_filename = '{0}_{1:d}_{2:d}.html'.format(filename_prefix, month, year)
    per_tac_csv_filename = '{0}_{1:d}_{2:d}.csv'.format(filename_prefix, month, year)
    condition_counts_filename = '{0}_{1:d}_{2:d}_condition_counts.csv'.format(filename_prefix, month, year)

    # Store a list of generate filenames so we can generate metadata
    generated_filenames = [html_filename, data_filename]

    # Write the raw JSON data to disk
    json_data = json.dumps(data, indent=4, sort_keys=True, cls=utils.JSONEncoder).encode('utf-8')
    with open(os.path.join(output_dir, data_filename), 'wb') as of:
        of.write(json_data)

    # Write the CSV per-TAC compliance data to disk
    if data['has_data']:
        condition_labels = [c['label'] for c in data['classification_conditions']]
        condition_table_headers = condition_labels + \
            ['IMEI count', 'IMEI gross adds count', 'IMEI-IMSI count', 'IMEI-MSISDN count', 'Subscriber triplet count',
             'Compliance Level']
        value_keys = ['num_imeis', 'num_imei_gross_adds', 'num_imei_imsis', 'num_imei_msisdns',
                      'num_subscriber_triplets', 'compliance_level']
        if per_tac_compliance_data is not None:
            with open(os.path.join(output_dir, per_tac_csv_filename), 'w', encoding='utf8') as of:
                writer = csv.writer(of)
                writer.writerow(['TAC'] + condition_table_headers)
                for tac, combinations in per_tac_compliance_data.items():
                    for combination, compliance_stats in combinations.items():
                        combination_list = list(combination)
                        writer.writerow([tac] + combination_list + [compliance_stats[key] for key in value_keys])
            generated_filenames.append(per_tac_csv_filename)
        else:
            logger.warning('No per-TAC compliance data will be output to CSV file, as compliance data was not '
                           'calculated or is empty')

    # Write the CSV condition combination data to disk
    condition_combination_table = data.get('condition_combination_table')
    if condition_combination_table is not None:
        with open(os.path.join(output_dir, condition_counts_filename), 'w', encoding='utf8') as of:
            writer = csv.writer(of)
            writer.writerow(condition_table_headers)
            for combination in data['condition_combination_table']:
                combination_list = [combination['combination'][label] for label in condition_labels]
                writer.writerow(combination_list + [combination[key] for key in value_keys])
        generated_filenames.append(condition_counts_filename)
    else:
        logger.warning('No condition counts table data will be output to CSV file, as table data is empty')

    # Generate the HTML report
    html = report.gen_html_report(data, css_filename, js_filename)

    # Write the HTML file to disk
    with open(os.path.join(output_dir, html_filename), 'wb') as of:
        of.write(html)

    return _gen_metadata_for_reports(generated_filenames, output_dir)

    Helper function to write an individual report to disk.

    Arguments:
        report: Report type to write
        month: month of the generated report
        year: year of the generated report
        output_dir: path of the output dir
        filename_prefix: Prefix of report file name
        css_filename: name of the css file
        js_filename: name of the JS file
        per_tac_compliance_data: compliance data of tacs
    Returns:
        Reports metadata

    
Helper function to write an individual report to disk.
Helper function to write an individual report to disk .
def write_report(report: callable, month: int, year: int, output_dir: str, filename_prefix: str,
                 css_filename: str, js_filename: str, per_tac_compliance_data: dict) -> callable:
    logger = logging.getLogger('dirbs.report')
    data = report.gen_report_data()
    data_filename = '{0}_{1:d}_{2:d}.json'.format(filename_prefix, month, year)
    html_filename = '{0}_{1:d}_{2:d}.html'.format(filename_prefix, month, year)
    per_tac_csv_filename = '{0}_{1:d}_{2:d}.csv'.format(filename_prefix, month, year)
    condition_counts_filename = '{0}_{1:d}_{2:d}_condition_counts.csv'.format(filename_prefix, month, year)
    generated_filenames = [html_filename, data_filename]
    json_data = json.dumps(data, indent=4, sort_keys=True, cls=utils.JSONEncoder).encode('utf-8')
    with open(os.path.join(output_dir, data_filename), 'wb') as of:
        of.write(json_data)
    if data['has_data']:
        condition_labels = [c['label'] for c in data['classification_conditions']]
        condition_table_headers = condition_labels + \
            ['IMEI count', 'IMEI gross adds count', 'IMEI-IMSI count', 'IMEI-MSISDN count', 'Subscriber triplet count',
             'Compliance Level']
        value_keys = ['num_imeis', 'num_imei_gross_adds', 'num_imei_imsis', 'num_imei_msisdns',
                      'num_subscriber_triplets', 'compliance_level']
        if per_tac_compliance_data is not None:
            with open(os.path.join(output_dir, per_tac_csv_filename), 'w', encoding='utf8') as of:
                writer = csv.writer(of)
                writer.writerow(['TAC'] + condition_table_headers)
                for tac, combinations in per_tac_compliance_data.items():
                    for combination, compliance_stats in combinations.items():
                        combination_list = list(combination)
                        writer.writerow([tac] + combination_list + [compliance_stats[key] for key in value_keys])
            generated_filenames.append(per_tac_csv_filename)
        else:
            logger.warning('No per-TAC compliance data will be output to CSV file, as compliance data was not '
                           'calculated or is empty')
    condition_combination_table = data.get('condition_combination_table')
    if condition_combination_table is not None:
        with open(os.path.join(output_dir, condition_counts_filename), 'w', encoding='utf8') as of:
            writer = csv.writer(of)
            writer.writerow(condition_table_headers)
            for combination in data['condition_combination_table']:
                combination_list = [combination['combination'][label] for label in condition_labels]
                writer.writerow(combination_list + [combination[key] for key in value_keys])
        generated_filenames.append(condition_counts_filename)
    else:
        logger.warning('No condition counts table data will be output to CSV file, as table data is empty')
    html = report.gen_html_report(data, css_filename, js_filename)
    with open(os.path.join(output_dir, html_filename), 'wb') as of:
        of.write(html)
    return _gen_metadata_for_reports(generated_filenames, output_dir)
def write_report ( report : callable , month : int , year : int , output_dir : str , filename_prefix : str , css_filename : str , js_filename : str , per_tac_compliance_data : dict ) -> callable : logger = logging . getLogger ( 'dirbs.report' ) data = report . gen_report_data ( ) data_filename = '{0}_{1:d}_{2:d}.json' . format ( filename_prefix , month , year ) html_filename = '{0}_{1:d}_{2:d}.html' . format ( filename_prefix , month , year ) per_tac_csv_filename = '{0}_{1:d}_{2:d}.csv' . format ( filename_prefix , month , year ) condition_counts_filename = '{0}_{1:d}_{2:d}_condition_counts.csv' . format ( filename_prefix , month , year ) generated_filenames = [ html_filename , data_filename ] json_data = json . dumps ( data , indent = 4 , sort_keys = True , cls = utils . JSONEncoder ) . encode ( 'utf-8' ) with open ( os . path . join ( output_dir , data_filename ) , 'wb' ) as of : of . write ( json_data ) if data [ 'has_data' ] : condition_labels = [ c [ 'label' ] for c in data [ 'classification_conditions' ] ] condition_table_headers = condition_labels + [ 'IMEI count' , 'IMEI gross adds count' , 'IMEI-IMSI count' , 'IMEI-MSISDN count' , 'Subscriber triplet count' , 'Compliance Level' ] value_keys = [ 'num_imeis' , 'num_imei_gross_adds' , 'num_imei_imsis' , 'num_imei_msisdns' , 'num_subscriber_triplets' , 'compliance_level' ] if per_tac_compliance_data is not None : with open ( os . path . join ( output_dir , per_tac_csv_filename ) , 'w' , encoding = 'utf8' ) as of : writer = csv . writer ( of ) writer . writerow ( [ 'TAC' ] + condition_table_headers ) for tac , combinations in per_tac_compliance_data . items ( ) : for combination , compliance_stats in combinations . items ( ) : combination_list = list ( combination ) writer . writerow ( [ tac ] + combination_list + [ compliance_stats [ key ] for key in value_keys ] ) generated_filenames . append ( per_tac_csv_filename ) else : logger . warning ( 'No per-TAC compliance data will be output to CSV file, as compliance data was not ' 'calculated or is empty' ) condition_combination_table = data . get ( 'condition_combination_table' ) if condition_combination_table is not None : with open ( os . path . join ( output_dir , condition_counts_filename ) , 'w' , encoding = 'utf8' ) as of : writer = csv . writer ( of ) writer . writerow ( condition_table_headers ) for combination in data [ 'condition_combination_table' ] : combination_list = [ combination [ 'combination' ] [ label ] for label in condition_labels ] writer . writerow ( combination_list + [ combination [ key ] for key in value_keys ] ) generated_filenames . append ( condition_counts_filename ) else : logger . warning ( 'No condition counts table data will be output to CSV file, as table data is empty' ) html = report . gen_html_report ( data , css_filename , js_filename ) with open ( os . path . join ( output_dir , html_filename ) , 'wb' ) as of : of . write ( html ) return _gen_metadata_for_reports ( generated_filenames , output_dir )
Helper function to write an individual report to disk.
Helper function to write an individual report to disk .
"""
    Helper function to write an individual report to disk.

    Arguments:
        report: Report type to write
        month: month of the generated report
        year: year of the generated report
        output_dir: path of the output dir
        filename_prefix: Prefix of report file name
        css_filename: name of the css file
        js_filename: name of the JS file
        per_tac_compliance_data: compliance data of tacs
    Returns:
        Reports metadata

    """ # Generate the raw data # Store a list of generate filenames so we can generate metadata # Write the raw JSON data to disk # Write the CSV per-TAC compliance data to disk # Write the CSV condition combination data to disk # Generate the HTML report # Write the HTML file to disk
None
report
callable
Report type to write
Report type to write
month
int
month of the generated report
month of the generated report
year
int
year of the generated report
year of the generated report
output_dir
str
path of the output dir
path of the output dir
filename_prefix
str
Prefix of report file name
Prefix of report file name
css_filename
str
name of the css file
name of the css file
js_filename
str
name of the JS file
name of the JS file
per_tac_compliance_data
dict
compliance data of tacs
compliance data of tacs