habulaj commited on
Commit
c45a0bc
·
verified ·
1 Parent(s): 903beee

Update routes/stylist.py

Browse files
Files changed (1) hide show
  1. routes/stylist.py +42 -8
routes/stylist.py CHANGED
@@ -119,8 +119,24 @@ def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
119
  current_amount = result[i]["amount"]
120
  previous_amount = result[i-1]["amount"]
121
 
122
- if previous_amount > 0:
123
- # Calcular a porcentagem de crescimento e arredondar para 1 casa decimal
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  growth = ((current_amount - previous_amount) / previous_amount) * 100
125
  direction = "up" if growth >= 0 else "down"
126
  result[i]["growth_percentage"] = {
@@ -193,10 +209,19 @@ def get_total_followers(user_id: str, user_token: str) -> int:
193
  )
194
 
195
  if response.status_code != 200:
196
- logger.error(f"❌ Error getting followers count: {response.text}")
197
  return 0
198
-
199
- # Extrair contagem do cabeçalho de resposta
 
 
 
 
 
 
 
 
 
200
  count = int(response.headers.get("Content-Range", "0-0/0").split("/")[-1])
201
  return count
202
 
@@ -223,10 +248,19 @@ def get_total_subscribers(user_id: str, user_token: str) -> int:
223
  )
224
 
225
  if response.status_code != 200:
226
- logger.error(f"❌ Error getting subscribers count: {response.text}")
227
  return 0
228
-
229
- # Extrair contagem do cabeçalho de resposta
 
 
 
 
 
 
 
 
 
230
  count = int(response.headers.get("Content-Range", "0-0/0").split("/")[-1])
231
  return count
232
 
 
119
  current_amount = result[i]["amount"]
120
  previous_amount = result[i-1]["amount"]
121
 
122
+ # Lógica corrigida para calcular o crescimento
123
+ if previous_amount == 0 and current_amount > 0:
124
+ # Se o mês anterior foi zero e o atual tem valor, é crescimento de 100%
125
+ result[i]["growth_percentage"] = {
126
+ "direction": "up",
127
+ "value": 100.0
128
+ }
129
+ elif previous_amount == 0 and current_amount == 0:
130
+ # Se ambos são zero, não há mudança
131
+ result[i]["growth_percentage"] = None
132
+ elif previous_amount > 0 and current_amount == 0:
133
+ # Se o anterior tinha valor e o atual é zero, é queda de 100%
134
+ result[i]["growth_percentage"] = {
135
+ "direction": "down",
136
+ "value": 100.0
137
+ }
138
+ elif previous_amount > 0:
139
+ # Cálculo normal quando ambos têm valores
140
  growth = ((current_amount - previous_amount) / previous_amount) * 100
141
  direction = "up" if growth >= 0 else "down"
142
  result[i]["growth_percentage"] = {
 
209
  )
210
 
211
  if response.status_code != 200:
212
+ logger.error(f"❌ Error getting followers count: Status code {response.status_code}")
213
  return 0
214
+
215
+ # Tentar obter o formato de resposta do corpo - formato diferente do esperado
216
+ try:
217
+ # Se a resposta for um array com um objeto que tem o campo count
218
+ response_data = response.json()
219
+ if isinstance(response_data, list) and len(response_data) > 0 and "count" in response_data[0]:
220
+ return int(response_data[0]["count"])
221
+ except Exception as e:
222
+ logger.error(f"❌ Error parsing followers count: {str(e)}")
223
+
224
+ # Tentar o método anterior como fallback
225
  count = int(response.headers.get("Content-Range", "0-0/0").split("/")[-1])
226
  return count
227
 
 
248
  )
249
 
250
  if response.status_code != 200:
251
+ logger.error(f"❌ Error getting subscribers count: Status code {response.status_code}")
252
  return 0
253
+
254
+ # Tentar obter o formato de resposta do corpo - formato diferente do esperado
255
+ try:
256
+ # Se a resposta for um array com um objeto que tem o campo count
257
+ response_data = response.json()
258
+ if isinstance(response_data, list) and len(response_data) > 0 and "count" in response_data[0]:
259
+ return int(response_data[0]["count"])
260
+ except Exception as e:
261
+ logger.error(f"❌ Error parsing subscribers count: {str(e)}")
262
+
263
+ # Tentar o método anterior como fallback
264
  count = int(response.headers.get("Content-Range", "0-0/0").split("/")[-1])
265
  return count
266