Skip to content
Snippets Groups Projects
Commit f62a66c1 authored by Kenny Gao's avatar Kenny Gao
Browse files

ascii format for multi site query finished

multi site query for ascii returns correctly
Error functionality not implemented
Works for the basic case
parent 5a0bf57a
Branches
No related tags found
1 merge request!2Order form
from xml.dom.minidom import Document
def createXML(code, message):
doc = Document()
header = 'metobs'
head = doc.createElement(header)
head.setAttribute('Status', 'Error')
head.setAttribute('Code', code)
head.setAttribute('Message', message)
head.setAttribute('Result', '0')
doc.appendChild(head)
return doc.toprettyxml(indent=" ", encoding="utf-8")
aoss_translation = {
't': 'air_temp',
'rh': 'rel_hum',
......@@ -40,3 +55,51 @@ buoy_translation = {
'chlor': 'chlorophyll',
'pc': 'phycocyanin'
}
json_500 = {
'status': 'Error',
'code': '500',
'result': '0',
'message': 'The server encountered an unexpected internal server error'
}
ascii_500 = '# Status: Error<br># Code: 500<br># Message: The server encountered an unexpected internal server error<br># Results: 0'
xml_500 = createXML('500', 'The server encountered an unexpected internal server error')
json_404 = {
'status': 'Error',
'code': '404',
'result': '0',
'message': 'The resource could not be found'
}
ascii_404 = '# Status: Error<br># Code: 404<br># Message: The resource could not be found<br># Results: 0'
xml_404 = createXML('404', 'The resource could not be found')
json_stamp = {
'status': 'Error',
'code': '400',
'result': '0',
'message': 'Could not parse timestamp, check format'
}
ascii_stamp = '# Status: Error<br># Code: 400<br># Message: Could not parse timestamp, check format<br># Results: 0'
xml_stamp = createXML('400', 'Could not parse timestamp, check format')
json_mal = {
'status': 'Error',
'code': '400',
'result': '0',
'message': 'Server could not recognize if request is single-site or multi-site request'
}
ascii_mal = '# Status: Error<br># Code: 400<br># Message: Server could not recognize if request is single-site or multi-site request<br># Results: 0'
xml_mal = createXML('400', 'Server could not recognize if request is single-site or multi-site request')
from xml.dom.minidom import Document
from datetime import datetime as dt
from datetime import timedelta as td
from queryInflux import query, query2
from queryInflux import query
from io import StringIO
import pandas as pd
from flask_json import as_json_p
......@@ -444,6 +444,10 @@ def modifyData(fmt, begin, end, site, inst, symbols, interval, sep, callback):
inst = instHandler(inst)
symbols = symbols.split(':')
for symbol in symbols:
if('.' in symbol):
return 'malformed'
if(not interval):
interval = '1m'
......@@ -456,13 +460,13 @@ def modifyData(fmt, begin, end, site, inst, symbols, interval, sep, callback):
unpack = symbolsHandler(site, inst, symbols)
if(not unpack[0]):
return 500
if(isinstance(unpack, int)):
if(unpack == 404):
return 404
if(not unpack[0]):
return 500
symbols = unpack[0]
windSpeed = unpack[1]
windDirection = unpack[2]
......@@ -472,12 +476,7 @@ def modifyData(fmt, begin, end, site, inst, symbols, interval, sep, callback):
if(not sep):
sep = ','
#if we decide to implement later
if(interval == 'query2'):
result = query2(site, inst, symbols, begin, end)
else:
result = query(site, inst, symbols, begin, end, interval)
result = query(site, inst, symbols, begin, end, interval)
frame = handleResult(fmt,result, symbols, interval, sep, windSpeed, windDirection)
......
from modifyData import beginEndHandler, handleInterval, siteHandler, instHandler, symbolsHandler, handleResult
from queryInflux import query
from io import StringIO
import Util
def modifyData(fmt, begin, end, site, inst, symbols, interval, sep, callback):
dates = beginEndHandler(begin, end)
if(dates == 400):
return '400_stamp'
begin = dates[0]
end = dates[1]
if(not inst or not site or not symbols):
return 500
site = siteHandler(site)
inst = instHandler(inst)
if(not interval):
interval = '1m'
else:
interval = handleInterval(interval)
if(interval == -1):
return 500
unpack = symbolsHandler(site, inst, symbols)
if(isinstance(unpack, int)):
if(unpack == 404):
return 404
if(not unpack[0]):
return 500
symbols = unpack[0]
windSpeed = unpack[1]
windDirection = unpack[2]
spd_idx = unpack[3]
dir_idx = unpack[4]
if(not sep):
sep = ','
result = query(site, inst, symbols, begin, end, interval)
frame = handleResult(fmt,result, symbols, interval, sep, windSpeed, windDirection)
return frame.transpose()
def processSymbols(modifiedSymbols):
newSymbols = []
for symbol in modifiedSymbols:
site_inst_list = symbol.split('.')
site = site_inst_list[0]
inst = site_inst_list[1]
actual_symbol = site_inst_list[2]
if(actual_symbol == 'spd'):
newSymbol = 'wind_speed'
elif(actual_symbol == 'dir'):
newSymbol = 'wind_direction'
else:
if(site == 'aoss' and inst == 'tower'):
newSymbol = Util.aoss_translation[actual_symbol]
elif(site == 'mendota' and inst == 'buoy'):
newSymbol = Util.buoy_translation[actual_symbol]
newSymbols.append(site + '.' + inst + '.' + newSymbol)
return newSymbols
def process(symbols):
site_inst_obj = {}
for symbol in symbols:
site_inst_list = symbol.split('.')
site = site_inst_list[0]
inst = site_inst_list[1]
actual_symbol = site_inst_list[2]
#if site isn't in dict, add it
if(site not in site_inst_obj):
site_inst_obj[site] = {inst: [actual_symbol]}
#if the site is in there, but the instrument isn't
elif(inst not in site_inst_obj[site]):
site_inst_obj[site][inst] = [actual_symbol]
# both in there, append symbol
else:
site_inst_obj[site][inst].append(actual_symbol)
return site_inst_obj
def asciiReturn(frame, sep, symbols):
output = StringIO()
frame = frame.transpose()
dates = list(frame.columns.values)
output.write('# Status: Success<br># Code: 200<br># Message:<br>')
output.write('# Result: ' + str(len(dates)) + '<br>')
data = list(frame.iterrows())
output.write("# Fields: YYYY-MM-DDTHH:MM:SSZ")
for symbol in symbols:
print(symbol)
output.write(sep + symbol)
output.write('<br/>')
for date in dates:
row = frame[date]
output.write(date)
for symbol in symbols:
output.write(sep + str(row[symbol]))
output.write('<br/>')
return output
......@@ -32,44 +32,6 @@ def query(site, inst, symbols, begin, end, value):
print(query.getvalue())
client = InfluxDBClient(host, port, username, password, DB)
result = client.query(query.getvalue())
result = client.query(query.getvalue())#, epoch='ms')
return result
def query2(site, inst, symbols, begin, end):
host = 'metobs01'
port = 8086
username = 'root'
password = 'root'
DB = 'metobs'
print(symbols)
query = StringIO()
query.write('select ')
idx = 0
for symbol in symbols:
if idx == len(symbols) - 1:
query.write(symbol + ' ')
else:
query.write(symbol + ', ')
idx += 1
query.write("FROM metobs")
query.write('WHERE time >= \'' + begin + '\' AND time <= \'' + end + '\'')
query.write(' AND inst=\'' + inst + '\' AND site=\'' + site + '\'')
#query.write("GROUP BY time(" + interval + ")")
print(query.getvalue())
client = InfluxDBClient(host, port, username, password, DB)
result = client.query(query.getvalue())
return result
#getData('aoss', 'tower', ['air_temp', 'rh', 'dewpoint', 'accum_precip', 'pressure', 'altimeter', 'solar_flux'], '2016-04-21T18:00:00Z', '2016-04-21T19:00:00Z', 'value_1m')
return result
\ No newline at end of file
......@@ -2,7 +2,10 @@
from flask import Flask, render_template, request, jsonify, Response
from flask_json import FlaskJSON, as_json_p
import Util
import modifyData
import multiData
import pandas as pd
#create application
......@@ -10,19 +13,65 @@ app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
json = FlaskJSON(app)
def getData(fmt):
def handleMultiSites(fmt):
beginTime = request.args.get('begin')
endTime = request.args.get('end')
site = request.args.get('site')
inst = request.args.get('inst')
symbols = request.args.get('symbols')
interval = request.args.get('interval')
sep = request.args.get('sep')
callback = request.args.get('callback')
#make sure station, sites, or symbols are not null
if(not inst or not site or not symbols):
return render_template('500.html'), 500
modifiedSymbols = symbols.split(':')
frames = {}
site_inst_symbolObj = multiData.process(modifiedSymbols)
for site in site_inst_symbolObj :
for inst in site_inst_symbolObj[site] :
getSymbols = site_inst_symbolObj[site][inst]
frame = multiData.modifyData(fmt, beginTime, endTime, site, inst, getSymbols, interval, sep, callback)
frames[site + '.' + inst] = frame
for identity in frames:
frame = frames[identity]
cols = frame.columns.tolist()
for idx, col in enumerate(cols):
col = identity + '.' + col
cols[idx] = col
frame.columns = cols
frames[identity] = frame
mergeFrames = []
for identity in frames:
mergeFrames.append(frames[identity])
frame = pd.concat(mergeFrames, axis=1)
modifiedSymbols = multiData.processSymbols(modifiedSymbols)
frame = frame[modifiedSymbols]
if(fmt == 'ascii'):
if(not sep):
sep = ','
return multiData.asciiReturn(frame, sep, modifiedSymbols).getvalue()
else:
return render_template('404.html'), 404
def handleSingleSite(fmt):
beginTime = request.args.get('begin')
endTime = request.args.get('end')
site = request.args.get('site')
inst = request.args.get('inst')
symbols = request.args.get('symbols')
interval = request.args.get('interval')
sep = request.args.get('sep')
callback = request.args.get('callback')
result = modifyData.modifyData(fmt, beginTime,
endTime, site, inst, symbols, interval,
......@@ -30,16 +79,60 @@ def getData(fmt):
if(isinstance(result, int)):
if(result == 500):
return render_template('500.html'), 500
if(fmt == 'json'):
return jsonify(**Util.json_500), 500
if(fmt == 'ascii'):
return Util.ascii_500, 500
if(fmt == 'xml'):
return Response(Util.xml_500, mimetype='text/xml'), 500
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_500), 500
if(result == 400):
return render_template('400.html', format=fmt), 400
if(result == 404):
return render_template('404.html'), 404
if(fmt == 'json'):
return jsonify(**Util.json_404), 404
if(fmt == 'ascii'):
return Util.ascii_404, 404
if(fmt == 'xml'):
return Response(Util.xml_404, mimetype='text/xml'), 404
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_404), 404
if(isinstance(result, str)):
if(result == '400_stamp'):
return render_template("stamp_error.html")
if(fmt == 'json'):
return jsonify(**Util.json_stamp), 400
if(fmt == 'ascii'):
return Util.ascii_stamp, 400
if(fmt == 'xml'):
return Response(Util.xml_stamp, mimetype='text/xml'), 400
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_stamp), 400
if(result == 'malformed'):
if(fmt == 'json'):
return jsonify(**Util.json_mal), 400
if(fmt == 'ascii'):
return Util.ascii_mal, 400
if(fmt == 'xml'):
return Response(Util.xml_mal, mimetype='text/xml'), 400
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_mal), 400
if(fmt == 'ascii'):
return result
......@@ -72,9 +165,13 @@ def internal_server(e):
def test(fmt):
print('here')
sites = request.args.get('site')
inst = request.args.get('inst')
if(sites):
return getData(fmt)
if(sites or inst):
return handleSingleSite(fmt)
else:
return handleMultiSites(fmt)
if __name__ == '__main__':
......
No preview for this file type
<!doctype html>
<h1>400 Bad Request</h1>
<p>The server could not comply with the request since it is either malformed or otherwise incorrect</p>
{% if format %}
<p>No appropriate response plugin found for type '{{ format }}'</p>
{% endif %}
\ No newline at end of file
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
"
The resource could not be found."
<br>
<br>
</body>
</html>
\ No newline at end of file
<html>
<head></head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap">
"Internal server Error
The server encountered an unexpected internal server error
(generated by waitress)"
</pre>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment