Commit 16d1bd5e authored by Bruce Flynn's avatar Bruce Flynn

Add stations_available

parent cded8b70
......@@ -33,6 +33,14 @@ class DataForm(Schema):
avg = Int(min=10, max=86400, if_missing=None, if_empty=None)
class StationsAvailableForm(Schema):
allow_extra_fields = True
filter_extra_fields = False
type = OneOf(['rdr', 'q1h'], if_missing='q1h', if_empty='q1h')
start = Regex(r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$', not_empty=True)
end = Regex(r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$', not_empty=True)
def stamp_to_dt(val):
return datetime.strptime(val, '%Y-%m-%dT%H:%M:%SZ')
......@@ -154,6 +162,17 @@ def stations_view(request):
return stations
@view_config(route_name='stations_available', renderer='json')
def stations_available(request):
db = request.registry.settings['db']
# query parameter parsing
try:
form = StationsAvailableForm.to_python(request.params)
except Invalid as err:
raise HTTPBadRequest('Invalid request params: %s' % str(err))
return db.stations_available(form['type'], form['start'], form['end'])
def csv_response(stations, sensors, form, rows):
fields = ['%s.%s' % (k, v)
for k in stations
......@@ -216,6 +235,7 @@ def main(global_config, **settings):
config.add_route('data_form_csv', '/data.csv')
config.add_route('data_form_netcdf', '/data.nc')
config.add_route('stations', '/stations.json')
config.add_route('stations_available', '/stations_available.json')
config.scan()
# wrapped with middleware to insert COORS headers
......
......@@ -103,6 +103,21 @@ def get_stations():
return zult
def stations_available(type_, start, end):
with connection() as conn:
query = sql.text('''
SELECT count(*), station
FROM %s
WHERE stamp >= :start
AND stamp < :end
GROUP BY station
HAVING count(*) > 0
ORDER BY station
''' % _data_table_name(type_))
return [{'name': r['station'], 'count': r['count']}
for r in conn.execute(query, start=start, end=end).fetchall()]
def get_station_names():
return list(stations.names())
......
......@@ -47,6 +47,10 @@ def mock_database(monkeypatch):
def get_q1h_slice(stations, symbols, start, end, **kwargs):
return Db.data
@staticmethod
def stations_available(type, start, end):
return []
db = Db()
monkeypatch.setattr(app, 'db', db)
return db
......@@ -87,3 +91,13 @@ class Test(object):
def test_get_stations(self, pyramid_app):
pyramid_app.get('/stations.json', status=200)
def test_stations_available(self, pyramid_app):
pyramid_app.get('/stations_available.json', status=400)
pyramid_app.get((
'/stations_available.json?'
'type=rdr&'
'start=2016-01-01T00:00:00Z&'
'end=2017-01-01T00:00:00Z'
), status=200)
......@@ -89,3 +89,26 @@ def test_get_q1h_slice(database):
data = db.get_q1h_slice(['STATION1'], ['air_temp'], start, end, avg)
# data should return 12 rows becasue there are 12 5min intervals in 1 hour
assert len(data) == 6
def test_stations_available(database):
with db.connection() as conn:
station = db.get_station_names()[0]
conn.execute(sa.text("""
INSERT INTO data VALUES
('2016-01-01 00:00:00', :station_id, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
('2016-01-01 00:10:00', :station_id, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0),
('2016-01-01 00:20:00', :station_id, 2.0, 2.0, 2.0, 2.0, 0.0, 0.0),
('2016-01-01 00:30:00', :station_id, 3.0, 3.0, 3.0, 3.0, 0.0, 0.0)
"""), station_id=station)
dat = db.stations_available('rdr', datetime(2016, 1, 1), datetime(2017, 1, 1))
assert len(dat) == 1
assert dat[0]['name']
assert dat[0]['count'] == 4
dat = db.stations_available('rdr', datetime(2015, 1, 1), datetime(2016, 1, 1))
assert len(dat) == 0
dat = db.stations_available('q1h', datetime(2016, 1, 1), datetime(2017, 1, 1))
assert len(dat) == 0
Markdown is supported
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