Browse Source

server: consolidate time processing and checks

tags/nilmdb-1.4.6^0
Jim Paris 11 years ago
parent
commit
5292319802
1 changed files with 19 additions and 32 deletions
  1. +19
    -32
      nilmdb/server/server.py

+ 19
- 32
nilmdb/server/server.py View File

@@ -174,6 +174,21 @@ class Root(NilmApp):
class Stream(NilmApp):
"""Stream-specific operations"""

# Helpers
def _get_times(self, start_param, end_param):
(start, end) = (None, None)
if start_param is not None:
start = string_to_timestamp(start_param)
if end_param is not None:
end = string_to_timestamp(end_param)
if start is not None and end is not None:
if start >= end:
raise cherrypy.HTTPError(
"400 Bad Request",
sprintf("start must precede end (%s >= %s)",
start_param, end_param))
return (start, end)

# /stream/list
# /stream/list?layout=float32_8
# /stream/list?path=/newton/prep&extended=1
@@ -307,11 +322,7 @@ class Stream(NilmApp):
raise cherrypy.HTTPError("404 Not Found", "No such stream")

# Check limits
start = string_to_timestamp(start)
end = string_to_timestamp(end)
if start >= end:
raise cherrypy.HTTPError("400 Bad Request",
"start must precede end")
(start, end) = self._get_times(start, end)

# Pass the data directly to nilmdb, which will parse it and
# raise a ValueError if there are any problems.
@@ -333,14 +344,7 @@ class Stream(NilmApp):
the interval [start, end). Returns the number of data points
removed.
"""
if start is not None:
start = string_to_timestamp(start)
if end is not None:
end = string_to_timestamp(end)
if start is not None and end is not None:
if start >= end:
raise cherrypy.HTTPError("400 Bad Request",
"start must precede end")
(start, end) = self._get_times(start, end)
total_removed = 0
while True:
(removed, restart) = self.db.stream_remove(path, start, end)
@@ -371,15 +375,7 @@ class Stream(NilmApp):
Note that the response type is the non-standard
'application/x-json-stream' for lack of a better option.
"""
if start is not None:
start = string_to_timestamp(start)
if end is not None:
end = string_to_timestamp(end)

if start is not None and end is not None:
if start >= end:
raise cherrypy.HTTPError("400 Bad Request",
"start must precede end")
(start, end) = self._get_times(start, end)

if len(self.db.stream_list(path = path)) != 1:
raise cherrypy.HTTPError("404", "No such stream: " + path)
@@ -417,16 +413,7 @@ class Stream(NilmApp):
If 'markup' is True, adds comments to the stream denoting each
interval's start and end timestamp.
"""
if start is not None:
start = string_to_timestamp(start)
if end is not None:
end = string_to_timestamp(end)

# Check parameters
if start is not None and end is not None:
if start >= end:
raise cherrypy.HTTPError("400 Bad Request",
"start must precede end")
(start, end) = self._get_times(start, end)

# Check path and get layout
streams = self.db.stream_list(path = path)


Loading…
Cancel
Save