File size: 6,109 Bytes
939b332
 
 
 
 
 
bcc0fd9
 
 
939b332
bcc0fd9
 
939b332
bcc0fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b290f2d
 
 
bcc0fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
939b332
bcc0fd9
 
 
 
 
 
 
 
 
 
 
 
 
939b332
bcc0fd9
 
 
 
 
 
939b332
 
bcc0fd9
 
 
939b332
bcc0fd9
 
 
 
 
 
 
 
 
939b332
 
 
 
 
 
 
 
 
 
50b11f5
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
import io
import time

import pandas as pd
import streamlit as st

# @st.cache_data
# def convert_dfs(dfs: list[pd.DataFrame], sheet_names: list[str]) -> bytes:
#     # IMPORTANT: Cache the conversion to prevent computation on every rerun

#     # Create a BytesIO object
#     bytes_io = io.BytesIO()

#     # Write the dataframes to the BytesIO object
#     with pd.ExcelWriter(bytes_io, engine="xlsxwriter") as writer:
#         for df, sheet_name in zip(dfs, sheet_names):
#             df.to_excel(writer, sheet_name=sheet_name, index=True)

#     # Get the bytes data
#     bytes_data = bytes_io.getvalue()

#     # Close the BytesIO object
#     bytes_io.close()

#     return bytes_data


def get_formats(workbook):
    return {
        "green": workbook.add_format(
            {"bg_color": "#37CC73", "bold": True, "border": 1}
        ),
        "blue": workbook.add_format({"bg_color": "#1A64FF", "bold": True, "border": 1}),
        "blue_light": workbook.add_format(
            {"bg_color": "#00B0F0", "bold": True, "border": 1}
        ),
        "beurre": workbook.add_format(
            {"bg_color": "#FFE699", "bold": True, "border": 1}
        ),
        "orange": workbook.add_format(
            {"bg_color": "#F47F31", "bold": True, "border": 1}
        ),
        "purple5": workbook.add_format(
            {"bg_color": "#E03DCD", "bold": True, "border": 1}
        ),
        "purple6": workbook.add_format(
            {"bg_color": "#AE83F8", "bold": True, "border": 1}
        ),
        "gray": workbook.add_format({"bg_color": "#D9D9D9", "bold": True, "border": 1}),
        "red": workbook.add_format({"bg_color": "#FF0000", "bold": True, "border": 1}),
    }


def get_format_map_by_format_type(formats: dict, format_type: str) -> dict:
    if format_type == "GSM_Analysis":
        return {
            # "name": formats["blue"],
            "amrSegLoadDepTchRateLower": formats["beurre"],
            "amrSegLoadDepTchRateUpper": formats["beurre"],
            "dedicatedGPRScapacity": formats["beurre"],
            "defaultGPRScapacity": formats["beurre"],
            "number_trx_per_cell": formats["blue_light"],
            "number_trx_per_bcf": formats["blue_light"],
            "number_tch_per_cell": formats["blue"],
            "number_sd_per_cell": formats["blue"],
            "number_bcch_per_cell": formats["blue"],
            "number_ccch_per_cell": formats["blue"],
            "number_cbc_per_cell": formats["blue"],
            "number_total_channels_per_cell": formats["blue"],
            "number_signals_per_cell": formats["blue"],
            "hf_rate_coef": formats["purple5"],
            "GPRS": formats["purple5"],
            "TCH Actual HR%": formats["green"],
            "Offered Traffic BH": formats["green"],
            "Max_Traffic BH": formats["green"],
            "Avg_Traffic BH": formats["green"],
            "Max_tch_call_blocking BH": formats["green"],
            "Avg_tch_call_blocking BH": formats["green"],
            "number_of_days_with_tch_blocking_exceeded": formats["green"],
            "Max_sdcch_real_blocking BH": formats["green"],
            "Avg_sdcch_real_blocking BH": formats["green"],
            "number_of_days_with_sdcch_blocking_exceeded": formats["green"],
            "TCH UTILIZATION (@Max Traffic)": formats["orange"],
            "Target FR CHs": formats["purple6"],
            "Target HR CHs": formats["purple6"],
            "Target TCHs": formats["purple6"],
            "Target TRXs": formats["purple6"],
            "Numberof required TRXs": formats["purple6"],
        }
    elif format_type == "database":
        return {
            "code": formats["blue"],
            "Azimut": formats["green"],
            "Longitude": formats["green"],
            "Latitude": formats["green"],
            "Hauteur": formats["green"],
            "number_trx_per_cell": formats["blue_light"],
            "number_trx_per_bcf": formats["blue_light"],
            "number_trx_per_site": formats["blue_light"],
        }
    # elif format_type == "LTE":
    #     return {
    #         "DL PRB Utilization": formats["orange"],
    #         "UL PRB Utilization": formats["orange"],
    #         "RSRP": formats["blue_light"],
    #         "RSRQ": formats["blue_light"],
    #         "Throughput (Mbps)": formats["green"],
    #     }
    else:
        return {}  # No formatting if format_type not matched


def _apply_custom_formatting(
    writer, df: pd.DataFrame, sheet_name: str, format_type: str
):
    workbook = writer.book
    worksheet = writer.sheets[sheet_name]

    formats = get_formats(workbook)
    format_map = get_format_map_by_format_type(formats, format_type)

    for col_idx, col_name in enumerate(df.columns):
        fmt = format_map.get(col_name)
        if fmt:
            worksheet.write(0, col_idx + 1, col_name, fmt)


def _write_to_excel(
    dfs: list[pd.DataFrame], sheet_names: list[str], index=True, format_type: str = None
) -> bytes:
    bytes_io = io.BytesIO()
    with pd.ExcelWriter(bytes_io, engine="xlsxwriter") as writer:
        for df, name in zip(dfs, sheet_names):
            # df.index.name = "index"
            df.to_excel(writer, sheet_name=name, index=index)
            if format_type:
                _apply_custom_formatting(writer, df, name, format_type)
    return bytes_io.getvalue()


@st.cache_data
def convert_dfs(dfs: list[pd.DataFrame], sheet_names: list[str]) -> bytes:
    return _write_to_excel(dfs, sheet_names, index=True)


@st.cache_data
def convert_gsm_dfs(dfs, sheet_names) -> bytes:
    return _write_to_excel(dfs, sheet_names, index=True, format_type="GSM_Analysis")


@st.cache_data
def convert_database_dfs(dfs, sheet_names) -> bytes:
    return _write_to_excel(dfs, sheet_names, index=True, format_type="database")


def save_dataframe(df: pd.DataFrame, sheet_name: str):
    """
    Save the dataframe to a csv file.

    Args:
        df (pd.DataFrame): The dataframe to save.
        sheet_name (str): The name of the sheet.
    """
    df.to_csv(f"data2/{sheet_name}_{time.time()}.csv", index=False, encoding="latin1")