habulaj commited on
Commit
da630b4
·
verified ·
1 Parent(s): 664c52f

Update routes/stylist.py

Browse files
Files changed (1) hide show
  1. routes/stylist.py +25 -15
routes/stylist.py CHANGED
@@ -62,20 +62,29 @@ def get_total_subscribers(user_id: str) -> int:
62
  return len(response.json())
63
  return 0
64
 
 
 
 
 
 
 
 
 
 
 
 
65
  def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
66
  ny_timezone = pytz.timezone('America/New_York')
67
  now_ny = datetime.now(ny_timezone)
68
  current_month = now_ny.month
69
  current_year = now_ny.year
70
- monthly_data = {}
71
  for i in range(6):
72
  target_date = now_ny - relativedelta(months=i)
73
  month_num = target_date.month
74
  month_name = target_date.strftime('%b')
75
  year = target_date.year
76
- month_key = f"{year}-{month_num}"
77
- is_current = (month_num == current_month and year == current_year)
78
- monthly_data[month_key] = {"month": month_num, "name": month_name, "current": is_current, "amount": 0}
79
  start_date = now_ny - relativedelta(months=6)
80
  start_timestamp = int(start_date.timestamp())
81
  try:
@@ -83,16 +92,18 @@ def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
83
  for transfer in transfers.data:
84
  transfer_date = datetime.fromtimestamp(transfer.created, ny_timezone)
85
  month_num = transfer_date.month
86
- year = transfer_date.year
87
- month_key = f"{year}-{month_num}"
88
- if month_key in monthly_data:
89
- monthly_data[month_key]["amount"] += transfer.amount
90
- result = list(monthly_data.values())
91
- result.sort(key=lambda x: (current_month - x["month"]) % 12)
92
- return result
 
 
93
  except Exception as e:
94
  logger.error(f"❌ Error getting monthly revenue: {str(e)}")
95
- return list(monthly_data.values())
96
 
97
  def get_account_balance(account_id: str) -> Dict[str, Any]:
98
  try:
@@ -136,6 +147,5 @@ def get_dashboard(user_token: str = Header(None, alias="User-key")):
136
  except HTTPException as http_err:
137
  raise http_err
138
  except Exception as e:
139
- error_message = str(e) if str(e) else "An unknown error occurred"
140
- logger.error(f"❌ Error: {error_message}")
141
- raise HTTPException(status_code=500, detail=error_message)
 
62
  return len(response.json())
63
  return 0
64
 
65
+ def calculate_growth(current: int, previous: int) -> Dict[str, Any]:
66
+ if previous == 0:
67
+ growth = 100 if current > 0 else 0
68
+ else:
69
+ growth = ((current - previous) / previous) * 100
70
+ return {
71
+ "trend": "up" if growth > 0 else "down" if growth < 0 else "neutral",
72
+ "percentage": round(growth, 1),
73
+ "formatted": f"{round(growth, 1)}%"
74
+ }
75
+
76
  def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
77
  ny_timezone = pytz.timezone('America/New_York')
78
  now_ny = datetime.now(ny_timezone)
79
  current_month = now_ny.month
80
  current_year = now_ny.year
81
+ monthly_data = []
82
  for i in range(6):
83
  target_date = now_ny - relativedelta(months=i)
84
  month_num = target_date.month
85
  month_name = target_date.strftime('%b')
86
  year = target_date.year
87
+ monthly_data.append({"month": month_num, "name": month_name, "current": (month_num == current_month and year == current_year), "amount": 0})
 
 
88
  start_date = now_ny - relativedelta(months=6)
89
  start_timestamp = int(start_date.timestamp())
90
  try:
 
92
  for transfer in transfers.data:
93
  transfer_date = datetime.fromtimestamp(transfer.created, ny_timezone)
94
  month_num = transfer_date.month
95
+ for month in monthly_data:
96
+ if month["month"] == month_num:
97
+ month["amount"] += transfer.amount
98
+ break
99
+ for i in range(1, len(monthly_data)):
100
+ growth_data = calculate_growth(monthly_data[i-1]["amount"], monthly_data[i]["amount"])
101
+ monthly_data[i-1]["growth"] = growth_data
102
+ monthly_data[-1]["growth"] = {"trend": "neutral", "percentage": 0, "formatted": "0%"}
103
+ return monthly_data
104
  except Exception as e:
105
  logger.error(f"❌ Error getting monthly revenue: {str(e)}")
106
+ return monthly_data
107
 
108
  def get_account_balance(account_id: str) -> Dict[str, Any]:
109
  try:
 
147
  except HTTPException as http_err:
148
  raise http_err
149
  except Exception as e:
150
+ logger.error(f"❌ Error: {str(e)}")
151
+ raise HTTPException(status_code=500, detail=str(e))