def get_median_driver_rating(): if 'driver_id' in request.json: try: assert type(request.json['driver_id']) is int except AssertionError: abort(400) result = {} result['drive_id'] = request.json['driver_id'] if all (i in request.json for i in ('start_time', 'end_time')): try: assert type(request.json['start_time']) is int assert type(request.json['end_time']) is int except AssertionError: abort(400) records = ridescoll.find({"$and": [{'driver_id': request.json['driver_id']}, {'start_time': {"$gt": request.json['start_time']}}, {'end_time': {"$lt": request.json['end_time']}} ] }).sort('rating', 1) else: records = ridescoll.find({'driver_id': request.json['driver_id']}).sort('rating', 1) if records.count() == 0: result['median_rating'] = -1 elif records.count() % 2 == 0: result['median_rating'] = (records[records.count()/2]['rating'] +records[(records.count()-1)/2]['rating'])/2 else: result['median_rating'] = records[records.count()/2]['rating'] else: abort(400) return jsonify(result), 200
def get_average_city_fare(): ''' Per city fare, city defined by a square. Data keys are defined by (lat, lng) tuples, not on boundary. It's easier to specify two coordinates than distance on a globe, I think. Time is represented as seconds since unix epoch. ''' result = {} sum = 0 if request.json and all (i in request.json for i in ('lat1', 'lng1', 'lat2', 'lng2')): try: assert (type(request.json['lat1']) is float or type(request.json['lat1'] is int)) and request.json['lat1'] > 0 assert (type(request.json['lat2']) is float or type(request.json['lat2'] is int)) and request.json['lat2'] > 0 assert (type(request.json['lng1']) is float or type(request.json['lng1'] is int)) and request.json['lng1'] > 0 assert (type(request.json['lng2']) is float or type(request.json['lng2'] is int)) and request.json['lng2'] > 0 except AssertionError: abort(400) if all (j in request.json for j in ('start_time', 'end_time')): try: assert type(request.json['start_time']) is int and request.json['start_time'] > 0 assert type(request.json['end_time']) is int and request.json['end_time'] > 0 except AssertionError: abort(400) records = ridescoll.find({"$and": [{'lat': {"$gt": request.json['lat1']}}, {'lat': {"$lt": request.json['lat2']}}, {'lng': {"$gt": request.json['lng1']}}, {'lng': {"$lt": request.json['lng2']}}, {'start_time': {"$gt": request.json['start_time']}}, {'start_time': {"$lt": request.json['end_time']}} ]}) result['start_time'] = request.json['start_time'] result['end_time'] = request.json['end_time'] else: records = ridescoll.find({"$and": [{'lat': {"$gt": request.json['lat1']}}, {'lat': {"$lt": request.json['lat2']}}, {'lng': {"$gt": request.json['lng1']}}, {'lng': {"$lt": request.json['lng2']}} ]}) for record in records: sum += record['fare'] else: abort(400) result['lat1'] = request.json['lat1'] result['lat2'] = request.json['lat2'] result['lng1'] = request.json['lng1'] result['lng2'] = request.json['lng2'] result['average_fare'] = sum return jsonify(result), 200
def get_total_trips(): ''' Total trips recorded. Time is represented as seconds since unix epoch. ''' result = {} trips = 0 if request.json and ('start_time' and 'end_time' in request.json): records = ridescoll.find({"$and": [{'start_time': {"$gt": request.json['start_time']}}, {'start_time': {"$lt": request.json['end_time']}} ]}) trips = records.count() else: trips = ridescoll.count() result['total_trips'] = trips return jsonify(result), 200
def get_total_clients_with_trips(): ''' Total number of clients who have taken trips. A client in the \'ridescollection\' indicates a trip taken. Time is represented as seconds since unix epoch. ''' result = {} count = 0 if request.json and all (i in request.json for i in ('start_time', 'end_time')): records = ridescoll.find({"$and": [{'start_time': {"$gt": request.json['start_time']}}, {'start_time': {"$lt": request.json['end_time']}} ]}) count = len(records.distinct('client_id')) elif request.json is None: count = len(ridescoll.distinct('client_id')) else: abort(400) result['client_count'] = count return jsonify(result), 200
def get_total_miles_per_client(): '''Time is represented as seconds since unix epoch. ''' result = {} coll = clienttrip.find() if request.json and all (i in request.json for i in ('start_time', 'end_time')): for i in coll: total = 0 records = ridescoll.find({"$and": [{'client_id': i['client_id']}, {'start_time': {"$gt": request.json['start_time']}}, {'start_time': {"$lt": request.json['end_time']}} ]}) for record in records: total += record['distance'] result[i['client_id']] = total elif request.json is None: for i in coll: result[i['client_id']] = i['distance'] else: abort(400) return jsonify(result), 200