src/dirbs/reports/stats_generator.py
def _generate_compliance_data_table(conn, year, month, operator, condition_tuples, *, run_id):
    """Generates per-TAC compliance data in table form."""
    # Get full list of matching IMEIs for each condition at end of period
    per_condition_counts = defaultdict(dict)
    network_triplets_partition_id = _monthly_network_triplets_partition(conn=conn, month=month, year=year,
                                                                        operator=operator)
    durations = []
    # Populate per-condition count defaults -- make sure we have an entry for conditions even if we have no matching
    # rows
    for c in condition_tuples:
        per_condition_counts[c.label] = dict(num_imeis=0, num_imei_gross_adds=0,
                                             num_triplets=0, num_imei_imsis=0, num_imei_msisdns=0)

    with conn.cursor() as cursor:
        # Create temp tables
        cursor.execute('CREATE TEMP TABLE matching_imeis(imei_norm TEXT NOT NULL, cond_name TEXT NOT NULL)')
        cursor.execute('CREATE TEMP TABLE matching_gross_adds(imei_norm TEXT NOT NULL, cond_name TEXT NOT NULL)')
        cursor.execute("""CREATE TEMP TABLE condition_info(cond_name TEXT NOT NULL,
                                                           cond_order INTEGER NOT NULL,
                                                           is_blocking BOOLEAN NOT NULL)""")

        # Popoulate condition order
        with utils.CodeProfiler() as scp:
            execute_values(cursor, 'INSERT INTO condition_info VALUES %s',
                           [(c.label, idx, c.blocking) for idx, c in enumerate(condition_tuples, start=1)])
            cursor.execute('CREATE UNIQUE INDEX ON condition_info(cond_name)')
            cursor.execute('ANALYZE condition_info')
        durations.append(scp.duration)

        # Populate matching imeis table
        with utils.CodeProfiler() as scp:
            cursor.execute(sql.SQL("""INSERT INTO matching_imeis
                                           SELECT imei_norm, cond_name
                                             FROM classification_state
                                            WHERE end_date IS NULL
                                              AND cond_name IN %s
                                              AND EXISTS (SELECT 1
                                                            FROM {network_triplets_partition_id}
                                                           WHERE imei_norm = classification_state.imei_norm)""")
                           .format(network_triplets_partition_id=network_triplets_partition_id),
                           [tuple([x.label for x in condition_tuples])])
            cursor.execute('CREATE INDEX ON matching_imeis(imei_norm)')
            cursor.execute('ANALYZE matching_imeis')
        durations.append(scp.duration)

        # Populate matching_gross_adds table
        with utils.CodeProfiler() as scp:
            cursor.execute("""INSERT INTO matching_gross_adds
                                   SELECT *
                                     FROM matching_imeis
                                    WHERE EXISTS (SELECT 1
                                                    FROM network_gross_adds
                                                   WHERE imei_norm = matching_imeis.imei_norm)""")
            cursor.execute('ANALYZE matching_gross_adds')
        durations.append(scp.duration)

        # Calculate counts per condition
        with utils.CodeProfiler() as scp:
            cursor.execute("""SELECT cond_name,
                                     COUNT(*) AS imei_count,
                                     COALESCE(SUM(per_imei_triplet_counts), 0) AS sum_per_imei_triplet_counts,
                                     COALESCE(SUM(per_imei_imsi_counts), 0) AS sum_per_imei_imsi_counts,
                                     COALESCE(SUM(per_imei_msisdn_counts), 0) AS sum_per_imei_msisdn_counts
                                FROM matching_imeis
                                JOIN network_triplet_counts
                               USING (imei_norm)
                            GROUP BY cond_name""")

            for res in cursor:
                per_condition_counts[res.cond_name]['num_imeis'] = res.imei_count
                per_condition_counts[res.cond_name]['num_triplets'] = res.sum_per_imei_triplet_counts
                per_condition_counts[res.cond_name]['num_imei_imsis'] = res.sum_per_imei_imsi_counts
                per_condition_counts[res.cond_name]['num_imei_msisdns'] = res.sum_per_imei_msisdn_counts
        durations.append(scp.duration)

        with utils.CodeProfiler() as scp:
            cursor.execute("""SELECT cond_name,
                                     COUNT(*) AS imei_count
                                FROM matching_gross_adds
                            GROUP BY cond_name""")

            for res in cursor:
                per_condition_counts[res.cond_name]['num_imei_gross_adds'] = res.imei_count
        durations.append(scp.duration)

        # Now generate an IMEI-level table that contains which conditions that IMEI as a array of booleans.
        # The first element in the array corresponds to the first element in condition_tuples and indicates
        # whether that IMEI meets that condition or not. All IMEIs are included in this so a fully-compliant
        # IMEI will also be in this table and all array elements will be False
        cursor.execute("""CREATE TEMP TABLE per_imei_compliance(imei_norm TEXT NOT NULL,
                                                                condition_status BOOLEAN[] NOT NULL,
                                                                is_gross_add BOOLEAN NOT NULL,
                                                                per_imei_imsi_counts BIGINT NOT NULL,
                                                                per_imei_msisdn_counts BIGINT NOT NULL,
                                                                per_imei_triplet_counts BIGINT NOT NULL,
                                                                meets_blocking BOOLEAN)""")
        with utils.CodeProfiler() as scp:
            cursor.execute("""INSERT INTO per_imei_compliance
                                   SELECT imei_norm,
                                          generate_imei_condition_status(met_condition_indices, %(num_conditions)s),
                                          ga_cond.exists,
                                          per_imei_imsi_counts,
                                          per_imei_msisdn_counts,
                                          per_imei_triplet_counts,
                                          meets_blocking
                                     FROM network_triplet_counts
                                LEFT JOIN (SELECT imei_norm,
                                                  array_agg(cond_order) AS met_condition_indices,
                                                  bool_or(is_blocking) AS meets_blocking
                                             FROM matching_imeis
                                             JOIN condition_info
                                            USING (cond_name)
                                         GROUP BY imei_norm) imeis_cond
                                    USING (imei_norm)
                                     JOIN LATERAL (SELECT EXISTS(SELECT 1
                                                                   FROM network_gross_adds
                                                                  WHERE imei_norm = network_triplet_counts.imei_norm)
                                                  ) ga_cond
                                          ON TRUE
                                    """,  # noqa: Q447, Q449
                           {'num_conditions': len(condition_tuples)})
            cursor.execute('ANALYZE per_imei_compliance')
        durations.append(scp.duration)

        # Now roll our IMEI level table up to the TAC level
        cursor.execute("""CREATE TEMP TABLE per_tac_compliance(tac TEXT NOT NULL,
                                                               condition_status BOOLEAN[] NOT NULL,
                                                               compliance_level SMALLINT NOT NULL,
                                                               num_imeis BIGINT NOT NULL,
                                                               num_imei_gross_adds BIGINT NOT NULL,
                                                               num_imei_imsis BIGINT NOT NULL,
                                                               num_imei_msisdns BIGINT NOT NULL,
                                                               num_subscriber_triplets BIGINT NOT NULL)""")
        with utils.CodeProfiler() as scp:
            cursor.execute("""INSERT INTO per_tac_compliance
                                   SELECT LEFT(imei_norm, 8) AS tac,
                                          condition_status,
                                          CASE FIRST(meets_blocking) WHEN TRUE THEN 0
                                                                     WHEN FALSE THEN 1
                                                                     ELSE 2 END AS compliance_level,
                                          COUNT(*) AS num_imeis,
                                          COUNT(*) filter (WHERE is_gross_add = TRUE) AS num_imei_gross_adds,
                                          SUM(per_imei_imsi_counts) AS num_imei_imsis,
                                          SUM(per_imei_msisdn_counts) AS num_imei_msisdns,
                                          SUM(per_imei_triplet_counts) AS num_triplets
                                     FROM per_imei_compliance
                                 GROUP BY LEFT(imei_norm, 8), condition_status""")  # noqa: Q447
            cursor.execute('ANALYZE per_tac_compliance')
        durations.append(scp.duration)

    # Create a data structure that is two-level map. The first level is indexed by
    # TAC. The next level is indexed by the tuple of condition flags
    per_tac_results = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
    results = defaultdict(lambda: defaultdict(int))

    # Walk through per-TAC results
    with conn.cursor(name='per_tac_counts') as cursor, utils.CodeProfiler() as scp:
        cursor.execute('SELECT * FROM per_tac_compliance')
        for res in cursor:
            # Pop off the tac and condition_status, which are used as keys rather than values in the results
            res_dict = res._asdict()
            tac = res_dict.pop('tac')
            condition_flags = res_dict.pop('condition_status')
            compliance_level = res_dict.pop('compliance_level')
            per_tac_results[tac][tuple(condition_flags)]['compliance_level'] = compliance_level
            results[tuple(condition_flags)]['compliance_level'] = compliance_level

            # At this point, everything is something that should be stored in the results dictionaries
            for k, v in res_dict.items():
                per_tac_results[tac][tuple(condition_flags)][k] = v
                results[tuple(condition_flags)][k] += v
    durations.append(scp.duration)

    return _defaultdict_to_regular(per_tac_results), \
        _defaultdict_to_regular(results), \
        _defaultdict_to_regular(per_condition_counts), \
        durations
