Skip to content

Commit

Permalink
Fixed get_usage endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ManjotSingh18 committed May 31, 2024
1 parent e76636e commit 4d97675
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
65 changes: 63 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,75 @@
import assets.responses as responses, assets.tsdb as tsdb
from flask import Flask, jsonify, make_response
# Test Comment for Push
from flask import Flask, jsonify, make_response, request
from datetime import datetime
import time
from flask_cors import CORS


app = Flask(__name__)
CORS(app)

FORMAT_DATA = "%Y-%m-%d %H:%M:%S%z"
@app.route("/bin/<int:bin_id>", methods=["GET"])
def get_bin(bin_id:int):

conn = tsdb.connect()
query = f"SELECT * from ts_bins_info NATURAL JOIN ts_sensor_data WHERE bin_id = {bin_id};"
bins = [dict(row) for row in tsdb.execute_query(conn, query)]
response = responses.get_bin_response(bins, bin_id)
conn.close()


return jsonify(response)

@app.route("/get_location/<int:bin_id>", methods=["GET"])
def get_location(bin_id:int):
conn = tsdb.connect()
query = f"SELECT latitude, longitude from ts_bins_info WHERE bin_id = {bin_id};"
bins = [dict(row) for row in tsdb.execute_query(conn, query)]
response = responses.get_bin_response(bins, bin_id)
conn.close()



return jsonify(response)

@app.route("/get_bins", methods=["GET"])
def get_bins():
conn = tsdb.connect()
query = f"SELECT * from ts_bins_info;"
bins = [dict(row) for row in tsdb.execute_query(conn, query)]
response = responses.get_bins_response(bins)
conn.close()




return jsonify(response)

@app.route("/get_usage/<int:bin_id>", methods=["GET"])
def get_usage(bin_id:int):
start = request.args.get('start')
end = request.args.get('end')
valid_date = True
conn = tsdb.connect()
query = f"SELECT usage, timestamp FROM ts_sensor_data WHERE bin_id = {bin_id}"
if start != None:
start_timestamp = datetime.strptime(start, FORMAT_DATA)
query += f" AND timestamp >= '{start_timestamp}'"
if end != None:
end_timestamp = datetime.strptime(end, FORMAT_DATA)
query += f" AND timestamp < '{end_timestamp}'"
query+=';'
if (start!=None and end !=None) and start_timestamp > end_timestamp:
valid_date = False
bins = [dict(row) for row in tsdb.execute_query(conn, query)]
response = responses.get_usage_response(bins, bin_id, valid_date)
if len(response )== 0:
single_query = f"SELECT * from ts_bins_info NATURAL JOIN ts_sensor_data WHERE bin_id = {bin_id};"
single_bin = [dict(row) for row in tsdb.execute_query(conn, single_query)]
response = responses.get_bin_response(single_bin, bin_id)

conn.close()
return jsonify(response)

@app.route("/")
Expand All @@ -25,3 +85,4 @@ def hello():
@app.errorhandler(404)
def resource_not_found(e):
return make_response(jsonify(error='Not found!'), 404)

54 changes: 53 additions & 1 deletion assets/responses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

def get_bin_response(bins, bin_id):

if len(bins) == 0:
return {
'statusCode': 404,
Expand All @@ -19,4 +20,55 @@ def get_bin_response(bins, bin_id):
"Content-Type": "application/json"
}
}


#Get location shares this handler to avoid redundancy.
def get_bins_response(bins):
if len(bins) == 0:
return {
'statusCode': 404,
'body': json.dumps({
'detail': f'Bins not found'
}),
'headers': {
"Content-Type": "application/json"
}
}
else:
return {
'statusCode': 200,
'body': json.dumps(bins, default=str),
'headers': {
"Content-Type": "application/json"
}
}

def get_usage_response(bins, bin_id, valid_date):
if valid_date == False:
return {
'statusCode': 404,
'body': json.dumps({
'detail': f'Invalid Dates'
}),
'headers': {
"Content-Type": "application/json"
}
}
if len(bins) == 0:
return {
'statusCode': 404,
'body': json.dumps({
'detail': f'No data found for bin: {bin_id}'
}),
'headers': {
"Content-Type": "application/json"
}
}

else:
return {
'statusCode': 200,
'body': json.dumps(bins, default=str),
'headers': {
"Content-Type": "application/json"
}
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Jinja2==3.1.3
MarkupSafe==2.1.5
Werkzeug==3.0.2
zipp==3.18.1
Flask-Cors==4.0.1

0 comments on commit 4d97675

Please sign in to comment.