Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -301,63 +301,65 @@ def get_spending_overview():
|
|
| 301 |
|
| 302 |
# Get transactions for the user.
|
| 303 |
transactions_ref = db.reference('transactions')
|
| 304 |
-
transactions = transactions_ref.order_by_child('uid').equal_to(uid).get()
|
| 305 |
-
|
| 306 |
-
if transactions is None:
|
| 307 |
-
return jsonify({
|
| 308 |
-
'daily_spending': [],
|
| 309 |
-
'recent_transactions': []
|
| 310 |
-
})
|
| 311 |
|
| 312 |
-
#
|
| 313 |
-
|
| 314 |
-
|
|
|
|
| 315 |
return jsonify({
|
| 316 |
'daily_spending': [],
|
| 317 |
'recent_transactions': []
|
| 318 |
})
|
| 319 |
|
| 320 |
-
#
|
| 321 |
-
df
|
| 322 |
|
| 323 |
-
#
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
|
|
|
| 326 |
if df.empty:
|
| 327 |
return jsonify({
|
| 328 |
'daily_spending': [],
|
| 329 |
'recent_transactions': []
|
| 330 |
})
|
| 331 |
|
| 332 |
-
#
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
'recent_transactions': []
|
| 339 |
-
})
|
| 340 |
|
| 341 |
-
#
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
-
#
|
| 345 |
-
# Extract the date part from the ISO string.
|
| 346 |
-
df['date_only'] = df['date'].apply(lambda d: d.split("T")[0])
|
| 347 |
-
daily_spending = df.groupby('date_only')['total'].sum().reset_index()
|
| 348 |
-
# Rename the column to "date" and ensure it's a string.
|
| 349 |
-
daily_spending.rename(columns={'date_only': 'date'}, inplace=True)
|
| 350 |
-
daily_spending['date'] = daily_spending['date'].astype(str)
|
| 351 |
daily_spending = daily_spending.replace({np.nan: None})
|
| 352 |
-
|
| 353 |
-
# Sort the transactions by timestamp (assuming it's in a sortable string format).
|
| 354 |
-
recent_transactions = df.sort_values(by='timestamp', ascending=False).head(10)
|
| 355 |
recent_transactions = recent_transactions.replace({np.nan: None})
|
| 356 |
|
| 357 |
-
|
|
|
|
| 358 |
'daily_spending': daily_spending.to_dict(orient='records'),
|
| 359 |
-
'recent_transactions': recent_transactions.drop(columns=['date_only']).to_dict(orient='records')
|
| 360 |
-
}
|
|
|
|
|
|
|
|
|
|
| 361 |
except Exception as e:
|
| 362 |
return jsonify({'error': str(e)}), 500
|
| 363 |
|
|
|
|
| 301 |
|
| 302 |
# Get transactions for the user.
|
| 303 |
transactions_ref = db.reference('transactions')
|
| 304 |
+
transactions = transactions_ref.order_by_child('uid').equal_to(uid).get() or {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
+
# Process transactions directly (similar to the admin overview approach)
|
| 307 |
+
transactions_list = [{'id': tid, **data} for tid, data in transactions.items()]
|
| 308 |
+
|
| 309 |
+
if not transactions_list:
|
| 310 |
return jsonify({
|
| 311 |
'daily_spending': [],
|
| 312 |
'recent_transactions': []
|
| 313 |
})
|
| 314 |
|
| 315 |
+
# Create a DataFrame
|
| 316 |
+
df = pd.DataFrame(transactions_list)
|
| 317 |
|
| 318 |
+
# Handle date formatting (only if the date column exists)
|
| 319 |
+
if 'date' in df.columns:
|
| 320 |
+
# Convert to datetime, but keep non-convertible values
|
| 321 |
+
df['date'] = pd.to_datetime(df['date'], errors='coerce')
|
| 322 |
+
# Filter out rows with invalid dates
|
| 323 |
+
df = df.dropna(subset=['date'])
|
| 324 |
+
|
| 325 |
+
if not df.empty:
|
| 326 |
+
# Convert datetime to ISO format
|
| 327 |
+
df['date'] = df['date'].apply(lambda d: d.isoformat())
|
| 328 |
+
# Extract date part for grouping
|
| 329 |
+
df['date_only'] = df['date'].apply(lambda d: d.split("T")[0])
|
| 330 |
|
| 331 |
+
# Return empty response if we don't have valid data after processing
|
| 332 |
if df.empty:
|
| 333 |
return jsonify({
|
| 334 |
'daily_spending': [],
|
| 335 |
'recent_transactions': []
|
| 336 |
})
|
| 337 |
|
| 338 |
+
# For daily spending, group by date
|
| 339 |
+
if 'date_only' in df.columns and 'total' in df.columns:
|
| 340 |
+
daily_spending = df.groupby('date_only')['total'].sum().reset_index()
|
| 341 |
+
daily_spending.rename(columns={'date_only': 'date'}, inplace=True)
|
| 342 |
+
else:
|
| 343 |
+
daily_spending = pd.DataFrame(columns=['date', 'total'])
|
|
|
|
|
|
|
| 344 |
|
| 345 |
+
# Sort the transactions by timestamp if available
|
| 346 |
+
if 'timestamp' in df.columns:
|
| 347 |
+
recent_transactions = df.sort_values(by='timestamp', ascending=False).head(10)
|
| 348 |
+
else:
|
| 349 |
+
recent_transactions = df.head(10)
|
| 350 |
|
| 351 |
+
# Replace NaN values with None for JSON serialization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
daily_spending = daily_spending.replace({np.nan: None})
|
|
|
|
|
|
|
|
|
|
| 353 |
recent_transactions = recent_transactions.replace({np.nan: None})
|
| 354 |
|
| 355 |
+
# Prepare response
|
| 356 |
+
response_data = {
|
| 357 |
'daily_spending': daily_spending.to_dict(orient='records'),
|
| 358 |
+
'recent_transactions': recent_transactions.drop(columns=['date_only'], errors='ignore').to_dict(orient='records')
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
return jsonify(response_data)
|
| 362 |
+
|
| 363 |
except Exception as e:
|
| 364 |
return jsonify({'error': str(e)}), 500
|
| 365 |
|