Generates per-TAC compliance data in table form.
Generates per-TAC compliance data in table form.
Generates per - TAC compliance data in table form .
def _generate_compliance_data_table(conn, year, month, operator, condition_tuples, *, run_id):
    per_condition_counts = defaultdict(dict)
    network_triplets_partition_id = _monthly_network_triplets_partition(conn=conn, month=month, year=year,
                                                                        operator=operator)
    durations = []
    for c in condition_tuples:
        per_condition_counts[c.label] = dict(num_imeis=0, num_imei_gross_adds=0,
                                             num_triplets=0, num_imei_imsis=0, num_imei_msisdns=0)
    with conn.cursor() as cursor:
        cursor.execute('CREATE TEMP TABLE matching_imeis(imei_norm TEXT NOT NULL, cond_name TEXT NOT NULL)')
        cursor.execute('CREATE TEMP TABLE matching_gross_adds(imei_norm TEXT NOT NULL, cond_name TEXT NOT NULL)')
        cursor.execute("""CREATE TEMP TABLE condition_info(cond_name TEXT NOT NULL,
                                                           cond_order INTEGER NOT NULL,
                                                           is_blocking BOOLEAN NOT NULL)""")
        with utils.CodeProfiler() as scp:
            execute_values(cursor, 'INSERT INTO condition_info VALUES %s',
                           [(c.label, idx, c.blocking) for idx, c in enumerate(condition_tuples, start=1)])
            cursor.execute('CREATE UNIQUE INDEX ON condition_info(cond_name)')
            cursor.execute('ANALYZE condition_info')
        durations.append(scp.duration)
        with utils.CodeProfiler() as scp:
            cursor.execute(sql.SQL("""INSERT INTO matching_imeis
                                           SELECT imei_norm, cond_name
                                             FROM classification_state
                                            WHERE end_date IS NULL
                                              AND cond_name IN %s
                                              AND EXISTS (SELECT 1
                                                            FROM {network_triplets_partition_id}
                                                           WHERE imei_norm = classification_state.imei_norm)""")
                           .format(network_triplets_partition_id=network_triplets_partition_id),
                           [tuple([x.label for x in condition_tuples])])
            cursor.execute('CREATE INDEX ON matching_imeis(imei_norm)')
            cursor.execute('ANALYZE matching_imeis')
        durations.append(scp.duration)
        with utils.CodeProfiler() as scp:
            cursor.execute("""INSERT INTO matching_gross_adds
                                   SELECT *
                                     FROM matching_imeis
                                    WHERE EXISTS (SELECT 1
                                                    FROM network_gross_adds
                                                   WHERE imei_norm = matching_imeis.imei_norm)""")
            cursor.execute('ANALYZE matching_gross_adds')
        durations.append(scp.duration)
        with utils.CodeProfiler() as scp:
            cursor.execute("""SELECT cond_name,
                                     COUNT(*) AS imei_count,
                                     COALESCE(SUM(per_imei_triplet_counts), 0) AS sum_per_imei_triplet_counts,
                                     COALESCE(SUM(per_imei_imsi_counts), 0) AS sum_per_imei_imsi_counts,
                                     COALESCE(SUM(per_imei_msisdn_counts), 0) AS sum_per_imei_msisdn_counts
                                FROM matching_imeis
                                JOIN network_triplet_counts
                               USING (imei_norm)
                            GROUP BY cond_name""")
            for res in cursor:
                per_condition_counts[res.cond_name]['num_imeis'] = res.imei_count
                per_condition_counts[res.cond_name]['num_triplets'] = res.sum_per_imei_triplet_counts
                per_condition_counts[res.cond_name]['num_imei_imsis'] = res.sum_per_imei_imsi_counts
                per_condition_counts[res.cond_name]['num_imei_msisdns'] = res.sum_per_imei_msisdn_counts
        durations.append(scp.duration)
        with utils.CodeProfiler() as scp:
            cursor.execute("""SELECT cond_name,
                                     COUNT(*) AS imei_count
                                FROM matching_gross_adds
                            GROUP BY cond_name""")
            for res in cursor:
                per_condition_counts[res.cond_name]['num_imei_gross_adds'] = res.imei_count
        durations.append(scp.duration)
        cursor.execute("""CREATE TEMP TABLE per_imei_compliance(imei_norm TEXT NOT NULL,
                                                                condition_status BOOLEAN[] NOT NULL,
                                                                is_gross_add BOOLEAN NOT NULL,
                                                                per_imei_imsi_counts BIGINT NOT NULL,
                                                                per_imei_msisdn_counts BIGINT NOT NULL,
                                                                per_imei_triplet_counts BIGINT NOT NULL,
                                                                meets_blocking BOOLEAN)""")
        with utils.CodeProfiler() as scp:
            cursor.execute("""INSERT INTO per_imei_compliance
                                   SELECT imei_norm,
                                          generate_imei_condition_status(met_condition_indices, %(num_conditions)s),
                                          ga_cond.exists,
                                          per_imei_imsi_counts,
                                          per_imei_msisdn_counts,
                                          per_imei_triplet_counts,
                                          meets_blocking
                                     FROM network_triplet_counts
                                LEFT JOIN (SELECT imei_norm,
                                                  array_agg(cond_order) AS met_condition_indices,
                                                  bool_or(is_blocking) AS meets_blocking
                                             FROM matching_imeis
                                             JOIN condition_info
                                            USING (cond_name)
                                         GROUP BY imei_norm) imeis_cond
                                    USING (imei_norm)
                                     JOIN LATERAL (SELECT EXISTS(SELECT 1
                                                                   FROM network_gross_adds
                                                                  WHERE imei_norm = network_triplet_counts.imei_norm)
                                                  ) ga_cond
                                          ON TRUE
                                    """,  
                           {'num_conditions': len(condition_tuples)})
            cursor.execute('ANALYZE per_imei_compliance')
        durations.append(scp.duration)
        cursor.execute("""CREATE TEMP TABLE per_tac_compliance(tac TEXT NOT NULL,
                                                               condition_status BOOLEAN[] NOT NULL,
                                                               compliance_level SMALLINT NOT NULL,
                                                               num_imeis BIGINT NOT NULL,
                                                               num_imei_gross_adds BIGINT NOT NULL,
                                                               num_imei_imsis BIGINT NOT NULL,
                                                               num_imei_msisdns BIGINT NOT NULL,
                                                               num_subscriber_triplets BIGINT NOT NULL)""")
        with utils.CodeProfiler() as scp:
            cursor.execute("""INSERT INTO per_tac_compliance
                                   SELECT LEFT(imei_norm, 8) AS tac,
                                          condition_status,
                                          CASE FIRST(meets_blocking) WHEN TRUE THEN 0
                                                                     WHEN FALSE THEN 1
                                                                     ELSE 2 END AS compliance_level,
                                          COUNT(*) AS num_imeis,
                                          COUNT(*) filter (WHERE is_gross_add = TRUE) AS num_imei_gross_adds,
                                          SUM(per_imei_imsi_counts) AS num_imei_imsis,
                                          SUM(per_imei_msisdn_counts) AS num_imei_msisdns,
                                          SUM(per_imei_triplet_counts) AS num_triplets
                                     FROM per_imei_compliance
                                 GROUP BY LEFT(imei_norm, 8), condition_status""")  
            cursor.execute('ANALYZE per_tac_compliance')
        durations.append(scp.duration)
    per_tac_results = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
    results = defaultdict(lambda: defaultdict(int))
    with conn.cursor(name='per_tac_counts') as cursor, utils.CodeProfiler() as scp:
        cursor.execute('SELECT * FROM per_tac_compliance')
        for res in cursor:
            res_dict = res._asdict()
            tac = res_dict.pop('tac')
            condition_flags = res_dict.pop('condition_status')
            compliance_level = res_dict.pop('compliance_level')
            per_tac_results[tac][tuple(condition_flags)]['compliance_level'] = compliance_level
            results[tuple(condition_flags)]['compliance_level'] = compliance_level
            for k, v in res_dict.items():
                per_tac_results[tac][tuple(condition_flags)][k] = v
                results[tuple(condition_flags)][k] += v
    durations.append(scp.duration)
    return _defaultdict_to_regular(per_tac_results), \
        _defaultdict_to_regular(results), \
        _defaultdict_to_regular(per_condition_counts), \
        durations
