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

Fixed errors and added records limit

Didn't throw appropriate error when symbols array is empty. Does it now
records limit added
parent bc006aa9
No related branches found
No related tags found
1 merge request!2Order form
No preview for this file type
......@@ -13,6 +13,22 @@ def createXML(code, message):
return doc.toprettyxml(indent=" ", encoding="utf-8")
def createJSON(code, message):
json = {}
json['status'] = 'error'
json['code'] = code
json['num_results'] = 0
json['message'] = message
return json
def createASCII(code, message):
returnString = '# status: error<br>'
returnString += '# code: ' + str(code) + '<br>'
returnString += '# num_results: 0<br># message: ' + message
return returnString
# aoss_translation = {
# 't': 'air_temp',
# 'rh': 'rel_hum',
......@@ -101,61 +117,52 @@ buoy_translation = {
resources = 'menodota buoy, and the aoss tower '
json_500 = {
'status': 'error',
'code': '400',
'num_results': '0',
'message': 'missing site or instrument parameter',
}
json_500 = createJSON(400, 'missing site or instrument parameter')
ascii_500 = '# status: error<br># code: 400<br># message: missing site or instrument parameter<br># num_results: 0'
ascii_500 = createASCII(400, 'missing site or instrument parameter')
xml_500 = createXML('400', 'missing site or instrument parameter')
json_404 = {
'status': 'error',
'code': '404',
'num_results': '0',
'message': 'the resource could not be found - The current resources supported are the ' + resources
}
json_404 = createJSON(404, 'the resource could not be found - The current resources supported are the ' + resources)
ascii_404 = '# status: error<br># code: 404<br># message: the resource could not be found - The current resources supported are the ' + resources + '<br># num_results: 0'
ascii_404 = createASCII(404, 'the resource could not be found - The current resources supported are the ' + resources)
xml_404 = createXML('404', 'the resource could not be found - The current resources supported are the ' + resources)
json_stamp = {
'status': 'error',
'code': '400',
'num_results': '0',
'message': 'could not parse timestamp, check format'
}
json_stamp = createJSON(400, 'could not parse timestamp, check format')
ascii_stamp = '# status: error<br># code: 400<br># message: could not parse timestamp, check format<br># num_results: 0'
ascii_stamp = createASCII(400, 'could not parse timestamp, check format')
xml_stamp = createXML('400', 'could not parse timestamp, check format')
json_mal = {
'status': 'error',
'code': '400',
'num_results': '0',
'message': 'server could not recognize if request is single-site or multi-site request'
}
json_mal = createJSON(400, '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># num_results: 0'
ascii_mal = createJSON(400, 'server could not recognize if request is single-site or multi-site request')
xml_mal = createXML('400', 'server could not recognize if request is single-site or multi-site request')
json_symbol = {
'status': 'error',
'code': '400',
'num_results': '0',
'message': 'server could not recognize symbol'
}
json_symbol = createJSON(400, 'server could not recognize symbol')
ascii_symbol = '# status: error<br># code: 400<br># message: server could not recognize symbol<br># num_results: 0'
ascii_symbol = createASCII(400, 'server could not recognize symbol')
xml_symbol = createXML('400', 'server could not recognize symbol')
json_413 = createJSON(413, 'too many records requested. Please pick an earlier end date, a later start date, or a bigger interval')
ascii_413 = createASCII(413, 'too many records requested. Please pick an earlier end date, a later start date, or a bigger interval')
xml_413 = createXML('413', 'too many records requested. Please pick an earlier end date, a later start date, or a bigger interval')
json_interval = createJSON(400, 'the interval given was not recognized')
ascii_interval = createASCII(400, 'the interval given was not recognized')
xml_interval = createXML('400', 'the interval given was not recognized')
api_version_header = "X-Metobs-API-VERSION"
api_version = 1.0
limit = 50000
......@@ -148,7 +148,9 @@ def symbolsHandler(site, inst, symbols):
returnList.append(wind_speed)
returnList.append(wind_dir)
returnList.append(spd_idx)
returnList.append(dir_idx)
returnList.append(dir_idx)
print(returnList)
return returnList
......@@ -183,7 +185,43 @@ def handleInterval(interval):
else:
interval = -1
return interval
return interval
def checkRecords(begin, end, interval):
startTime = dt.strptime(begin, "%Y-%m-%dT%H:%M:%SZ")
endTime = dt.strptime(end, "%Y-%m-%dT%H:%M:%SZ")
diff = endTime - startTime
interval_secs = -1
if(interval == '1m'):
interval_secs = 60
elif(interval == '5m'):
interval_secs = 300
elif(interval == '30m'):
interval_secs = 1800
elif(interval == '1h'):
interval_secs = 3600
elif(interval == '3h'):
interval_secs = 10800
elif(interval == '6h'):
interval_secs = 21600
elif(interval == '12h'):
interval_secs = 43200
elif(interval == '24h'):
interval_secs = 86400
return diff.total_seconds() / interval_secs
def asciiReturn(frame, sep, symbols, site, inst, epoch):
output = StringIO()
......@@ -469,7 +507,7 @@ def modifyData(fmt, begin, end, site, inst, symbols, interval, sep, callback, js
interval = handleInterval(interval)
if(interval == -1):
return 500
return 'interval_error'
unpack = symbolsHandler(site, inst, symbols)
......@@ -489,6 +527,9 @@ def modifyData(fmt, begin, end, site, inst, symbols, interval, sep, callback, js
if(not sep):
sep = ','
if(checkRecords(begin, end, interval) > Util.limit):
return 413
result = query(site, inst, symbols, begin, end, interval, epoch)
frame = handleResult(fmt,result, symbols, interval, sep, windSpeed, windDirection)
......
from modifyData import beginEndHandler, handleInterval, siteHandler, instHandler, symbolsHandler, handleResult
from modifyData import beginEndHandler, handleInterval, siteHandler, instHandler, symbolsHandler, handleResult, checkRecords
from queryInflux import query
from xml.dom.minidom import Document
from io import StringIO
......@@ -26,7 +26,10 @@ def modifyData(fmt, begin, end, site, inst, symbols, interval, sep, callback, ep
interval = handleInterval(interval)
if(interval == -1):
return 500
return 'interval_error'
if(checkRecords(begin, end, interval) > Util.limit):
return 413
unpack = symbolsHandler(site, inst, symbols)
......@@ -35,7 +38,7 @@ def modifyData(fmt, begin, end, site, inst, symbols, interval, sep, callback, ep
return 404
if(not unpack[0]):
return 500
return 'unrecognizedSymbol'
symbols = unpack[0]
windSpeed = unpack[1]
......
......@@ -30,10 +30,10 @@ def handleIntError(statusNo, fmt):
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_500), 500
if(statusNo == 400):
elif(statusNo == 400):
return render_template('400.html', format=fmt), 400
if(statusNo == 404):
elif(statusNo == 404):
if(fmt == 'json'):
return jsonify(**Util.json_404), 404
......@@ -46,6 +46,19 @@ def handleIntError(statusNo, fmt):
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_404), 404
elif(statusNo == 413):
if(fmt == 'json'):
return jsonify(**Util.json_413), 413
if(fmt == 'ascii'):
return Util.ascii_413, 413
if(fmt == 'xml'):
return Response(Util.xml_413, mimetype='text/xml'), 413
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_413), 413
def handleStringError(statusStr, fmt):
if(statusStr == '400_stamp'):
if(fmt == 'json'):
......@@ -85,7 +98,20 @@ def handleStringError(statusStr, fmt):
return Response(Util.xml_symbol, mimetype='text/xml'), 400
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_symbol), 400
return modifyData.jsonpReturn(Util.json_symbol), 400
elif(statusStr == 'interval_error'):
if fmt == 'json':
return jsonify(**Util.json_interval), 400
if(fmt == 'ascii'):
return Util.ascii_interval, 400
if(fmt == 'xml'):
return Util.xml_interval, 400
if(fmt == 'jsonp'):
return modifyData.jsonpReturn(Util.json_interval), 400
else:
return statusStr
......@@ -210,7 +236,9 @@ def handleSingleSite(fmt):
else:
return render_template('400.html'), 400
# @app.route('/')
@app.route('/api/')
def createIndex():
return render_template('index.html')
# TODO: Add documentation on main/index page
@app.errorhandler(404)
......@@ -234,7 +262,7 @@ def test(fmt):
@app.after_request
def apply_header(response):
response.headers["X-Metobs-API-VERSION"] = 1.0
response.headers[Util.api_version_header] = Util.api_version
return response
......
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Metobs Data Provider</title>
<!-- <link rel="stylesheet" href="style.css"> -->
</head>
<body>
<div class="container-fluid">
</div>
<!-- <script type="text/javascript" src="index.js"></script> -->
</body>
\ 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