src/dirbs/reports/stats_generator.py
def _calc_invalid_id_pair_and_triplet_counts(db_config, month, year):
    """Calculate per-operator monthly stats about invalid data (pairs and triplets) for a given month and year."""
    with utils.create_db_connection(db_config) as conn, conn.cursor() as cursor, utils.CodeProfiler() as cp:
        results = defaultdict(lambda: dict(num_invalid_imei_imsis=0, num_invalid_imei_msisdns=0,
                                           num_invalid_triplets=0))
        durations = []
        for columns, result_label in [(['imei_norm', 'imsi'], 'num_invalid_imei_imsis'),
                                      (['imei_norm', 'msisdn'], 'num_invalid_imei_msisdns'),
                                      (['imei_norm', 'imsi', 'msisdn'], 'num_invalid_triplets')]:
            # First, generate stats for each operator, for each stat
            col_identifiers = sql.SQL(', ').join(map(sql.Identifier, columns))
            null_filters = sql.SQL('({0})') \
                .format(sql.SQL(' OR ').join(map(lambda x: sql.SQL('{0} IS NULL').format(sql.Identifier(x)), columns)))

            with utils.CodeProfiler() as scp:
                # Calculate per-operator invalid stat count
                cursor.execute(sql.SQL("""SELECT operator_id,
                                                 COUNT(*) AS cnt
                                            FROM (SELECT DISTINCT operator_id, {0}
                                                    FROM monthly_network_triplets_per_mno
                                                   WHERE triplet_month = %s
                                                     AND triplet_year = %s
                                                     AND {1}) sq
                                        GROUP BY operator_id""").format(col_identifiers, null_filters),
                               [month, year])
                for res in cursor:
                    results[res.operator_id][result_label] = res.cnt
            durations.append(scp.duration)

            with utils.CodeProfiler() as scp:
                # Calculate per-country invalid stat count
                cursor.execute(sql.SQL("""SELECT COUNT(*) AS cnt
                                            FROM (SELECT DISTINCT {0}
                                                    FROM monthly_network_triplets_country
                                                   WHERE triplet_month = %s
                                                     AND triplet_year = %s
                                                     AND {1}) sq""").format(col_identifiers, null_filters),
                               [month, year])
                for res in cursor:
                    results[OperatorConfig.COUNTRY_OPERATOR_NAME][result_label] = res.cnt
            durations.append(scp.duration)

    return _defaultdict_to_regular(results), cp.duration, durations
Calculate per-operator monthly stats about invalid data (pairs and triplets) for a given month and year.
Calculate per-operator monthly stats about invalid data (pairs and triplets) for a given month and year.
Calculate per - operator monthly stats about invalid data ( pairs and triplets ) for a given month and year .
def _calc_invalid_id_pair_and_triplet_counts(db_config, month, year):
    with utils.create_db_connection(db_config) as conn, conn.cursor() as cursor, utils.CodeProfiler() as cp:
        results = defaultdict(lambda: dict(num_invalid_imei_imsis=0, num_invalid_imei_msisdns=0,
                                           num_invalid_triplets=0))
        durations = []
        for columns, result_label in [(['imei_norm', 'imsi'], 'num_invalid_imei_imsis'),
                                      (['imei_norm', 'msisdn'], 'num_invalid_imei_msisdns'),
                                      (['imei_norm', 'imsi', 'msisdn'], 'num_invalid_triplets')]:
            col_identifiers = sql.SQL(', ').join(map(sql.Identifier, columns))
            null_filters = sql.SQL('({0})') \
                .format(sql.SQL(' OR ').join(map(lambda x: sql.SQL('{0} IS NULL').format(sql.Identifier(x)), columns)))
            with utils.CodeProfiler() as scp:
                cursor.execute(sql.SQL("""SELECT operator_id,
                                                 COUNT(*) AS cnt
                                            FROM (SELECT DISTINCT operator_id, {0}
                                                    FROM monthly_network_triplets_per_mno
                                                   WHERE triplet_month = %s
                                                     AND triplet_year = %s
                                                     AND {1}) sq
                                        GROUP BY operator_id""").format(col_identifiers, null_filters),
                               [month, year])
                for res in cursor:
                    results[res.operator_id][result_label] = res.cnt
            durations.append(scp.duration)
            with utils.CodeProfiler() as scp:
                cursor.execute(sql.SQL("""SELECT COUNT(*) AS cnt
                                            FROM (SELECT DISTINCT {0}
                                                    FROM monthly_network_triplets_country
                                                   WHERE triplet_month = %s
                                                     AND triplet_year = %s
                                                     AND {1}) sq""").format(col_identifiers, null_filters),
                               [month, year])
                for res in cursor:
                    results[OperatorConfig.COUNTRY_OPERATOR_NAME][result_label] = res.cnt
            durations.append(scp.duration)
    return _defaultdict_to_regular(results), cp.duration, durations
def _calc_invalid_id_pair_and_triplet_counts ( db_config , month , year ) : with utils . create_db_connection ( db_config ) as conn , conn . cursor ( ) as cursor , utils . CodeProfiler ( ) as cp : results = defaultdict ( lambda : dict ( num_invalid_imei_imsis = 0 , num_invalid_imei_msisdns = 0 , num_invalid_triplets = 0 ) ) durations = [ ] for columns , result_label in [ ( [ 'imei_norm' , 'imsi' ] , 'num_invalid_imei_imsis' ) , ( [ 'imei_norm' , 'msisdn' ] , 'num_invalid_imei_msisdns' ) , ( [ 'imei_norm' , 'imsi' , 'msisdn' ] , 'num_invalid_triplets' ) ] : col_identifiers = sql . SQL ( ', ' ) . join ( map ( sql . Identifier , columns ) ) null_filters = sql . SQL ( '({0})' ) . format ( sql . SQL ( ' OR ' ) . join ( map ( lambda x : sql . SQL ( '{0} IS NULL' ) . format ( sql . Identifier ( x ) ) , columns ) ) ) with utils . CodeProfiler ( ) as scp : cursor . execute ( sql . SQL ( """SELECT operator_id,
                                                 COUNT(*) AS cnt
                                            FROM (SELECT DISTINCT operator_id, {0}
                                                    FROM monthly_network_triplets_per_mno
                                                   WHERE triplet_month = %s
                                                     AND triplet_year = %s
                                                     AND {1}) sq
                                        GROUP BY operator_id""" ) . format ( col_identifiers , null_filters ) , [ month , year ] ) for res in cursor : results [ res . operator_id ] [ result_label ] = res . cnt durations . append ( scp . duration ) with utils . CodeProfiler ( ) as scp : cursor . execute ( sql . SQL ( """SELECT COUNT(*) AS cnt
                                            FROM (SELECT DISTINCT {0}
                                                    FROM monthly_network_triplets_country
                                                   WHERE triplet_month = %s
                                                     AND triplet_year = %s
                                                     AND {1}) sq""" ) . format ( col_identifiers , null_filters ) , [ month , year ] ) for res in cursor : results [ OperatorConfig . COUNTRY_OPERATOR_NAME ] [ result_label ] = res . cnt durations . append ( scp . duration ) return _defaultdict_to_regular ( results ) , cp . duration , durations
Calculate per-operator monthly stats about invalid data (pairs and triplets) for a given month and year.
Calculate per - operator monthly stats about invalid data ( pairs and triplets ) for a given month and year .
"""Calculate per-operator monthly stats about invalid data (pairs and triplets) for a given month and year.""" # First, generate stats for each operator, for each stat # Calculate per-operator invalid stat count # Calculate per-country invalid stat count
db_config
month
year