Skip to content
Snippets Groups Projects
Unverified Commit 1b57f220 authored by David Hoese's avatar David Hoese
Browse files

Replace original data api logic for short symbol names

When site and inst are specified don't use the whole symbol name
parent aba7f006
No related branches found
No related tags found
No related merge requests found
...@@ -121,7 +121,7 @@ def calc_num_records(begin, end, interval): ...@@ -121,7 +121,7 @@ def calc_num_records(begin, end, interval):
return diff / data_responses.INTERVALS[interval] return diff / data_responses.INTERVALS[interval]
def handle_csv(frame, symbols, epoch, sep=',', def handle_csv(frame, epoch, sep=',',
message='', code=200, status='success', **kwargs): message='', code=200, status='success', **kwargs):
output = """# status: {status} output = """# status: {status}
# code: {code:d} # code: {code:d}
...@@ -150,7 +150,7 @@ def handle_csv(frame, symbols, epoch, sep=',', ...@@ -150,7 +150,7 @@ def handle_csv(frame, symbols, epoch, sep=',',
message=message, message=message,
num_results=frame.shape[0], num_results=frame.shape[0],
epoch_str=epoch_str, epoch_str=epoch_str,
symbol_list=sep.join(symbols), symbol_list=sep.join(frame.columns),
symbol_data="\n".join(data_lines), symbol_data="\n".join(data_lines),
) )
...@@ -158,7 +158,7 @@ def handle_csv(frame, symbols, epoch, sep=',', ...@@ -158,7 +158,7 @@ def handle_csv(frame, symbols, epoch, sep=',',
@as_json_p(optional=True) @as_json_p(optional=True)
def handle_json(frame, symbols, epoch, order='columns', def handle_json(frame, epoch, order='columns',
message='', code=200, status='success', **kwargs): message='', code=200, status='success', **kwargs):
output = {} output = {}
output['status'] = status output['status'] = status
...@@ -177,16 +177,16 @@ def handle_json(frame, symbols, epoch, order='columns', ...@@ -177,16 +177,16 @@ def handle_json(frame, symbols, epoch, order='columns',
package['timestamps'] = newStamps package['timestamps'] = newStamps
if order == 'column': if order == 'column':
package['data'] = dict(frame[symbols]) package['data'] = dict(frame)
else: else:
package['symbols'] = symbols package['symbols'] = frame.columns
package['data'] = [frame.iloc[i].values for i in range(frame.shape[0])] package['data'] = [frame.iloc[i].values for i in range(frame.shape[0])]
# package['data'] = frame.values # package['data'] = frame.values
output['results'] = package output['results'] = package
return output, code return output, code
def handle_xml(frame, symbols, epoch, sep=',', def handle_xml(frame, epoch, sep=',',
message='', code=200, status='success', **kwargs): message='', code=200, status='success', **kwargs):
doc = Document() doc = Document()
header = 'metobs' header = 'metobs'
...@@ -276,9 +276,11 @@ def modify_data(fmt, begin, end, site, inst, symbols, interval, ...@@ -276,9 +276,11 @@ def modify_data(fmt, begin, end, site, inst, symbols, interval,
if site and inst: if site and inst:
# shorthand for symbols that all use the same site and inst # shorthand for symbols that all use the same site and inst
symbols = ["{}.{}.{}".format(site, inst, s) for s in symbols.split(':')] short_symbols = symbols.split(':')
symbols = ["{}.{}.{}".format(site, inst, s) for s in short_symbols]
elif not site and not inst: elif not site and not inst:
# each symbol is fully qualified with site.inst.symbol # each symbol is fully qualified with site.inst.symbol
short_symbols = None
symbols = symbols.split(':') symbols = symbols.split(':')
else: else:
return handle_error(fmt, 'missing_site_inst') return handle_error(fmt, 'missing_site_inst')
...@@ -304,8 +306,10 @@ def modify_data(fmt, begin, end, site, inst, symbols, interval, ...@@ -304,8 +306,10 @@ def modify_data(fmt, begin, end, site, inst, symbols, interval,
# order the resulting symbols the way the user requested # order the resulting symbols the way the user requested
# assume time is the first column # assume time is the first column
frame = frame[symbols] frame = frame[symbols]
if site:
frame.columns = short_symbols
handler = RESPONSE_HANDLERS[fmt] handler = RESPONSE_HANDLERS[fmt]
return handler(frame, symbols, epoch, return handler(frame, epoch,
sep=sep, order=order, sep=sep, order=order,
status=status, code=code, message=message) status=status, code=code, message=message)
...@@ -117,7 +117,7 @@ class TestDataAPI(unittest.TestCase): ...@@ -117,7 +117,7 @@ class TestDataAPI(unittest.TestCase):
res = json.loads(res.data.decode()) res = json.loads(res.data.decode())
self.assertEqual(res['code'], 200) self.assertEqual(res['code'], 200)
self.assertEqual(res['num_results'], 9) self.assertEqual(res['num_results'], 9)
self.assertListEqual(res['results']['symbols'], ['aoss.tower.air_temp']) self.assertListEqual(res['results']['symbols'], ['air_temp'])
self.assertEqual(len(res['results']['timestamps']), 9) self.assertEqual(len(res['results']['timestamps']), 9)
self.assertEqual(len(res['results']['data']), 9) self.assertEqual(len(res['results']['data']), 9)
self.assertEqual(len(res['results']['data'][0]), 1) self.assertEqual(len(res['results']['data'][0]), 1)
...@@ -130,8 +130,8 @@ class TestDataAPI(unittest.TestCase): ...@@ -130,8 +130,8 @@ class TestDataAPI(unittest.TestCase):
res = json.loads(res.data.decode()) res = json.loads(res.data.decode())
self.assertEqual(res['code'], 200) self.assertEqual(res['code'], 200)
self.assertEqual(res['num_results'], 9) self.assertEqual(res['num_results'], 9)
self.assertIn('aoss.tower.air_temp', res['results']['data']) self.assertIn('air_temp', res['results']['data'])
self.assertEqual(len(res['results']['data']['aoss.tower.air_temp']), 9) self.assertEqual(len(res['results']['data']['air_temp']), 9)
self.assertEqual(len(res['results']['timestamps']), 9) self.assertEqual(len(res['results']['timestamps']), 9)
@mock.patch('metobsapi.data_api.query') @mock.patch('metobsapi.data_api.query')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment