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

date as param in API

parent 3db2b7ae
...@@ -37,7 +37,7 @@ CORS(app) ...@@ -37,7 +37,7 @@ CORS(app)
#port = app.config['PORT'] #port = app.config['PORT']
port = '80' port = '80'
postgresport = '5433' postgresport = '5433'
database = 'portic_v4' database = 'portic_v5'
def retrieveDataFromPostgres(query) : def retrieveDataFromPostgres(query) :
""" """
...@@ -140,7 +140,7 @@ def readFieldnames(api, schema='navigoviz') : ...@@ -140,7 +140,7 @@ def readFieldnames(api, schema='navigoviz') :
#API;name;shortname;type;description #API;name;shortname;type;description
query = """SELECT case when c.table_name= 'built_travels' then 'travels' else 'pointcalls' end as API, query = """SELECT case when c.table_name= 'built_travels' then 'travels' else 'pointcalls' end as API,
c.column_name as name, c.column_name as name,
case when c.table_name= 'built_travels' then 't' else 'p' end||navigo.pystrip(to_char(c.ordinal_position::int, '09')) as shortname, case when c.table_name= 'built_travels' then 't' else 'p' end||navigo.pystrip(to_char(c.ordinal_position::int, '009')) as shortname,
c.data_type as type, pgd.description as description c.data_type as type, pgd.description as description
FROM information_schema.columns c FROM information_schema.columns c
left outer join pg_catalog.pg_description pgd on (pgd.objsubid=c.ordinal_position ) left outer join pg_catalog.pg_description pgd on (pgd.objsubid=c.ordinal_position )
...@@ -185,9 +185,10 @@ def getPointcalls(): ...@@ -185,9 +185,10 @@ def getPointcalls():
Return the pointcalls as specified in API Return the pointcalls as specified in API
Will be extracted from postgres, schema navigoviz, table pointcall (see navigocorpus/ETL) Will be extracted from postgres, schema navigoviz, table pointcall (see navigocorpus/ETL)
- params : **all** | tableau des noms longs des attributs de l'API à renvoyer - params : **all** | tableau des noms longs des attributs de l'API à renvoyer
- date : 4 digits representing a year, to extract data for this given year only
http://127.0.0.1:80/api/pointcalls/?params=pointcall,pointcall_uhgs_id&shortenfields=true http://127.0.0.1:80/api/pointcalls/?params=pointcall,pointcall_uhgs_id&shortenfields=true
http://127.0.0.1:80/api/pointcalls/?format=csv http://127.0.0.1:80/api/pointcalls/?format=csv
http://127.0.0.1/api/pointcalls/?params=pointcall,pointcall_uhgs_id&date=1787&format=csv
http://127.0.0.1:80/api/pointcalls/?params=pointcall,pointcall_uhgs_id&shortenfields=false http://127.0.0.1:80/api/pointcalls/?params=pointcall,pointcall_uhgs_id&shortenfields=false
http://127.0.0.1:80/api/pointcalls/?format=json&params=id,pointcall,ship_name,destination,destination_uhgs_id&shortenfields=true http://127.0.0.1:80/api/pointcalls/?format=json&params=id,pointcall,ship_name,destination,destination_uhgs_id&shortenfields=true
...@@ -224,6 +225,12 @@ def getPointcalls(): ...@@ -224,6 +225,12 @@ def getPointcalls():
query = 'select '+attributes+' from navigoviz.pointcall' query = 'select '+attributes+' from navigoviz.pointcall'
#dfcsv = dfcsv.loc[:, labels] #dfcsv = dfcsv.loc[:, labels]
## filter following a date
date = request.args.get("date")
if (date is not None and len(date)==4) :
query = query + """ where (substring(pointcall_out_date for 4) = '%s' or substring(pointcall_in_date for 4) = '%s') """ % (date, date)
print(query)
dfcsv = retrieveDataFromPostgres(query) dfcsv = retrieveDataFromPostgres(query)
return formatOutput(dfcsv, 'pointcalls') return formatOutput(dfcsv, 'pointcalls')
...@@ -241,7 +248,7 @@ def getTravels(): ...@@ -241,7 +248,7 @@ def getTravels():
- both_to : true | **false** - both_to : true | **false**
http://127.0.0.1:5004/api/travels/?format=csv&both_to=false http://127.0.0.1:5004/api/travels/?format=csv&both_to=false
http://127.0.0.1:80/api/travels/?format=csv&both_to=true&shortenfields=true http://127.0.0.1:80/api/travels/?format=csv&both_to=true&shortenfields=true&date=1789
http://127.0.0.1:80/api/travels/?format=json&params=id,departure,destination,destination_uhgs_id http://127.0.0.1:80/api/travels/?format=json&params=id,departure,destination,destination_uhgs_id
http://127.0.0.1:80/api/travels/?format=json&params=id,departure,destination,destination_uhgs_id&shortenfields=true http://127.0.0.1:80/api/travels/?format=json&params=id,departure,destination,destination_uhgs_id&shortenfields=true
...@@ -275,7 +282,16 @@ def getTravels(): ...@@ -275,7 +282,16 @@ def getTravels():
#print('filtering duplicates out of travels') #print('filtering duplicates out of travels')
#dfcsv = dfcsv[dfcsv['source_entry']!='both-to'] #dfcsv = dfcsv[dfcsv['source_entry']!='both-to']
query = query + " where source_entry <> 'both-to'" query = query + " where source_entry <> 'both-to'"
## filter following a date
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) dfcsv = retrieveDataFromPostgres(query)
return formatOutput(dfcsv, 'travels') return formatOutput(dfcsv, 'travels')
...@@ -290,7 +306,7 @@ def getDeparturesDetails(): ...@@ -290,7 +306,7 @@ def getDeparturesDetails():
Will be extracted from postgres, schema navigoviz, table built_travels (see navigocorpus/ETL), Will be extracted from postgres, schema navigoviz, table built_travels (see navigocorpus/ETL),
but with a filter by default : only source_entry = from and both-from, to avoid duplicates but with a filter by default : only source_entry = from and both-from, to avoid duplicates
http://localhost:80/api/details/departures?lat=46&lon=-1&radius=100 http://localhost:80/api/details/departures?lat=46&lon=-1&radius=100&date=1787
http://localhost/api/details/departures/?lat=45.2333&lon=-1.5&radius=100 http://localhost/api/details/departures/?lat=45.2333&lon=-1.5&radius=100
http://localhost/api/details/departures/?lat=45.2333&lon=toto&radius=100 http://localhost/api/details/departures/?lat=45.2333&lon=toto&radius=100
""" """
...@@ -318,6 +334,11 @@ def getDeparturesDetails(): ...@@ -318,6 +334,11 @@ def getDeparturesDetails():
#df1 = calcul_isInside(lat, lon, radius) #df1 = calcul_isInside(lat, lon, radius)
#dfcsv = df1[['departure','departure_uhgs_id','departure_latitude','departure_longitude']].drop_duplicates() #dfcsv = df1[['departure','departure_uhgs_id','departure_latitude','departure_longitude']].drop_duplicates()
## filter following a date
date = request.args.get("date")
if (date is not None and len(date)==4) :
query = query + """ AND (substring(departure_out_date for 4) = '%s' or substring(destination_in_date for 4) = '%s') """ % (date, date)
#print(query) #print(query)
dfcsv = retrieveDataFromPostgres(query) dfcsv = retrieveDataFromPostgres(query)
...@@ -346,16 +367,23 @@ def getDeparturesAgg(): ...@@ -346,16 +367,23 @@ def getDeparturesAgg():
#df2 = calcul_isInside(lat, lon, radius).departure.value_counts().reset_index() #df2 = calcul_isInside(lat, lon, radius).departure.value_counts().reset_index()
#df2.columns = ['departure', 'count'] #df2.columns = ['departure', 'count']
## filter following a date
date = request.args.get("date")
filterclause = ""
if (date is not None and len(date)==4) :
filterclause = filterclause + """ and (substring(departure_out_date for 4) = '%s' or substring(destination_in_date for 4) = '%s') """ % (date, date)
if lat != None and lon != None and radius!= None : if lat != None and lon != None and radius!= None :
query = """select departure, count(*) as count from navigoviz.built_travels query = """select departure, count(*) as count from navigoviz.built_travels
where source_entry<> 'both-to' and where source_entry<> 'both-to' %s
st_distance(departure_point, st_setsrid(st_transform(st_setsrid(st_makepoint(%f, %f), 4326), 3857), 3857)) < %f st_distance(departure_point, st_setsrid(st_transform(st_setsrid(st_makepoint(%f, %f), 4326), 3857), 3857)) < %f
group by departure""" % (lon,lat,radius*1000) group by departure""" % (filterclause, lon,lat,radius*1000)
else : else :
query = """select departure, count(*) query = """select departure, count(*)
from navigoviz.built_travels from navigoviz.built_travels
where source_entry<> 'both-to' where source_entry<> 'both-to' %s
group by departure""" group by departure""" % (filterclause)
#print(query) #print(query)
df2 = retrieveDataFromPostgres(query) df2 = retrieveDataFromPostgres(query)
...@@ -371,10 +399,18 @@ def getDestinationsAgg(): ...@@ -371,10 +399,18 @@ def getDestinationsAgg():
Will be extracted from postgres, schema navigoviz, table built_travels (see navigocorpus/ETL), Will be extracted from postgres, schema navigoviz, table built_travels (see navigocorpus/ETL),
but with a filter by default : only source_entry = from and both-from, to avoid duplicates but with a filter by default : only source_entry = from and both-from, to avoid duplicates
http://localhost/api/agg/destinations/?lat=45.2333&lon=-1&radius=100 http://localhost/api/agg/destinations/?lat=45.2333&lon=-1&radius=100&date=1789
""" """
## filter following a date
date = request.args.get("date")
filterclause = ""
if (date is not None and len(date)==4) :
filterclause = filterclause + """ and (substring(departure_out_date for 4) = '%s' or substring(destination_in_date for 4) = '%s') """ % (date, date)
lat = None lat = None
lon = None lon = None
radius = None radius = None
...@@ -391,14 +427,14 @@ def getDestinationsAgg(): ...@@ -391,14 +427,14 @@ def getDestinationsAgg():
if lat != None and lon != None and radius!= None : if lat != None and lon != None and radius!= None :
query = """select destination_admiralty as label, count(*) as value from navigoviz.built_travels query = """select destination_admiralty as label, count(*) as value from navigoviz.built_travels
where source_entry<> 'both-to' where source_entry<> 'both-to' %s
and st_distance(departure_point, st_setsrid(st_transform(st_setsrid(st_makepoint(%f, %f), 4326), 3857), 3857)) < %f and st_distance(departure_point, st_setsrid(st_transform(st_setsrid(st_makepoint(%f, %f), 4326), 3857), 3857)) < %f
group by destination_admiralty""" % (lon,lat,radius*1000) group by destination_admiralty""" % (filterclause, lon,lat,radius*1000)
else : else :
query = """select destination_admiralty as label, count(*) as value query = """select destination_admiralty as label, count(*) as value
from navigoviz.built_travels from navigoviz.built_travels
where source_entry<> 'both-to' where source_entry<> 'both-to' %s
group by destination_admiralty""" group by destination_admiralty""" % (filterclause)
#print(query) #print(query)
df3 = retrieveDataFromPostgres(query) df3 = retrieveDataFromPostgres(query)
df3['id'] = df3['label'] df3['id'] = df3['label']
...@@ -422,6 +458,7 @@ def getFlows(): ...@@ -422,6 +458,7 @@ def getFlows():
- params : **all** | tableau des noms longs des attributs de l'API à renvoyer - params : **all** | tableau des noms longs des attributs de l'API à renvoyer
params=pointcall,pointcall_uhgs_id for instance params=pointcall,pointcall_uhgs_id for instance
- both_to : true | **false** - both_to : true | **false**
- date : **1787** | yyyy (4 digits)
http://localhost:80/api/flows/?format=csv&both_to=false http://localhost:80/api/flows/?format=csv&both_to=false
http://localhost:80/api/flows/?format=csv&both_to=true&shortenfields=true http://localhost:80/api/flows/?format=csv&both_to=true&shortenfields=true
...@@ -429,8 +466,11 @@ def getFlows(): ...@@ -429,8 +466,11 @@ def getFlows():
http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=In&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=In&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=Out&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=Out&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
http://localhost/api/flows/?format=json&ports=A0180923&direction=In-out&params=id,departure,destination&shortenfields=true http://localhost/api/flows/?format=json&ports=A0180923&direction=In-out&params=id,departure,destination&shortenfields=true
http://localhost/api/flows/?format=json&ports=A0180923,A0152606&direction=Out&date=1787&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
[{"t01":"0008663N- 05","t04":"Bordeaux","t19":"Bordeaux"},{"t01":"0009557N- 01","t04":"Bordeaux","t19":"Bordeaux"},{"t01":"0010656N- 01","t04":"Bordeaux","t19":"Bordeaux"}] [{"t01":"0008663N- 05","t04":"Bordeaux","t19":"Bordeaux"},{"t01":"0009557N- 01","t04":"Bordeaux","t19":"Bordeaux"},{"t01":"0010656N- 01","t04":"Bordeaux","t19":"Bordeaux"}]
[{"t001":"0008663N- 05","t004":"Bordeaux","t020":"Bordeaux"},{"t001":"0010656N- 01","t004":"Bordeaux","t020":"Bordeaux"}]
http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=No&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity http://localhost:80/api/flows/?format=json&ports=A0180923,A0152606&direction=No&params=travel_rank,ship_id,departure,destination,departure_action,destination_action,distance_dep_dest,travel_uncertainity
...@@ -447,6 +487,7 @@ def getFlows(): ...@@ -447,6 +487,7 @@ def getFlows():
attributes = '*' attributes = '*'
filter_clause = 'true' filter_clause = 'true'
## Filter the result according requested params ## Filter the result according requested params
params = request.args.get("params") params = request.args.get("params")
if (params is not None and len(params)>0) : if (params is not None and len(params)>0) :
...@@ -469,7 +510,16 @@ def getFlows(): ...@@ -469,7 +510,16 @@ def getFlows():
#print('filtering duplicates out of travels') #print('filtering duplicates out of travels')
#dfcsv = dfcsv[dfcsv['source_entry']!='both-to'] #dfcsv = dfcsv[dfcsv['source_entry']!='both-to']
filter_clause = " source_entry <> 'both-to' " filter_clause = " source_entry <> 'both-to' "
## filter following a date
date = request.args.get("date")
if (date is not None and len(date)==4) :
filter_clause = filter_clause + """ and (substring(departure_out_date for 4) = '%s' or substring(destination_in_date for 4) = '%s')""" % (date, date)
else :
#By default, we return only 1787 flow data
filter_clause = filter_clause + """ and (substring(departure_out_date for 4) = '1787' or substring(destination_in_date for 4) = '1787')"""
query = 'select '+attributes+' from navigoviz.built_travels '+filter_clause query = 'select '+attributes+' from navigoviz.built_travels '+filter_clause
## Filter the result according requested ports ## Filter the result according requested ports
......
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