File size: 12,848 Bytes
d269960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import numpy as np
import pandas as pd

from queries.process_gsm import combined_gsm_database
from utils.check_sheet_exist import execute_checks_sheets_exist
from utils.convert_to_excel import convert_dfs, save_dataframe
from utils.kpi_analysis_utils import (
    GsmAnalysis,
    create_daily_date,
    create_dfs_per_kpi,
    create_hourly_date,
    kpi_naming_cleaning,
)


class GsmCapacity:
    final_results = None


GSM_COLUMNS = [
    "ID_BTS",
    "site_name",
    "name",
    "BSC",
    "BCF",
    "BTS",
    "code",
    "Region",
    "adminState",
    "frequencyBandInUse",
    "amrSegLoadDepTchRateLower",
    "amrSegLoadDepTchRateUpper",
    "dedicatedGPRScapacity",
    "defaultGPRScapacity",
    "cellId",
    "band",
    "site_config_band",
    "trxRfPower",
    "BCCH",
    "number_trx_per_cell",
    "number_trx_per_bcf",
    "TRX_TCH",
    "MAL_TCH",
]

TRX_COLUMNS = [
    "ID_BTS",
    "number_tch_per_cell",
    "number_sd_per_cell",
    "number_bcch_per_cell",
    "number_ccch_per_cell",
    "number_cbc_per_cell",
    "number_total_channels_per_cell",
    "number_signals_per_cell",
]

KPI_COLUMNS = [
    "date",
    "BTS_name",
    "TCH_availability_ratio",
    "2G_Carried_Traffic",
    "TCH_call_blocking",
    "TCH_ABIS_FAIL_CALL_c001084",
    "SDCCH_real_blocking",
]
BH_COLUMNS_FOR_CAPACITY = [
    "Max_Traffic BH",
    "Avg_Traffic BH",
    "Max_tch_call_blocking BH",
    "Avg_tch_call_blocking BH",
    "number_of_days_with_tch_blocking_exceeded",
    "Max_sdcch_real_blocking BH",
    "Avg_sdcch_real_blocking BH",
    "number_of_days_with_sdcch_blocking_exceeded",
]


def bh_tch_call_blocking_analysis(
    df: pd.DataFrame,
    number_of_kpi_days: int,
    tch_blocking_threshold: int,
    number_of_threshold_days: int,
) -> pd.DataFrame:

    result_df = df.copy()
    last_days_df = result_df.iloc[:, -number_of_kpi_days:]
    # last_days_df = last_days_df.fillna(0)

    result_df["Avg_tch_call_blocking BH"] = last_days_df.mean(axis=1).round(2)
    result_df["Max_tch_call_blocking BH"] = last_days_df.max(axis=1)
    # Count the number of days above threshold
    result_df["number_of_days_with_tch_blocking_exceeded"] = last_days_df.apply(
        lambda row: sum(1 for x in row if x >= tch_blocking_threshold), axis=1
    )
    return result_df


def bh_sdcch_call_blocking_analysis(
    df: pd.DataFrame,
    number_of_kpi_days: int,
    sdcch_blocking_threshold: int,
    number_of_threshold_days: int,
) -> pd.DataFrame:

    result_df = df.copy()
    last_days_df = result_df.iloc[:, -number_of_kpi_days:]
    # last_days_df = last_days_df.fillna(0)

    result_df["Avg_sdcch_real_blocking BH"] = last_days_df.mean(axis=1).round(2)
    result_df["Max_sdcch_real_blocking BH"] = last_days_df.max(axis=1)
    # Count the number of days above threshold
    result_df["number_of_days_with_sdcch_blocking_exceeded"] = last_days_df.apply(
        lambda row: sum(1 for x in row if x >= sdcch_blocking_threshold), axis=1
    )
    return result_df


