update
This commit is contained in:
74
backend/routes/transport_routes.py
Normal file
74
backend/routes/transport_routes.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from flask import Blueprint, jsonify
|
||||
from database import db
|
||||
import redis
|
||||
import os
|
||||
import json
|
||||
|
||||
transport_bp = Blueprint('transport', __name__)
|
||||
|
||||
REDIS_URL = os.getenv('REDIS_URL', 'redis://redis:6379')
|
||||
|
||||
|
||||
def get_redis_client():
|
||||
"""Get Redis client"""
|
||||
return redis.from_url(REDIS_URL, decode_responses=True)
|
||||
|
||||
|
||||
@transport_bp.route('/api/transport/crawl', methods=['POST'])
|
||||
def trigger_transport_crawl():
|
||||
"""Trigger transport disruption crawl asynchronously via Redis queue"""
|
||||
try:
|
||||
r = get_redis_client()
|
||||
|
||||
# Publish message to Redis queue
|
||||
message = {
|
||||
'task': 'crawl_transport',
|
||||
'timestamp': str(os.times())
|
||||
}
|
||||
r.lpush('transport_crawl_queue', json.dumps(message))
|
||||
|
||||
# Return immediately without waiting
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
'message': 'Transport crawl task queued'
|
||||
}), 202 # 202 Accepted
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'status': 'error',
|
||||
'message': 'Failed to queue transport crawl',
|
||||
'details': str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
@transport_bp.route('/api/transport/disruptions', methods=['GET'])
|
||||
def get_transport_disruptions():
|
||||
"""Get current transport disruptions from MongoDB"""
|
||||
try:
|
||||
collection = db['transport_alerts']
|
||||
|
||||
# Get active disruptions
|
||||
disruptions = list(collection.find(
|
||||
{'is_active': True},
|
||||
{'_id': 0}
|
||||
).sort('updated_at', -1))
|
||||
|
||||
# Convert datetime to ISO format
|
||||
for d in disruptions:
|
||||
if d.get('start_time'):
|
||||
d['start_time'] = d['start_time'].isoformat()
|
||||
if d.get('end_time'):
|
||||
d['end_time'] = d['end_time'].isoformat()
|
||||
if d.get('updated_at'):
|
||||
d['updated_at'] = d['updated_at'].isoformat()
|
||||
|
||||
return jsonify({
|
||||
'total': len(disruptions),
|
||||
'disruptions': disruptions
|
||||
}), 200
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'error': 'Failed to fetch disruptions from database',
|
||||
'details': str(e)
|
||||
}), 500
|
||||
Reference in New Issue
Block a user