Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -78,52 +78,46 @@ def your_continuous_function_old(X_Tenant):
|
|
78 |
# # Start the continuous function as a background task.
|
79 |
# asyncio.create_task(your_continuous_function())
|
80 |
|
81 |
-
async def
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
async def your_continuous_function(X_Tenant):
|
87 |
-
combined_df = pd.DataFrame()
|
88 |
-
base_url = "https://dev3.api.curfox.parallaxtec.com/api/ml/order-list"
|
89 |
-
headers = {
|
90 |
-
'Accept': 'application/json',
|
91 |
-
'X-Tenant': X_Tenant,
|
92 |
-
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiZWQzYjVkN2JkNTU5YmQxNWNmYzdiNThhM2UyZDlmNGEyMGQzMDFjMWY4ZWVlNDY2ZDBlZTAxYmMzZmVjMTU1ZWNjNzMxOWUxMGUxZGY3NDMiLCJpYXQiOjE3MDIyNzIyMDcuNjg0OTE2LCJuYmYiOjE3MDIyNzIyMDcuNjg0OTIzLCJleHAiOjE3MzM4OTQ2MDcuNjczNDYyLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.NFZvGO0GjoD7u3FRiIewRRoWu7ouUmKTKnCei8LMwQWzLntBLYcj_Bs21amjcHtzdbQNyCovHSDHJQaLJnD04kY1JRAdDC_OLi2YiZoSvnSJxNjWiuC4kwNE59Ndwu3o2iAzB-nd1EvyMnU_na7WxICRP8OegrpM-_q6M-wgnv7igaNeWjdxnXdtxbr-Zz7N2Xv2skWZwoDce37kWvH1tK7eqMK0uWqqyhBpli22CmkKPduHUNKMNOEnGTskeDaTuX5za2Lr8CNa34_FdKu3Y5CrFMGDBHT_UGALocpr80_38iifXm7WDl6ZIA1iYy6dBvCTeoC_aFo1X5FIrFbJgMCokW4VH0Q2ljm9ty0W7ATAiKrM1GIVFS5Dir4A1KI3LSeE459SqZpqsoJmaU95zSYbfnU_oZ9UpvW59nFgD6yJ8hGHyYnjhCS0jmxk3cq93T9X1rNWo2t0A3XYXgqZYnZrZpdrSbn-JVoX_NW1QC6RtmAGm7AtZ3GBrzxwu3m_7MicMI7Tu4W6d2WD9kZjq0khBUrm2DVZJzN2BRmH-a7JkAqJ0icpHQ_2Tc6T-95axebp6QEmHHXBKILNNwWxucZ0l-Ny0TuUivqn0m9gSJJDkA8ssWyBkzzJ9fUeRmJGbUFTeemPhMrF3_cvTUZ0J7IC2CK7qWePcHPQ-sy0is4'
|
93 |
-
}
|
94 |
-
|
95 |
-
async with aiohttp.ClientSession() as session:
|
96 |
-
# Fetch the order count to determine the number of pages
|
97 |
-
order_metadata_url = "https://dev3.api.curfox.parallaxtec.com/api/ml/order-metadata"
|
98 |
-
metadata_response = await fetch_data(session, order_metadata_url, headers)
|
99 |
-
order_count = metadata_response["data"]["order_count"] // 200 + 2
|
100 |
-
|
101 |
-
tasks = []
|
102 |
-
for page in range(1, 30):
|
103 |
-
page_url = f"{base_url}?sort=id&paginate=200&page={page}"
|
104 |
-
tasks.append(fetch_data(session, page_url, headers))
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
if data:
|
111 |
-
df = pd.json_normalize(data)
|
112 |
-
combined_df = pd.concat([combined_df, df], ignore_index=True)
|
113 |
-
|
114 |
-
# After processing, you can return the DataFrame as a response or save it as needed.
|
115 |
-
# Here, for example, we're just returning the number of rows fetched.
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
122 |
|
123 |
|
124 |
@app.get("/kpi_results")
|
125 |
async def read_root(X_Tenant):
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
status_counts_more_than_80 = data[data['delivery_possibility'] > 80]['status.name'].value_counts()
|
128 |
|
129 |
status_counts_50_to_80 = data[(data['delivery_possibility'] >= 50) & (data['delivery_possibility'] <= 80)]['status.name'].value_counts()
|
|
|
78 |
# # Start the continuous function as a background task.
|
79 |
# asyncio.create_task(your_continuous_function())
|
80 |
|
81 |
+
async def fetch_page(session, page ,X_Tenant):
|
82 |
+
try:
|
83 |
+
url = f"https://dev3.api.curfox.parallaxtec.com/api/ml/order-list?sort=id&paginate=200&page={page}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
+
headers = {
|
86 |
+
'Accept': 'application/json',
|
87 |
+
'X-Tenant': X_Tenant,#'royalexpress',
|
88 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
async with session.get(url, headers=headers) as response:
|
91 |
+
json_response = await response.json()
|
92 |
+
data = json_response['data']
|
93 |
+
df = pd.json_normalize(data)
|
94 |
+
return df
|
95 |
+
except Exception as e:
|
96 |
+
print(f"Failed to fetch data for page {page}: {e}")
|
97 |
+
return pd.DataFrame() # Return an empty DataFrame in case of error
|
98 |
+
|
99 |
|
100 |
|
101 |
@app.get("/kpi_results")
|
102 |
async def read_root(X_Tenant):
|
103 |
+
combined_df = pd.DataFrame()
|
104 |
+
|
105 |
+
async with aiohttp.ClientSession() as session:
|
106 |
+
tasks = [fetch_page(session, page, X_Tenant) for page in range(1, 30)]
|
107 |
+
results = await asyncio.gather(*tasks)
|
108 |
+
|
109 |
+
# Combine all the DataFrames from each page
|
110 |
+
combined_df = pd.concat(results, ignore_index=True)
|
111 |
+
|
112 |
+
print("Data collected....")
|
113 |
+
|
114 |
+
# Filter the data
|
115 |
+
filtered_data = combined_df[combined_df['status.name'].isin(['RETURN TO CLIENT', 'DELIVERED'])]
|
116 |
+
filtered_data = filtered_data[['delivery_possibility', 'status.name']]
|
117 |
+
filtered_data = filtered_data[filtered_data['delivery_possibility'].between(0, 100)]
|
118 |
+
|
119 |
+
# existing code===========================
|
120 |
+
data = filtered_data
|
121 |
status_counts_more_than_80 = data[data['delivery_possibility'] > 80]['status.name'].value_counts()
|
122 |
|
123 |
status_counts_50_to_80 = data[(data['delivery_possibility'] >= 50) & (data['delivery_possibility'] <= 80)]['status.name'].value_counts()
|