def bh_traffic_analysis(
    df: pd.DataFrame,
    number_of_kpi_days: int,
) -> pd.DataFrame:

    result_df = df.copy()
    last_days_df = result_df.iloc[:, -number_of_kpi_days:]
    # last_days_df = last_days_df.fillna(0)

    result_df["Avg_Traffic BH"] = last_days_df.mean(axis=1).round(2)
    result_df["Max_Traffic BH"] = last_days_df.max(axis=1)
    return result_df


def bh_dfs_per_kpi(
    df: pd.DataFrame,
    number_of_kpi_days: int = 7,
    tch_blocking_threshold: int = 0.50,
    sdcch_blocking_threshold: int = 0.50,
    number_of_threshold_days: int = 3,
) -> pd.DataFrame:
    """
    Create pivoted DataFrames for each KPI and perform analysis.

    Args:
        df: DataFrame containing KPI data
        number_of_kpi_days: Number of days to analyze
        threshold: Utilization threshold percentage for flagging
        number_of_threshold_days: Minimum days above threshold to flag for upgrade

    Returns:
        DataFrame with combined analysis results
    """
    pivoted_kpi_dfs = {}

    pivoted_kpi_dfs = create_dfs_per_kpi(
        df=df,
        pivot_date_column="date",
        pivot_name_column="BTS_name",
        kpi_columns_from=2,
    )

    tch_call_blocking_df: pd.DataFrame = pivoted_kpi_dfs["TCH_call_blocking"]
    sdcch_real_blocking_df: pd.DataFrame = pivoted_kpi_dfs["SDCCH_real_blocking"]
    Carried_Traffic_df: pd.DataFrame = pivoted_kpi_dfs["2G_Carried_Traffic"]
    tch_availability_ratio_df: pd.DataFrame = pivoted_kpi_dfs["TCH_availability_ratio"]

    # ANALISYS

    tch_call_blocking_df = bh_tch_call_blocking_analysis(
        df=tch_call_blocking_df,
        number_of_kpi_days=number_of_kpi_days,
        tch_blocking_threshold=tch_blocking_threshold,
        number_of_threshold_days=number_of_threshold_days,
    )

    sdcch_real_blocking_df = bh_sdcch_call_blocking_analysis(
        df=sdcch_real_blocking_df,
        number_of_kpi_days=number_of_kpi_days,
        sdcch_blocking_threshold=sdcch_blocking_threshold,
        number_of_threshold_days=number_of_threshold_days,
    )

    Carried_Traffic_df = bh_traffic_analysis(
        df=Carried_Traffic_df,
        number_of_kpi_days=number_of_kpi_days,
    )

    # Carried_Traffic_df["Max_Traffic BH"] = Carried_Traffic_df.max(axis=1)
    # Carried_Traffic_df["Avg_Traffic BH"] = Carried_Traffic_df.mean(axis=1)

    bh_kpi_df = pd.concat(
        [
            tch_availability_ratio_df,
            Carried_Traffic_df,
            tch_call_blocking_df,
            sdcch_real_blocking_df,
        ],
        axis=1,
    )
    # print(Carried_Traffic_df)

    return bh_kpi_df


def analyse_bh_data(
    bh_report_path: str,
    number_of_kpi_days: int,
    tch_blocking_threshold: int,
    sdcch_blocking_threshold: int,
    number_of_threshold_days: int,
) -> pd.DataFrame:
    df = pd.read_csv(bh_report_path, delimiter=";")
    df = kpi_naming_cleaning(df)
    df = create_hourly_date(df)
    df = df[KPI_COLUMNS]
    df = bh_dfs_per_kpi(
        df=df,
        number_of_kpi_days=number_of_kpi_days,
        tch_blocking_threshold=tch_blocking_threshold,
        sdcch_blocking_threshold=sdcch_blocking_threshold,
        number_of_threshold_days=number_of_threshold_days,
    )

    bh_df_for_capacity = df.copy()
    bh_df_for_capacity = bh_df_for_capacity[BH_COLUMNS_FOR_CAPACITY]
    bh_df_for_capacity = bh_df_for_capacity.reset_index()

    # If columns have multiple levels (MultiIndex), flatten them
    if isinstance(bh_df_for_capacity.columns, pd.MultiIndex):
        bh_df_for_capacity.columns = [
            "_".join([str(el) for el in col if el])
            for col in bh_df_for_capacity.columns.values
        ]
    # bh_df_for_capacity = bh_df_for_capacity.reset_index()

    # rename Bts_name to name
    bh_df_for_capacity = bh_df_for_capacity.rename(columns={"BTS_name": "name"})

    return [bh_df_for_capacity, df]


