from flask import Flask, jsonify, request
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
app = Flask(__name__)
contact_points = ['172.16.177.58']
auth_provider = PlainTextAuthProvider('hobs_analytics', 'hobs_analytics')
cluster = Cluster(contact_points=contact_points, auth_provider=auth_provider)
session = cluster.connect()
@app.route('/details', methods=['GET'])
def get_details():
username = request.args.get('username')
event_date = request.args.get('event_date')
status = request.args.get('status')
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
if username is not None and event_date is not None and status is None and start_date is None and end_date is None:
rows = session.execute("SELECT * FROM hos_cart_details WHERE username=%s AND event_date=%s", (username, event_date))
elif username is not None and event_date is None and status is None and start_date is None and end_date is None:
rows = session.execute("SELECT * FROM hos_cart_details WHERE username=%s", (username,))
elif username is None and event_date is not None and status is None and start_date is None and end_date is None:
rows = session.execute("SELECT * FROM hos_cart_details_by_date WHERE event_date=%s", (event_date,))
elif username is None and event_date is None and status is not None and start_date is None and end_date is None:
rows = session.execute("SELECT * FROM hos_cart_details_by_status WHERE status=%s", (status,))
elif username is None and event_date is None and status is None and start_date is not None and end_date is not None:
rows = session.execute("SELECT * FROM hos_cart_details WHERE event_date>=%s AND event_date<=%s", (start_date, end_date))
else:
return jsonify({'error': 'Invalid request parameters'})
results = []
for row in rows:
results.append({
'username': row.username,
'event_date': str(row.event_date),
'valuetype': row.valuetype,
'key': row.key,
'buid': row.buid,
'businessid': row.businessid,
'createdtime': str(row.createdtime),
'opid': row.opid,
'status': row.status,
'types': row.types,
'updatedtime': str(row.updatedtime),
'value': row.value.hex() if row.value is not None else None
})
return jsonify(results)