Arafath10 commited on
Commit
1817e05
·
verified ·
1 Parent(s): 164af34

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +44 -1
main.py CHANGED
@@ -18,7 +18,7 @@ app.add_middleware(
18
 
19
  # Declare the continuous function as an async function.
20
  #async def your_continuous_function():
21
- def your_continuous_function(X_Tenant):
22
  import pandas as pd
23
  while True:
24
  print("data fetcher running.....")
@@ -77,6 +77,49 @@ def your_continuous_function(X_Tenant):
77
  # # Start the continuous function as a background task.
78
  # asyncio.create_task(your_continuous_function())
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  @app.get("/kpi_results")
81
  def read_root(X_Tenant):
82
  data = your_continuous_function(X_Tenant)
 
18
 
19
  # Declare the continuous function as an async function.
20
  #async def your_continuous_function():
21
+ def your_continuous_function_old(X_Tenant):
22
  import pandas as pd
23
  while True:
24
  print("data fetcher running.....")
 
77
  # # Start the continuous function as a background task.
78
  # asyncio.create_task(your_continuous_function())
79
 
80
+ async def fetch_data(session, url, headers):
81
+ async with session.get(url, headers=headers) as response:
82
+ return await response.json()
83
+
84
+
85
+ async def your_continuous_function(X_Tenant):
86
+ combined_df = pd.DataFrame()
87
+ base_url = "https://dev3.api.curfox.parallaxtec.com/api/ml/order-list"
88
+ headers = {
89
+ 'Accept': 'application/json',
90
+ 'X-Tenant': X_Tenant,
91
+ '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'
92
+ }
93
+
94
+ async with aiohttp.ClientSession() as session:
95
+ # Fetch the order count to determine the number of pages
96
+ order_metadata_url = "https://dev3.api.curfox.parallaxtec.com/api/ml/order-metadata"
97
+ metadata_response = await fetch_data(session, order_metadata_url, headers)
98
+ order_count = metadata_response["data"]["order_count"] // 200 + 2
99
+
100
+ tasks = []
101
+ for page in range(1, 30):
102
+ page_url = f"{base_url}?sort=id&paginate=200&page={page}"
103
+ tasks.append(fetch_data(session, page_url, headers))
104
+
105
+ responses = await asyncio.gather(*tasks)
106
+
107
+ for json_response in responses:
108
+ data = json_response.get('data', [])
109
+ if data:
110
+ df = pd.json_normalize(data)
111
+ combined_df = pd.concat([combined_df, df], ignore_index=True)
112
+
113
+ # After processing, you can return the DataFrame as a response or save it as needed.
114
+ # Here, for example, we're just returning the number of rows fetched.
115
+
116
+ print("data collected....")
117
+ data = combined_df[combined_df['status.name'].isin(['RETURN TO CLIENT', 'DELIVERED'])]
118
+ data = data[['delivery_possibility','status.name']]
119
+ data = data[data['delivery_possibility'].between(0, 100)]
120
+ return data
121
+
122
+
123
  @app.get("/kpi_results")
124
  def read_root(X_Tenant):
125
  data = your_continuous_function(X_Tenant)