def daily_dfs_per_kpi(
    df: pd.DataFrame,
    number_of_kpi_days: int = 7,
    availability_threshold: int = 95,
    number_of_threshold_days: int = 3,
    tch_abis_fails_threshold: int = 10,
) -> pd.DataFrame:
    """
    Create pivoted DataFrames for each KPI and perform analysis.

    Args:
        df: DataFrame containing KPI data
        number_of_kpi_days: Number of days to analyze
        threshold: Utilization threshold percentage for flagging
        number_of_threshold_days: Minimum days above threshold to flag for upgrade

    Returns:
        DataFrame with combined analysis results
    """
    pivoted_kpi_dfs = {}

    pivoted_kpi_dfs = create_dfs_per_kpi(
        df=df,
        pivot_date_column="date",
        pivot_name_column="BTS_name",
        kpi_columns_from=2,
    )

    tch_call_blocking_df: pd.DataFrame = pivoted_kpi_dfs["TCH_call_blocking"]
    sdcch_real_blocking_df: pd.DataFrame = pivoted_kpi_dfs["SDCCH_real_blocking"]
    Carried_Traffic_df: pd.DataFrame = pivoted_kpi_dfs["2G_Carried_Traffic"]
    tch_availability_ratio_df: pd.DataFrame = pivoted_kpi_dfs["TCH_availability_ratio"]
    tch_abis_fails_df: pd.DataFrame = pivoted_kpi_dfs["TCH_ABIS_FAIL_CALL_c001084"]


def analyse_daily_data(
    daily_report_path: str,
    number_of_kpi_days: int,
    tch_abis_fails_threshold: int,
    availability_threshold: int,
    number_of_threshold_days: int,
) -> pd.DataFrame:
    df = pd.read_csv(daily_report_path, delimiter=";")
    df = kpi_naming_cleaning(df)
    df = create_daily_date(df)
    df = df[KPI_COLUMNS]
    df = daily_dfs_per_kpi(
        df=df,
        number_of_kpi_days=number_of_kpi_days,
        availability_threshold=availability_threshold,
        tch_abis_fails_threshold=tch_abis_fails_threshold,
        number_of_threshold_days=number_of_threshold_days,
    )
    # print(df)


def get_gsm_databases(dump_path: str) -> pd.DataFrame:

    dfs = combined_gsm_database(dump_path)
    bts_df: pd.DataFrame = dfs[0]
    trx_df: pd.DataFrame = dfs[2]

    # Clean GSM df
    bts_df = bts_df[GSM_COLUMNS]
    trx_df = trx_df[TRX_COLUMNS]

    # Remove duplicate in TRX df
    trx_df = trx_df.drop_duplicates(subset=["ID_BTS"])

    gsm_df = pd.merge(bts_df, trx_df, on="ID_BTS", how="left")

    # add hf_rate_coef
    gsm_df["hf_rate_coef"] = gsm_df["amrSegLoadDepTchRateLower"].map(
        GsmAnalysis.hf_rate_coef
    )
    # Add "GPRS" colomn equal to (dedicatedGPRScapacity * number_tch_per_cell)/100
    gsm_df["GPRS"] = (
        gsm_df["dedicatedGPRScapacity"] * gsm_df["number_tch_per_cell"]
    ) / 100

    # "TCH Actual HR%" equal to "number of TCH" multiplyed by "Coef HF rate"
    gsm_df["TCH Actual HR%"] = gsm_df["number_tch_per_cell"] * gsm_df["hf_rate_coef"]

    # Remove empty rows
    gsm_df = gsm_df.dropna(subset=["TCH Actual HR%"])

    # Get "Offered Traffic BH" by mapping approximate "TCH Actual HR%" to 2G analysis_utility "erlangB" dict
    gsm_df["Offered Traffic BH"] = gsm_df["TCH Actual HR%"].apply(
        lambda x: GsmAnalysis.erlangB_table.get(int(x), 0)
    )

    # save_dataframe(gsm_df, "GSM")
    return gsm_df


