File size: 8,041 Bytes
e2483e1
 
 
a035e35
 
e2483e1
 
73f4a97
 
e2483e1
 
 
 
73f4a97
 
 
 
 
 
 
 
 
 
e2483e1
 
73f4a97
a035e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2483e1
 
73f4a97
 
 
 
 
 
e2483e1
73f4a97
e2483e1
 
73f4a97
a035e35
 
 
 
 
 
 
 
 
 
 
 
 
e2483e1
 
 
 
 
 
 
 
 
 
 
 
 
 
73f4a97
e2483e1
 
73f4a97
a035e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2483e1
 
73f4a97
e2483e1
73f4a97
 
e2483e1
73f4a97
 
a035e35
73f4a97
 
 
 
 
 
 
 
 
e2483e1
 
73f4a97
a035e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2483e1
 
73f4a97
e2483e1
73f4a97
e2483e1
 
 
 
 
 
 
 
 
 
 
 
73f4a97
 
a035e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import gradio as gr
from typing import List
import plotly.express as px
from tabs.tool_win import sort_key


HEIGHT = 600
WIDTH = 1000


def get_error_data(tools_df: pd.DataFrame, inc_tools: List[str]) -> pd.DataFrame:
    """Gets the error data for the given tools and calculates the error percentage."""
    tools_inc = tools_df[tools_df["tool"].isin(inc_tools)]
    error = (
        tools_inc.groupby(["tool", "request_month_year_week", "error"])
        .size()
        .unstack()
        .fillna(0)
        .reset_index()
    )
    error["error_perc"] = (error[1] / (error[0] + error[1])) * 100
    error["total_requests"] = error[0] + error[1]
    return error


def get_error_data_by_market(
    tools_df: pd.DataFrame, inc_tools: List[str]
) -> pd.DataFrame:
    """Gets the error data for the given tools and calculates the error percentage."""
    tools_inc = tools_df[tools_df["tool"].isin(inc_tools)]
    error = (
        tools_inc.groupby(
            ["tool", "request_month_year_week", "market_creator", "error"], sort=False
        )
        .size()
        .unstack()
        .fillna(0)
        .reset_index()
    )
    error["error_perc"] = (error[1] / (error[0] + error[1])) * 100
    error["total_requests"] = error[0] + error[1]
    return error


def get_error_data_overall(error_df: pd.DataFrame) -> pd.DataFrame:
    """Gets the error data for the given tools and calculates the error percentage."""
    error_total = (
        error_df.groupby("request_month_year_week")
        .agg({"total_requests": "sum", 1: "sum", 0: "sum"})
        .reset_index()
    )
    error_total["error_perc"] = (error_total[1] / error_total["total_requests"]) * 100
    error_total.columns = error_total.columns.astype(str)
    error_total["error_perc"] = error_total["error_perc"].apply(lambda x: round(x, 4))
    return error_total


def get_error_data_overall_by_market(error_df: pd.DataFrame) -> pd.DataFrame:
    """Gets the error data for the given tools and calculates the error percentage."""
    error_total = (
        error_df.groupby(["request_month_year_week", "market_creator"], sort=False)
        .agg({"total_requests": "sum", 1: "sum", 0: "sum"})
        .reset_index()
    )
    error_total["error_perc"] = (error_total[1] / error_total["total_requests"]) * 100
    error_total.columns = error_total.columns.astype(str)
    error_total["error_perc"] = error_total["error_perc"].apply(lambda x: round(x, 4))
    return error_total


def plot_error_data(error_all_df: pd.DataFrame) -> gr.BarPlot:
    """Plots the error data for the given tools and calculates the error percentage."""
    return gr.BarPlot(
        value=error_all_df,
        x="request_month_year_week",
        y="error_perc",
        title="Error Percentage",
        x_title="Week",
        y_title="Error Percentage",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["request_month_year_week", "error_perc"],
        height=HEIGHT,
        width=WIDTH,
    )


def plot_error_data_by_market(error_all_df: pd.DataFrame) -> gr.Plot:

    # Sort the unique values of request_month_year_week
    sorted_categories = sorted(
        error_all_df["request_month_year_week"].unique(), key=sort_key
    )
    # Create a categorical type with a specific order
    error_all_df["request_month_year_week"] = pd.Categorical(
        error_all_df["request_month_year_week"],
        categories=sorted_categories,
        ordered=True,
    )

    # Sort the DataFrame based on the new categorical column
    error_all_df = error_all_df.sort_values("request_month_year_week")

    fig = px.bar(
        error_all_df,
        x="request_month_year_week",
        y="error_perc",
        color="market_creator",
        barmode="group",
        color_discrete_sequence=["purple", "goldenrod", "darkgreen"],
        category_orders={
            "market_creator": ["pearl", "quickstart", "all"],
            "request_month_year_week": sorted_categories,
        },
    )
    fig.update_layout(
        xaxis_title="Week",
        yaxis_title="Error Percentage",
        legend=dict(yanchor="top", y=0.5),
    )
    fig.update_layout(width=WIDTH, height=HEIGHT)
    fig.update_xaxes(tickformat="%b %d\n%Y")
    return gr.Plot(value=fig)


def plot_tool_error_data(error_df: pd.DataFrame, tool: str) -> gr.BarPlot:
    """Plots the error data for the given tool."""
    error_tool = error_df[error_df["tool"] == tool]
    error_tool.columns = error_tool.columns.astype(str)
    error_tool["error_perc"] = error_tool["error_perc"].apply(lambda x: round(x, 4))

    return gr.BarPlot(
        title="Error Percentage",
        x_title="Week",
        y_title="Error Percentage %",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["request_month_year_week", "error_perc"],
        value=error_tool,
        x="request_month_year_week",
        y="error_perc",
        height=HEIGHT,
        width=WIDTH,
    )


def plot_tool_error_data_by_market(error_df: pd.DataFrame, tool: str) -> gr.Plot:
    error_tool = error_df[error_df["tool"] == tool]
    error_tool.columns = error_tool.columns.astype(str)
    error_tool["error_perc"] = error_tool["error_perc"].apply(lambda x: round(x, 4))

    # Sort the unique values of request_month_year_week
    sorted_categories = sorted(
        error_tool["request_month_year_week"].unique(), key=sort_key
    )
    # Create a categorical type with a specific order
    error_tool["request_month_year_week"] = pd.Categorical(
        error_tool["request_month_year_week"],
        categories=sorted_categories,
        ordered=True,
    )

    # Sort the DataFrame based on the new categorical column
    error_tool = error_tool.sort_values("request_month_year_week")

    fig = px.bar(
        error_tool,
        x="request_month_year_week",
        y="error_perc",
        color="market_creator",
        barmode="group",
        color_discrete_sequence=["purple", "goldenrod", "darkgreen"],
        category_orders={
            "market_creator": ["pearl", "quickstart", "all"],
            "request_month_year_week": sorted_categories,
        },
    )
    fig.update_layout(
        xaxis_title="Week",
        yaxis_title="Error Percentage %",
        legend=dict(yanchor="top", y=0.5),
    )
    fig.update_layout(width=WIDTH, height=HEIGHT)
    fig.update_xaxes(tickformat="%b %d\n%Y")
    return gr.Plot(value=fig)


def plot_week_error_data(error_df: pd.DataFrame, week: str) -> gr.BarPlot:
    """Plots the error data for the given week."""
    error_week = error_df[error_df["request_month_year_week"] == week]
    error_week.columns = error_week.columns.astype(str)
    error_week["error_perc"] = error_week["error_perc"].apply(lambda x: round(x, 4))
    return gr.BarPlot(
        value=error_week,
        x="tool",
        y="error_perc",
        title="Error Percentage",
        x_title="Tool",
        y_title="Error Percentage",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["tool", "error_perc"],
        height=HEIGHT,
        width=WIDTH,
    )


def plot_week_error_data_by_market(error_df: pd.DataFrame, week: str) -> gr.Plot:
    error_week = error_df[error_df["request_month_year_week"] == week]
    error_week.columns = error_week.columns.astype(str)
    error_week["error_perc"] = error_week["error_perc"].apply(lambda x: round(x, 4))

    fig = px.bar(
        error_week,
        x="tool",
        y="error_perc",
        color="market_creator",
        barmode="group",
        color_discrete_sequence=["purple", "goldenrod", "darkgreen"],
        category_orders={
            "market_creator": ["pearl", "quickstart", "all"],
        },
    )
    fig.update_layout(
        xaxis_title="Tool",
        yaxis_title="Error Percentage %",
        legend=dict(yanchor="top", y=0.5),
    )
    fig.update_layout(width=WIDTH, height=HEIGHT)
    fig.update_xaxes(tickformat="%b %d\n%Y")
    return gr.Plot(value=fig)