diff --git a/app/src/tiles/dataDefinition.ts b/app/src/tiles/dataDefinition.ts index 49c8e4ea..ec2b0e5f 100644 --- a/app/src/tiles/dataDefinition.ts +++ b/app/src/tiles/dataDefinition.ts @@ -143,15 +143,15 @@ const LAYER_QUERIES = { INNER JOIN buildings ON building_properties.building_id = buildings.building_id`, planning_applications_status_recent: `SELECT buildings.geometry_id, building_properties.uprn, building_properties.building_id, planning_data.status AS status, planning_data.uprn, - EXTRACT(epoch FROM age(decision_date))/3600/24 AS days_since_decision_date, - EXTRACT(epoch FROM age(registered_with_local_authority_date))/3600/24 AS days_since_registered_with_local_authority_date + 1 AS days_since_decision_date, + 1 AS days_since_registered_with_local_authority_date FROM building_properties INNER JOIN planning_data ON building_properties.uprn = planning_data.uprn INNER JOIN buildings ON building_properties.building_id = buildings.building_id`, planning_applications_status_very_recent: `SELECT buildings.geometry_id, building_properties.uprn, building_properties.building_id, planning_data.status AS status, planning_data.uprn, - EXTRACT(epoch FROM age(decision_date))/3600/24 AS days_since_decision_date, - EXTRACT(epoch FROM age(registered_with_local_authority_date))/3600/24 AS days_since_registered_with_local_authority_date + planning_data.days_since_decision_date_cached AS days_since_decision_date, + planning_data.days_since_registration_cached AS days_since_registered_with_local_authority_date FROM building_properties INNER JOIN planning_data ON building_properties.uprn = planning_data.uprn INNER JOIN buildings ON building_properties.building_id = buildings.building_id`, diff --git a/etl/planning_data/obtain_livestream_data_and_load_into_database.py b/etl/planning_data/obtain_livestream_data_and_load_into_database.py index 26cef4f5..3068f4f7 100644 --- a/etl/planning_data/obtain_livestream_data_and_load_into_database.py +++ b/etl/planning_data/obtain_livestream_data_and_load_into_database.py @@ -136,18 +136,21 @@ def filepath(): return os.path.dirname(os.path.realpath(__file__)) + os.sep + "data.json" def insert_entry(cursor, e): + now = datetime.datetime.now() application_url = None if e["application_url"] != None: application_url = e["application_url"] cursor.execute('''INSERT INTO - planning_data (planning_application_id, planning_application_link, description, registered_with_local_authority_date, decision_date, last_synced_date, status, status_before_aliasing, data_source, data_source_link, uprn) + planning_data (planning_application_id, planning_application_link, description, registered_with_local_authority_date, days_since_registration_cached, decision_date, days_since_decision_date_cached, last_synced_date, status, status_before_aliasing, data_source, data_source_link, uprn) VALUES - (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ''', ( e["application_id"], application_url, e["description"], date_object_into_date_string(e["registered_with_local_authority_date"]), + days_since(e["registered_with_local_authority_date"], now), date_object_into_date_string(e["decision_date"]), + days_since(e["decision_date"], now), date_object_into_date_string(e["last_synced_date"]), e["status"], e["status_before_aliasing"], e["data_source"], @@ -155,6 +158,11 @@ def insert_entry(cursor, e): e["uprn"]) ) +def days_since(date, now): + if(date == None): + return None + return (now - date).days + def date_object_into_date_string(date): if(date == None): return None diff --git a/migrations/034.planning_livestream_data.up.sql b/migrations/034.planning_livestream_data.up.sql index 5e023255..b51aec5f 100644 --- a/migrations/034.planning_livestream_data.up.sql +++ b/migrations/034.planning_livestream_data.up.sql @@ -7,7 +7,9 @@ CREATE TABLE IF NOT EXISTS planning_data ( planning_application_link VARCHAR(260), description VARCHAR, registered_with_local_authority_date date, + days_since_registration_cached smallint, decision_date date, + days_since_decision_date_cached smallint, last_synced_date date, status VARCHAR(50), status_before_aliasing VARCHAR(50),