def analyze_gsm_data(
    dump_path: str,
    daily_report_path: str,
    bh_report_path: str,
    number_of_kpi_days: int,
    number_of_threshold_days: int,
    availability_threshold: int,
    tch_abis_fails_threshold: int,
    sddch_blocking_threshold: float,
    tch_blocking_threshold: float,
):
    # print("Analyzing data...")
    # print(f"Number of days: {number_of_kpi_days}")
    # print(f"availability_threshold: {availability_threshold}")

    analyse_daily_data(
        daily_report_path=daily_report_path,
        number_of_kpi_days=number_of_kpi_days,
        availability_threshold=availability_threshold,
        tch_abis_fails_threshold=tch_abis_fails_threshold,
        number_of_threshold_days=number_of_threshold_days,
    )

    gsm_database_df: pd.DataFrame = get_gsm_databases(dump_path)

    bh_kpi_dfs = analyse_bh_data(
        bh_report_path=bh_report_path,
        number_of_kpi_days=number_of_kpi_days,
        tch_blocking_threshold=tch_blocking_threshold,
        sdcch_blocking_threshold=sddch_blocking_threshold,
        number_of_threshold_days=number_of_threshold_days,
    )

    bh_kpi_df = bh_kpi_dfs[0]
    bh_kpi_full_df = bh_kpi_dfs[1]

    gsm_analysis_df = gsm_database_df.merge(bh_kpi_df, on="name", how="left")

    # "TCH UTILIZATION (@Max Traffic)" equal to "(Max_Trafic" divided by "Offered Traffic BH)*100"
    gsm_analysis_df["TCH UTILIZATION (@Max Traffic)"] = (
        gsm_analysis_df["Max_Traffic BH"] / gsm_analysis_df["Offered Traffic BH"]
    ) * 100

    # Add "ERLANGB value" =MAX TRAFFIC/(1-(MAX TCH call blocking/200))
    gsm_analysis_df["ErlabngB_value"] = gsm_analysis_df["Max_Traffic BH"] / (
        1 - (gsm_analysis_df["Max_tch_call_blocking BH"] / 200)
    )

    # - Get "Target FR CHs" by mapping "ERLANG value" to 2G analysis_utility "erlangB" dict
    gsm_analysis_df["Target FR CHs"] = gsm_analysis_df["ErlabngB_value"].apply(
        lambda x: GsmAnalysis.erlangB_table.get(int(x) if pd.notnull(x) else 0, 0)
    )

    # "Target HR CHs" equal to  "Target FR CHs" * 2
    gsm_analysis_df["Target HR CHs"] = gsm_analysis_df["Target FR CHs"] * 2

    # - Target TCHs equal to Target HR CHs + Signal + GPRS + SDCCH
    gsm_analysis_df["Target TCHs"] = (
        gsm_analysis_df["Target HR CHs"]
        + gsm_analysis_df["number_signals_per_cell"]
        + gsm_analysis_df["GPRS"]
        + gsm_analysis_df["number_sd_per_cell"]
    )
    # "Target TRXs" equal to roundup(Target TCHs/8)
    gsm_analysis_df["Target TRXs"] = np.ceil(
        gsm_analysis_df["Target TCHs"] / 8
    )  # df["Target TCHs"] / 8

    # "Numberof required TRXs" equal to difference between "Target TRXs" and "number_trx_per_cell"
    gsm_analysis_df["Numberof required TRXs"] = (
        gsm_analysis_df["Target TRXs"] - gsm_analysis_df["number_trx_per_cell"]
    )

    return [gsm_analysis_df, bh_kpi_full_df]