Skip to content

Commit

Permalink
sorting sql query in postgres before returning results. Fixes 2 bugs:…
Browse files Browse the repository at this point in the history
… 1) table not sorted, 2) graph displays unsorted data, line plot connects data out of order
  • Loading branch information
c3pko committed Jan 21, 2023
1 parent ea9f64e commit cd7fef4
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions display_aqi_for_city_and_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,12 @@ async def query_db_with_user_inputs(sensor_city, start_date, end_date):

new_cursor = new_connection.cursor()
get_data_by_city_only = """SELECT * FROM aqi_history_table WHERE sensor_city = '""" + sensor_city + """' """
get_data_by_all_variables = """SELECT * FROM public.aqi_history_table WHERE sensor_city = '""" + sensor_city + """' """ + """ AND date >= '""" + start_date + """' """ + """ AND date <= '""" + end_date + """' """
get_data_by_all_variables = """SELECT * FROM public.aqi_history_table WHERE sensor_city = '""" + sensor_city + """' """ + """ AND date >= '""" + start_date + """' """ + """ AND date <= '""" + end_date + """' ORDER BY DATE"""
print("querying database with following query: \n", get_data_by_all_variables)
new_cursor.execute(get_data_by_all_variables)
new_connection.commit()

#in future add way to sort data by date in Python instead of relying on Postgres query to sort data for you
result = engine.execute(get_data_by_all_variables)
json_data = json.dumps([dict(r) for r in result], default=alchemyencoder)
print(" json data: \n", json_data)
Expand Down Expand Up @@ -704,7 +706,19 @@ async def add_test_data():
end_date = '2022-12-08'
query_existing_table(sensor_city, start_date, end_date)


async def testing():
"""things to test (that may or may not yet exist in code):
1. Data loaded from CSVs is being accurately loaded
2. Data from CSVs is being cleaned properly--handling NAs/mismatching format types
3. User inputs are being handled properly when there are errors. Examples:
a. user doesn't make any selection
b. user selects dates out of range of data
4. User inputs are being sent accurately from frontend to backend server
5. User inputs are being used correctly (selecting correct city and date range)
6. User query is being sorted by date correctly
7. Table sorting works as expected
"""

async def main():

Expand Down

0 comments on commit cd7fef4

Please sign in to comment.