Skip to content
Snippets Groups Projects
Commit bbf32c80 authored by Riccardo Boero's avatar Riccardo Boero :innocent:
Browse files

Moved database connectionin loops of region for FACT_air

parent 211abb15
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,7 @@ This function queries a database for air traffic information, such as the number
This example queries air traffic data for specified European regions and returns the results in a DataFrame.
"""
function eu_air(conn::MySQL.Connection, selection::Dict)
function eu_air(conn_dict::Dict, selection::Dict)
# getting valuable info from dictionary: acceptable years are 1961, 71, 81, 91, 2001, 2011
geo_id = selection["geo_id"]
shape_obj = selection["shape_obj"]
......@@ -29,8 +29,12 @@ function eu_air(conn::MySQL.Connection, selection::Dict)
for (id, shape) in zip(geo_id, shape_obj)
# prepare query
query = "SELECT COUNT(DISTINCT(ea.airp_icao_)) as airports, SUM(et.flights) as flights FROM eu_airports_2013 ea JOIN eu_traffic et ON ea.airp_icao_ = et.ICAO_id WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), ea.SHAPE) AND et.Year = 2022;"
# create connection
conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"])
# execute
result = query_connection(conn, query)
# close connection
close_connection(conn)
# Check if the result is empty
if !isempty(result)
# add geo_id col
......@@ -67,7 +71,7 @@ This function queries a database for air traffic information, such as the number
This example queries air traffic data for specified U.S. regions and returns the results in a DataFrame.
"""
function us_air(conn::MySQL.Connection, selection::Dict)
function us_air(conn_dict::Dict, selection::Dict)
# getting valuable info from dictionary: acceptable years are 1961, 71, 81, 91, 2001, 2011
geo_id = selection["geo_id"]
......@@ -78,9 +82,12 @@ function us_air(conn::MySQL.Connection, selection::Dict)
for (id, shape) in zip(geo_id, shape_obj)
# prepare query
query = "SELECT count(*) as airports, SUM(COALESCE(COMMERCIAL, 0) + COALESCE(COMMUTER_O, 0) + COALESCE(AIR_TAXI_O, 0) + COALESCE(LOCAL_OPS, 0) + COALESCE(ITNRNT_OPS, 0) + COALESCE(MIL_ACFT_O, 0)) as flights FROM us_airports WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);"
# create connection
conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"])
# execute
result = query_connection(conn, query)
# close connection
close_connection(conn)
# Check if the result is empty
if !isempty(result)
# add geo_id col
......@@ -118,6 +125,14 @@ This function determines whether to fetch European or U.S. air traffic data base
In this example, European air traffic data for the specified region is retrieved and returned in a DataFrame.
"""
function get_airports_traffic(table::String, selection::Dict; host::String="127.0.0.1")
conn_dict = Dict(
"host" => host,
"port" => 3310,
"database" => "FACT_air",
"user" => "root",
"password" => "devops"
)
table_dict = Dict(
"eu_air" => eu_air,
"us_air" => us_air
......@@ -136,7 +151,7 @@ function get_airports_traffic(table::String, selection::Dict; host::String="127.
end
# data retrieval
df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_air", selection; host=host, port=3310)
df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict)
return df
end
......
......@@ -158,7 +158,7 @@ selection = Dict("id" => 123)
result = handle_selection_action("users", table_dict, conn, selection)
```
"""
function handle_selection_action(table::String, table_dict::Dict, conn::MySQL.Connection, selection::Dict)
function handle_selection_action(table::String, table_dict::Dict, conn_dict::Dict, selection::Dict)
# Default function returns an empty DataFrame with an appropriate message
default_func = () -> DataFrame(Message=["No corresponding table: $table"])
......@@ -166,7 +166,7 @@ function handle_selection_action(table::String, table_dict::Dict, conn::MySQL.Co
func = get(table_dict, table, default_func)
# Call the function with the additional parameters
return func(conn, selection)
return func(conn_dict, selection)
end
"""
......@@ -198,12 +198,10 @@ database = "my_database"
result_df = get_selected_objects_from_table("users", df, table_dict, database, selection)
```
"""
function get_selected_objects_from_table(table::String, df::DataFrame, table_dict::Dict, database::String, selection::Dict; host::String="127.0.0.1", port::Int=3306)
function get_selected_objects_from_table(table::String, df::DataFrame, table_dict::Dict, selection::Dict, conn_dict::Dict)
# Establish the connection
conn = establish_connection(host, "root", "devops", database; port=port)
# get results
result = handle_selection_action(String(table), table_dict, conn, selection)
result = handle_selection_action(String(table), table_dict, conn_dict, selection)
# start converting data types to fix mistakes in SQL types interpretation
result = result[:, names(df)]
# Check and convert each column in result to match the type in df
......@@ -224,8 +222,5 @@ function get_selected_objects_from_table(table::String, df::DataFrame, table_dic
end
df = vcat(df, result)
# Close the connection when done
close_connection(conn)
return df
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment