Compare commits
66 Commits
nilmdb-1.5
...
nilmdb-1.9
Author | SHA1 | Date | |
---|---|---|---|
04f815a24b | |||
6868f5f126 | |||
ca0943ec19 | |||
68addb4e4a | |||
68c33b1f14 | |||
8dd8741100 | |||
8e6341ae5d | |||
422b1e2df2 | |||
0f745b3047 | |||
71cd7ed9b7 | |||
a79d6104d5 | |||
8e8ec59e30 | |||
b89b945a0f | |||
bd7bdb2eb8 | |||
840cd2fd13 | |||
bbd59c8b50 | |||
405c110fd7 | |||
274adcd856 | |||
a1850c9c2c | |||
6cd28b67b1 | |||
d6d215d53d | |||
e02143ddb2 | |||
e275384d03 | |||
a6a67ec15c | |||
fc43107307 | |||
90633413bb | |||
c7c3aff0fb | |||
e2347c954e | |||
222a5c6c53 | |||
1ca2c143e5 | |||
b5df575c79 | |||
2768a5ad15 | |||
a105543c38 | |||
309f38d0ed | |||
9a27b6ef6a | |||
99532cf9e0 | |||
dfdd0e5c74 | |||
9a2699adfc | |||
9bbb95b18b | |||
6bbed322c5 | |||
2317894355 | |||
539c92226c | |||
77c766d85d | |||
49d04db1d6 | |||
ea838d05ae | |||
f2a48bdb2a | |||
6d14e0b8aa | |||
b31b9327b9 | |||
b98ff1331a | |||
00e6ba1124 | |||
01029230c9 | |||
ecc4e5ef9d | |||
23f31c472b | |||
a1e2746360 | |||
1c40d59a52 | |||
bfb09a189f | |||
416a499866 | |||
637d193807 | |||
b7fa5745ce | |||
0104c8edd9 | |||
cf3b8e787d | |||
83d022016c | |||
43b740ecaa | |||
4ce059b920 | |||
99a4228285 | |||
230ec72609 |
@@ -7,4 +7,4 @@
|
|||||||
exclude_lines =
|
exclude_lines =
|
||||||
pragma: no cover
|
pragma: no cover
|
||||||
if 0:
|
if 0:
|
||||||
omit = nilmdb/utils/datetime_tz*,nilmdb/scripts,nilmdb/_version.py
|
omit = nilmdb/utils/datetime_tz*,nilmdb/scripts,nilmdb/_version.py,nilmdb/fsck
|
||||||
|
6
Makefile
6
Makefile
@@ -1,5 +1,5 @@
|
|||||||
# By default, run the tests.
|
# By default, run the tests.
|
||||||
all: test
|
all: fscktest
|
||||||
|
|
||||||
version:
|
version:
|
||||||
python setup.py version
|
python setup.py version
|
||||||
@@ -23,6 +23,10 @@ docs:
|
|||||||
lint:
|
lint:
|
||||||
pylint --rcfile=.pylintrc nilmdb
|
pylint --rcfile=.pylintrc nilmdb
|
||||||
|
|
||||||
|
fscktest:
|
||||||
|
python -c "import nilmdb.fsck; nilmdb.fsck.Fsck('/home/jim/wsgi/db').check()"
|
||||||
|
# python -c "import nilmdb.fsck; nilmdb.fsck.Fsck('/home/jim/mnt/bucket/mnt/sharon/data/db', True).check()"
|
||||||
|
|
||||||
test:
|
test:
|
||||||
ifeq ($(INSIDE_EMACS), t)
|
ifeq ($(INSIDE_EMACS), t)
|
||||||
# Use the slightly more flexible script
|
# Use the slightly more flexible script
|
||||||
|
@@ -8,7 +8,8 @@ Prerequisites:
|
|||||||
|
|
||||||
# Base NilmDB dependencies
|
# Base NilmDB dependencies
|
||||||
sudo apt-get install python-cherrypy3 python-decorator python-simplejson
|
sudo apt-get install python-cherrypy3 python-decorator python-simplejson
|
||||||
sudo apt-get install python-requests python-dateutil python-tz python-psutil
|
sudo apt-get install python-requests python-dateutil python-tz
|
||||||
|
sudo apt-get install python-progressbar python-psutil
|
||||||
|
|
||||||
# Other dependencies (required by some modules)
|
# Other dependencies (required by some modules)
|
||||||
sudo apt-get install python-numpy
|
sudo apt-get install python-numpy
|
||||||
@@ -26,6 +27,7 @@ Install:
|
|||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
nilmdb-server --help
|
nilmdb-server --help
|
||||||
|
nilmdb-fsck --help
|
||||||
nilmtool --help
|
nilmtool --help
|
||||||
|
|
||||||
See docs/wsgi.md for info on setting up a WSGI application in Apache.
|
See docs/wsgi.md for info on setting up a WSGI application in Apache.
|
||||||
|
@@ -421,3 +421,20 @@ and has all of the same functions. It adds three new functions:
|
|||||||
It is significantly faster! It is about 20 times faster to decimate a
|
It is significantly faster! It is about 20 times faster to decimate a
|
||||||
stream with `nilm-decimate` when the filter code is using the new
|
stream with `nilm-decimate` when the filter code is using the new
|
||||||
binary/numpy interface.
|
binary/numpy interface.
|
||||||
|
|
||||||
|
|
||||||
|
WSGI interface & chunked requests
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
|
mod_wsgi requires "WSGIChunkedRequest On" to handle
|
||||||
|
"Transfer-encoding: Chunked" requests. However, `/stream/insert`
|
||||||
|
doesn't handle this correctly right now, because:
|
||||||
|
|
||||||
|
- The `cherrpy.request.body.read()` call needs to be fixed for chunked requests
|
||||||
|
|
||||||
|
- We don't want to just buffer endlessly in the server, and it will
|
||||||
|
require some thought on how to handle data in chunks (what to do about
|
||||||
|
interval endpoints).
|
||||||
|
|
||||||
|
It is probably better to just keep the endpoint management on the client
|
||||||
|
side, so leave "WSGIChunkedRequest off" for now.
|
||||||
|
@@ -19,12 +19,12 @@ Then, set up Apache with a configuration like:
|
|||||||
|
|
||||||
<VirtualHost>
|
<VirtualHost>
|
||||||
WSGIScriptAlias /nilmdb /home/nilm/nilmdb.wsgi
|
WSGIScriptAlias /nilmdb /home/nilm/nilmdb.wsgi
|
||||||
WSGIApplicationGroup nilmdb-appgroup
|
|
||||||
WSGIProcessGroup nilmdb-procgroup
|
|
||||||
WSGIDaemonProcess nilmdb-procgroup threads=32 user=nilm group=nilm
|
WSGIDaemonProcess nilmdb-procgroup threads=32 user=nilm group=nilm
|
||||||
|
|
||||||
# Access control example:
|
|
||||||
<Location /nilmdb>
|
<Location /nilmdb>
|
||||||
|
WSGIProcessGroup nilmdb-procgroup
|
||||||
|
WSGIApplicationGroup nilmdb-appgroup
|
||||||
|
|
||||||
|
# Access control example:
|
||||||
Order deny,allow
|
Order deny,allow
|
||||||
Deny from all
|
Deny from all
|
||||||
Allow from 1.2.3.4
|
Allow from 1.2.3.4
|
||||||
|
50
extras/fix-oversize-files.py
Normal file
50
extras/fix-oversize-files.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import cPickle as pickle
|
||||||
|
import argparse
|
||||||
|
import fcntl
|
||||||
|
import re
|
||||||
|
from nilmdb.client.numpyclient import layout_to_dtype
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description = """
|
||||||
|
Fix database corruption where binary writes caused too much data to be
|
||||||
|
written to the file. Truncates files to the correct length. This was
|
||||||
|
fixed by b98ff1331a515ad47fd3203615e835b529b039f9.
|
||||||
|
""")
|
||||||
|
parser.add_argument("path", action="store", help='Database root path')
|
||||||
|
parser.add_argument("-y", "--yes", action="store_true", help='Fix them')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
lock = os.path.join(args.path, "data.lock")
|
||||||
|
with open(lock, "w") as f:
|
||||||
|
fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
|
|
||||||
|
fix = {}
|
||||||
|
|
||||||
|
for (path, dirs, files) in os.walk(args.path):
|
||||||
|
if "_format" in files:
|
||||||
|
with open(os.path.join(path, "_format")) as format:
|
||||||
|
fmt = pickle.load(format)
|
||||||
|
rowsize = layout_to_dtype(fmt["layout"]).itemsize
|
||||||
|
maxsize = rowsize * fmt["rows_per_file"]
|
||||||
|
fix[path] = maxsize
|
||||||
|
if maxsize < 128000000: # sanity check
|
||||||
|
raise Exception("bad maxsize " + str(maxsize))
|
||||||
|
|
||||||
|
for fixpath in fix:
|
||||||
|
for (path, dirs, files) in os.walk(fixpath):
|
||||||
|
for fn in files:
|
||||||
|
if not re.match("^[0-9a-f]{4,}$", fn):
|
||||||
|
continue
|
||||||
|
fn = os.path.join(path, fn)
|
||||||
|
size = os.path.getsize(fn)
|
||||||
|
maxsize = fix[fixpath]
|
||||||
|
if size > maxsize:
|
||||||
|
diff = size - maxsize
|
||||||
|
print diff, "too big:", fn
|
||||||
|
if args.yes:
|
||||||
|
with open(fn, "a+") as dbfile:
|
||||||
|
dbfile.truncate(maxsize)
|
@@ -6,7 +6,6 @@ import nilmdb.utils
|
|||||||
import nilmdb.client.httpclient
|
import nilmdb.client.httpclient
|
||||||
from nilmdb.client.errors import ClientError
|
from nilmdb.client.errors import ClientError
|
||||||
|
|
||||||
import re
|
|
||||||
import time
|
import time
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
import contextlib
|
import contextlib
|
||||||
@@ -59,6 +58,11 @@ class Client(object):
|
|||||||
return self.http.get("dbinfo")
|
return self.http.get("dbinfo")
|
||||||
|
|
||||||
def stream_list(self, path = None, layout = None, extended = False):
|
def stream_list(self, path = None, layout = None, extended = False):
|
||||||
|
"""Return a sorted list of [path, layout] lists. If 'path' or
|
||||||
|
'layout' are specified, only return streams that match those
|
||||||
|
exact values. If 'extended' is True, the returned lists have
|
||||||
|
extended info, e.g.: [path, layout, extent_min, extent_max,
|
||||||
|
total_rows, total_seconds."""
|
||||||
params = {}
|
params = {}
|
||||||
if path is not None:
|
if path is not None:
|
||||||
params["path"] = path
|
params["path"] = path
|
||||||
@@ -66,14 +70,11 @@ class Client(object):
|
|||||||
params["layout"] = layout
|
params["layout"] = layout
|
||||||
if extended:
|
if extended:
|
||||||
params["extended"] = 1
|
params["extended"] = 1
|
||||||
def sort_streams_nicely(x):
|
streams = self.http.get("stream/list", params)
|
||||||
"""Human-friendly sort (/stream/2 before /stream/10)"""
|
return nilmdb.utils.sort.sort_human(streams, key = lambda s: s[0])
|
||||||
num = lambda t: int(t) if t.isdigit() else t
|
|
||||||
key = lambda k: [ num(c) for c in re.split('([0-9]+)', k[0]) ]
|
|
||||||
return sorted(x, key = key)
|
|
||||||
return sort_streams_nicely(self.http.get("stream/list", params))
|
|
||||||
|
|
||||||
def stream_get_metadata(self, path, keys = None):
|
def stream_get_metadata(self, path, keys = None):
|
||||||
|
"""Get stream metadata"""
|
||||||
params = { "path": path }
|
params = { "path": path }
|
||||||
if keys is not None:
|
if keys is not None:
|
||||||
params["key"] = keys
|
params["key"] = keys
|
||||||
@@ -122,7 +123,10 @@ class Client(object):
|
|||||||
params["start"] = timestamp_to_string(start)
|
params["start"] = timestamp_to_string(start)
|
||||||
if end is not None:
|
if end is not None:
|
||||||
params["end"] = timestamp_to_string(end)
|
params["end"] = timestamp_to_string(end)
|
||||||
return self.http.post("stream/remove", params)
|
total = 0
|
||||||
|
for count in self.http.post_gen("stream/remove", params):
|
||||||
|
total += int(count)
|
||||||
|
return total
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def stream_insert_context(self, path, start = None, end = None):
|
def stream_insert_context(self, path, start = None, end = None):
|
||||||
@@ -146,6 +150,7 @@ class Client(object):
|
|||||||
ctx = StreamInserter(self, path, start, end)
|
ctx = StreamInserter(self, path, start, end)
|
||||||
yield ctx
|
yield ctx
|
||||||
ctx.finalize()
|
ctx.finalize()
|
||||||
|
ctx.destroy()
|
||||||
|
|
||||||
def stream_insert(self, path, data, start = None, end = None):
|
def stream_insert(self, path, data, start = None, end = None):
|
||||||
"""Insert rows of data into a stream. data should be a string
|
"""Insert rows of data into a stream. data should be a string
|
||||||
@@ -295,6 +300,15 @@ class StreamInserter(object):
|
|||||||
self._block_data = []
|
self._block_data = []
|
||||||
self._block_len = 0
|
self._block_len = 0
|
||||||
|
|
||||||
|
self.destroyed = False
|
||||||
|
|
||||||
|
def destroy(self):
|
||||||
|
"""Ensure this object can't be used again without raising
|
||||||
|
an error"""
|
||||||
|
def error(*args, **kwargs):
|
||||||
|
raise Exception("don't reuse this context object")
|
||||||
|
self._send_block = self.insert = self.finalize = self.send = error
|
||||||
|
|
||||||
def insert(self, data):
|
def insert(self, data):
|
||||||
"""Insert a chunk of ASCII formatted data in string form. The
|
"""Insert a chunk of ASCII formatted data in string form. The
|
||||||
overall data must consist of lines terminated by '\\n'."""
|
overall data must consist of lines terminated by '\\n'."""
|
||||||
@@ -441,7 +455,7 @@ class StreamInserter(object):
|
|||||||
self._interval_start = end_ts
|
self._interval_start = end_ts
|
||||||
|
|
||||||
# Double check endpoints
|
# Double check endpoints
|
||||||
if start_ts is None or end_ts is None:
|
if (start_ts is None or end_ts is None) or (start_ts == end_ts):
|
||||||
# If the block has no non-comment lines, it's OK
|
# If the block has no non-comment lines, it's OK
|
||||||
try:
|
try:
|
||||||
self._get_first_noncomment(block)
|
self._get_first_noncomment(block)
|
||||||
|
@@ -123,19 +123,50 @@ class HTTPClient(object):
|
|||||||
"""
|
"""
|
||||||
(response, isjson) = self._do_req(method, url, query, body,
|
(response, isjson) = self._do_req(method, url, query, body,
|
||||||
stream = True, headers = headers)
|
stream = True, headers = headers)
|
||||||
|
|
||||||
|
# Like the iter_lines function in Requests, but only splits on
|
||||||
|
# the specified line ending.
|
||||||
|
def lines(source, ending):
|
||||||
|
pending = None
|
||||||
|
for chunk in source:
|
||||||
|
if pending is not None:
|
||||||
|
chunk = pending + chunk
|
||||||
|
tmp = chunk.split(ending)
|
||||||
|
lines = tmp[:-1]
|
||||||
|
if chunk.endswith(ending):
|
||||||
|
pending = None
|
||||||
|
else:
|
||||||
|
pending = tmp[-1]
|
||||||
|
for line in lines:
|
||||||
|
yield line
|
||||||
|
if pending is not None: # pragma: no cover (missing newline)
|
||||||
|
yield pending
|
||||||
|
|
||||||
|
# Yield the chunks or lines as requested
|
||||||
if binary:
|
if binary:
|
||||||
for chunk in response.iter_content(chunk_size = 65536):
|
for chunk in response.iter_content(chunk_size = 65536):
|
||||||
yield chunk
|
yield chunk
|
||||||
elif isjson:
|
elif isjson:
|
||||||
for line in response.iter_lines():
|
for line in lines(response.iter_content(chunk_size = 1),
|
||||||
|
ending = '\r\n'):
|
||||||
yield json.loads(line)
|
yield json.loads(line)
|
||||||
else:
|
else:
|
||||||
for line in response.iter_lines():
|
for line in lines(response.iter_content(chunk_size = 65536),
|
||||||
|
ending = '\n'):
|
||||||
yield line
|
yield line
|
||||||
|
|
||||||
def get_gen(self, url, params = None, binary = False):
|
def get_gen(self, url, params = None, binary = False):
|
||||||
"""Simple GET (parameters in URL) returning a generator"""
|
"""Simple GET (parameters in URL) returning a generator"""
|
||||||
return self._req_gen("GET", url, params, binary = binary)
|
return self._req_gen("GET", url, params, binary = binary)
|
||||||
|
|
||||||
|
def post_gen(self, url, params = None):
|
||||||
|
"""Simple POST (parameters in body) returning a generator"""
|
||||||
|
if self.post_json:
|
||||||
|
return self._req_gen("POST", url, None,
|
||||||
|
json.dumps(params),
|
||||||
|
{ 'Content-type': 'application/json' })
|
||||||
|
else:
|
||||||
|
return self._req_gen("POST", url, None, params)
|
||||||
|
|
||||||
# Not much use for a POST or PUT generator, since they don't
|
# Not much use for a POST or PUT generator, since they don't
|
||||||
# return much data.
|
# return much data.
|
||||||
|
@@ -98,6 +98,7 @@ class NumpyClient(nilmdb.client.client.Client):
|
|||||||
ctx = StreamInserterNumpy(self, path, start, end, dtype)
|
ctx = StreamInserterNumpy(self, path, start, end, dtype)
|
||||||
yield ctx
|
yield ctx
|
||||||
ctx.finalize()
|
ctx.finalize()
|
||||||
|
ctx.destroy()
|
||||||
|
|
||||||
def stream_insert_numpy(self, path, data, start = None, end = None,
|
def stream_insert_numpy(self, path, data, start = None, end = None,
|
||||||
layout = None):
|
layout = None):
|
||||||
@@ -133,16 +134,8 @@ class StreamInserterNumpy(nilmdb.client.client.StreamInserter):
|
|||||||
contiguous interval and may be None. 'dtype' is the Numpy
|
contiguous interval and may be None. 'dtype' is the Numpy
|
||||||
dtype for this stream.
|
dtype for this stream.
|
||||||
"""
|
"""
|
||||||
self.last_response = None
|
super(StreamInserterNumpy, self).__init__(client, path, start, end)
|
||||||
|
|
||||||
self._dtype = dtype
|
self._dtype = dtype
|
||||||
self._client = client
|
|
||||||
self._path = path
|
|
||||||
|
|
||||||
# Start and end for the overall contiguous interval we're
|
|
||||||
# filling
|
|
||||||
self._interval_start = start
|
|
||||||
self._interval_end = end
|
|
||||||
|
|
||||||
# Max rows to send at once
|
# Max rows to send at once
|
||||||
self._max_rows = self._max_data // self._dtype.itemsize
|
self._max_rows = self._max_data // self._dtype.itemsize
|
||||||
@@ -162,9 +155,12 @@ class StreamInserterNumpy(nilmdb.client.client.StreamInserter):
|
|||||||
elif array.ndim == 2:
|
elif array.ndim == 2:
|
||||||
# Convert to structured array
|
# Convert to structured array
|
||||||
sarray = numpy.zeros(array.shape[0], dtype=self._dtype)
|
sarray = numpy.zeros(array.shape[0], dtype=self._dtype)
|
||||||
sarray['timestamp'] = array[:,0]
|
try:
|
||||||
# Need the squeeze in case sarray['data'] is 1 dimensional
|
sarray['timestamp'] = array[:,0]
|
||||||
sarray['data'] = numpy.squeeze(array[:,1:])
|
# Need the squeeze in case sarray['data'] is 1 dimensional
|
||||||
|
sarray['data'] = numpy.squeeze(array[:,1:])
|
||||||
|
except (IndexError, ValueError):
|
||||||
|
raise ValueError("wrong number of fields for this data type")
|
||||||
array = sarray
|
array = sarray
|
||||||
else:
|
else:
|
||||||
raise ValueError("wrong number of dimensions in array")
|
raise ValueError("wrong number of dimensions in array")
|
||||||
@@ -247,9 +243,12 @@ class StreamInserterNumpy(nilmdb.client.client.StreamInserter):
|
|||||||
# Next block continues where this one ended
|
# Next block continues where this one ended
|
||||||
self._interval_start = end_ts
|
self._interval_start = end_ts
|
||||||
|
|
||||||
# If we have no endpoints, it's because we had no data to send.
|
# If we have no endpoints, or equal endpoints, it's OK as long
|
||||||
if start_ts is None or end_ts is None:
|
# as there's no data to send
|
||||||
return
|
if (start_ts is None or end_ts is None) or (start_ts == end_ts):
|
||||||
|
if len(array) == 0:
|
||||||
|
return
|
||||||
|
raise ClientError("have data to send, but invalid start/end times")
|
||||||
|
|
||||||
# Send it
|
# Send it
|
||||||
data = array.tostring()
|
data = array.tostring()
|
||||||
|
@@ -10,6 +10,7 @@ import sys
|
|||||||
import os
|
import os
|
||||||
import argparse
|
import argparse
|
||||||
from argparse import ArgumentDefaultsHelpFormatter as def_form
|
from argparse import ArgumentDefaultsHelpFormatter as def_form
|
||||||
|
import signal
|
||||||
|
|
||||||
try: # pragma: no cover
|
try: # pragma: no cover
|
||||||
import argcomplete
|
import argcomplete
|
||||||
@@ -18,9 +19,8 @@ except ImportError: # pragma: no cover
|
|||||||
|
|
||||||
# Valid subcommands. Defined in separate files just to break
|
# Valid subcommands. Defined in separate files just to break
|
||||||
# things up -- they're still called with Cmdline as self.
|
# things up -- they're still called with Cmdline as self.
|
||||||
subcommands = [ "help", "info", "create", "list", "metadata",
|
subcommands = [ "help", "info", "create", "rename", "list", "intervals",
|
||||||
"insert", "extract", "remove", "destroy",
|
"metadata", "insert", "extract", "remove", "destroy" ]
|
||||||
"intervals", "rename" ]
|
|
||||||
|
|
||||||
# Import the subcommand modules
|
# Import the subcommand modules
|
||||||
subcmd_mods = {}
|
subcmd_mods = {}
|
||||||
@@ -28,6 +28,14 @@ for cmd in subcommands:
|
|||||||
subcmd_mods[cmd] = __import__("nilmdb.cmdline." + cmd, fromlist = [ cmd ])
|
subcmd_mods[cmd] = __import__("nilmdb.cmdline." + cmd, fromlist = [ cmd ])
|
||||||
|
|
||||||
class JimArgumentParser(argparse.ArgumentParser):
|
class JimArgumentParser(argparse.ArgumentParser):
|
||||||
|
def parse_args(self, args=None, namespace=None):
|
||||||
|
# Look for --version anywhere and change it to just "nilmtool
|
||||||
|
# --version". This makes "nilmtool cmd --version" work, which
|
||||||
|
# is needed by help2man.
|
||||||
|
if "--version" in (args or sys.argv[1:]):
|
||||||
|
args = [ "--version" ]
|
||||||
|
return argparse.ArgumentParser.parse_args(self, args, namespace)
|
||||||
|
|
||||||
def error(self, message):
|
def error(self, message):
|
||||||
self.print_usage(sys.stderr)
|
self.print_usage(sys.stderr)
|
||||||
self.exit(2, sprintf("error: %s\n", message))
|
self.exit(2, sprintf("error: %s\n", message))
|
||||||
@@ -71,15 +79,27 @@ class Complete(object): # pragma: no cover
|
|||||||
path = parsed_args.path
|
path = parsed_args.path
|
||||||
if not path:
|
if not path:
|
||||||
return []
|
return []
|
||||||
return ( self.escape(k + '=' + v)
|
results = []
|
||||||
for (k,v) in client.stream_get_metadata(path).iteritems()
|
# prefix comes in as UTF-8, but results need to be Unicode,
|
||||||
if k.startswith(prefix) )
|
# weird. Still doesn't work in all cases, but that's bugs in
|
||||||
|
# argcomplete.
|
||||||
|
prefix = nilmdb.utils.unicode.decode(prefix)
|
||||||
|
for (k,v) in client.stream_get_metadata(path).iteritems():
|
||||||
|
kv = self.escape(k + '=' + v)
|
||||||
|
if kv.startswith(prefix):
|
||||||
|
results.append(kv)
|
||||||
|
return results
|
||||||
|
|
||||||
class Cmdline(object):
|
class Cmdline(object):
|
||||||
|
|
||||||
def __init__(self, argv = None):
|
def __init__(self, argv = None):
|
||||||
self.argv = argv or sys.argv[1:]
|
self.argv = argv or sys.argv[1:]
|
||||||
|
try:
|
||||||
|
# Assume command line arguments are encoded with stdin's encoding,
|
||||||
|
# and reverse it. Won't be needed in Python 3, but for now..
|
||||||
|
self.argv = [ x.decode(sys.stdin.encoding) for x in self.argv ]
|
||||||
|
except Exception: # pragma: no cover
|
||||||
|
pass
|
||||||
self.client = None
|
self.client = None
|
||||||
self.def_url = os.environ.get("NILMDB_URL", "http://localhost/nilmdb/")
|
self.def_url = os.environ.get("NILMDB_URL", "http://localhost/nilmdb/")
|
||||||
self.subcmd = {}
|
self.subcmd = {}
|
||||||
@@ -126,6 +146,13 @@ class Cmdline(object):
|
|||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
# Set SIGPIPE to its default handler -- we don't need Python
|
||||||
|
# to catch it for us.
|
||||||
|
try:
|
||||||
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
||||||
|
except ValueError: # pragma: no cover
|
||||||
|
pass
|
||||||
|
|
||||||
# Clear cached timezone, so that we can pick up timezone changes
|
# Clear cached timezone, so that we can pick up timezone changes
|
||||||
# while running this from the test suite.
|
# while running this from the test suite.
|
||||||
datetime_tz._localtz = None
|
datetime_tz._localtz = None
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
from nilmdb.utils.printf import *
|
from nilmdb.utils.printf import *
|
||||||
import nilmdb.client
|
import nilmdb.client
|
||||||
|
import fnmatch
|
||||||
|
|
||||||
from argparse import ArgumentDefaultsHelpFormatter as def_form
|
from argparse import ArgumentDefaultsHelpFormatter as def_form
|
||||||
|
|
||||||
@@ -10,25 +11,39 @@ def setup(self, sub):
|
|||||||
Destroy the stream at the specified path.
|
Destroy the stream at the specified path.
|
||||||
The stream must be empty. All metadata
|
The stream must be empty. All metadata
|
||||||
related to the stream is permanently deleted.
|
related to the stream is permanently deleted.
|
||||||
|
|
||||||
|
Wildcards and multiple paths are supported.
|
||||||
""")
|
""")
|
||||||
cmd.set_defaults(handler = cmd_destroy)
|
cmd.set_defaults(handler = cmd_destroy)
|
||||||
group = cmd.add_argument_group("Options")
|
group = cmd.add_argument_group("Options")
|
||||||
group.add_argument("-R", "--remove", action="store_true",
|
group.add_argument("-R", "--remove", action="store_true",
|
||||||
help="Remove all data before destroying stream")
|
help="Remove all data before destroying stream")
|
||||||
|
group.add_argument("-q", "--quiet", action="store_true",
|
||||||
|
help="Don't display names when destroying "
|
||||||
|
"multiple paths")
|
||||||
group = cmd.add_argument_group("Required arguments")
|
group = cmd.add_argument_group("Required arguments")
|
||||||
group.add_argument("path",
|
group.add_argument("path", nargs='+',
|
||||||
help="Path of the stream to delete, e.g. /foo/bar",
|
help="Path of the stream to delete, e.g. /foo/bar/*",
|
||||||
).completer = self.complete.path
|
).completer = self.complete.path
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
def cmd_destroy(self):
|
def cmd_destroy(self):
|
||||||
"""Destroy stream"""
|
"""Destroy stream"""
|
||||||
if self.args.remove:
|
streams = [ s[0] for s in self.client.stream_list() ]
|
||||||
|
paths = []
|
||||||
|
for path in self.args.path:
|
||||||
|
new = fnmatch.filter(streams, path)
|
||||||
|
if not new:
|
||||||
|
self.die("error: no stream matched path: %s", path)
|
||||||
|
paths.extend(new)
|
||||||
|
|
||||||
|
for path in paths:
|
||||||
|
if not self.args.quiet and len(paths) > 1:
|
||||||
|
printf("Destroying %s\n", path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
count = self.client.stream_remove(self.args.path)
|
if self.args.remove:
|
||||||
|
count = self.client.stream_remove(path)
|
||||||
|
self.client.stream_destroy(path)
|
||||||
except nilmdb.client.ClientError as e:
|
except nilmdb.client.ClientError as e:
|
||||||
self.die("error removing data: %s", str(e))
|
self.die("error destroying stream: %s", str(e))
|
||||||
try:
|
|
||||||
self.client.stream_destroy(self.args.path)
|
|
||||||
except nilmdb.client.ClientError as e:
|
|
||||||
self.die("error destroying stream: %s", str(e))
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
from nilmdb.utils.printf import *
|
from nilmdb.utils.printf import *
|
||||||
import nilmdb.client
|
import nilmdb.client
|
||||||
|
import sys
|
||||||
|
|
||||||
def setup(self, sub):
|
def setup(self, sub):
|
||||||
cmd = sub.add_parser("extract", help="Extract data",
|
cmd = sub.add_parser("extract", help="Extract data",
|
||||||
@@ -24,6 +25,8 @@ def setup(self, sub):
|
|||||||
).completer = self.complete.time
|
).completer = self.complete.time
|
||||||
|
|
||||||
group = cmd.add_argument_group("Output format")
|
group = cmd.add_argument_group("Output format")
|
||||||
|
group.add_argument("-B", "--binary", action="store_true",
|
||||||
|
help="Raw binary output")
|
||||||
group.add_argument("-b", "--bare", action="store_true",
|
group.add_argument("-b", "--bare", action="store_true",
|
||||||
help="Exclude timestamps from output lines")
|
help="Exclude timestamps from output lines")
|
||||||
group.add_argument("-a", "--annotate", action="store_true",
|
group.add_argument("-a", "--annotate", action="store_true",
|
||||||
@@ -42,6 +45,11 @@ def cmd_extract_verify(self):
|
|||||||
if self.args.start > self.args.end:
|
if self.args.start > self.args.end:
|
||||||
self.parser.error("start is after end")
|
self.parser.error("start is after end")
|
||||||
|
|
||||||
|
if self.args.binary:
|
||||||
|
if (self.args.bare or self.args.annotate or self.args.markup or
|
||||||
|
self.args.timestamp_raw or self.args.count):
|
||||||
|
self.parser.error("--binary cannot be combined with other options")
|
||||||
|
|
||||||
def cmd_extract(self):
|
def cmd_extract(self):
|
||||||
streams = self.client.stream_list(self.args.path)
|
streams = self.client.stream_list(self.args.path)
|
||||||
if len(streams) != 1:
|
if len(streams) != 1:
|
||||||
@@ -60,16 +68,23 @@ def cmd_extract(self):
|
|||||||
printf("# end: %s\n", time_string(self.args.end))
|
printf("# end: %s\n", time_string(self.args.end))
|
||||||
|
|
||||||
printed = False
|
printed = False
|
||||||
|
if self.args.binary:
|
||||||
|
printer = sys.stdout.write
|
||||||
|
else:
|
||||||
|
printer = print
|
||||||
|
bare = self.args.bare
|
||||||
|
count = self.args.count
|
||||||
for dataline in self.client.stream_extract(self.args.path,
|
for dataline in self.client.stream_extract(self.args.path,
|
||||||
self.args.start,
|
self.args.start,
|
||||||
self.args.end,
|
self.args.end,
|
||||||
self.args.count,
|
self.args.count,
|
||||||
self.args.markup):
|
self.args.markup,
|
||||||
if self.args.bare and not self.args.count:
|
self.args.binary):
|
||||||
|
if bare and not count:
|
||||||
# Strip timestamp (first element). Doesn't make sense
|
# Strip timestamp (first element). Doesn't make sense
|
||||||
# if we are only returning a count.
|
# if we are only returning a count.
|
||||||
dataline = ' '.join(dataline.split(' ')[1:])
|
dataline = ' '.join(dataline.split(' ')[1:])
|
||||||
print(dataline)
|
printer(dataline)
|
||||||
printed = True
|
printed = True
|
||||||
if not printed:
|
if not printed:
|
||||||
if self.args.annotate:
|
if self.args.annotate:
|
||||||
|
@@ -21,5 +21,8 @@ def cmd_info(self):
|
|||||||
printf("Server URL: %s\n", self.client.geturl())
|
printf("Server URL: %s\n", self.client.geturl())
|
||||||
dbinfo = self.client.dbinfo()
|
dbinfo = self.client.dbinfo()
|
||||||
printf("Server database path: %s\n", dbinfo["path"])
|
printf("Server database path: %s\n", dbinfo["path"])
|
||||||
printf("Server database size: %s\n", human_size(dbinfo["size"]))
|
for (desc, field) in [("used by NilmDB", "size"),
|
||||||
printf("Server database free space: %s\n", human_size(dbinfo["free"]))
|
("used by other", "other"),
|
||||||
|
("reserved", "reserved"),
|
||||||
|
("free", "free")]:
|
||||||
|
printf("Server disk space %s: %s\n", desc, human_size(dbinfo[field]))
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
from nilmdb.utils.printf import *
|
from nilmdb.utils.printf import *
|
||||||
import nilmdb.utils.time
|
import nilmdb.utils.time
|
||||||
|
from nilmdb.utils.interval import Interval
|
||||||
|
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import argparse
|
import argparse
|
||||||
@@ -42,6 +43,8 @@ def setup(self, sub):
|
|||||||
group = cmd.add_argument_group("Misc options")
|
group = cmd.add_argument_group("Misc options")
|
||||||
group.add_argument("-T", "--timestamp-raw", action="store_true",
|
group.add_argument("-T", "--timestamp-raw", action="store_true",
|
||||||
help="Show raw timestamps when printing times")
|
help="Show raw timestamps when printing times")
|
||||||
|
group.add_argument("-o", "--optimize", action="store_true",
|
||||||
|
help="Optimize (merge adjacent) intervals")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
@@ -58,9 +61,16 @@ def cmd_intervals(self):
|
|||||||
time_string = nilmdb.utils.time.timestamp_to_human
|
time_string = nilmdb.utils.time.timestamp_to_human
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for (start, end) in self.client.stream_intervals(
|
intervals = ( Interval(start, end) for (start, end) in
|
||||||
self.args.path, self.args.start, self.args.end, self.args.diff):
|
self.client.stream_intervals(self.args.path,
|
||||||
printf("[ %s -> %s ]\n", time_string(start), time_string(end))
|
self.args.start,
|
||||||
|
self.args.end,
|
||||||
|
self.args.diff) )
|
||||||
|
if self.args.optimize:
|
||||||
|
intervals = nilmdb.utils.interval.optimize(intervals)
|
||||||
|
for i in intervals:
|
||||||
|
printf("[ %s -> %s ]\n", time_string(i.start), time_string(i.end))
|
||||||
|
|
||||||
except nilmdb.client.ClientError as e:
|
except nilmdb.client.ClientError as e:
|
||||||
self.die("error listing intervals: %s", str(e))
|
self.die("error listing intervals: %s", str(e))
|
||||||
|
|
||||||
|
@@ -10,22 +10,16 @@ def setup(self, sub):
|
|||||||
formatter_class = def_form,
|
formatter_class = def_form,
|
||||||
description="""
|
description="""
|
||||||
List streams available in the database,
|
List streams available in the database,
|
||||||
optionally filtering by layout or path. Wildcards
|
optionally filtering by path. Wildcards
|
||||||
are accepted.
|
are accepted; non-matching paths or wildcards
|
||||||
|
are ignored.
|
||||||
""")
|
""")
|
||||||
cmd.set_defaults(verify = cmd_list_verify,
|
cmd.set_defaults(verify = cmd_list_verify,
|
||||||
handler = cmd_list)
|
handler = cmd_list)
|
||||||
|
|
||||||
group = cmd.add_argument_group("Stream filtering")
|
group = cmd.add_argument_group("Stream filtering")
|
||||||
group.add_argument("-p", "--path", metavar="PATH", default="*",
|
group.add_argument("path", metavar="PATH", default=["*"], nargs='*',
|
||||||
help="Match only this path (-p can be omitted)",
|
|
||||||
).completer = self.complete.path
|
).completer = self.complete.path
|
||||||
group.add_argument("path_positional", default="*",
|
|
||||||
nargs="?", help=argparse.SUPPRESS,
|
|
||||||
).completer = self.complete.path
|
|
||||||
group.add_argument("-l", "--layout", default="*",
|
|
||||||
help="Match only this stream layout",
|
|
||||||
).completer = self.complete.layout
|
|
||||||
|
|
||||||
group = cmd.add_argument_group("Interval info")
|
group = cmd.add_argument_group("Interval info")
|
||||||
group.add_argument("-E", "--ext", action="store_true",
|
group.add_argument("-E", "--ext", action="store_true",
|
||||||
@@ -49,20 +43,12 @@ def setup(self, sub):
|
|||||||
group = cmd.add_argument_group("Misc options")
|
group = cmd.add_argument_group("Misc options")
|
||||||
group.add_argument("-T", "--timestamp-raw", action="store_true",
|
group.add_argument("-T", "--timestamp-raw", action="store_true",
|
||||||
help="Show raw timestamps when printing times")
|
help="Show raw timestamps when printing times")
|
||||||
|
group.add_argument("-l", "--layout", action="store_true",
|
||||||
|
help="Show layout type next to path name")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
def cmd_list_verify(self):
|
def cmd_list_verify(self):
|
||||||
# A hidden "path_positional" argument lets the user leave off the
|
|
||||||
# "-p" when specifying the path. Handle it here.
|
|
||||||
got_opt = self.args.path != "*"
|
|
||||||
got_pos = self.args.path_positional != "*"
|
|
||||||
if got_pos:
|
|
||||||
if got_opt:
|
|
||||||
self.parser.error("too many paths specified")
|
|
||||||
else:
|
|
||||||
self.args.path = self.args.path_positional
|
|
||||||
|
|
||||||
if self.args.start is not None and self.args.end is not None:
|
if self.args.start is not None and self.args.end is not None:
|
||||||
if self.args.start >= self.args.end:
|
if self.args.start >= self.args.end:
|
||||||
self.parser.error("start must precede end")
|
self.parser.error("start must precede end")
|
||||||
@@ -80,29 +66,33 @@ def cmd_list(self):
|
|||||||
else:
|
else:
|
||||||
time_string = nilmdb.utils.time.timestamp_to_human
|
time_string = nilmdb.utils.time.timestamp_to_human
|
||||||
|
|
||||||
for stream in streams:
|
for argpath in self.args.path:
|
||||||
(path, layout, int_min, int_max, rows, time) = stream[:6]
|
for stream in streams:
|
||||||
if not (fnmatch.fnmatch(path, self.args.path) and
|
(path, layout, int_min, int_max, rows, time) = stream[:6]
|
||||||
fnmatch.fnmatch(layout, self.args.layout)):
|
if not fnmatch.fnmatch(path, argpath):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
printf("%s %s\n", path, layout)
|
if self.args.layout:
|
||||||
|
printf("%s %s\n", path, layout)
|
||||||
if self.args.ext:
|
|
||||||
if int_min is None or int_max is None:
|
|
||||||
printf(" interval extents: (no data)\n")
|
|
||||||
else:
|
else:
|
||||||
printf(" interval extents: %s -> %s\n",
|
printf("%s\n", path)
|
||||||
time_string(int_min), time_string(int_max))
|
|
||||||
printf(" total data: %d rows, %.6f seconds\n",
|
|
||||||
rows or 0,
|
|
||||||
nilmdb.utils.time.timestamp_to_seconds(time or 0))
|
|
||||||
|
|
||||||
if self.args.detail:
|
if self.args.ext:
|
||||||
printed = False
|
if int_min is None or int_max is None:
|
||||||
for (start, end) in self.client.stream_intervals(
|
printf(" interval extents: (no data)\n")
|
||||||
path, self.args.start, self.args.end):
|
else:
|
||||||
printf(" [ %s -> %s ]\n", time_string(start), time_string(end))
|
printf(" interval extents: %s -> %s\n",
|
||||||
printed = True
|
time_string(int_min), time_string(int_max))
|
||||||
if not printed:
|
printf(" total data: %d rows, %.6f seconds\n",
|
||||||
printf(" (no intervals)\n")
|
rows or 0,
|
||||||
|
nilmdb.utils.time.timestamp_to_seconds(time or 0))
|
||||||
|
|
||||||
|
if self.args.detail:
|
||||||
|
printed = False
|
||||||
|
for (start, end) in self.client.stream_intervals(
|
||||||
|
path, self.args.start, self.args.end):
|
||||||
|
printf(" [ %s -> %s ]\n",
|
||||||
|
time_string(start), time_string(end))
|
||||||
|
printed = True
|
||||||
|
if not printed:
|
||||||
|
printf(" (no intervals)\n")
|
||||||
|
@@ -41,10 +41,10 @@ def cmd_metadata(self):
|
|||||||
if self.args.set is not None or self.args.update is not None:
|
if self.args.set is not None or self.args.update is not None:
|
||||||
# Either set, or update
|
# Either set, or update
|
||||||
if self.args.set is not None:
|
if self.args.set is not None:
|
||||||
keyvals = self.args.set
|
keyvals = map(nilmdb.utils.unicode.decode, self.args.set)
|
||||||
handler = self.client.stream_set_metadata
|
handler = self.client.stream_set_metadata
|
||||||
else:
|
else:
|
||||||
keyvals = self.args.update
|
keyvals = map(nilmdb.utils.unicode.decode, self.args.update)
|
||||||
handler = self.client.stream_update_metadata
|
handler = self.client.stream_update_metadata
|
||||||
|
|
||||||
# Extract key=value pairs
|
# Extract key=value pairs
|
||||||
@@ -62,7 +62,9 @@ def cmd_metadata(self):
|
|||||||
self.die("error setting/updating metadata: %s", str(e))
|
self.die("error setting/updating metadata: %s", str(e))
|
||||||
elif self.args.delete is not None:
|
elif self.args.delete is not None:
|
||||||
# Delete (by setting values to empty strings)
|
# Delete (by setting values to empty strings)
|
||||||
keys = self.args.delete or None
|
keys = None
|
||||||
|
if self.args.delete:
|
||||||
|
keys = map(nilmdb.utils.unicode.decode, self.args.delete)
|
||||||
try:
|
try:
|
||||||
data = self.client.stream_get_metadata(self.args.path, keys)
|
data = self.client.stream_get_metadata(self.args.path, keys)
|
||||||
for key in data:
|
for key in data:
|
||||||
@@ -72,7 +74,9 @@ def cmd_metadata(self):
|
|||||||
self.die("error deleting metadata: %s", str(e))
|
self.die("error deleting metadata: %s", str(e))
|
||||||
else:
|
else:
|
||||||
# Get (or unspecified)
|
# Get (or unspecified)
|
||||||
keys = self.args.get or None
|
keys = None
|
||||||
|
if self.args.get:
|
||||||
|
keys = map(nilmdb.utils.unicode.decode, self.args.get)
|
||||||
try:
|
try:
|
||||||
data = self.client.stream_get_metadata(self.args.path, keys)
|
data = self.client.stream_get_metadata(self.args.path, keys)
|
||||||
except nilmdb.client.ClientError as e:
|
except nilmdb.client.ClientError as e:
|
||||||
@@ -81,4 +85,6 @@ def cmd_metadata(self):
|
|||||||
# Print nonexistant keys as having empty value
|
# Print nonexistant keys as having empty value
|
||||||
if value is None:
|
if value is None:
|
||||||
value = ""
|
value = ""
|
||||||
printf("%s=%s\n", key, value)
|
printf("%s=%s\n",
|
||||||
|
nilmdb.utils.unicode.encode(key),
|
||||||
|
nilmdb.utils.unicode.encode(value))
|
||||||
|
@@ -1,17 +1,19 @@
|
|||||||
from nilmdb.utils.printf import *
|
from nilmdb.utils.printf import *
|
||||||
import nilmdb.client
|
import nilmdb.client
|
||||||
|
import fnmatch
|
||||||
|
|
||||||
def setup(self, sub):
|
def setup(self, sub):
|
||||||
cmd = sub.add_parser("remove", help="Remove data",
|
cmd = sub.add_parser("remove", help="Remove data",
|
||||||
description="""
|
description="""
|
||||||
Remove all data from a specified time range within a
|
Remove all data from a specified time range within a
|
||||||
stream.
|
stream. If multiple streams or wildcards are provided,
|
||||||
|
the same time range is removed from all streams.
|
||||||
""")
|
""")
|
||||||
cmd.set_defaults(handler = cmd_remove)
|
cmd.set_defaults(handler = cmd_remove)
|
||||||
|
|
||||||
group = cmd.add_argument_group("Data selection")
|
group = cmd.add_argument_group("Data selection")
|
||||||
group.add_argument("path",
|
group.add_argument("path", nargs='+',
|
||||||
help="Path of stream, e.g. /foo/bar",
|
help="Path of stream, e.g. /foo/bar/*",
|
||||||
).completer = self.complete.path
|
).completer = self.complete.path
|
||||||
group.add_argument("-s", "--start", required=True,
|
group.add_argument("-s", "--start", required=True,
|
||||||
metavar="TIME", type=self.arg_time,
|
metavar="TIME", type=self.arg_time,
|
||||||
@@ -23,18 +25,31 @@ def setup(self, sub):
|
|||||||
).completer = self.complete.time
|
).completer = self.complete.time
|
||||||
|
|
||||||
group = cmd.add_argument_group("Output format")
|
group = cmd.add_argument_group("Output format")
|
||||||
|
group.add_argument("-q", "--quiet", action="store_true",
|
||||||
|
help="Don't display names when removing "
|
||||||
|
"from multiple paths")
|
||||||
group.add_argument("-c", "--count", action="store_true",
|
group.add_argument("-c", "--count", action="store_true",
|
||||||
help="Output number of data points removed")
|
help="Output number of data points removed")
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
def cmd_remove(self):
|
def cmd_remove(self):
|
||||||
|
streams = [ s[0] for s in self.client.stream_list() ]
|
||||||
|
paths = []
|
||||||
|
for path in self.args.path:
|
||||||
|
new = fnmatch.filter(streams, path)
|
||||||
|
if not new:
|
||||||
|
self.die("error: no stream matched path: %s", path)
|
||||||
|
paths.extend(new)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
count = self.client.stream_remove(self.args.path,
|
for path in paths:
|
||||||
self.args.start, self.args.end)
|
if not self.args.quiet and len(paths) > 1:
|
||||||
|
printf("Removing from %s\n", path)
|
||||||
|
count = self.client.stream_remove(path,
|
||||||
|
self.args.start, self.args.end)
|
||||||
|
if self.args.count:
|
||||||
|
printf("%d\n", count);
|
||||||
except nilmdb.client.ClientError as e:
|
except nilmdb.client.ClientError as e:
|
||||||
self.die("error removing data: %s", str(e))
|
self.die("error removing data: %s", str(e))
|
||||||
|
|
||||||
if self.args.count:
|
|
||||||
printf("%d\n", count)
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
5
nilmdb/fsck/__init__.py
Normal file
5
nilmdb/fsck/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
"""nilmdb.fsck"""
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from nilmdb.fsck.fsck import Fsck
|
458
nilmdb/fsck/fsck.py
Normal file
458
nilmdb/fsck/fsck.py
Normal file
@@ -0,0 +1,458 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""Check database consistency, with some ability to fix problems.
|
||||||
|
This should be able to fix cases where a database gets corrupted due
|
||||||
|
to unexpected system shutdown, and detect other cases that may cause
|
||||||
|
NilmDB to return errors when trying to manipulate the database."""
|
||||||
|
|
||||||
|
import nilmdb.utils
|
||||||
|
import nilmdb.server
|
||||||
|
import nilmdb.client.numpyclient
|
||||||
|
from nilmdb.utils.interval import IntervalError
|
||||||
|
from nilmdb.server.interval import Interval, IntervalSet
|
||||||
|
from nilmdb.utils.printf import *
|
||||||
|
from nilmdb.utils.time import timestamp_to_string
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
|
import sqlite3
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import progressbar
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
import shutil
|
||||||
|
import cPickle as pickle
|
||||||
|
import numpy
|
||||||
|
|
||||||
|
class FsckError(Exception):
|
||||||
|
def __init__(self, msg = "", *args):
|
||||||
|
if args:
|
||||||
|
msg = sprintf(msg, *args)
|
||||||
|
Exception.__init__(self, msg)
|
||||||
|
class FixableFsckError(FsckError):
|
||||||
|
def __init__(self, msg = "", *args):
|
||||||
|
if args:
|
||||||
|
msg = sprintf(msg, *args)
|
||||||
|
FsckError.__init__(self, "%s\nThis may be fixable with \"--fix\".", msg)
|
||||||
|
class RetryFsck(FsckError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def log(format, *args):
|
||||||
|
printf(format, *args)
|
||||||
|
|
||||||
|
def err(format, *args):
|
||||||
|
fprintf(sys.stderr, format, *args)
|
||||||
|
|
||||||
|
# Decorator that retries a function if it returns a specific value
|
||||||
|
def retry_if_raised(exc, message = None, max_retries = 100):
|
||||||
|
def f1(func):
|
||||||
|
def f2(*args, **kwargs):
|
||||||
|
for n in range(max_retries):
|
||||||
|
try:
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
except exc as e:
|
||||||
|
if message:
|
||||||
|
log("%s\n\n", message)
|
||||||
|
raise Exception("Max number of retries (%d) exceeded; giving up")
|
||||||
|
return f2
|
||||||
|
return f1
|
||||||
|
|
||||||
|
class Progress(object):
|
||||||
|
def __init__(self, maxval):
|
||||||
|
self.bar = progressbar.ProgressBar(
|
||||||
|
maxval = maxval,
|
||||||
|
widgets = [ progressbar.Percentage(), ' ',
|
||||||
|
progressbar.Bar(), ' ',
|
||||||
|
progressbar.ETA() ])
|
||||||
|
if self.bar.term_width == 0:
|
||||||
|
self.bar.term_width = 75
|
||||||
|
def __enter__(self):
|
||||||
|
self.bar.start()
|
||||||
|
self.last_update = 0
|
||||||
|
return self
|
||||||
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
|
if exc_type is None:
|
||||||
|
self.bar.finish()
|
||||||
|
else:
|
||||||
|
printf("\n")
|
||||||
|
def update(self, val):
|
||||||
|
self.bar.update(val)
|
||||||
|
|
||||||
|
class Fsck(object):
|
||||||
|
|
||||||
|
def __init__(self, path, fix = False):
|
||||||
|
self.basepath = path
|
||||||
|
self.sqlpath = os.path.join(path, "data.sql")
|
||||||
|
self.bulkpath = os.path.join(path, "data")
|
||||||
|
self.bulklock = os.path.join(path, "data.lock")
|
||||||
|
self.fix = fix
|
||||||
|
|
||||||
|
### Main checks
|
||||||
|
|
||||||
|
@retry_if_raised(RetryFsck, "Something was fixed: restarting fsck")
|
||||||
|
def check(self, skip_data = False):
|
||||||
|
self.bulk = None
|
||||||
|
self.sql = None
|
||||||
|
try:
|
||||||
|
self.check_paths()
|
||||||
|
self.check_sql()
|
||||||
|
self.check_streams()
|
||||||
|
self.check_intervals()
|
||||||
|
if skip_data:
|
||||||
|
log("skipped data check\n")
|
||||||
|
else:
|
||||||
|
self.check_data()
|
||||||
|
finally:
|
||||||
|
if self.bulk:
|
||||||
|
self.bulk.close()
|
||||||
|
if self.sql:
|
||||||
|
self.sql.commit()
|
||||||
|
self.sql.close()
|
||||||
|
log("ok\n")
|
||||||
|
|
||||||
|
### Check basic path structure
|
||||||
|
|
||||||
|
def check_paths(self):
|
||||||
|
log("checking paths\n")
|
||||||
|
if self.bulk:
|
||||||
|
self.bulk.close()
|
||||||
|
if not os.path.isfile(self.sqlpath):
|
||||||
|
raise FsckError("SQL database missing (%s)", self.sqlpath)
|
||||||
|
if not os.path.isdir(self.bulkpath):
|
||||||
|
raise FsckError("Bulk data directory missing (%s)", self.bulkpath)
|
||||||
|
with open(self.bulklock, "w") as lockfile:
|
||||||
|
if not nilmdb.utils.lock.exclusive_lock(lockfile):
|
||||||
|
raise FsckError('Database already locked by another process\n'
|
||||||
|
'Make sure all other processes that might be '
|
||||||
|
'using the database are stopped.\n'
|
||||||
|
'Restarting apache will cause it to unlock '
|
||||||
|
'the db until a request is received.')
|
||||||
|
# unlocked immediately
|
||||||
|
self.bulk = nilmdb.server.bulkdata.BulkData(self.basepath)
|
||||||
|
|
||||||
|
### Check SQL database health
|
||||||
|
|
||||||
|
def check_sql(self):
|
||||||
|
log("checking sqlite database\n")
|
||||||
|
|
||||||
|
self.sql = sqlite3.connect(self.sqlpath)
|
||||||
|
with self.sql:
|
||||||
|
cur = self.sql.cursor()
|
||||||
|
ver = cur.execute("PRAGMA user_version").fetchone()[0]
|
||||||
|
good = max(nilmdb.server.nilmdb._sql_schema_updates.keys())
|
||||||
|
if ver != good:
|
||||||
|
raise FsckError("database version %d too old, should be %d",
|
||||||
|
ver, good)
|
||||||
|
self.stream_path = {}
|
||||||
|
self.stream_layout = {}
|
||||||
|
log(" loading paths\n")
|
||||||
|
result = cur.execute("SELECT id, path, layout FROM streams")
|
||||||
|
for r in result:
|
||||||
|
if r[0] in self.stream_path:
|
||||||
|
raise FsckError("duplicated ID %d in stream IDs", r[0])
|
||||||
|
self.stream_path[r[0]] = r[1]
|
||||||
|
self.stream_layout[r[0]] = r[2]
|
||||||
|
|
||||||
|
log(" loading intervals\n")
|
||||||
|
self.stream_interval = defaultdict(list)
|
||||||
|
result = cur.execute("SELECT stream_id, start_time, end_time, "
|
||||||
|
"start_pos, end_pos FROM ranges "
|
||||||
|
"ORDER BY start_time")
|
||||||
|
for r in result:
|
||||||
|
if r[0] not in self.stream_path:
|
||||||
|
raise FsckError("interval ID %d not in streams", k)
|
||||||
|
self.stream_interval[r[0]].append((r[1], r[2], r[3], r[4]))
|
||||||
|
|
||||||
|
log(" loading metadata\n")
|
||||||
|
self.stream_meta = defaultdict(dict)
|
||||||
|
result = cur.execute("SELECT stream_id, key, value FROM metadata")
|
||||||
|
for r in result:
|
||||||
|
if r[0] not in self.stream_path:
|
||||||
|
raise FsckError("metadata ID %d not in streams", k)
|
||||||
|
if r[1] in self.stream_meta[r[0]]:
|
||||||
|
raise FsckError("duplicate metadata key '%s' for stream %d",
|
||||||
|
r[1], r[0])
|
||||||
|
self.stream_meta[r[0]][r[1]] = r[2]
|
||||||
|
|
||||||
|
### Check streams and basic interval overlap
|
||||||
|
|
||||||
|
def check_streams(self):
|
||||||
|
ids = self.stream_path.keys()
|
||||||
|
log("checking %s streams\n", "{:,d}".format(len(ids)))
|
||||||
|
with Progress(len(ids)) as pbar:
|
||||||
|
for i, sid in enumerate(ids):
|
||||||
|
pbar.update(i)
|
||||||
|
path = self.stream_path[sid]
|
||||||
|
|
||||||
|
# unique path, valid layout
|
||||||
|
if self.stream_path.values().count(path) != 1:
|
||||||
|
raise FsckError("duplicated path %s", path)
|
||||||
|
layout = self.stream_layout[sid].split('_')[0]
|
||||||
|
if layout not in ('int8', 'int16', 'int32', 'int64',
|
||||||
|
'uint8', 'uint16', 'uint32', 'uint64',
|
||||||
|
'float32', 'float64'):
|
||||||
|
raise FsckError("bad layout %s for %s", layout, path)
|
||||||
|
count = int(self.stream_layout[sid].split('_')[1])
|
||||||
|
if count < 1 or count > 1024:
|
||||||
|
raise FsckError("bad count %d for %s", count, path)
|
||||||
|
|
||||||
|
# must exist in bulkdata
|
||||||
|
bulk = self.bulkpath + path
|
||||||
|
if not os.path.isdir(bulk):
|
||||||
|
raise FsckError("%s: missing bulkdata dir", path)
|
||||||
|
if not nilmdb.server.bulkdata.Table.exists(bulk):
|
||||||
|
raise FsckError("%s: bad bulkdata table", path)
|
||||||
|
|
||||||
|
# intervals don't overlap. Abuse IntervalSet to check
|
||||||
|
# for intervals in file positions, too.
|
||||||
|
timeiset = IntervalSet()
|
||||||
|
posiset = IntervalSet()
|
||||||
|
for (stime, etime, spos, epos) in self.stream_interval[sid]:
|
||||||
|
new = Interval(stime, etime)
|
||||||
|
try:
|
||||||
|
timeiset += new
|
||||||
|
except IntervalError:
|
||||||
|
raise FsckError("%s: overlap in intervals:\n"
|
||||||
|
"set: %s\nnew: %s",
|
||||||
|
path, str(timeiset), str(new))
|
||||||
|
if spos != epos:
|
||||||
|
new = Interval(spos, epos)
|
||||||
|
try:
|
||||||
|
posiset += new
|
||||||
|
except IntervalError:
|
||||||
|
raise FsckError("%s: overlap in file offsets:\n"
|
||||||
|
"set: %s\nnew: %s",
|
||||||
|
path, str(posiset), str(new))
|
||||||
|
|
||||||
|
# check bulkdata
|
||||||
|
self.check_bulkdata(sid, path, bulk)
|
||||||
|
|
||||||
|
# Check that we can open bulkdata
|
||||||
|
try:
|
||||||
|
tab = None
|
||||||
|
try:
|
||||||
|
tab = nilmdb.server.bulkdata.Table(bulk)
|
||||||
|
except Exception as e:
|
||||||
|
raise FsckError("%s: can't open bulkdata: %s",
|
||||||
|
path, str(e))
|
||||||
|
finally:
|
||||||
|
if tab:
|
||||||
|
tab.close()
|
||||||
|
|
||||||
|
### Check that bulkdata is good enough to be opened
|
||||||
|
|
||||||
|
@retry_if_raised(RetryFsck)
|
||||||
|
def check_bulkdata(self, sid, path, bulk):
|
||||||
|
with open(os.path.join(bulk, "_format"), "rb") as f:
|
||||||
|
fmt = pickle.load(f)
|
||||||
|
if fmt["version"] != 3:
|
||||||
|
raise FsckError("%s: bad or unsupported bulkdata version %d",
|
||||||
|
path, fmt["version"])
|
||||||
|
row_per_file = int(fmt["rows_per_file"])
|
||||||
|
files_per_dir = int(fmt["files_per_dir"])
|
||||||
|
layout = fmt["layout"]
|
||||||
|
if layout != self.stream_layout[sid]:
|
||||||
|
raise FsckError("%s: layout mismatch %s != %s", path,
|
||||||
|
layout, self.stream_layout[sid])
|
||||||
|
|
||||||
|
# Every file should have a size that's the multiple of the row size
|
||||||
|
rkt = nilmdb.server.rocket.Rocket(layout, None)
|
||||||
|
row_size = rkt.binary_size
|
||||||
|
rkt.close()
|
||||||
|
|
||||||
|
# Find all directories
|
||||||
|
regex = re.compile("^[0-9a-f]{4,}$")
|
||||||
|
subdirs = sorted(filter(regex.search, os.listdir(bulk)),
|
||||||
|
key = lambda x: int(x, 16), reverse = True)
|
||||||
|
for subdir in subdirs:
|
||||||
|
# Find all files in that dir
|
||||||
|
subpath = os.path.join(bulk, subdir)
|
||||||
|
files = filter(regex.search, os.listdir(subpath))
|
||||||
|
if not files:
|
||||||
|
self.fix_empty_subdir(subpath)
|
||||||
|
raise RetryFsck
|
||||||
|
# Verify that their size is a multiple of the row size
|
||||||
|
for filename in files:
|
||||||
|
filepath = os.path.join(subpath, filename)
|
||||||
|
offset = os.path.getsize(filepath)
|
||||||
|
if offset % row_size:
|
||||||
|
self.fix_bad_filesize(path, filepath, offset, row_size)
|
||||||
|
|
||||||
|
def fix_empty_subdir(self, subpath):
|
||||||
|
msg = sprintf("bulkdata path %s is missing data files", subpath)
|
||||||
|
if not self.fix:
|
||||||
|
raise FixableFsckError(msg)
|
||||||
|
# Try to fix it by just deleting whatever is present,
|
||||||
|
# as long as it's only ".removed" files.
|
||||||
|
err("\n%s\n", msg)
|
||||||
|
for fn in os.listdir(subpath):
|
||||||
|
if not fn.endswith(".removed"):
|
||||||
|
raise FsckError("can't fix automatically: please manually "
|
||||||
|
"remove the file %s and try again",
|
||||||
|
os.path.join(subpath, fn))
|
||||||
|
# Remove the whole thing
|
||||||
|
err("Removing empty subpath\n")
|
||||||
|
shutil.rmtree(subpath)
|
||||||
|
raise RetryFsck
|
||||||
|
|
||||||
|
def fix_bad_filesize(self, path, filepath, offset, row_size):
|
||||||
|
extra = offset % row_size
|
||||||
|
msg = sprintf("%s: size of file %s (%d) is not a multiple" +
|
||||||
|
" of row size (%d): %d extra bytes present",
|
||||||
|
path, filepath, offset, row_size, extra)
|
||||||
|
if not self.fix:
|
||||||
|
raise FixableFsckError(msg)
|
||||||
|
# Try to fix it by just truncating the file
|
||||||
|
err("\n%s\n", msg)
|
||||||
|
newsize = offset - extra
|
||||||
|
err("Truncating file to %d bytes and retrying\n", newsize)
|
||||||
|
with open(filepath, "r+b") as f:
|
||||||
|
f.truncate(newsize)
|
||||||
|
raise RetryFsck
|
||||||
|
|
||||||
|
### Check interval endpoints
|
||||||
|
|
||||||
|
def check_intervals(self):
|
||||||
|
total_ints = sum(len(x) for x in self.stream_interval.values())
|
||||||
|
log("checking %s intervals\n", "{:,d}".format(total_ints))
|
||||||
|
done = 0
|
||||||
|
with Progress(total_ints) as pbar:
|
||||||
|
for sid in self.stream_interval:
|
||||||
|
try:
|
||||||
|
bulk = self.bulkpath + self.stream_path[sid]
|
||||||
|
tab = nilmdb.server.bulkdata.Table(bulk)
|
||||||
|
def update(x):
|
||||||
|
pbar.update(done + x)
|
||||||
|
ints = self.stream_interval[sid]
|
||||||
|
done += self.check_table_intervals(sid, ints, tab, update)
|
||||||
|
finally:
|
||||||
|
tab.close()
|
||||||
|
|
||||||
|
def check_table_intervals(self, sid, ints, tab, update):
|
||||||
|
# look in the table to make sure we can pick out the interval's
|
||||||
|
# endpoints
|
||||||
|
path = self.stream_path[sid]
|
||||||
|
tab.file_open.cache_remove_all()
|
||||||
|
for (i, intv) in enumerate(ints):
|
||||||
|
update(i)
|
||||||
|
(stime, etime, spos, epos) = intv
|
||||||
|
if spos == epos and spos >= 0 and spos <= tab.nrows:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
srow = tab[spos]
|
||||||
|
erow = tab[epos-1]
|
||||||
|
except Exception as e:
|
||||||
|
self.fix_bad_interval(sid, intv, tab, str(e))
|
||||||
|
raise RetryFsck
|
||||||
|
return len(ints)
|
||||||
|
|
||||||
|
def fix_bad_interval(self, sid, intv, tab, msg):
|
||||||
|
path = self.stream_path[sid]
|
||||||
|
msg = sprintf("%s: interval %s error accessing rows: %s",
|
||||||
|
path, str(intv), str(msg))
|
||||||
|
if not self.fix:
|
||||||
|
raise FixableFsckError(msg)
|
||||||
|
err("\n%s\n", msg)
|
||||||
|
|
||||||
|
(stime, etime, spos, epos) = intv
|
||||||
|
# If it's just that the end pos is more than the number of rows
|
||||||
|
# in the table, lower end pos and truncate interval time too.
|
||||||
|
if spos < tab.nrows and epos >= tab.nrows:
|
||||||
|
err("end position is past endrows, but it can be truncated\n")
|
||||||
|
err("old end: time %d, pos %d\n", etime, epos)
|
||||||
|
new_epos = tab.nrows
|
||||||
|
new_etime = tab[new_epos-1] + 1
|
||||||
|
err("new end: time %d, pos %d\n", new_etime, new_epos)
|
||||||
|
if stime < new_etime:
|
||||||
|
# Change it in SQL
|
||||||
|
with self.sql:
|
||||||
|
cur = self.sql.cursor()
|
||||||
|
cur.execute("UPDATE ranges SET end_time=?, end_pos=? "
|
||||||
|
"WHERE stream_id=? AND start_time=? AND "
|
||||||
|
"end_time=? AND start_pos=? AND end_pos=?",
|
||||||
|
(new_etime, new_epos, sid, stime, etime,
|
||||||
|
spos, epos))
|
||||||
|
if cur.rowcount != 1:
|
||||||
|
raise FsckError("failed to fix SQL database")
|
||||||
|
raise RetryFsck
|
||||||
|
err("actually it can't be truncated; times are bad too")
|
||||||
|
|
||||||
|
# Otherwise, the only hope is to delete the interval entirely.
|
||||||
|
err("*** Deleting the entire interval from SQL.\n")
|
||||||
|
err("This may leave stale data on disk. To fix that, copy all\n")
|
||||||
|
err("data from this stream to a new stream, then remove all data\n")
|
||||||
|
err("from and destroy %s.\n")
|
||||||
|
with self.sql:
|
||||||
|
cur = self.sql.cursor()
|
||||||
|
cur.execute("DELETE FROM ranges WHERE "
|
||||||
|
"stream_id=? AND start_time=? AND "
|
||||||
|
"end_time=? AND start_pos=? AND end_pos=?",
|
||||||
|
(sid, stime, etime, spos, epos))
|
||||||
|
if cur.rowcount != 1:
|
||||||
|
raise FsckError("failed to remove interval")
|
||||||
|
raise RetryFsck
|
||||||
|
|
||||||
|
### Check data in each interval
|
||||||
|
|
||||||
|
def check_data(self):
|
||||||
|
total_rows = sum(sum((y[3] - y[2]) for y in x)
|
||||||
|
for x in self.stream_interval.values())
|
||||||
|
log("checking %s rows of data\n", "{:,d}".format(total_rows))
|
||||||
|
done = 0
|
||||||
|
with Progress(total_rows) as pbar:
|
||||||
|
for sid in self.stream_interval:
|
||||||
|
try:
|
||||||
|
bulk = self.bulkpath + self.stream_path[sid]
|
||||||
|
tab = nilmdb.server.bulkdata.Table(bulk)
|
||||||
|
def update(x):
|
||||||
|
pbar.update(done + x)
|
||||||
|
ints = self.stream_interval[sid]
|
||||||
|
done += self.check_table_data(sid, ints, tab, update)
|
||||||
|
finally:
|
||||||
|
tab.close()
|
||||||
|
|
||||||
|
def check_table_data(self, sid, ints, tab, update):
|
||||||
|
# Pull out all of the interval's data and verify that it's
|
||||||
|
# monotonic.
|
||||||
|
maxrows = 100000
|
||||||
|
path = self.stream_path[sid]
|
||||||
|
layout = self.stream_layout[sid]
|
||||||
|
dtype = nilmdb.client.numpyclient.layout_to_dtype(layout)
|
||||||
|
tab.file_open.cache_remove_all()
|
||||||
|
done = 0
|
||||||
|
for intv in ints:
|
||||||
|
last_ts = None
|
||||||
|
(stime, etime, spos, epos) = intv
|
||||||
|
if spos == epos:
|
||||||
|
continue
|
||||||
|
for start in xrange(*slice(spos, epos, maxrows).indices(epos)):
|
||||||
|
stop = min(start + maxrows, epos)
|
||||||
|
count = stop - start
|
||||||
|
# Get raw data, convert to NumPy arary
|
||||||
|
try:
|
||||||
|
raw = tab.get_data(start, stop, binary = True)
|
||||||
|
data = numpy.fromstring(raw, dtype)
|
||||||
|
except Exception as e:
|
||||||
|
raise FsckError("%s: failed to grab rows %d through %d: %s",
|
||||||
|
path, start, stop, repr(e))
|
||||||
|
|
||||||
|
# Verify that timestamps are monotonic
|
||||||
|
if (numpy.diff(data['timestamp']) <= 0).any():
|
||||||
|
raise FsckError("%s: non-monotonic timestamp(s) in rows "
|
||||||
|
"%d through %d", path, start, stop)
|
||||||
|
first_ts = data['timestamp'][0]
|
||||||
|
if last_ts is not None and first_ts <= last_ts:
|
||||||
|
raise FsckError("%s: first interval timestamp %d is not "
|
||||||
|
"greater than the previous last interval "
|
||||||
|
"timestamp %d, at row %d",
|
||||||
|
path, first_ts, last_ts, start)
|
||||||
|
last_ts = data['timestamp'][-1]
|
||||||
|
|
||||||
|
# These are probably fixable, by removing the offending
|
||||||
|
# intervals. But I'm not going to bother implementing
|
||||||
|
# that yet.
|
||||||
|
|
||||||
|
# Done
|
||||||
|
done += count
|
||||||
|
update(done)
|
||||||
|
return done
|
27
nilmdb/scripts/nilmdb_fsck.py
Executable file
27
nilmdb/scripts/nilmdb_fsck.py
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import nilmdb.fsck
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main entry point for the 'nilmdb-fsck' command line script"""
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description = 'Check database consistency',
|
||||||
|
formatter_class = argparse.ArgumentDefaultsHelpFormatter)
|
||||||
|
parser.add_argument("-V", "--version", action="version",
|
||||||
|
version = nilmdb.__version__)
|
||||||
|
parser.add_argument("-f", "--fix", action="store_true",
|
||||||
|
default=False, help = 'Fix errors when possible '
|
||||||
|
'(which may involve removing data)')
|
||||||
|
parser.add_argument("-n", "--no-data", action="store_true",
|
||||||
|
default=False, help = 'Skip the slow full-data check')
|
||||||
|
parser.add_argument('database', help = 'Database directory')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
nilmdb.fsck.Fsck(args.database, args.fix).check(skip_data = args.no_data)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@@ -19,8 +19,8 @@ from . import rocket
|
|||||||
|
|
||||||
# Up to 256 open file descriptors at any given time.
|
# Up to 256 open file descriptors at any given time.
|
||||||
# These variables are global so they can be used in the decorator arguments.
|
# These variables are global so they can be used in the decorator arguments.
|
||||||
table_cache_size = 16
|
table_cache_size = 32
|
||||||
fd_cache_size = 16
|
fd_cache_size = 8
|
||||||
|
|
||||||
@nilmdb.utils.must_close(wrap_verify = False)
|
@nilmdb.utils.must_close(wrap_verify = False)
|
||||||
class BulkData(object):
|
class BulkData(object):
|
||||||
@@ -330,7 +330,8 @@ class Table(object):
|
|||||||
|
|
||||||
# Find the last directory. We sort and loop through all of them,
|
# Find the last directory. We sort and loop through all of them,
|
||||||
# starting with the numerically greatest, because the dirs could be
|
# starting with the numerically greatest, because the dirs could be
|
||||||
# empty if something was deleted.
|
# empty if something was deleted but the directory was unexpectedly
|
||||||
|
# not deleted.
|
||||||
subdirs = sorted(filter(regex.search, os.listdir(self.root)),
|
subdirs = sorted(filter(regex.search, os.listdir(self.root)),
|
||||||
key = lambda x: int(x, 16), reverse = True)
|
key = lambda x: int(x, 16), reverse = True)
|
||||||
|
|
||||||
|
@@ -176,7 +176,7 @@ class NilmDB(object):
|
|||||||
raise NilmDBError("start must precede end")
|
raise NilmDBError("start must precede end")
|
||||||
return (start, end)
|
return (start, end)
|
||||||
|
|
||||||
@nilmdb.utils.lru_cache(size = 16)
|
@nilmdb.utils.lru_cache(size = 64)
|
||||||
def _get_intervals(self, stream_id):
|
def _get_intervals(self, stream_id):
|
||||||
"""
|
"""
|
||||||
Return a mutable IntervalSet corresponding to the given stream ID.
|
Return a mutable IntervalSet corresponding to the given stream ID.
|
||||||
@@ -675,6 +675,7 @@ class NilmDB(object):
|
|||||||
|
|
||||||
# Count how many were removed
|
# Count how many were removed
|
||||||
removed += row_end - row_start
|
removed += row_end - row_start
|
||||||
|
remaining -= row_end - row_start
|
||||||
|
|
||||||
if restart is not None:
|
if restart is not None:
|
||||||
break
|
break
|
||||||
|
@@ -5,6 +5,9 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define __STDC_FORMAT_MACROS
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
/* Values missing from stdint.h */
|
/* Values missing from stdint.h */
|
||||||
#define UINT8_MIN 0
|
#define UINT8_MIN 0
|
||||||
#define UINT16_MIN 0
|
#define UINT16_MIN 0
|
||||||
@@ -19,16 +22,9 @@
|
|||||||
|
|
||||||
typedef int64_t timestamp_t;
|
typedef int64_t timestamp_t;
|
||||||
|
|
||||||
/* This code probably needs to be double-checked for the case where
|
|
||||||
sizeof(long) != 8, so enforce that here with something that will
|
|
||||||
fail at build time. We assume that the python integer type can
|
|
||||||
hold an int64_t. */
|
|
||||||
const static char __long_ok[1 - 2*!(sizeof(int64_t) ==
|
|
||||||
sizeof(long int))] = { 0 };
|
|
||||||
|
|
||||||
/* Somewhat arbitrary, just so we can use fixed sizes for strings
|
/* Somewhat arbitrary, just so we can use fixed sizes for strings
|
||||||
etc. */
|
etc. */
|
||||||
static const int MAX_LAYOUT_COUNT = 128;
|
static const int MAX_LAYOUT_COUNT = 1024;
|
||||||
|
|
||||||
/* Error object and constants */
|
/* Error object and constants */
|
||||||
static PyObject *ParseError;
|
static PyObject *ParseError;
|
||||||
@@ -58,7 +54,7 @@ static PyObject *raise_str(int line, int col, int code, const char *string)
|
|||||||
static PyObject *raise_int(int line, int col, int code, int64_t num)
|
static PyObject *raise_int(int line, int col, int code, int64_t num)
|
||||||
{
|
{
|
||||||
PyObject *o;
|
PyObject *o;
|
||||||
o = Py_BuildValue("(iiil)", line, col, code, num);
|
o = Py_BuildValue("(iiiL)", line, col, code, (long long)num);
|
||||||
if (o != NULL) {
|
if (o != NULL) {
|
||||||
PyErr_SetObject(ParseError, o);
|
PyErr_SetObject(ParseError, o);
|
||||||
Py_DECREF(o);
|
Py_DECREF(o);
|
||||||
@@ -249,11 +245,11 @@ static PyObject *Rocket_get_file_size(Rocket *self)
|
|||||||
/****
|
/****
|
||||||
* Append from string
|
* Append from string
|
||||||
*/
|
*/
|
||||||
static inline long int strtol10(const char *nptr, char **endptr) {
|
static inline long int strtoll10(const char *nptr, char **endptr) {
|
||||||
return strtol(nptr, endptr, 10);
|
return strtoll(nptr, endptr, 10);
|
||||||
}
|
}
|
||||||
static inline long int strtoul10(const char *nptr, char **endptr) {
|
static inline long int strtoull10(const char *nptr, char **endptr) {
|
||||||
return strtoul(nptr, endptr, 10);
|
return strtoull(nptr, endptr, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* .append_string(count, data, offset, linenum, start, end, last_timestamp) */
|
/* .append_string(count, data, offset, linenum, start, end, last_timestamp) */
|
||||||
@@ -264,6 +260,7 @@ static PyObject *Rocket_append_string(Rocket *self, PyObject *args)
|
|||||||
int offset;
|
int offset;
|
||||||
const char *linestart;
|
const char *linestart;
|
||||||
int linenum;
|
int linenum;
|
||||||
|
long long ll1, ll2, ll3;
|
||||||
timestamp_t start;
|
timestamp_t start;
|
||||||
timestamp_t end;
|
timestamp_t end;
|
||||||
timestamp_t last_timestamp;
|
timestamp_t last_timestamp;
|
||||||
@@ -280,10 +277,13 @@ static PyObject *Rocket_append_string(Rocket *self, PyObject *args)
|
|||||||
but we need the null termination for strto*. If we had
|
but we need the null termination for strto*. If we had
|
||||||
strnto* that took a length, we could use t# and not require
|
strnto* that took a length, we could use t# and not require
|
||||||
a copy. */
|
a copy. */
|
||||||
if (!PyArg_ParseTuple(args, "isiilll:append_string", &count,
|
if (!PyArg_ParseTuple(args, "isiiLLL:append_string", &count,
|
||||||
&data, &offset, &linenum,
|
&data, &offset, &linenum,
|
||||||
&start, &end, &last_timestamp))
|
&ll1, &ll2, &ll3))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
start = ll1;
|
||||||
|
end = ll2;
|
||||||
|
last_timestamp = ll3;
|
||||||
|
|
||||||
/* Skip spaces, but don't skip over a newline. */
|
/* Skip spaces, but don't skip over a newline. */
|
||||||
#define SKIP_BLANK(buf) do { \
|
#define SKIP_BLANK(buf) do { \
|
||||||
@@ -372,14 +372,14 @@ static PyObject *Rocket_append_string(Rocket *self, PyObject *args)
|
|||||||
goto extra_data_on_line; \
|
goto extra_data_on_line; \
|
||||||
break
|
break
|
||||||
|
|
||||||
CS(INT8, strtol10, t64.i, t8.i, t8.u, , 1);
|
CS(INT8, strtoll10, t64.i, t8.i, t8.u, , 1);
|
||||||
CS(UINT8, strtoul10, t64.u, t8.u, t8.u, , 1);
|
CS(UINT8, strtoull10, t64.u, t8.u, t8.u, , 1);
|
||||||
CS(INT16, strtol10, t64.i, t16.i, t16.u, le16toh, 2);
|
CS(INT16, strtoll10, t64.i, t16.i, t16.u, le16toh, 2);
|
||||||
CS(UINT16, strtoul10, t64.u, t16.u, t16.u, le16toh, 2);
|
CS(UINT16, strtoull10, t64.u, t16.u, t16.u, le16toh, 2);
|
||||||
CS(INT32, strtol10, t64.i, t32.i, t32.u, le32toh, 4);
|
CS(INT32, strtoll10, t64.i, t32.i, t32.u, le32toh, 4);
|
||||||
CS(UINT32, strtoul10, t64.u, t32.u, t32.u, le32toh, 4);
|
CS(UINT32, strtoull10, t64.u, t32.u, t32.u, le32toh, 4);
|
||||||
CS(INT64, strtol10, t64.i, t64.i, t64.u, le64toh, 8);
|
CS(INT64, strtoll10, t64.i, t64.i, t64.u, le64toh, 8);
|
||||||
CS(UINT64, strtoul10, t64.u, t64.u, t64.u, le64toh, 8);
|
CS(UINT64, strtoull10, t64.u, t64.u, t64.u, le64toh, 8);
|
||||||
CS(FLOAT32, strtod, t64.d, t32.f, t32.u, le32toh, 4);
|
CS(FLOAT32, strtod, t64.d, t32.f, t32.u, le32toh, 4);
|
||||||
CS(FLOAT64, strtod, t64.d, t64.d, t64.u, le64toh, 8);
|
CS(FLOAT64, strtod, t64.d, t64.d, t64.u, le64toh, 8);
|
||||||
#undef CS
|
#undef CS
|
||||||
@@ -397,7 +397,8 @@ static PyObject *Rocket_append_string(Rocket *self, PyObject *args)
|
|||||||
/* Build return value and return */
|
/* Build return value and return */
|
||||||
offset = buf - data;
|
offset = buf - data;
|
||||||
PyObject *o;
|
PyObject *o;
|
||||||
o = Py_BuildValue("(iili)", written, offset, last_timestamp, linenum);
|
o = Py_BuildValue("(iiLi)", written, offset,
|
||||||
|
(long long)last_timestamp, linenum);
|
||||||
return o;
|
return o;
|
||||||
err:
|
err:
|
||||||
PyErr_SetFromErrno(PyExc_OSError);
|
PyErr_SetFromErrno(PyExc_OSError);
|
||||||
@@ -431,14 +432,18 @@ static PyObject *Rocket_append_binary(Rocket *self, PyObject *args)
|
|||||||
int data_len;
|
int data_len;
|
||||||
int linenum;
|
int linenum;
|
||||||
int offset;
|
int offset;
|
||||||
|
long long ll1, ll2, ll3;
|
||||||
timestamp_t start;
|
timestamp_t start;
|
||||||
timestamp_t end;
|
timestamp_t end;
|
||||||
timestamp_t last_timestamp;
|
timestamp_t last_timestamp;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "it#iilll:append_binary",
|
if (!PyArg_ParseTuple(args, "it#iiLLL:append_binary",
|
||||||
&count, &data, &data_len, &offset,
|
&count, &data, &data_len, &offset,
|
||||||
&linenum, &start, &end, &last_timestamp))
|
&linenum, &ll1, &ll2, &ll3))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
start = ll1;
|
||||||
|
end = ll2;
|
||||||
|
last_timestamp = ll3;
|
||||||
|
|
||||||
/* Advance to offset */
|
/* Advance to offset */
|
||||||
if (offset > data_len)
|
if (offset > data_len)
|
||||||
@@ -468,7 +473,7 @@ static PyObject *Rocket_append_binary(Rocket *self, PyObject *args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Write binary data */
|
/* Write binary data */
|
||||||
if (fwrite(data, data_len, 1, self->file) != 1) {
|
if (fwrite(data, self->binary_size, rows, self->file) != rows) {
|
||||||
PyErr_SetFromErrno(PyExc_OSError);
|
PyErr_SetFromErrno(PyExc_OSError);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -476,8 +481,8 @@ static PyObject *Rocket_append_binary(Rocket *self, PyObject *args)
|
|||||||
|
|
||||||
/* Build return value and return */
|
/* Build return value and return */
|
||||||
PyObject *o;
|
PyObject *o;
|
||||||
o = Py_BuildValue("(iili)", rows, offset + rows * self->binary_size,
|
o = Py_BuildValue("(iiLi)", rows, offset + rows * self->binary_size,
|
||||||
last_timestamp, linenum);
|
(long long)last_timestamp, linenum);
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -534,7 +539,7 @@ static PyObject *Rocket_extract_string(Rocket *self, PyObject *args)
|
|||||||
if (fread(&t64.u, 8, 1, self->file) != 1)
|
if (fread(&t64.u, 8, 1, self->file) != 1)
|
||||||
goto err;
|
goto err;
|
||||||
t64.u = le64toh(t64.u);
|
t64.u = le64toh(t64.u);
|
||||||
ret = sprintf(&str[len], "%ld", t64.i);
|
ret = sprintf(&str[len], "%" PRId64, t64.i);
|
||||||
if (ret <= 0)
|
if (ret <= 0)
|
||||||
goto err;
|
goto err;
|
||||||
len += ret;
|
len += ret;
|
||||||
@@ -556,14 +561,14 @@ static PyObject *Rocket_extract_string(Rocket *self, PyObject *args)
|
|||||||
len += ret; \
|
len += ret; \
|
||||||
} \
|
} \
|
||||||
break
|
break
|
||||||
CASE(INT8, "%hhd", t8.i, t8.u, , 1);
|
CASE(INT8, "%" PRId8, t8.i, t8.u, , 1);
|
||||||
CASE(UINT8, "%hhu", t8.u, t8.u, , 1);
|
CASE(UINT8, "%" PRIu8, t8.u, t8.u, , 1);
|
||||||
CASE(INT16, "%hd", t16.i, t16.u, le16toh, 2);
|
CASE(INT16, "%" PRId16, t16.i, t16.u, le16toh, 2);
|
||||||
CASE(UINT16, "%hu", t16.u, t16.u, le16toh, 2);
|
CASE(UINT16, "%" PRIu16, t16.u, t16.u, le16toh, 2);
|
||||||
CASE(INT32, "%d", t32.i, t32.u, le32toh, 4);
|
CASE(INT32, "%" PRId32, t32.i, t32.u, le32toh, 4);
|
||||||
CASE(UINT32, "%u", t32.u, t32.u, le32toh, 4);
|
CASE(UINT32, "%" PRIu32, t32.u, t32.u, le32toh, 4);
|
||||||
CASE(INT64, "%ld", t64.i, t64.u, le64toh, 8);
|
CASE(INT64, "%" PRId64, t64.i, t64.u, le64toh, 8);
|
||||||
CASE(UINT64, "%lu", t64.u, t64.u, le64toh, 8);
|
CASE(UINT64, "%" PRIu64, t64.u, t64.u, le64toh, 8);
|
||||||
/* These next two are a bit debatable. floats
|
/* These next two are a bit debatable. floats
|
||||||
are 6-9 significant figures, so we print 7.
|
are 6-9 significant figures, so we print 7.
|
||||||
Doubles are 15-19, so we print 17. This is
|
Doubles are 15-19, so we print 17. This is
|
||||||
@@ -653,7 +658,7 @@ static PyObject *Rocket_extract_timestamp(Rocket *self, PyObject *args)
|
|||||||
|
|
||||||
/* Convert and return */
|
/* Convert and return */
|
||||||
t64.u = le64toh(t64.u);
|
t64.u = le64toh(t64.u);
|
||||||
return Py_BuildValue("l", t64.i);
|
return Py_BuildValue("L", (long long)t64.i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/****
|
/****
|
||||||
|
@@ -17,126 +17,26 @@ import decorator
|
|||||||
import psutil
|
import psutil
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
from nilmdb.server.serverutil import (
|
||||||
|
chunked_response,
|
||||||
|
response_type,
|
||||||
|
workaround_cp_bug_1200,
|
||||||
|
exception_to_httperror,
|
||||||
|
CORS_allow,
|
||||||
|
json_to_request_params,
|
||||||
|
json_error_page,
|
||||||
|
cherrypy_start,
|
||||||
|
cherrypy_stop,
|
||||||
|
bool_param,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add CORS_allow tool
|
||||||
|
cherrypy.tools.CORS_allow = cherrypy.Tool('on_start_resource', CORS_allow)
|
||||||
|
|
||||||
class NilmApp(object):
|
class NilmApp(object):
|
||||||
def __init__(self, db):
|
def __init__(self, db):
|
||||||
self.db = db
|
self.db = db
|
||||||
|
|
||||||
# Decorators
|
|
||||||
def chunked_response(func):
|
|
||||||
"""Decorator to enable chunked responses."""
|
|
||||||
# Set this to False to get better tracebacks from some requests
|
|
||||||
# (/stream/extract, /stream/intervals).
|
|
||||||
func._cp_config = { 'response.stream': True }
|
|
||||||
return func
|
|
||||||
|
|
||||||
def response_type(content_type):
|
|
||||||
"""Return a decorator-generating function that sets the
|
|
||||||
response type to the specified string."""
|
|
||||||
def wrapper(func, *args, **kwargs):
|
|
||||||
cherrypy.response.headers['Content-Type'] = content_type
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
return decorator.decorator(wrapper)
|
|
||||||
|
|
||||||
@decorator.decorator
|
|
||||||
def workaround_cp_bug_1200(func, *args, **kwargs): # pragma: no cover
|
|
||||||
"""Decorator to work around CherryPy bug #1200 in a response
|
|
||||||
generator.
|
|
||||||
|
|
||||||
Even if chunked responses are disabled, LookupError or
|
|
||||||
UnicodeError exceptions may still be swallowed by CherryPy due to
|
|
||||||
bug #1200. This throws them as generic Exceptions instead so that
|
|
||||||
they make it through.
|
|
||||||
"""
|
|
||||||
exc_info = None
|
|
||||||
try:
|
|
||||||
for val in func(*args, **kwargs):
|
|
||||||
yield val
|
|
||||||
except (LookupError, UnicodeError):
|
|
||||||
# Re-raise it, but maintain the original traceback
|
|
||||||
exc_info = sys.exc_info()
|
|
||||||
new_exc = Exception(exc_info[0].__name__ + ": " + str(exc_info[1]))
|
|
||||||
raise new_exc, None, exc_info[2]
|
|
||||||
finally:
|
|
||||||
del exc_info
|
|
||||||
|
|
||||||
def exception_to_httperror(*expected):
|
|
||||||
"""Return a decorator-generating function that catches expected
|
|
||||||
errors and throws a HTTPError describing it instead.
|
|
||||||
|
|
||||||
@exception_to_httperror(NilmDBError, ValueError)
|
|
||||||
def foo():
|
|
||||||
pass
|
|
||||||
"""
|
|
||||||
def wrapper(func, *args, **kwargs):
|
|
||||||
exc_info = None
|
|
||||||
try:
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
except expected:
|
|
||||||
# Re-raise it, but maintain the original traceback
|
|
||||||
exc_info = sys.exc_info()
|
|
||||||
new_exc = cherrypy.HTTPError("400 Bad Request", str(exc_info[1]))
|
|
||||||
raise new_exc, None, exc_info[2]
|
|
||||||
finally:
|
|
||||||
del exc_info
|
|
||||||
# We need to preserve the function's argspecs for CherryPy to
|
|
||||||
# handle argument errors correctly. Decorator.decorator takes
|
|
||||||
# care of that.
|
|
||||||
return decorator.decorator(wrapper)
|
|
||||||
|
|
||||||
# Custom CherryPy tools
|
|
||||||
|
|
||||||
def CORS_allow(methods):
|
|
||||||
"""This does several things:
|
|
||||||
|
|
||||||
Handles CORS preflight requests.
|
|
||||||
Adds Allow: header to all requests.
|
|
||||||
Raise 405 if request.method not in method.
|
|
||||||
|
|
||||||
It is similar to cherrypy.tools.allow, with the CORS stuff added.
|
|
||||||
"""
|
|
||||||
request = cherrypy.request.headers
|
|
||||||
response = cherrypy.response.headers
|
|
||||||
|
|
||||||
if not isinstance(methods, (tuple, list)): # pragma: no cover
|
|
||||||
methods = [ methods ]
|
|
||||||
methods = [ m.upper() for m in methods if m ]
|
|
||||||
if not methods: # pragma: no cover
|
|
||||||
methods = [ 'GET', 'HEAD' ]
|
|
||||||
elif 'GET' in methods and 'HEAD' not in methods: # pragma: no cover
|
|
||||||
methods.append('HEAD')
|
|
||||||
response['Allow'] = ', '.join(methods)
|
|
||||||
|
|
||||||
# Allow all origins
|
|
||||||
if 'Origin' in request:
|
|
||||||
response['Access-Control-Allow-Origin'] = request['Origin']
|
|
||||||
|
|
||||||
# If it's a CORS request, send response.
|
|
||||||
request_method = request.get("Access-Control-Request-Method", None)
|
|
||||||
request_headers = request.get("Access-Control-Request-Headers", None)
|
|
||||||
if (cherrypy.request.method == "OPTIONS" and
|
|
||||||
request_method and request_headers):
|
|
||||||
response['Access-Control-Allow-Headers'] = request_headers
|
|
||||||
response['Access-Control-Allow-Methods'] = ', '.join(methods)
|
|
||||||
# Try to stop further processing and return a 200 OK
|
|
||||||
cherrypy.response.status = "200 OK"
|
|
||||||
cherrypy.response.body = ""
|
|
||||||
cherrypy.request.handler = lambda: ""
|
|
||||||
return
|
|
||||||
|
|
||||||
# Reject methods that were not explicitly allowed
|
|
||||||
if cherrypy.request.method not in methods:
|
|
||||||
raise cherrypy.HTTPError(405)
|
|
||||||
|
|
||||||
cherrypy.tools.CORS_allow = cherrypy.Tool('on_start_resource', CORS_allow)
|
|
||||||
|
|
||||||
# Helper for json_in tool to process JSON data into normal request
|
|
||||||
# parameters.
|
|
||||||
def json_to_request_params(body):
|
|
||||||
cherrypy.lib.jsontools.json_processor(body)
|
|
||||||
if not isinstance(cherrypy.request.json, dict):
|
|
||||||
raise cherrypy.HTTPError(415)
|
|
||||||
cherrypy.request.params.update(cherrypy.request.json)
|
|
||||||
|
|
||||||
# CherryPy apps
|
# CherryPy apps
|
||||||
class Root(NilmApp):
|
class Root(NilmApp):
|
||||||
"""Root application for NILM database"""
|
"""Root application for NILM database"""
|
||||||
@@ -147,7 +47,10 @@ class Root(NilmApp):
|
|||||||
# /
|
# /
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def index(self):
|
def index(self):
|
||||||
raise cherrypy.NotFound()
|
cherrypy.response.headers['Content-Type'] = 'text/plain'
|
||||||
|
msg = sprintf("This is NilmDB version %s, running on host %s.\n",
|
||||||
|
nilmdb.__version__, socket.getfqdn())
|
||||||
|
return msg
|
||||||
|
|
||||||
# /favicon.ico
|
# /favicon.ico
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
@@ -167,9 +70,13 @@ class Root(NilmApp):
|
|||||||
"""Return a dictionary with the database path,
|
"""Return a dictionary with the database path,
|
||||||
size of the database in bytes, and free disk space in bytes"""
|
size of the database in bytes, and free disk space in bytes"""
|
||||||
path = self.db.get_basepath()
|
path = self.db.get_basepath()
|
||||||
|
usage = psutil.disk_usage(path)
|
||||||
|
dbsize = nilmdb.utils.du(path)
|
||||||
return { "path": path,
|
return { "path": path,
|
||||||
"size": nilmdb.utils.du(path),
|
"size": dbsize,
|
||||||
"free": psutil.disk_usage(path).free }
|
"other": usage.used - dbsize,
|
||||||
|
"reserved": usage.total - usage.used - usage.free,
|
||||||
|
"free": usage.free }
|
||||||
|
|
||||||
class Stream(NilmApp):
|
class Stream(NilmApp):
|
||||||
"""Stream-specific operations"""
|
"""Stream-specific operations"""
|
||||||
@@ -177,10 +84,18 @@ class Stream(NilmApp):
|
|||||||
# Helpers
|
# Helpers
|
||||||
def _get_times(self, start_param, end_param):
|
def _get_times(self, start_param, end_param):
|
||||||
(start, end) = (None, None)
|
(start, end) = (None, None)
|
||||||
if start_param is not None:
|
try:
|
||||||
start = string_to_timestamp(start_param)
|
if start_param is not None:
|
||||||
if end_param is not None:
|
start = string_to_timestamp(start_param)
|
||||||
end = string_to_timestamp(end_param)
|
except Exception:
|
||||||
|
raise cherrypy.HTTPError("400 Bad Request", sprintf(
|
||||||
|
"invalid start (%s): must be a numeric timestamp", start_param))
|
||||||
|
try:
|
||||||
|
if end_param is not None:
|
||||||
|
end = string_to_timestamp(end_param)
|
||||||
|
except Exception:
|
||||||
|
raise cherrypy.HTTPError("400 Bad Request", sprintf(
|
||||||
|
"invalid end (%s): must be a numeric timestamp", end_param))
|
||||||
if start is not None and end is not None:
|
if start is not None and end is not None:
|
||||||
if start >= end:
|
if start >= end:
|
||||||
raise cherrypy.HTTPError(
|
raise cherrypy.HTTPError(
|
||||||
@@ -199,10 +114,10 @@ class Stream(NilmApp):
|
|||||||
layout parameter, just list streams that match the given path
|
layout parameter, just list streams that match the given path
|
||||||
or layout.
|
or layout.
|
||||||
|
|
||||||
If extent is not given, returns a list of lists containing
|
If extended is missing or zero, returns a list of lists
|
||||||
the path and layout: [ path, layout ]
|
containing the path and layout: [ path, layout ]
|
||||||
|
|
||||||
If extended is provided, returns a list of lists containing
|
If extended is true, returns a list of lists containing
|
||||||
extended info: [ path, layout, extent_min, extent_max,
|
extended info: [ path, layout, extent_min, extent_max,
|
||||||
total_rows, total_seconds ]. More data may be added.
|
total_rows, total_seconds ]. More data may be added.
|
||||||
"""
|
"""
|
||||||
@@ -315,6 +230,8 @@ class Stream(NilmApp):
|
|||||||
little-endian and matches the database types (including an
|
little-endian and matches the database types (including an
|
||||||
int64 timestamp).
|
int64 timestamp).
|
||||||
"""
|
"""
|
||||||
|
binary = bool_param(binary)
|
||||||
|
|
||||||
# Important that we always read the input before throwing any
|
# Important that we always read the input before throwing any
|
||||||
# errors, to keep lengths happy for persistent connections.
|
# errors, to keep lengths happy for persistent connections.
|
||||||
# Note that CherryPy 3.2.2 has a bug where this fails for GET
|
# Note that CherryPy 3.2.2 has a bug where this fails for GET
|
||||||
@@ -347,24 +264,34 @@ class Stream(NilmApp):
|
|||||||
# /stream/remove?path=/newton/prep&start=1234567890.0&end=1234567899.0
|
# /stream/remove?path=/newton/prep&start=1234567890.0&end=1234567899.0
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
@cherrypy.tools.json_in()
|
@cherrypy.tools.json_in()
|
||||||
@cherrypy.tools.json_out()
|
|
||||||
@exception_to_httperror(NilmDBError)
|
|
||||||
@cherrypy.tools.CORS_allow(methods = ["POST"])
|
@cherrypy.tools.CORS_allow(methods = ["POST"])
|
||||||
|
@chunked_response
|
||||||
|
@response_type("application/x-json-stream")
|
||||||
def remove(self, path, start = None, end = None):
|
def remove(self, path, start = None, end = None):
|
||||||
"""
|
"""
|
||||||
Remove data from the backend database. Removes all data in
|
Remove data from the backend database. Removes all data in
|
||||||
the interval [start, end). Returns the number of data points
|
the interval [start, end).
|
||||||
removed.
|
|
||||||
|
Returns the number of data points removed. Since this is a potentially
|
||||||
|
long-running operation, multiple numbers may be returned as the
|
||||||
|
data gets removed from the backend database. The total number of
|
||||||
|
points removed is the sum of all of these numbers.
|
||||||
"""
|
"""
|
||||||
(start, end) = self._get_times(start, end)
|
(start, end) = self._get_times(start, end)
|
||||||
total_removed = 0
|
|
||||||
while True:
|
if len(self.db.stream_list(path = path)) != 1:
|
||||||
(removed, restart) = self.db.stream_remove(path, start, end)
|
raise cherrypy.HTTPError("404", "No such stream: " + path)
|
||||||
total_removed += removed
|
|
||||||
if restart is None:
|
@workaround_cp_bug_1200
|
||||||
break
|
def content(start, end):
|
||||||
start = restart
|
# Note: disable chunked responses to see tracebacks from here.
|
||||||
return total_removed
|
while True:
|
||||||
|
(removed, restart) = self.db.stream_remove(path, start, end)
|
||||||
|
yield json.dumps(removed) + "\r\n"
|
||||||
|
if restart is None:
|
||||||
|
break
|
||||||
|
start = restart
|
||||||
|
return content(start, end)
|
||||||
|
|
||||||
# /stream/intervals?path=/newton/prep
|
# /stream/intervals?path=/newton/prep
|
||||||
# /stream/intervals?path=/newton/prep&start=1234567890.0&end=1234567899.0
|
# /stream/intervals?path=/newton/prep&start=1234567890.0&end=1234567899.0
|
||||||
@@ -429,6 +356,10 @@ class Stream(NilmApp):
|
|||||||
little-endian and matches the database types (including an
|
little-endian and matches the database types (including an
|
||||||
int64 timestamp).
|
int64 timestamp).
|
||||||
"""
|
"""
|
||||||
|
binary = bool_param(binary)
|
||||||
|
markup = bool_param(markup)
|
||||||
|
count = bool_param(count)
|
||||||
|
|
||||||
(start, end) = self._get_times(start, end)
|
(start, end) = self._get_times(start, end)
|
||||||
|
|
||||||
# Check path and get layout
|
# Check path and get layout
|
||||||
@@ -556,70 +487,14 @@ class Server(object):
|
|||||||
|
|
||||||
def json_error_page(self, status, message, traceback, version):
|
def json_error_page(self, status, message, traceback, version):
|
||||||
"""Return a custom error page in JSON so the client can parse it"""
|
"""Return a custom error page in JSON so the client can parse it"""
|
||||||
errordata = { "status" : status,
|
return json_error_page(status, message, traceback, version,
|
||||||
"message" : message,
|
self.force_traceback)
|
||||||
"traceback" : traceback }
|
|
||||||
# Don't send a traceback if the error was 400-499 (client's fault)
|
|
||||||
try:
|
|
||||||
code = int(status.split()[0])
|
|
||||||
if not self.force_traceback:
|
|
||||||
if code >= 400 and code <= 499:
|
|
||||||
errordata["traceback"] = ""
|
|
||||||
except Exception: # pragma: no cover
|
|
||||||
pass
|
|
||||||
# Override the response type, which was previously set to text/html
|
|
||||||
cherrypy.serving.response.headers['Content-Type'] = (
|
|
||||||
"application/json;charset=utf-8" )
|
|
||||||
# Undo the HTML escaping that cherrypy's get_error_page function applies
|
|
||||||
# (cherrypy issue 1135)
|
|
||||||
for k, v in errordata.iteritems():
|
|
||||||
v = v.replace("<","<")
|
|
||||||
v = v.replace(">",">")
|
|
||||||
v = v.replace("&","&")
|
|
||||||
errordata[k] = v
|
|
||||||
return json.dumps(errordata, separators=(',',':'))
|
|
||||||
|
|
||||||
def start(self, blocking = False, event = None):
|
def start(self, blocking = False, event = None):
|
||||||
|
cherrypy_start(blocking, event, self.embedded)
|
||||||
if not self.embedded: # pragma: no cover
|
|
||||||
# Handle signals nicely
|
|
||||||
if hasattr(cherrypy.engine, "signal_handler"):
|
|
||||||
cherrypy.engine.signal_handler.subscribe()
|
|
||||||
if hasattr(cherrypy.engine, "console_control_handler"):
|
|
||||||
cherrypy.engine.console_control_handler.subscribe()
|
|
||||||
|
|
||||||
# Cherrypy stupidly calls os._exit(70) when it can't bind the
|
|
||||||
# port. At least try to print a reasonable error and continue
|
|
||||||
# in this case, rather than just dying silently (as we would
|
|
||||||
# otherwise do in embedded mode)
|
|
||||||
real_exit = os._exit
|
|
||||||
def fake_exit(code): # pragma: no cover
|
|
||||||
if code == os.EX_SOFTWARE:
|
|
||||||
fprintf(sys.stderr, "error: CherryPy called os._exit!\n")
|
|
||||||
else:
|
|
||||||
real_exit(code)
|
|
||||||
os._exit = fake_exit
|
|
||||||
cherrypy.engine.start()
|
|
||||||
os._exit = real_exit
|
|
||||||
|
|
||||||
# Signal that the engine has started successfully
|
|
||||||
if event is not None:
|
|
||||||
event.set()
|
|
||||||
|
|
||||||
if blocking:
|
|
||||||
try:
|
|
||||||
cherrypy.engine.wait(cherrypy.engine.states.EXITING,
|
|
||||||
interval = 0.1, channel = 'main')
|
|
||||||
except (KeyboardInterrupt, IOError): # pragma: no cover
|
|
||||||
cherrypy.engine.log('Keyboard Interrupt: shutting down bus')
|
|
||||||
cherrypy.engine.exit()
|
|
||||||
except SystemExit: # pragma: no cover
|
|
||||||
cherrypy.engine.log('SystemExit raised: shutting down bus')
|
|
||||||
cherrypy.engine.exit()
|
|
||||||
raise
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
cherrypy.engine.exit()
|
cherrypy_stop()
|
||||||
|
|
||||||
# Use a single global nilmdb.server.NilmDB and nilmdb.server.Server
|
# Use a single global nilmdb.server.NilmDB and nilmdb.server.Server
|
||||||
# instance since the database can only be opened once. For this to
|
# instance since the database can only be opened once. For this to
|
||||||
|
214
nilmdb/server/serverutil.py
Normal file
214
nilmdb/server/serverutil.py
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
"""Miscellaneous decorators and other helpers for running a CherryPy
|
||||||
|
server"""
|
||||||
|
|
||||||
|
import cherrypy
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import decorator
|
||||||
|
import simplejson as json
|
||||||
|
|
||||||
|
# Helper to parse parameters into booleans
|
||||||
|
def bool_param(s):
|
||||||
|
"""Return a bool indicating whether parameter 's' was True or False,
|
||||||
|
supporting a few different types for 's'."""
|
||||||
|
try:
|
||||||
|
ss = s.lower()
|
||||||
|
if ss in [ "0", "false", "f", "no", "n" ]:
|
||||||
|
return False
|
||||||
|
if ss in [ "1", "true", "t", "yes", "y" ]:
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return bool(s)
|
||||||
|
raise cherrypy.HTTPError("400 Bad Request",
|
||||||
|
"can't parse parameter: " + ss)
|
||||||
|
|
||||||
|
# Decorators
|
||||||
|
def chunked_response(func):
|
||||||
|
"""Decorator to enable chunked responses."""
|
||||||
|
# Set this to False to get better tracebacks from some requests
|
||||||
|
# (/stream/extract, /stream/intervals).
|
||||||
|
func._cp_config = { 'response.stream': True }
|
||||||
|
return func
|
||||||
|
|
||||||
|
def response_type(content_type):
|
||||||
|
"""Return a decorator-generating function that sets the
|
||||||
|
response type to the specified string."""
|
||||||
|
def wrapper(func, *args, **kwargs):
|
||||||
|
cherrypy.response.headers['Content-Type'] = content_type
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
return decorator.decorator(wrapper)
|
||||||
|
|
||||||
|
@decorator.decorator
|
||||||
|
def workaround_cp_bug_1200(func, *args, **kwargs): # pragma: no cover
|
||||||
|
"""Decorator to work around CherryPy bug #1200 in a response
|
||||||
|
generator.
|
||||||
|
|
||||||
|
Even if chunked responses are disabled, LookupError or
|
||||||
|
UnicodeError exceptions may still be swallowed by CherryPy due to
|
||||||
|
bug #1200. This throws them as generic Exceptions instead so that
|
||||||
|
they make it through.
|
||||||
|
"""
|
||||||
|
exc_info = None
|
||||||
|
try:
|
||||||
|
for val in func(*args, **kwargs):
|
||||||
|
yield val
|
||||||
|
except (LookupError, UnicodeError):
|
||||||
|
# Re-raise it, but maintain the original traceback
|
||||||
|
exc_info = sys.exc_info()
|
||||||
|
new_exc = Exception(exc_info[0].__name__ + ": " + str(exc_info[1]))
|
||||||
|
raise new_exc, None, exc_info[2]
|
||||||
|
finally:
|
||||||
|
del exc_info
|
||||||
|
|
||||||
|
def exception_to_httperror(*expected):
|
||||||
|
"""Return a decorator-generating function that catches expected
|
||||||
|
errors and throws a HTTPError describing it instead.
|
||||||
|
|
||||||
|
@exception_to_httperror(NilmDBError, ValueError)
|
||||||
|
def foo():
|
||||||
|
pass
|
||||||
|
"""
|
||||||
|
def wrapper(func, *args, **kwargs):
|
||||||
|
exc_info = None
|
||||||
|
try:
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
except expected:
|
||||||
|
# Re-raise it, but maintain the original traceback
|
||||||
|
exc_info = sys.exc_info()
|
||||||
|
new_exc = cherrypy.HTTPError("400 Bad Request", str(exc_info[1]))
|
||||||
|
raise new_exc, None, exc_info[2]
|
||||||
|
finally:
|
||||||
|
del exc_info
|
||||||
|
# We need to preserve the function's argspecs for CherryPy to
|
||||||
|
# handle argument errors correctly. Decorator.decorator takes
|
||||||
|
# care of that.
|
||||||
|
return decorator.decorator(wrapper)
|
||||||
|
|
||||||
|
# Custom CherryPy tools
|
||||||
|
|
||||||
|
def CORS_allow(methods):
|
||||||
|
"""This does several things:
|
||||||
|
|
||||||
|
Handles CORS preflight requests.
|
||||||
|
Adds Allow: header to all requests.
|
||||||
|
Raise 405 if request.method not in method.
|
||||||
|
|
||||||
|
It is similar to cherrypy.tools.allow, with the CORS stuff added.
|
||||||
|
|
||||||
|
Add this to CherryPy with:
|
||||||
|
cherrypy.tools.CORS_allow = cherrypy.Tool('on_start_resource', CORS_allow)
|
||||||
|
"""
|
||||||
|
request = cherrypy.request.headers
|
||||||
|
response = cherrypy.response.headers
|
||||||
|
|
||||||
|
if not isinstance(methods, (tuple, list)): # pragma: no cover
|
||||||
|
methods = [ methods ]
|
||||||
|
methods = [ m.upper() for m in methods if m ]
|
||||||
|
if not methods: # pragma: no cover
|
||||||
|
methods = [ 'GET', 'HEAD' ]
|
||||||
|
elif 'GET' in methods and 'HEAD' not in methods: # pragma: no cover
|
||||||
|
methods.append('HEAD')
|
||||||
|
response['Allow'] = ', '.join(methods)
|
||||||
|
|
||||||
|
# Allow all origins
|
||||||
|
if 'Origin' in request:
|
||||||
|
response['Access-Control-Allow-Origin'] = request['Origin']
|
||||||
|
|
||||||
|
# If it's a CORS request, send response.
|
||||||
|
request_method = request.get("Access-Control-Request-Method", None)
|
||||||
|
request_headers = request.get("Access-Control-Request-Headers", None)
|
||||||
|
if (cherrypy.request.method == "OPTIONS" and
|
||||||
|
request_method and request_headers):
|
||||||
|
response['Access-Control-Allow-Headers'] = request_headers
|
||||||
|
response['Access-Control-Allow-Methods'] = ', '.join(methods)
|
||||||
|
# Try to stop further processing and return a 200 OK
|
||||||
|
cherrypy.response.status = "200 OK"
|
||||||
|
cherrypy.response.body = ""
|
||||||
|
cherrypy.request.handler = lambda: ""
|
||||||
|
return
|
||||||
|
|
||||||
|
# Reject methods that were not explicitly allowed
|
||||||
|
if cherrypy.request.method not in methods:
|
||||||
|
raise cherrypy.HTTPError(405)
|
||||||
|
|
||||||
|
|
||||||
|
# Helper for json_in tool to process JSON data into normal request
|
||||||
|
# parameters.
|
||||||
|
def json_to_request_params(body):
|
||||||
|
cherrypy.lib.jsontools.json_processor(body)
|
||||||
|
if not isinstance(cherrypy.request.json, dict):
|
||||||
|
raise cherrypy.HTTPError(415)
|
||||||
|
cherrypy.request.params.update(cherrypy.request.json)
|
||||||
|
|
||||||
|
# Used as an "error_page.default" handler
|
||||||
|
def json_error_page(status, message, traceback, version,
|
||||||
|
force_traceback = False):
|
||||||
|
"""Return a custom error page in JSON so the client can parse it"""
|
||||||
|
errordata = { "status" : status,
|
||||||
|
"message" : message,
|
||||||
|
"traceback" : traceback }
|
||||||
|
# Don't send a traceback if the error was 400-499 (client's fault)
|
||||||
|
try:
|
||||||
|
code = int(status.split()[0])
|
||||||
|
if not force_traceback:
|
||||||
|
if code >= 400 and code <= 499:
|
||||||
|
errordata["traceback"] = ""
|
||||||
|
except Exception: # pragma: no cover
|
||||||
|
pass
|
||||||
|
# Override the response type, which was previously set to text/html
|
||||||
|
cherrypy.serving.response.headers['Content-Type'] = (
|
||||||
|
"application/json;charset=utf-8" )
|
||||||
|
# Undo the HTML escaping that cherrypy's get_error_page function applies
|
||||||
|
# (cherrypy issue 1135)
|
||||||
|
for k, v in errordata.iteritems():
|
||||||
|
v = v.replace("<","<")
|
||||||
|
v = v.replace(">",">")
|
||||||
|
v = v.replace("&","&")
|
||||||
|
errordata[k] = v
|
||||||
|
return json.dumps(errordata, separators=(',',':'))
|
||||||
|
|
||||||
|
# Start/stop CherryPy standalone server
|
||||||
|
def cherrypy_start(blocking = False, event = False, embedded = False):
|
||||||
|
"""Start the CherryPy server, handling errors and signals
|
||||||
|
somewhat gracefully."""
|
||||||
|
|
||||||
|
if not embedded: # pragma: no cover
|
||||||
|
# Handle signals nicely
|
||||||
|
if hasattr(cherrypy.engine, "signal_handler"):
|
||||||
|
cherrypy.engine.signal_handler.subscribe()
|
||||||
|
if hasattr(cherrypy.engine, "console_control_handler"):
|
||||||
|
cherrypy.engine.console_control_handler.subscribe()
|
||||||
|
|
||||||
|
# Cherrypy stupidly calls os._exit(70) when it can't bind the
|
||||||
|
# port. At least try to print a reasonable error and continue
|
||||||
|
# in this case, rather than just dying silently (as we would
|
||||||
|
# otherwise do in embedded mode)
|
||||||
|
real_exit = os._exit
|
||||||
|
def fake_exit(code): # pragma: no cover
|
||||||
|
if code == os.EX_SOFTWARE:
|
||||||
|
fprintf(sys.stderr, "error: CherryPy called os._exit!\n")
|
||||||
|
else:
|
||||||
|
real_exit(code)
|
||||||
|
os._exit = fake_exit
|
||||||
|
cherrypy.engine.start()
|
||||||
|
os._exit = real_exit
|
||||||
|
|
||||||
|
# Signal that the engine has started successfully
|
||||||
|
if event is not None:
|
||||||
|
event.set()
|
||||||
|
|
||||||
|
if blocking:
|
||||||
|
try:
|
||||||
|
cherrypy.engine.wait(cherrypy.engine.states.EXITING,
|
||||||
|
interval = 0.1, channel = 'main')
|
||||||
|
except (KeyboardInterrupt, IOError): # pragma: no cover
|
||||||
|
cherrypy.engine.log('Keyboard Interrupt: shutting down bus')
|
||||||
|
cherrypy.engine.exit()
|
||||||
|
except SystemExit: # pragma: no cover
|
||||||
|
cherrypy.engine.log('SystemExit raised: shutting down bus')
|
||||||
|
cherrypy.engine.exit()
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Stop CherryPy server
|
||||||
|
def cherrypy_stop():
|
||||||
|
cherrypy.engine.exit()
|
@@ -13,3 +13,5 @@ import nilmdb.utils.time
|
|||||||
import nilmdb.utils.iterator
|
import nilmdb.utils.iterator
|
||||||
import nilmdb.utils.interval
|
import nilmdb.utils.interval
|
||||||
import nilmdb.utils.lock
|
import nilmdb.utils.lock
|
||||||
|
import nilmdb.utils.sort
|
||||||
|
import nilmdb.utils.unicode
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import errno
|
||||||
from math import log
|
from math import log
|
||||||
|
|
||||||
def human_size(num):
|
def human_size(num):
|
||||||
@@ -16,10 +17,17 @@ def human_size(num):
|
|||||||
return '1 byte'
|
return '1 byte'
|
||||||
|
|
||||||
def du(path):
|
def du(path):
|
||||||
"""Like du -sb, returns total size of path in bytes."""
|
"""Like du -sb, returns total size of path in bytes. Ignore
|
||||||
size = os.path.getsize(path)
|
errors that might occur if we encounter broken symlinks or
|
||||||
if os.path.isdir(path):
|
files in the process of being removed."""
|
||||||
for thisfile in os.listdir(path):
|
try:
|
||||||
filepath = os.path.join(path, thisfile)
|
size = os.path.getsize(path)
|
||||||
size += du(filepath)
|
if os.path.isdir(path):
|
||||||
return size
|
for thisfile in os.listdir(path):
|
||||||
|
filepath = os.path.join(path, thisfile)
|
||||||
|
size += du(filepath)
|
||||||
|
return size
|
||||||
|
except OSError as e: # pragma: no cover
|
||||||
|
if e.errno != errno.ENOENT:
|
||||||
|
raise
|
||||||
|
return 0
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
"""Interval. Like nilmdb.server.interval, but re-implemented here
|
"""Interval. Like nilmdb.server.interval, but re-implemented here
|
||||||
in plain Python so clients have easier access to it.
|
in plain Python so clients have easier access to it, and with a few
|
||||||
|
helper functions.
|
||||||
|
|
||||||
Intervals are half-open, ie. they include data points with timestamps
|
Intervals are half-open, ie. they include data points with timestamps
|
||||||
[start, end)
|
[start, end)
|
||||||
@@ -34,6 +35,10 @@ class Interval:
|
|||||||
return ("[" + nilmdb.utils.time.timestamp_to_string(self.start) +
|
return ("[" + nilmdb.utils.time.timestamp_to_string(self.start) +
|
||||||
" -> " + nilmdb.utils.time.timestamp_to_string(self.end) + ")")
|
" -> " + nilmdb.utils.time.timestamp_to_string(self.end) + ")")
|
||||||
|
|
||||||
|
def human_string(self):
|
||||||
|
return ("[ " + nilmdb.utils.time.timestamp_to_human(self.start) +
|
||||||
|
" -> " + nilmdb.utils.time.timestamp_to_human(self.end) + " ]")
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __cmp__(self, other):
|
||||||
"""Compare two intervals. If non-equal, order by start then end"""
|
"""Compare two intervals. If non-equal, order by start then end"""
|
||||||
return cmp(self.start, other.start) or cmp(self.end, other.end)
|
return cmp(self.start, other.start) or cmp(self.end, other.end)
|
||||||
@@ -53,18 +58,11 @@ class Interval:
|
|||||||
raise IntervalError("not a subset")
|
raise IntervalError("not a subset")
|
||||||
return Interval(start, end)
|
return Interval(start, end)
|
||||||
|
|
||||||
def set_difference(a, b):
|
def _interval_math_helper(a, b, op, subset = True):
|
||||||
"""
|
"""Helper for set_difference, intersection functions,
|
||||||
Compute the difference (a \\ b) between the intervals in 'a' and
|
to compute interval subsets based on a math operator on ranges
|
||||||
the intervals in 'b'; i.e., the ranges that are present in 'self'
|
present in A and B. Subsets are computed from A, or new intervals
|
||||||
but not 'other'.
|
are generated if subset = False."""
|
||||||
|
|
||||||
'a' and 'b' must both be iterables.
|
|
||||||
|
|
||||||
Returns a generator that yields each interval in turn.
|
|
||||||
Output intervals are built as subsets of the intervals in the
|
|
||||||
first argument (a).
|
|
||||||
"""
|
|
||||||
# Iterate through all starts and ends in sorted order. Add a
|
# Iterate through all starts and ends in sorted order. Add a
|
||||||
# tag to the iterator so that we can figure out which one they
|
# tag to the iterator so that we can figure out which one they
|
||||||
# were, after sorting.
|
# were, after sorting.
|
||||||
@@ -79,28 +77,71 @@ def set_difference(a, b):
|
|||||||
# At each point, evaluate which type of end it is, to determine
|
# At each point, evaluate which type of end it is, to determine
|
||||||
# how to build up the output intervals.
|
# how to build up the output intervals.
|
||||||
a_interval = None
|
a_interval = None
|
||||||
b_interval = None
|
in_a = False
|
||||||
|
in_b = False
|
||||||
out_start = None
|
out_start = None
|
||||||
for (ts, k, i) in nilmdb.utils.iterator.imerge(a_iter, b_iter):
|
for (ts, k, i) in nilmdb.utils.iterator.imerge(a_iter, b_iter):
|
||||||
if k == 0:
|
if k == 0:
|
||||||
# start a interval
|
|
||||||
a_interval = i
|
a_interval = i
|
||||||
if b_interval is None:
|
in_a = True
|
||||||
out_start = ts
|
|
||||||
elif k == 1:
|
elif k == 1:
|
||||||
# start b interval
|
in_b = True
|
||||||
b_interval = i
|
|
||||||
if out_start is not None and out_start != ts:
|
|
||||||
yield a_interval.subset(out_start, ts)
|
|
||||||
out_start = None
|
|
||||||
elif k == 2:
|
elif k == 2:
|
||||||
# end a interval
|
in_a = False
|
||||||
if out_start is not None and out_start != ts:
|
|
||||||
yield a_interval.subset(out_start, ts)
|
|
||||||
out_start = None
|
|
||||||
a_interval = None
|
|
||||||
elif k == 3:
|
elif k == 3:
|
||||||
# end b interval
|
in_b = False
|
||||||
b_interval = None
|
include = op(in_a, in_b)
|
||||||
if a_interval:
|
if include and out_start is None:
|
||||||
out_start = ts
|
out_start = ts
|
||||||
|
elif not include:
|
||||||
|
if out_start is not None and out_start != ts:
|
||||||
|
if subset:
|
||||||
|
yield a_interval.subset(out_start, ts)
|
||||||
|
else:
|
||||||
|
yield Interval(out_start, ts)
|
||||||
|
out_start = None
|
||||||
|
|
||||||
|
def set_difference(a, b):
|
||||||
|
"""
|
||||||
|
Compute the difference (a \\ b) between the intervals in 'a' and
|
||||||
|
the intervals in 'b'; i.e., the ranges that are present in 'self'
|
||||||
|
but not 'other'.
|
||||||
|
|
||||||
|
'a' and 'b' must both be iterables.
|
||||||
|
|
||||||
|
Returns a generator that yields each interval in turn.
|
||||||
|
Output intervals are built as subsets of the intervals in the
|
||||||
|
first argument (a).
|
||||||
|
"""
|
||||||
|
return _interval_math_helper(a, b, (lambda a, b: a and not b))
|
||||||
|
|
||||||
|
def intersection(a, b):
|
||||||
|
"""
|
||||||
|
Compute the intersection between the intervals in 'a' and the
|
||||||
|
intervals in 'b'; i.e., the ranges that are present in both 'a'
|
||||||
|
and 'b'.
|
||||||
|
|
||||||
|
'a' and 'b' must both be iterables.
|
||||||
|
|
||||||
|
Returns a generator that yields each interval in turn.
|
||||||
|
Output intervals are built as subsets of the intervals in the
|
||||||
|
first argument (a).
|
||||||
|
"""
|
||||||
|
return _interval_math_helper(a, b, (lambda a, b: a and b))
|
||||||
|
|
||||||
|
def optimize(it):
|
||||||
|
"""
|
||||||
|
Given an iterable 'it' with intervals, optimize them by joining
|
||||||
|
together intervals that are adjacent in time, and return a generator
|
||||||
|
that yields the new intervals.
|
||||||
|
"""
|
||||||
|
saved_int = None
|
||||||
|
for interval in it:
|
||||||
|
if saved_int is not None:
|
||||||
|
if saved_int.end == interval.start:
|
||||||
|
interval.start = saved_int.start
|
||||||
|
else:
|
||||||
|
yield saved_int
|
||||||
|
saved_int = interval
|
||||||
|
if saved_int is not None:
|
||||||
|
yield saved_int
|
||||||
|
@@ -91,6 +91,20 @@ def serializer_proxy(obj_or_type):
|
|||||||
r = SerializerCallProxy(self.__call_queue, attr, self)
|
r = SerializerCallProxy(self.__call_queue, attr, self)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
# For an interable object, on __iter__(), save the object's
|
||||||
|
# iterator and return this proxy. On next(), call the object's
|
||||||
|
# iterator through this proxy.
|
||||||
|
def __iter__(self):
|
||||||
|
attr = getattr(self.__object, "__iter__")
|
||||||
|
self.__iter = SerializerCallProxy(self.__call_queue, attr, self)()
|
||||||
|
return self
|
||||||
|
def next(self):
|
||||||
|
return SerializerCallProxy(self.__call_queue,
|
||||||
|
self.__iter.next, self)()
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return self.__getattr__("__getitem__")(key)
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
"""Call this to instantiate the type, if a type was passed
|
"""Call this to instantiate the type, if a type was passed
|
||||||
to serializer_proxy. Otherwise, pass the call through."""
|
to serializer_proxy. Otherwise, pass the call through."""
|
||||||
|
18
nilmdb/utils/sort.py
Normal file
18
nilmdb/utils/sort.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
def sort_human(items, key = None):
|
||||||
|
"""Human-friendly sort (/stream/2 before /stream/10)"""
|
||||||
|
def to_num(val):
|
||||||
|
try:
|
||||||
|
return int(val)
|
||||||
|
except Exception:
|
||||||
|
return val
|
||||||
|
|
||||||
|
def human_key(text):
|
||||||
|
if key:
|
||||||
|
text = key(text)
|
||||||
|
# Break into character and numeric chunks.
|
||||||
|
chunks = re.split(r'([0-9]+)', text)
|
||||||
|
return [ to_num(c) for c in chunks ]
|
||||||
|
|
||||||
|
return sorted(items, key = human_key)
|
@@ -6,7 +6,7 @@ import time
|
|||||||
|
|
||||||
# Range
|
# Range
|
||||||
min_timestamp = (-2**63)
|
min_timestamp = (-2**63)
|
||||||
max_timestamp = (2**62 - 1)
|
max_timestamp = (2**63 - 1)
|
||||||
|
|
||||||
# Smallest representable step
|
# Smallest representable step
|
||||||
epsilon = 1
|
epsilon = 1
|
||||||
@@ -32,6 +32,10 @@ def timestamp_to_human(timestamp):
|
|||||||
"""Convert a timestamp (integer microseconds since epoch) to a
|
"""Convert a timestamp (integer microseconds since epoch) to a
|
||||||
human-readable string, using the local timezone for display
|
human-readable string, using the local timezone for display
|
||||||
(e.g. from the TZ env var)."""
|
(e.g. from the TZ env var)."""
|
||||||
|
if timestamp == min_timestamp:
|
||||||
|
return "(minimum)"
|
||||||
|
if timestamp == max_timestamp:
|
||||||
|
return "(maximum)"
|
||||||
dt = datetime_tz.datetime_tz.fromtimestamp(timestamp_to_unix(timestamp))
|
dt = datetime_tz.datetime_tz.fromtimestamp(timestamp_to_unix(timestamp))
|
||||||
return dt.strftime("%a, %d %b %Y %H:%M:%S.%f %z")
|
return dt.strftime("%a, %d %b %Y %H:%M:%S.%f %z")
|
||||||
|
|
||||||
@@ -56,7 +60,7 @@ def rate_to_period(hz, cycles = 1):
|
|||||||
def parse_time(toparse):
|
def parse_time(toparse):
|
||||||
"""
|
"""
|
||||||
Parse a free-form time string and return a nilmdb timestamp
|
Parse a free-form time string and return a nilmdb timestamp
|
||||||
(integer seconds since epoch). If the string doesn't contain a
|
(integer microseconds since epoch). If the string doesn't contain a
|
||||||
timestamp, the current local timezone is assumed (e.g. from the TZ
|
timestamp, the current local timezone is assumed (e.g. from the TZ
|
||||||
env var).
|
env var).
|
||||||
"""
|
"""
|
||||||
|
29
nilmdb/utils/unicode.py
Normal file
29
nilmdb/utils/unicode.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
if sys.version_info[0] >= 3: # pragma: no cover (future Python3 compat)
|
||||||
|
text_type = str
|
||||||
|
else:
|
||||||
|
text_type = unicode
|
||||||
|
|
||||||
|
def encode(u):
|
||||||
|
"""Try to encode something from Unicode to a string using the
|
||||||
|
default encoding. If it fails, try encoding as UTF-8."""
|
||||||
|
if not isinstance(u, text_type):
|
||||||
|
return u
|
||||||
|
try:
|
||||||
|
return u.encode()
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
return u.encode("utf-8")
|
||||||
|
|
||||||
|
def decode(s):
|
||||||
|
"""Try to decode someting from string to Unicode using the
|
||||||
|
default encoding. If it fails, try decoding as UTF-8."""
|
||||||
|
if isinstance(s, text_type):
|
||||||
|
return s
|
||||||
|
try:
|
||||||
|
return s.decode()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
try:
|
||||||
|
return s.decode("utf-8")
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
return s # best we can do
|
3
setup.py
3
setup.py
@@ -118,6 +118,7 @@ setup(name='nilmdb',
|
|||||||
'pytz',
|
'pytz',
|
||||||
'psutil >= 0.3.0',
|
'psutil >= 0.3.0',
|
||||||
'requests >= 1.1.0, < 2.0.0',
|
'requests >= 1.1.0, < 2.0.0',
|
||||||
|
'progressbar >= 2.2',
|
||||||
],
|
],
|
||||||
packages = [ 'nilmdb',
|
packages = [ 'nilmdb',
|
||||||
'nilmdb.utils',
|
'nilmdb.utils',
|
||||||
@@ -126,11 +127,13 @@ setup(name='nilmdb',
|
|||||||
'nilmdb.client',
|
'nilmdb.client',
|
||||||
'nilmdb.cmdline',
|
'nilmdb.cmdline',
|
||||||
'nilmdb.scripts',
|
'nilmdb.scripts',
|
||||||
|
'nilmdb.fsck',
|
||||||
],
|
],
|
||||||
entry_points = {
|
entry_points = {
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
'nilmtool = nilmdb.scripts.nilmtool:main',
|
'nilmtool = nilmdb.scripts.nilmtool:main',
|
||||||
'nilmdb-server = nilmdb.scripts.nilmdb_server:main',
|
'nilmdb-server = nilmdb.scripts.nilmdb_server:main',
|
||||||
|
'nilmdb-fsck = nilmdb.scripts.nilmdb_fsck:main',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
ext_modules = ext_modules,
|
ext_modules = ext_modules,
|
||||||
|
8
tests/data/timestamped
Normal file
8
tests/data/timestamped
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
-10000000000 2.61246e+05 2.22735e+05 4.60340e+03 2.58221e+03 8.42804e+03 3.41890e+03 9.57898e+02 4.00585e+03
|
||||||
|
-100000000 2.61246e+05 2.22735e+05 4.60340e+03 2.58221e+03 8.42804e+03 3.41890e+03 9.57898e+02 4.00585e+03
|
||||||
|
-100000 2.61246e+05 2.22735e+05 4.60340e+03 2.58221e+03 8.42804e+03 3.41890e+03 9.57898e+02 4.00585e+03
|
||||||
|
-1000 2.61246e+05 2.22735e+05 4.60340e+03 2.58221e+03 8.42804e+03 3.41890e+03 9.57898e+02 4.00585e+03
|
||||||
|
1 2.61246e+05 2.22735e+05 4.60340e+03 2.58221e+03 8.42804e+03 3.41890e+03 9.57898e+02 4.00585e+03
|
||||||
|
1000 2.61246e+05 2.22735e+05 4.60340e+03 2.58221e+03 8.42804e+03 3.41890e+03 9.57898e+02 4.00585e+03
|
||||||
|
1000000 2.61246e+05 2.22735e+05 4.60340e+03 2.58221e+03 8.42804e+03 3.41890e+03 9.57898e+02 4.00585e+03
|
||||||
|
1000000000 2.61246e+05 2.22735e+05 4.60340e+03 2.58221e+03 8.42804e+03 3.41890e+03 9.57898e+02 4.00585e+03
|
@@ -105,16 +105,19 @@ class TestClient(object):
|
|||||||
client.http.post("/stream/list")
|
client.http.post("/stream/list")
|
||||||
client = nilmdb.client.Client(url = testurl)
|
client = nilmdb.client.Client(url = testurl)
|
||||||
|
|
||||||
# Create three streams
|
# Create four streams
|
||||||
client.stream_create("/newton/prep", "float32_8")
|
client.stream_create("/newton/prep", "float32_8")
|
||||||
client.stream_create("/newton/raw", "uint16_6")
|
client.stream_create("/newton/raw", "uint16_6")
|
||||||
client.stream_create("/newton/zzz/rawnotch", "uint16_9")
|
client.stream_create("/newton/zzz/rawnotch2", "uint16_9")
|
||||||
|
client.stream_create("/newton/zzz/rawnotch11", "uint16_9")
|
||||||
|
|
||||||
# Verify we got 3 streams
|
# Verify we got 4 streams in the right order
|
||||||
eq_(client.stream_list(), [ ["/newton/prep", "float32_8"],
|
eq_(client.stream_list(), [ ["/newton/prep", "float32_8"],
|
||||||
["/newton/raw", "uint16_6"],
|
["/newton/raw", "uint16_6"],
|
||||||
["/newton/zzz/rawnotch", "uint16_9"]
|
["/newton/zzz/rawnotch2", "uint16_9"],
|
||||||
|
["/newton/zzz/rawnotch11", "uint16_9"]
|
||||||
])
|
])
|
||||||
|
|
||||||
# Match just one type or one path
|
# Match just one type or one path
|
||||||
eq_(client.stream_list(layout="uint16_6"),
|
eq_(client.stream_list(layout="uint16_6"),
|
||||||
[ ["/newton/raw", "uint16_6"] ])
|
[ ["/newton/raw", "uint16_6"] ])
|
||||||
@@ -239,6 +242,19 @@ class TestClient(object):
|
|||||||
in_("400 Bad Request", str(e.exception))
|
in_("400 Bad Request", str(e.exception))
|
||||||
in_("start must precede end", str(e.exception))
|
in_("start must precede end", str(e.exception))
|
||||||
|
|
||||||
|
# Invalid times in HTTP request
|
||||||
|
with assert_raises(ClientError) as e:
|
||||||
|
client.http.put("stream/insert", "", { "path": "/newton/prep",
|
||||||
|
"start": "asdf", "end": 0 })
|
||||||
|
in_("400 Bad Request", str(e.exception))
|
||||||
|
in_("invalid start", str(e.exception))
|
||||||
|
|
||||||
|
with assert_raises(ClientError) as e:
|
||||||
|
client.http.put("stream/insert", "", { "path": "/newton/prep",
|
||||||
|
"start": 0, "end": "asdf" })
|
||||||
|
in_("400 Bad Request", str(e.exception))
|
||||||
|
in_("invalid end", str(e.exception))
|
||||||
|
|
||||||
# Good content type
|
# Good content type
|
||||||
with assert_raises(ClientError) as e:
|
with assert_raises(ClientError) as e:
|
||||||
client.http.put("stream/insert", "",
|
client.http.put("stream/insert", "",
|
||||||
@@ -327,6 +343,10 @@ class TestClient(object):
|
|||||||
2525.169921875, 8350.83984375, 3724.699951171875,
|
2525.169921875, 8350.83984375, 3724.699951171875,
|
||||||
1355.3399658203125, 2039.0))
|
1355.3399658203125, 2039.0))
|
||||||
|
|
||||||
|
# Just get some coverage
|
||||||
|
with assert_raises(ClientError) as e:
|
||||||
|
client.http.post("/stream/remove", { "path": "/none" })
|
||||||
|
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
def test_client_06_generators(self):
|
def test_client_06_generators(self):
|
||||||
@@ -347,10 +367,6 @@ class TestClient(object):
|
|||||||
with assert_raises(ServerError) as e:
|
with assert_raises(ServerError) as e:
|
||||||
client.http.get_gen("http://nosuchurl.example.com./").next()
|
client.http.get_gen("http://nosuchurl.example.com./").next()
|
||||||
|
|
||||||
# Trigger a curl error in generator
|
|
||||||
with assert_raises(ServerError) as e:
|
|
||||||
client.http.get_gen("http://nosuchurl.example.com./").next()
|
|
||||||
|
|
||||||
# Check 404 for missing streams
|
# Check 404 for missing streams
|
||||||
for function in [ client.stream_intervals, client.stream_extract ]:
|
for function in [ client.stream_intervals, client.stream_extract ]:
|
||||||
with assert_raises(ClientError) as e:
|
with assert_raises(ClientError) as e:
|
||||||
@@ -389,27 +405,38 @@ class TestClient(object):
|
|||||||
headers())
|
headers())
|
||||||
|
|
||||||
# Extract
|
# Extract
|
||||||
x = http.get("stream/extract",
|
x = http.get("stream/extract", { "path": "/newton/prep",
|
||||||
{ "path": "/newton/prep",
|
"start": "123", "end": "124" })
|
||||||
"start": "123",
|
|
||||||
"end": "124" })
|
|
||||||
if "transfer-encoding: chunked" not in headers():
|
if "transfer-encoding: chunked" not in headers():
|
||||||
warnings.warn("Non-chunked HTTP response for /stream/extract")
|
warnings.warn("Non-chunked HTTP response for /stream/extract")
|
||||||
if "content-type: text/plain;charset=utf-8" not in headers():
|
if "content-type: text/plain;charset=utf-8" not in headers():
|
||||||
raise AssertionError("/stream/extract is not text/plain:\n" +
|
raise AssertionError("/stream/extract is not text/plain:\n" +
|
||||||
headers())
|
headers())
|
||||||
|
|
||||||
x = http.get("stream/extract",
|
x = http.get("stream/extract", { "path": "/newton/prep",
|
||||||
{ "path": "/newton/prep",
|
"start": "123", "end": "124",
|
||||||
"start": "123",
|
"binary": "1" })
|
||||||
"end": "124",
|
|
||||||
"binary": "1" })
|
|
||||||
if "transfer-encoding: chunked" not in headers():
|
if "transfer-encoding: chunked" not in headers():
|
||||||
warnings.warn("Non-chunked HTTP response for /stream/extract")
|
warnings.warn("Non-chunked HTTP response for /stream/extract")
|
||||||
if "content-type: application/octet-stream" not in headers():
|
if "content-type: application/octet-stream" not in headers():
|
||||||
raise AssertionError("/stream/extract is not binary:\n" +
|
raise AssertionError("/stream/extract is not binary:\n" +
|
||||||
headers())
|
headers())
|
||||||
|
|
||||||
|
# Make sure a binary of "0" is really off
|
||||||
|
x = http.get("stream/extract", { "path": "/newton/prep",
|
||||||
|
"start": "123", "end": "124",
|
||||||
|
"binary": "0" })
|
||||||
|
if "content-type: application/octet-stream" in headers():
|
||||||
|
raise AssertionError("/stream/extract is not text:\n" +
|
||||||
|
headers())
|
||||||
|
|
||||||
|
# Invalid parameters
|
||||||
|
with assert_raises(ClientError) as e:
|
||||||
|
x = http.get("stream/extract", { "path": "/newton/prep",
|
||||||
|
"start": "123", "end": "124",
|
||||||
|
"binary": "asdfasfd" })
|
||||||
|
in_("can't parse parameter", str(e.exception))
|
||||||
|
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
def test_client_08_unicode(self):
|
def test_client_08_unicode(self):
|
||||||
@@ -613,8 +640,12 @@ class TestClient(object):
|
|||||||
with client.stream_insert_context("/empty/test", end = 950):
|
with client.stream_insert_context("/empty/test", end = 950):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Equal start and end is OK as long as there's no data
|
||||||
|
with client.stream_insert_context("/empty/test", start=9, end=9):
|
||||||
|
pass
|
||||||
|
|
||||||
# Try various things that might cause problems
|
# Try various things that might cause problems
|
||||||
with client.stream_insert_context("/empty/test", 1000, 1050):
|
with client.stream_insert_context("/empty/test", 1000, 1050) as ctx:
|
||||||
ctx.finalize() # inserts [1000, 1050]
|
ctx.finalize() # inserts [1000, 1050]
|
||||||
ctx.finalize() # nothing
|
ctx.finalize() # nothing
|
||||||
ctx.finalize() # nothing
|
ctx.finalize() # nothing
|
||||||
|
@@ -59,8 +59,7 @@ class TestCmdline(object):
|
|||||||
|
|
||||||
def run(self, arg_string, infile=None, outfile=None):
|
def run(self, arg_string, infile=None, outfile=None):
|
||||||
"""Run a cmdline client with the specified argument string,
|
"""Run a cmdline client with the specified argument string,
|
||||||
passing the given input. Returns a tuple with the output and
|
passing the given input. Save the output and exit code."""
|
||||||
exit code"""
|
|
||||||
# printf("TZ=UTC ./nilmtool.py %s\n", arg_string)
|
# printf("TZ=UTC ./nilmtool.py %s\n", arg_string)
|
||||||
os.environ['NILMDB_URL'] = "http://localhost:32180/"
|
os.environ['NILMDB_URL'] = "http://localhost:32180/"
|
||||||
class stdio_wrapper:
|
class stdio_wrapper:
|
||||||
@@ -88,7 +87,7 @@ class TestCmdline(object):
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
except SystemExit as e:
|
except SystemExit as e:
|
||||||
exitcode = e.code
|
exitcode = e.code
|
||||||
captured = outfile.getvalue()
|
captured = nilmdb.utils.unicode.decode(outfile.getvalue())
|
||||||
self.captured = captured
|
self.captured = captured
|
||||||
self.exitcode = exitcode
|
self.exitcode = exitcode
|
||||||
|
|
||||||
@@ -160,6 +159,12 @@ class TestCmdline(object):
|
|||||||
self.ok("--help")
|
self.ok("--help")
|
||||||
self.contain("usage:")
|
self.contain("usage:")
|
||||||
|
|
||||||
|
# help
|
||||||
|
self.ok("--version")
|
||||||
|
ver = self.captured
|
||||||
|
self.ok("list --version")
|
||||||
|
eq_(self.captured, ver)
|
||||||
|
|
||||||
# fail for no args
|
# fail for no args
|
||||||
self.fail("")
|
self.fail("")
|
||||||
|
|
||||||
@@ -245,8 +250,10 @@ class TestCmdline(object):
|
|||||||
self.contain("Client version: " + nilmdb.__version__)
|
self.contain("Client version: " + nilmdb.__version__)
|
||||||
self.contain("Server version: " + test_server.version)
|
self.contain("Server version: " + test_server.version)
|
||||||
self.contain("Server database path")
|
self.contain("Server database path")
|
||||||
self.contain("Server database size")
|
self.contain("Server disk space used by NilmDB")
|
||||||
self.contain("Server database free space")
|
self.contain("Server disk space used by other")
|
||||||
|
self.contain("Server disk space reserved")
|
||||||
|
self.contain("Server disk space free")
|
||||||
|
|
||||||
def test_04_createlist(self):
|
def test_04_createlist(self):
|
||||||
# Basic stream tests, like those in test_client.
|
# Basic stream tests, like those in test_client.
|
||||||
@@ -300,38 +307,19 @@ class TestCmdline(object):
|
|||||||
|
|
||||||
# Verify we got those 3 streams and they're returned in
|
# Verify we got those 3 streams and they're returned in
|
||||||
# alphabetical order.
|
# alphabetical order.
|
||||||
self.ok("list")
|
self.ok("list -l")
|
||||||
self.match("/newton/prep float32_8\n"
|
self.match("/newton/prep float32_8\n"
|
||||||
"/newton/raw uint16_6\n"
|
"/newton/raw uint16_6\n"
|
||||||
"/newton/zzz/rawnotch uint16_9\n")
|
"/newton/zzz/rawnotch uint16_9\n")
|
||||||
|
|
||||||
# Match just one type or one path. Also check
|
# Match just one type or one path. Also check
|
||||||
# that --path is optional
|
# that --path is optional
|
||||||
self.ok("list --path /newton/raw")
|
self.ok("list --layout /newton/raw")
|
||||||
self.match("/newton/raw uint16_6\n")
|
|
||||||
|
|
||||||
self.ok("list /newton/raw")
|
|
||||||
self.match("/newton/raw uint16_6\n")
|
|
||||||
|
|
||||||
self.fail("list -p /newton/raw /newton/raw")
|
|
||||||
self.contain("too many paths")
|
|
||||||
|
|
||||||
self.ok("list --layout uint16_6")
|
|
||||||
self.match("/newton/raw uint16_6\n")
|
self.match("/newton/raw uint16_6\n")
|
||||||
|
|
||||||
# Wildcard matches
|
# Wildcard matches
|
||||||
self.ok("list --layout uint16*")
|
self.ok("list *zzz*")
|
||||||
self.match("/newton/raw uint16_6\n"
|
self.match("/newton/zzz/rawnotch\n")
|
||||||
"/newton/zzz/rawnotch uint16_9\n")
|
|
||||||
|
|
||||||
self.ok("list --path *zzz* --layout uint16*")
|
|
||||||
self.match("/newton/zzz/rawnotch uint16_9\n")
|
|
||||||
|
|
||||||
self.ok("list *zzz* --layout uint16*")
|
|
||||||
self.match("/newton/zzz/rawnotch uint16_9\n")
|
|
||||||
|
|
||||||
self.ok("list --path *zzz* --layout float32*")
|
|
||||||
self.match("")
|
|
||||||
|
|
||||||
# reversed range
|
# reversed range
|
||||||
self.fail("list /newton/prep --start 2020-01-01 --end 2000-01-01")
|
self.fail("list /newton/prep --start 2020-01-01 --end 2000-01-01")
|
||||||
@@ -492,33 +480,40 @@ class TestCmdline(object):
|
|||||||
# bad start time
|
# bad start time
|
||||||
self.fail("insert -t -r 120 --start 'whatever' /newton/prep /dev/null")
|
self.fail("insert -t -r 120 --start 'whatever' /newton/prep /dev/null")
|
||||||
|
|
||||||
|
# Test negative times
|
||||||
|
self.ok("insert --start @-10000000000 --end @1000000001 /newton/prep"
|
||||||
|
" tests/data/timestamped")
|
||||||
|
self.ok("extract -c /newton/prep --start min --end @1000000001")
|
||||||
|
self.match("8\n")
|
||||||
|
self.ok("remove /newton/prep --start min --end @1000000001")
|
||||||
|
|
||||||
def test_07_detail_extended(self):
|
def test_07_detail_extended(self):
|
||||||
# Just count the number of lines, it's probably fine
|
# Just count the number of lines, it's probably fine
|
||||||
self.ok("list --detail")
|
self.ok("list --detail")
|
||||||
lines_(self.captured, 8)
|
lines_(self.captured, 8)
|
||||||
|
|
||||||
self.ok("list --detail --path *prep")
|
self.ok("list --detail *prep")
|
||||||
lines_(self.captured, 4)
|
lines_(self.captured, 4)
|
||||||
|
|
||||||
self.ok("list --detail --path *prep --start='23 Mar 2012 10:02'")
|
self.ok("list --detail *prep --start='23 Mar 2012 10:02'")
|
||||||
lines_(self.captured, 3)
|
lines_(self.captured, 3)
|
||||||
|
|
||||||
self.ok("list --detail --path *prep --start='23 Mar 2012 10:05'")
|
self.ok("list --detail *prep --start='23 Mar 2012 10:05'")
|
||||||
lines_(self.captured, 2)
|
lines_(self.captured, 2)
|
||||||
|
|
||||||
self.ok("list --detail --path *prep --start='23 Mar 2012 10:05:15'")
|
self.ok("list --detail *prep --start='23 Mar 2012 10:05:15'")
|
||||||
lines_(self.captured, 2)
|
lines_(self.captured, 2)
|
||||||
self.contain("10:05:15.000")
|
self.contain("10:05:15.000")
|
||||||
|
|
||||||
self.ok("list --detail --path *prep --start='23 Mar 2012 10:05:15.50'")
|
self.ok("list --detail *prep --start='23 Mar 2012 10:05:15.50'")
|
||||||
lines_(self.captured, 2)
|
lines_(self.captured, 2)
|
||||||
self.contain("10:05:15.500")
|
self.contain("10:05:15.500")
|
||||||
|
|
||||||
self.ok("list --detail --path *prep --start='23 Mar 2012 19:05:15.50'")
|
self.ok("list --detail *prep --start='23 Mar 2012 19:05:15.50'")
|
||||||
lines_(self.captured, 2)
|
lines_(self.captured, 2)
|
||||||
self.contain("no intervals")
|
self.contain("no intervals")
|
||||||
|
|
||||||
self.ok("list --detail --path *prep --start='23 Mar 2012 10:05:15.50'"
|
self.ok("list --detail *prep --start='23 Mar 2012 10:05:15.50'"
|
||||||
+ " --end='23 Mar 2012 10:05:15.51'")
|
+ " --end='23 Mar 2012 10:05:15.51'")
|
||||||
lines_(self.captured, 2)
|
lines_(self.captured, 2)
|
||||||
self.contain("10:05:15.500")
|
self.contain("10:05:15.500")
|
||||||
@@ -527,15 +522,15 @@ class TestCmdline(object):
|
|||||||
lines_(self.captured, 8)
|
lines_(self.captured, 8)
|
||||||
|
|
||||||
# Verify the "raw timestamp" output
|
# Verify the "raw timestamp" output
|
||||||
self.ok("list --detail --path *prep --timestamp-raw "
|
self.ok("list --detail *prep --timestamp-raw "
|
||||||
"--start='23 Mar 2012 10:05:15.50'")
|
"--start='23 Mar 2012 10:05:15.50'")
|
||||||
lines_(self.captured, 2)
|
lines_(self.captured, 2)
|
||||||
self.contain("[ 1332497115500000 -> 1332497160000000 ]")
|
self.contain("[ 1332497115500000 -> 1332497160000000 ]")
|
||||||
|
|
||||||
# bad time
|
# bad time
|
||||||
self.fail("list --detail --path *prep -T --start='9332497115.612'")
|
self.fail("list --detail *prep -T --start='9332497115.612'")
|
||||||
# good time
|
# good time
|
||||||
self.ok("list --detail --path *prep -T --start='1332497115.612'")
|
self.ok("list --detail *prep -T --start='1332497115.612'")
|
||||||
lines_(self.captured, 2)
|
lines_(self.captured, 2)
|
||||||
self.contain("[ 1332497115612000 -> 1332497160000000 ]")
|
self.contain("[ 1332497115612000 -> 1332497160000000 ]")
|
||||||
|
|
||||||
@@ -615,11 +610,19 @@ class TestCmdline(object):
|
|||||||
test(8, "10:01:59.9", "10:02:00.1", extra="-m")
|
test(8, "10:01:59.9", "10:02:00.1", extra="-m")
|
||||||
|
|
||||||
# all data put in by tests
|
# all data put in by tests
|
||||||
self.ok("extract -a /newton/prep --start 2000-01-01 --end 2020-01-01")
|
self.ok("extract -a /newton/prep --start min --end max")
|
||||||
lines_(self.captured, 43204)
|
lines_(self.captured, 43204)
|
||||||
self.ok("extract -c /newton/prep --start 2000-01-01 --end 2020-01-01")
|
self.ok("extract -c /newton/prep --start 2000-01-01 --end 2020-01-01")
|
||||||
self.match("43200\n")
|
self.match("43200\n")
|
||||||
|
|
||||||
|
# test binary mode
|
||||||
|
self.fail("extract -c -B /newton/prep -s min -e max")
|
||||||
|
self.contain("binary cannot be combined")
|
||||||
|
self.fail("extract -m -B /newton/prep -s min -e max")
|
||||||
|
self.contain("binary cannot be combined")
|
||||||
|
self.ok("extract -B /newton/prep -s min -e max")
|
||||||
|
eq_(len(self.captured), 43200 * (8 + 8*4))
|
||||||
|
|
||||||
# markup for 3 intervals, plus extra markup lines whenever we had
|
# markup for 3 intervals, plus extra markup lines whenever we had
|
||||||
# a "restart" from the nilmdb.stream_extract function
|
# a "restart" from the nilmdb.stream_extract function
|
||||||
self.ok("extract -m /newton/prep --start 2000-01-01 --end 2020-01-01")
|
self.ok("extract -m /newton/prep --start 2000-01-01 --end 2020-01-01")
|
||||||
@@ -639,7 +642,7 @@ class TestCmdline(object):
|
|||||||
|
|
||||||
# Try nonexistent stream
|
# Try nonexistent stream
|
||||||
self.fail("remove /no/such/foo --start 2000-01-01 --end 2020-01-01")
|
self.fail("remove /no/such/foo --start 2000-01-01 --end 2020-01-01")
|
||||||
self.contain("No stream at path")
|
self.contain("no stream matched path")
|
||||||
|
|
||||||
# empty or backward ranges return errors
|
# empty or backward ranges return errors
|
||||||
self.fail("remove /newton/prep --start 2020-01-01 --end 2000-01-01")
|
self.fail("remove /newton/prep --start 2020-01-01 --end 2000-01-01")
|
||||||
@@ -667,9 +670,14 @@ class TestCmdline(object):
|
|||||||
"--start '23 Mar 2022 20:00:30' " +
|
"--start '23 Mar 2022 20:00:30' " +
|
||||||
"--end '23 Mar 2022 20:00:31'")
|
"--end '23 Mar 2022 20:00:31'")
|
||||||
self.match("0\n")
|
self.match("0\n")
|
||||||
|
self.ok("remove -c /newton/prep /newton/pre* " +
|
||||||
|
"--start '23 Mar 2022 20:00:30' " +
|
||||||
|
"--end '23 Mar 2022 20:00:31'")
|
||||||
|
self.match("Removing from /newton/prep\n0\n" +
|
||||||
|
"Removing from /newton/prep\n0\n")
|
||||||
|
|
||||||
# Make sure we have the data we expect
|
# Make sure we have the data we expect
|
||||||
self.ok("list --detail /newton/prep")
|
self.ok("list -l --detail /newton/prep")
|
||||||
self.match("/newton/prep float32_8\n" +
|
self.match("/newton/prep float32_8\n" +
|
||||||
" [ Fri, 23 Mar 2012 10:00:00.000000 +0000"
|
" [ Fri, 23 Mar 2012 10:00:00.000000 +0000"
|
||||||
" -> Fri, 23 Mar 2012 10:01:59.991668 +0000 ]\n"
|
" -> Fri, 23 Mar 2012 10:01:59.991668 +0000 ]\n"
|
||||||
@@ -704,7 +712,7 @@ class TestCmdline(object):
|
|||||||
self.match("24000\n")
|
self.match("24000\n")
|
||||||
|
|
||||||
# See the missing chunks in list output
|
# See the missing chunks in list output
|
||||||
self.ok("list --detail /newton/prep")
|
self.ok("list --layout --detail /newton/prep")
|
||||||
self.match("/newton/prep float32_8\n" +
|
self.match("/newton/prep float32_8\n" +
|
||||||
" [ Fri, 23 Mar 2012 10:00:00.000000 +0000"
|
" [ Fri, 23 Mar 2012 10:00:00.000000 +0000"
|
||||||
" -> Fri, 23 Mar 2012 10:00:05.000000 +0000 ]\n"
|
" -> Fri, 23 Mar 2012 10:00:05.000000 +0000 ]\n"
|
||||||
@@ -718,7 +726,7 @@ class TestCmdline(object):
|
|||||||
# Remove all data, verify it's missing
|
# Remove all data, verify it's missing
|
||||||
self.ok("remove /newton/prep --start 2000-01-01 --end 2020-01-01")
|
self.ok("remove /newton/prep --start 2000-01-01 --end 2020-01-01")
|
||||||
self.match("") # no count requested this time
|
self.match("") # no count requested this time
|
||||||
self.ok("list --detail /newton/prep")
|
self.ok("list -l --detail /newton/prep")
|
||||||
self.match("/newton/prep float32_8\n" +
|
self.match("/newton/prep float32_8\n" +
|
||||||
" (no intervals)\n")
|
" (no intervals)\n")
|
||||||
|
|
||||||
@@ -736,16 +744,16 @@ class TestCmdline(object):
|
|||||||
self.contain("too few arguments")
|
self.contain("too few arguments")
|
||||||
|
|
||||||
self.fail("destroy /no/such/stream")
|
self.fail("destroy /no/such/stream")
|
||||||
self.contain("No stream at path")
|
self.contain("no stream matched path")
|
||||||
|
|
||||||
self.fail("destroy -R /no/such/stream")
|
self.fail("destroy -R /no/such/stream")
|
||||||
self.contain("No stream at path")
|
self.contain("no stream matched path")
|
||||||
|
|
||||||
self.fail("destroy asdfasdf")
|
self.fail("destroy asdfasdf")
|
||||||
self.contain("No stream at path")
|
self.contain("no stream matched path")
|
||||||
|
|
||||||
# From previous tests, we have:
|
# From previous tests, we have:
|
||||||
self.ok("list")
|
self.ok("list -l")
|
||||||
self.match("/newton/prep float32_8\n"
|
self.match("/newton/prep float32_8\n"
|
||||||
"/newton/raw uint16_6\n"
|
"/newton/raw uint16_6\n"
|
||||||
"/newton/zzz/rawnotch uint16_9\n")
|
"/newton/zzz/rawnotch uint16_9\n")
|
||||||
@@ -761,13 +769,13 @@ class TestCmdline(object):
|
|||||||
lines_(self.captured, 7)
|
lines_(self.captured, 7)
|
||||||
|
|
||||||
# Destroy for real
|
# Destroy for real
|
||||||
self.ok("destroy -R /newton/prep")
|
self.ok("destroy -R /n*/prep")
|
||||||
self.ok("list")
|
self.ok("list -l")
|
||||||
self.match("/newton/raw uint16_6\n"
|
self.match("/newton/raw uint16_6\n"
|
||||||
"/newton/zzz/rawnotch uint16_9\n")
|
"/newton/zzz/rawnotch uint16_9\n")
|
||||||
|
|
||||||
self.ok("destroy /newton/zzz/rawnotch")
|
self.ok("destroy /newton/zzz/rawnotch")
|
||||||
self.ok("list")
|
self.ok("list -l")
|
||||||
self.match("/newton/raw uint16_6\n")
|
self.match("/newton/raw uint16_6\n")
|
||||||
|
|
||||||
self.ok("destroy /newton/raw")
|
self.ok("destroy /newton/raw")
|
||||||
@@ -786,18 +794,17 @@ class TestCmdline(object):
|
|||||||
self.ok("list")
|
self.ok("list")
|
||||||
self.contain(path)
|
self.contain(path)
|
||||||
# Make sure it was created empty
|
# Make sure it was created empty
|
||||||
self.ok("list --detail --path " + path)
|
self.ok("list --detail " + path)
|
||||||
self.contain("(no intervals)")
|
self.contain("(no intervals)")
|
||||||
|
|
||||||
def test_12_unicode(self):
|
def test_12_unicode(self):
|
||||||
# Unicode paths.
|
# Unicode paths.
|
||||||
self.ok("destroy /newton/asdf/qwer")
|
self.ok("destroy /newton/asdf/qwer")
|
||||||
self.ok("destroy /newton/prep")
|
self.ok("destroy /newton/prep /newton/raw")
|
||||||
self.ok("destroy /newton/raw")
|
|
||||||
self.ok("destroy /newton/zzz")
|
self.ok("destroy /newton/zzz")
|
||||||
|
|
||||||
self.ok(u"create /düsseldorf/raw uint16_6")
|
self.ok(u"create /düsseldorf/raw uint16_6")
|
||||||
self.ok("list --detail")
|
self.ok("list -l --detail")
|
||||||
self.contain(u"/düsseldorf/raw uint16_6")
|
self.contain(u"/düsseldorf/raw uint16_6")
|
||||||
self.contain("(no intervals)")
|
self.contain("(no intervals)")
|
||||||
|
|
||||||
@@ -883,7 +890,7 @@ class TestCmdline(object):
|
|||||||
du_before = nilmdb.utils.diskusage.du(testdb)
|
du_before = nilmdb.utils.diskusage.du(testdb)
|
||||||
|
|
||||||
# Make sure we have the data we expect
|
# Make sure we have the data we expect
|
||||||
self.ok("list --detail")
|
self.ok("list -l --detail")
|
||||||
self.match("/newton/prep float32_8\n" +
|
self.match("/newton/prep float32_8\n" +
|
||||||
" [ Fri, 23 Mar 2012 10:00:00.000000 +0000"
|
" [ Fri, 23 Mar 2012 10:00:00.000000 +0000"
|
||||||
" -> Fri, 23 Mar 2012 10:01:59.991668 +0000 ]\n"
|
" -> Fri, 23 Mar 2012 10:01:59.991668 +0000 ]\n"
|
||||||
@@ -919,7 +926,7 @@ class TestCmdline(object):
|
|||||||
self.match("3600\n")
|
self.match("3600\n")
|
||||||
|
|
||||||
# See the missing chunks in list output
|
# See the missing chunks in list output
|
||||||
self.ok("list --detail")
|
self.ok("list -l --detail")
|
||||||
self.match("/newton/prep float32_8\n" +
|
self.match("/newton/prep float32_8\n" +
|
||||||
" [ Fri, 23 Mar 2012 10:00:00.000000 +0000"
|
" [ Fri, 23 Mar 2012 10:00:00.000000 +0000"
|
||||||
" -> Fri, 23 Mar 2012 10:00:05.000000 +0000 ]\n"
|
" -> Fri, 23 Mar 2012 10:00:05.000000 +0000 ]\n"
|
||||||
@@ -1009,6 +1016,18 @@ class TestCmdline(object):
|
|||||||
self.match("[ Thu, 01 Jan 2004 00:00:00.000000 +0000 -"
|
self.match("[ Thu, 01 Jan 2004 00:00:00.000000 +0000 -"
|
||||||
"> Sat, 01 Jan 2005 00:00:00.000000 +0000 ]\n")
|
"> Sat, 01 Jan 2005 00:00:00.000000 +0000 ]\n")
|
||||||
|
|
||||||
|
# optimize
|
||||||
|
self.ok("insert -s 01-01-2002 -e 01-01-2004 /diff/1 /dev/null")
|
||||||
|
self.ok("intervals /diff/1")
|
||||||
|
self.match("[ Sat, 01 Jan 2000 00:00:00.000000 +0000 -"
|
||||||
|
"> Thu, 01 Jan 2004 00:00:00.000000 +0000 ]\n"
|
||||||
|
"[ Thu, 01 Jan 2004 00:00:00.000000 +0000 -"
|
||||||
|
"> Sat, 01 Jan 2005 00:00:00.000000 +0000 ]\n")
|
||||||
|
self.ok("intervals /diff/1 --optimize")
|
||||||
|
self.ok("intervals /diff/1 -o")
|
||||||
|
self.match("[ Sat, 01 Jan 2000 00:00:00.000000 +0000 -"
|
||||||
|
"> Sat, 01 Jan 2005 00:00:00.000000 +0000 ]\n")
|
||||||
|
|
||||||
self.ok("destroy -R /diff/1")
|
self.ok("destroy -R /diff/1")
|
||||||
self.ok("destroy -R /diff/2")
|
self.ok("destroy -R /diff/2")
|
||||||
|
|
||||||
@@ -1043,7 +1062,7 @@ class TestCmdline(object):
|
|||||||
else:
|
else:
|
||||||
raise AssertionError("data not found at " + seek)
|
raise AssertionError("data not found at " + seek)
|
||||||
# Verify "list" output
|
# Verify "list" output
|
||||||
self.ok("list")
|
self.ok("list -l")
|
||||||
self.match("/" + "/".join(components) + " float32_8\n")
|
self.match("/" + "/".join(components) + " float32_8\n")
|
||||||
|
|
||||||
# Lots of renames
|
# Lots of renames
|
||||||
|
@@ -59,6 +59,14 @@ class TestInterval:
|
|||||||
self.test_interval_intersect()
|
self.test_interval_intersect()
|
||||||
Interval = NilmdbInterval
|
Interval = NilmdbInterval
|
||||||
|
|
||||||
|
# Other helpers in nilmdb.utils.interval
|
||||||
|
i = [ UtilsInterval(1,2), UtilsInterval(2,3), UtilsInterval(4,5) ]
|
||||||
|
eq_(list(nilmdb.utils.interval.optimize(i)),
|
||||||
|
[ UtilsInterval(1,3), UtilsInterval(4,5) ])
|
||||||
|
eq_(UtilsInterval(1234567890123456, 1234567890654321).human_string(),
|
||||||
|
"[ Fri, 13 Feb 2009 18:31:30.123456 -0500 -> " +
|
||||||
|
"Fri, 13 Feb 2009 18:31:30.654321 -0500 ]")
|
||||||
|
|
||||||
def test_interval(self):
|
def test_interval(self):
|
||||||
# Test Interval class
|
# Test Interval class
|
||||||
os.environ['TZ'] = "America/New_York"
|
os.environ['TZ'] = "America/New_York"
|
||||||
@@ -226,13 +234,16 @@ class TestInterval:
|
|||||||
x = makeset("[--)") & 1234
|
x = makeset("[--)") & 1234
|
||||||
|
|
||||||
def do_test(a, b, c, d):
|
def do_test(a, b, c, d):
|
||||||
# a & b == c
|
# a & b == c (using nilmdb.server.interval)
|
||||||
ab = IntervalSet()
|
ab = IntervalSet()
|
||||||
for x in b:
|
for x in b:
|
||||||
for i in (a & x):
|
for i in (a & x):
|
||||||
ab += i
|
ab += i
|
||||||
eq_(ab,c)
|
eq_(ab,c)
|
||||||
|
|
||||||
|
# a & b == c (using nilmdb.utils.interval)
|
||||||
|
eq_(IntervalSet(nilmdb.utils.interval.intersection(a,b)), c)
|
||||||
|
|
||||||
# a \ b == d
|
# a \ b == d
|
||||||
eq_(IntervalSet(nilmdb.utils.interval.set_difference(a,b)), d)
|
eq_(IntervalSet(nilmdb.utils.interval.set_difference(a,b)), d)
|
||||||
|
|
||||||
@@ -302,6 +313,17 @@ class TestInterval:
|
|||||||
eq_(nilmdb.utils.interval.set_difference(
|
eq_(nilmdb.utils.interval.set_difference(
|
||||||
a.intersection(list(c)[0]), b.intersection(list(c)[0])), d)
|
a.intersection(list(c)[0]), b.intersection(list(c)[0])), d)
|
||||||
|
|
||||||
|
# Fill out test coverage for non-subsets
|
||||||
|
def diff2(a,b, subset):
|
||||||
|
return nilmdb.utils.interval._interval_math_helper(
|
||||||
|
a, b, (lambda a, b: b and not a), subset=subset)
|
||||||
|
with assert_raises(nilmdb.utils.interval.IntervalError):
|
||||||
|
list(diff2(a,b,True))
|
||||||
|
list(diff2(a,b,False))
|
||||||
|
|
||||||
|
# Empty second set
|
||||||
|
eq_(nilmdb.utils.interval.set_difference(a, IntervalSet()), a)
|
||||||
|
|
||||||
# Empty second set
|
# Empty second set
|
||||||
eq_(nilmdb.utils.interval.set_difference(a, IntervalSet()), a)
|
eq_(nilmdb.utils.interval.set_difference(a, IntervalSet()), a)
|
||||||
|
|
||||||
|
@@ -157,11 +157,14 @@ class TestServer(object):
|
|||||||
|
|
||||||
def test_server(self):
|
def test_server(self):
|
||||||
# Make sure we can't force an exit, and test other 404 errors
|
# Make sure we can't force an exit, and test other 404 errors
|
||||||
for url in [ "/exit", "/", "/favicon.ico" ]:
|
for url in [ "/exit", "/favicon.ico" ]:
|
||||||
with assert_raises(HTTPError) as e:
|
with assert_raises(HTTPError) as e:
|
||||||
geturl(url)
|
geturl(url)
|
||||||
eq_(e.exception.code, 404)
|
eq_(e.exception.code, 404)
|
||||||
|
|
||||||
|
# Root page
|
||||||
|
in_("This is NilmDB", geturl("/"))
|
||||||
|
|
||||||
# Check version
|
# Check version
|
||||||
eq_(distutils.version.LooseVersion(getjson("/version")),
|
eq_(distutils.version.LooseVersion(getjson("/version")),
|
||||||
distutils.version.LooseVersion(nilmdb.__version__))
|
distutils.version.LooseVersion(nilmdb.__version__))
|
||||||
|
@@ -28,7 +28,10 @@ def setup_module():
|
|||||||
recursive_unlink(testdb)
|
recursive_unlink(testdb)
|
||||||
|
|
||||||
# Start web app on a custom port
|
# Start web app on a custom port
|
||||||
test_db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(testdb)
|
test_db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(
|
||||||
|
testdb, bulkdata_args = { "file_size" : 16384,
|
||||||
|
"files_per_dir" : 3 } )
|
||||||
|
|
||||||
test_server = nilmdb.server.Server(test_db, host = "127.0.0.1",
|
test_server = nilmdb.server.Server(test_db, host = "127.0.0.1",
|
||||||
port = 32180, stoppable = False,
|
port = 32180, stoppable = False,
|
||||||
fast_shutdown = True,
|
fast_shutdown = True,
|
||||||
@@ -130,6 +133,15 @@ class TestNumpyClient(object):
|
|||||||
[4, 5]]]))
|
[4, 5]]]))
|
||||||
in_("wrong number of dimensions", str(e.exception))
|
in_("wrong number of dimensions", str(e.exception))
|
||||||
|
|
||||||
|
# Wrong number of fields
|
||||||
|
with assert_raises(ValueError) as e:
|
||||||
|
client.stream_insert_numpy("/test/1",
|
||||||
|
np.array([[0, 1, 2],
|
||||||
|
[1, 2, 3],
|
||||||
|
[3, 4, 5],
|
||||||
|
[4, 5, 6]]))
|
||||||
|
in_("wrong number of fields", str(e.exception))
|
||||||
|
|
||||||
# Unstructured
|
# Unstructured
|
||||||
client.stream_create("/test/2", "float32_8")
|
client.stream_create("/test/2", "float32_8")
|
||||||
client.stream_insert_numpy(
|
client.stream_insert_numpy(
|
||||||
@@ -170,6 +182,17 @@ class TestNumpyClient(object):
|
|||||||
assert(np.array_equal(a,b))
|
assert(np.array_equal(a,b))
|
||||||
assert(np.array_equal(a,c))
|
assert(np.array_equal(a,c))
|
||||||
|
|
||||||
|
# Make sure none of the files are greater than 16384 bytes as
|
||||||
|
# we configured with the bulkdata_args above.
|
||||||
|
datapath = os.path.join(testdb, "data")
|
||||||
|
for (dirpath, dirnames, filenames) in os.walk(datapath):
|
||||||
|
for f in filenames:
|
||||||
|
fn = os.path.join(dirpath, f)
|
||||||
|
size = os.path.getsize(fn)
|
||||||
|
if size > 16384:
|
||||||
|
raise AssertionError(sprintf("%s is too big: %d > %d\n",
|
||||||
|
fn, size, 16384))
|
||||||
|
|
||||||
nilmdb.client.numpyclient.StreamInserterNumpy._max_data = old_max_data
|
nilmdb.client.numpyclient.StreamInserterNumpy._max_data = old_max_data
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
@@ -286,8 +309,25 @@ class TestNumpyClient(object):
|
|||||||
with client.stream_insert_numpy_context("/empty/test", end = 950):
|
with client.stream_insert_numpy_context("/empty/test", end = 950):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Equal start and end is OK as long as there's no data
|
||||||
|
with assert_raises(ClientError) as e:
|
||||||
|
with client.stream_insert_numpy_context("/empty/test",
|
||||||
|
start=9, end=9) as ctx:
|
||||||
|
ctx.insert([[9, 9]])
|
||||||
|
ctx.finalize()
|
||||||
|
in_("have data to send, but invalid start/end times", str(e.exception))
|
||||||
|
|
||||||
|
with client.stream_insert_numpy_context("/empty/test",
|
||||||
|
start=9, end=9) as ctx:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# reusing a context object is bad
|
||||||
|
with assert_raises(Exception) as e:
|
||||||
|
ctx.insert([[9, 9]])
|
||||||
|
|
||||||
# Try various things that might cause problems
|
# Try various things that might cause problems
|
||||||
with client.stream_insert_numpy_context("/empty/test", 1000, 1050):
|
with client.stream_insert_numpy_context("/empty/test",
|
||||||
|
1000, 1050) as ctx:
|
||||||
ctx.finalize() # inserts [1000, 1050]
|
ctx.finalize() # inserts [1000, 1050]
|
||||||
ctx.finalize() # nothing
|
ctx.finalize() # nothing
|
||||||
ctx.finalize() # nothing
|
ctx.finalize() # nothing
|
||||||
|
@@ -62,6 +62,28 @@ class Base(object):
|
|||||||
eq_(self.foo.val, 20)
|
eq_(self.foo.val, 20)
|
||||||
eq_(self.foo.init_thread, self.foo.test_thread)
|
eq_(self.foo.init_thread, self.foo.test_thread)
|
||||||
|
|
||||||
|
class ListLike(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.thread = threading.current_thread().name
|
||||||
|
self.foo = 0
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
eq_(threading.current_thread().name, self.thread)
|
||||||
|
self.foo = 0
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
eq_(threading.current_thread().name, self.thread)
|
||||||
|
return key
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
eq_(threading.current_thread().name, self.thread)
|
||||||
|
if self.foo < 5:
|
||||||
|
self.foo += 1
|
||||||
|
return self.foo
|
||||||
|
else:
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
class TestUnserialized(Base):
|
class TestUnserialized(Base):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.foo = Foo()
|
self.foo = Foo()
|
||||||
@@ -84,3 +106,9 @@ class TestSerializer(Base):
|
|||||||
sp(sp(Foo("x"))).t()
|
sp(sp(Foo("x"))).t()
|
||||||
sp(sp(Foo)("x")).t()
|
sp(sp(Foo)("x")).t()
|
||||||
sp(sp(Foo))("x").t()
|
sp(sp(Foo))("x").t()
|
||||||
|
|
||||||
|
def test_iter(self):
|
||||||
|
sp = nilmdb.utils.serializer_proxy
|
||||||
|
i = sp(ListLike)()
|
||||||
|
eq_(list(i), [1,2,3,4,5])
|
||||||
|
eq_(i[3], 3)
|
||||||
|
Reference in New Issue
Block a user