def _generate_compliance_data_table ( conn , year , month , operator , condition_tuples , * , run_id ) : per_condition_counts = defaultdict ( dict ) network_triplets_partition_id = _monthly_network_triplets_partition ( conn = conn , month = month , year = year , operator = operator ) durations = [ ] for c in condition_tuples : per_condition_counts [ c . label ] = dict ( num_imeis = 0 , num_imei_gross_adds = 0 , num_triplets = 0 , num_imei_imsis = 0 , num_imei_msisdns = 0 ) with conn . cursor ( ) as cursor : cursor . execute ( 'CREATE TEMP TABLE matching_imeis(imei_norm TEXT NOT NULL, cond_name TEXT NOT NULL)' ) cursor . execute ( 'CREATE TEMP TABLE matching_gross_adds(imei_norm TEXT NOT NULL, cond_name TEXT NOT NULL)' ) cursor . execute ( """CREATE TEMP TABLE condition_info(cond_name TEXT NOT NULL,
                                                           cond_order INTEGER NOT NULL,
                                                           is_blocking BOOLEAN NOT NULL)""" ) with utils . CodeProfiler ( ) as scp : execute_values ( cursor , 'INSERT INTO condition_info VALUES %s' , [ ( c . label , idx , c . blocking ) for idx , c in enumerate ( condition_tuples , start = 1 ) ] ) cursor . execute ( 'CREATE UNIQUE INDEX ON condition_info(cond_name)' ) cursor . execute ( 'ANALYZE condition_info' ) durations . append ( scp . duration ) with utils . CodeProfiler ( ) as scp : cursor . execute ( sql . SQL ( """INSERT INTO matching_imeis
                                           SELECT imei_norm, cond_name
                                             FROM classification_state
                                            WHERE end_date IS NULL
                                              AND cond_name IN %s
                                              AND EXISTS (SELECT 1
                                                            FROM {network_triplets_partition_id}
                                                           WHERE imei_norm = classification_state.imei_norm)""" ) . format ( network_triplets_partition_id = network_triplets_partition_id ) , [ tuple ( [ x . label for x in condition_tuples ] ) ] ) cursor . execute ( 'CREATE INDEX ON matching_imeis(imei_norm)' ) cursor . execute ( 'ANALYZE matching_imeis' ) durations . append ( scp . duration ) with utils . CodeProfiler ( ) as scp : cursor . execute ( """INSERT INTO matching_gross_adds
                                   SELECT *
                                     FROM matching_imeis
                                    WHERE EXISTS (SELECT 1
                                                    FROM network_gross_adds
                                                   WHERE imei_norm = matching_imeis.imei_norm)""" ) cursor . execute ( 'ANALYZE matching_gross_adds' ) durations . append ( scp . duration ) with utils . CodeProfiler ( ) as scp : cursor . execute ( """SELECT cond_name,
                                     COUNT(*) AS imei_count,
                                     COALESCE(SUM(per_imei_triplet_counts), 0) AS sum_per_imei_triplet_counts,
                                     COALESCE(SUM(per_imei_imsi_counts), 0) AS sum_per_imei_imsi_counts,
                                     COALESCE(SUM(per_imei_msisdn_counts), 0) AS sum_per_imei_msisdn_counts
                                FROM matching_imeis
                                JOIN network_triplet_counts
                               USING (imei_norm)
                            GROUP BY cond_name""" ) for res in cursor : per_condition_counts [ res . cond_name ] [ 'num_imeis' ] = res . imei_count per_condition_counts [ res . cond_name ] [ 'num_triplets' ] = res . sum_per_imei_triplet_counts per_condition_counts [ res . cond_name ] [ 'num_imei_imsis' ] = res . sum_per_imei_imsi_counts per_condition_counts [ res . cond_name ] [ 'num_imei_msisdns' ] = res . sum_per_imei_msisdn_counts durations . append ( scp . duration ) with utils . CodeProfiler ( ) as scp : cursor . execute ( """SELECT cond_name,
                                     COUNT(*) AS imei_count
                                FROM matching_gross_adds
                            GROUP BY cond_name""" ) for res in cursor : per_condition_counts [ res . cond_name ] [ 'num_imei_gross_adds' ] = res . imei_count durations . append ( scp . duration ) cursor . execute ( """CREATE TEMP TABLE per_imei_compliance(imei_norm TEXT NOT NULL,
                                                                condition_status BOOLEAN[] NOT NULL,
                                                                is_gross_add BOOLEAN NOT NULL,
                                                                per_imei_imsi_counts BIGINT NOT NULL,
                                                                per_imei_msisdn_counts BIGINT NOT NULL,
                                                                per_imei_triplet_counts BIGINT NOT NULL,
                                                                meets_blocking BOOLEAN)""" ) with utils . CodeProfiler ( ) as scp : cursor . execute ( """INSERT INTO per_imei_compliance
                                   SELECT imei_norm,
                                          generate_imei_condition_status(met_condition_indices, %(num_conditions)s),
                                          ga_cond.exists,
                                          per_imei_imsi_counts,
                                          per_imei_msisdn_counts,
                                          per_imei_triplet_counts,
                                          meets_blocking
                                     FROM network_triplet_counts
                                LEFT JOIN (SELECT imei_norm,
                                                  array_agg(cond_order) AS met_condition_indices,
                                                  bool_or(is_blocking) AS meets_blocking
                                             FROM matching_imeis
                                             JOIN condition_info
                                            USING (cond_name)
                                         GROUP BY imei_norm) imeis_cond
                                    USING (imei_norm)
                                     JOIN LATERAL (SELECT EXISTS(SELECT 1
                                                                   FROM network_gross_adds
                                                                  WHERE imei_norm = network_triplet_counts.imei_norm)
                                                  ) ga_cond
                                          ON TRUE
                                    """ , { 'num_conditions' : len ( condition_tuples ) } ) cursor . execute ( 'ANALYZE per_imei_compliance' ) durations . append ( scp . duration ) cursor . execute ( """CREATE TEMP TABLE per_tac_compliance(tac TEXT NOT NULL,
                                                               condition_status BOOLEAN[] NOT NULL,
                                                               compliance_level SMALLINT NOT NULL,
                                                               num_imeis BIGINT NOT NULL,
                                                               num_imei_gross_adds BIGINT NOT NULL,
                                                               num_imei_imsis BIGINT NOT NULL,
                                                               num_imei_msisdns BIGINT NOT NULL,
                                                               num_subscriber_triplets BIGINT NOT NULL)""" ) with utils . CodeProfiler ( ) as scp : cursor . execute ( """INSERT INTO per_tac_compliance
                                   SELECT LEFT(imei_norm, 8) AS tac,
                                          condition_status,
                                          CASE FIRST(meets_blocking) WHEN TRUE THEN 0
                                                                     WHEN FALSE THEN 1
                                                                     ELSE 2 END AS compliance_level,
                                          COUNT(*) AS num_imeis,
                                          COUNT(*) filter (WHERE is_gross_add = TRUE) AS num_imei_gross_adds,
                                          SUM(per_imei_imsi_counts) AS num_imei_imsis,
                                          SUM(per_imei_msisdn_counts) AS num_imei_msisdns,
                                          SUM(per_imei_triplet_counts) AS num_triplets
                                     FROM per_imei_compliance
                                 GROUP BY LEFT(imei_norm, 8), condition_status""" ) cursor . execute ( 'ANALYZE per_tac_compliance' ) durations . append ( scp . duration ) per_tac_results = defaultdict ( lambda : defaultdict ( lambda : defaultdict ( int ) ) ) results = defaultdict ( lambda : defaultdict ( int ) ) with conn . cursor ( name = 'per_tac_counts' ) as cursor , utils . CodeProfiler ( ) as scp : cursor . execute ( 'SELECT * FROM per_tac_compliance' ) for res in cursor : res_dict = res . _asdict ( ) tac = res_dict . pop ( 'tac' ) condition_flags = res_dict . pop ( 'condition_status' ) compliance_level = res_dict . pop ( 'compliance_level' ) per_tac_results [ tac ] [ tuple ( condition_flags ) ] [ 'compliance_level' ] = compliance_level results [ tuple ( condition_flags ) ] [ 'compliance_level' ] = compliance_level for k , v in res_dict . items ( ) : per_tac_results [ tac ] [ tuple ( condition_flags ) ] [ k ] = v results [ tuple ( condition_flags ) ] [ k ] += v durations . append ( scp . duration ) return _defaultdict_to_regular ( per_tac_results ) , _defaultdict_to_regular ( results ) , _defaultdict_to_regular ( per_condition_counts ) , durations
Generates per-TAC compliance data in table form.
Generates per - TAC compliance data in table form .
"""Generates per-TAC compliance data in table form.""" # Get full list of matching IMEIs for each condition at end of period # Populate per-condition count defaults -- make sure we have an entry for conditions even if we have no matching # rows # Create temp tables # Popoulate condition order # Populate matching imeis table # Populate matching_gross_adds table # Calculate counts per condition # Now generate an IMEI-level table that contains which conditions that IMEI as a array of booleans. # The first element in the array corresponds to the first element in condition_tuples and indicates # whether that IMEI meets that condition or not. All IMEIs are included in this so a fully-compliant # IMEI will also be in this table and all array elements will be False # noqa: Q447, Q449 # Now roll our IMEI level table up to the TAC level # noqa: Q447 # Create a data structure that is two-level map. The first level is indexed by # TAC. The next level is indexed by the tuple of condition flags # Walk through per-TAC results # Pop off the tac and condition_status, which are used as keys rather than values in the results # At this point, everything is something that should be stored in the results dictionaries
conn
year
month
operator
condition_tuples
run_id