Commit e17683f6 authored by Christine Plumejeaud's avatar Christine Plumejeaud
Browse files

new api rawflows to get all flows. Will be deployed on 19/07/2021 afternoon....

new api rawflows to get all flows. Will be deployed on 19/07/2021 afternoon. New comments for columns are set in the git in static/data
parent f5aca07b
......@@ -36,8 +36,8 @@ CORS(app)
#app.config.from_object('config')
#port = app.config['PORT']
port = '80'
postgresport = '5433'
database = 'portic_v5'
postgresport = '5432'
database = 'portic_v6'
def retrieveDataFromPostgres(query) :
"""
......@@ -131,6 +131,8 @@ def readFieldnames(api, schema='navigoviz') :
if api is not None :
if api == 'travels' :
table_name = 'built_travels'
if api == 'rawflows' :
table_name = 'raw_flows'
if api == 'pointcalls' :
table_name = 'pointcall'
if api == 'ports' :
......@@ -147,7 +149,7 @@ def readFieldnames(api, schema='navigoviz') :
left outer join pg_catalog.pg_statio_all_tables st on (pgd.objoid=st.relid and c.table_schema=st.schemaname and c.table_name=st.relname)
where c.table_name in ('%s') and c.table_schema = '%s' and pgd.objoid = st.relid;"""% (api, table_name, schema)
#print(query)
print(query)
metadata = retrieveDataFromPostgres(query)
return metadata
"""
......@@ -299,6 +301,60 @@ def getTravels():
return formatOutput(dfcsv, 'travels')
@app.route('/api/rawflows/')
def getRawFlows():
"""
Return the raw flows as specified in API (same as travel for the attributes)
Raw_flows is built by making a auto-join on pointcall for source_doc_id,
and departure are "Out" action, destination are "In" action, ordered chronologically
Thus we do not take care of net_route_marker (A or Z), neither of ship_id to built travels.
Data about the cargo, the captain, the homeport are those filled at the departure (there is no doublons)
Will be extracted from postgres, schema navigoviz, table raw_flows (see navigocorpus/ETL)
- params : **all** | tableau des noms longs des attributs de l'API à renvoyer
params=pointcall,pointcall_uhgs_id for instance
http://127.0.0.1:5004/api/rawflows/?format=csv
http://127.0.0.1:80/api/rawflows/?format=csv&shortenfields=true&date=1789
http://127.0.0.1:80/api/rawflows/?format=json&params=id,departure,destination,destination_uhgs_id
http://127.0.0.1:80/api/rawflows/?format=json&params=id,departure,destination,destination_uhgs_id&shortenfields=true
"""
import pandas as pd
#filename = os.path.join(APP_DATA, 'travels_API_11mai2020.csv')
#dfcsv = pd.read_csv(filename, sep = ';')
query = 'select * from navigoviz.raw_flows'
## Filter the result according requested params
## raw_flows has same attributes than built_travels
params = request.args.get("params")
if (params is not None and len(params)>0) :
#print('selecting some columns')
fields = readFieldnames('rawflows')
keepparams = str(params).split(',')
#https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike
labels = []
for k in keepparams:
if k in fields['name'].tolist() :
labels.append(k)
attributes = ",".join(labels)
query = 'select '+attributes+' from navigoviz.raw_flows'
## filter following a date given as a year (4 digits)
date = request.args.get("date")
if (date is not None and len(date)==4) :
if 'where' in query :
query = query + """ and (substring(departure_out_date for 4) = '%s' or substring(destination_in_date for 4) = '%s')""" % (date, date)
else :
query = query + """ where (substring(departure_out_date for 4) = '%s' or substring(destination_in_date for 4) = '%s') """ % (date, date)
print(query)
dfcsv = retrieveDataFromPostgres(query)
return formatOutput(dfcsv, 'rawflows')
@app.route('/api/details/departures/', methods = ['GET'])
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment