Browse Source

server: Replace /dbpath and /dbsize with a more generic /dbinfo

Update tests accordingly.  This isn't backwards compatible, but
existing clients don't rely on it.
tags/nilmdb-1.1
Jim Paris 11 years ago
parent
commit
841b2dab5c
8 changed files with 28 additions and 29 deletions
  1. +1
    -1
      README.txt
  2. +4
    -7
      nilmdb/client/client.py
  3. +5
    -2
      nilmdb/cmdline/info.py
  4. +9
    -9
      nilmdb/server/server.py
  5. +1
    -1
      nilmdb/utils/__init__.py
  6. +3
    -7
      nilmdb/utils/diskusage.py
  7. +1
    -0
      setup.py
  8. +4
    -2
      tests/test_cmdline.py

+ 1
- 1
README.txt View File

@@ -7,7 +7,7 @@ Prerequisites:
sudo apt-get install python2.7 python2.7-dev python-setuptools cython

# Base NilmDB dependencies
sudo apt-get install python-cherrypy3 python-decorator python-simplejson python-pycurl python-dateutil python-tz
sudo apt-get install python-cherrypy3 python-decorator python-simplejson python-pycurl python-dateutil python-tz python-psutil

# Tools for running tests
sudo apt-get install python-nose python-coverage


+ 4
- 7
nilmdb/client/client.py View File

@@ -34,13 +34,10 @@ class Client(object):
"""Return server version"""
return self.http.get("version")

def dbpath(self):
"""Return server database path"""
return self.http.get("dbpath")

def dbsize(self):
"""Return server database size as human readable string"""
return self.http.get("dbsize")
def dbinfo(self):
"""Return server database info (path, size, free space)
as a dictionary."""
return self.http.get("dbinfo")

def stream_list(self, path = None, layout = None):
params = {}


+ 5
- 2
nilmdb/cmdline/info.py View File

@@ -1,5 +1,6 @@
import nilmdb
from nilmdb.utils.printf import *
from nilmdb.utils import human_size

from argparse import ArgumentDefaultsHelpFormatter as def_form

@@ -17,5 +18,7 @@ def cmd_info(self):
printf("Client version: %s\n", nilmdb.__version__)
printf("Server version: %s\n", self.client.version())
printf("Server URL: %s\n", self.client.geturl())
printf("Server database path: %s\n", self.client.dbpath())
printf("Server database size: %s\n", self.client.dbsize())
dbinfo = self.client.dbinfo()
printf("Server database path: %s\n", dbinfo["path"])
printf("Server database size: %s\n", human_size(dbinfo["size"]))
printf("Server database free space: %s\n", human_size(dbinfo["free"]))

+ 9
- 9
nilmdb/server/server.py View File

@@ -13,6 +13,7 @@ import os
import simplejson as json
import decorator
import traceback
import psutil

try:
cherrypy.tools.json_out
@@ -99,17 +100,16 @@ class Root(NilmApp):
def version(self):
return nilmdb.__version__

# /dbpath
# /dbinfo
@cherrypy.expose
@cherrypy.tools.json_out()
def dbpath(self):
return self.db.get_basepath()

# /dbsize
@cherrypy.expose
@cherrypy.tools.json_out()
def dbsize(self):
return nilmdb.utils.du(self.db.get_basepath())
def dbinfo(self):
"""Return a dictionary with the database path,
size of the database in bytes, and free disk space in bytes"""
path = self.db.get_basepath()
return { "path": path,
"size": nilmdb.utils.du(path),
"free": psutil.disk_usage(path).free }

class Stream(NilmApp):
"""Stream-specific operations"""


+ 1
- 1
nilmdb/utils/__init__.py View File

@@ -4,7 +4,7 @@ from nilmdb.utils.timer import Timer
from nilmdb.utils.iteratorizer import Iteratorizer
from nilmdb.utils.serializer import Serializer
from nilmdb.utils.lrucache import lru_cache
from nilmdb.utils.diskusage import du
from nilmdb.utils.diskusage import du, human_size
from nilmdb.utils.mustclose import must_close
from nilmdb.utils.urllib import urlencode
from nilmdb.utils import misc


+ 3
- 7
nilmdb/utils/diskusage.py View File

@@ -1,7 +1,7 @@
import os
from math import log

def sizeof_fmt(num):
def human_size(num):
"""Human friendly file size"""
unit_list = zip(['bytes', 'kiB', 'MiB', 'GiB', 'TiB'], [0, 0, 1, 2, 2])
if num > 1:
@@ -15,15 +15,11 @@ def sizeof_fmt(num):
if num == 1: # pragma: no cover
return '1 byte'

def du_bytes(path):
def du(path):
"""Like du -sb, returns total size of path in bytes."""
size = os.path.getsize(path)
if os.path.isdir(path):
for thisfile in os.listdir(path):
filepath = os.path.join(path, thisfile)
size += du_bytes(filepath)
size += du(filepath)
return size

def du(path):
"""Like du -sh, returns total size of path as a human-readable string."""
return sizeof_fmt(du_bytes(path))

+ 1
- 0
setup.py View File

@@ -114,6 +114,7 @@ setup(name='nilmdb',
'pycurl',
'python-dateutil',
'pytz',
'psutil >= 0.3.0',
],
packages = [ 'nilmdb',
'nilmdb.utils',


+ 4
- 2
tests/test_cmdline.py View File

@@ -194,9 +194,11 @@ class TestCmdline(object):
def test_02_info(self):
self.ok("info")
self.contain("Server URL: http://localhost:12380/")
self.contain("Client version: " + nilmdb.__version__)
self.contain("Server version: " + test_server.version)
self.contain("Server database path")
self.contain("Server database size")
self.contain("Server database free space")

def test_03_createlist(self):
# Basic stream tests, like those in test_client.
@@ -765,7 +767,7 @@ class TestCmdline(object):
"tests/data/prep-20120323T1000")

# Should take up about 2.8 MB here (including directory entries)
du_before = nilmdb.utils.diskusage.du_bytes(testdb)
du_before = nilmdb.utils.diskusage.du(testdb)

# Make sure we have the data we expect
self.ok("list --detail")
@@ -815,7 +817,7 @@ class TestCmdline(object):

# We have 1/8 of the data that we had before, so the file size
# should have dropped below 1/4 of what it used to be
du_after = nilmdb.utils.diskusage.du_bytes(testdb)
du_after = nilmdb.utils.diskusage.du(testdb)
lt_(du_after, (du_before / 4))

# Remove anything that came from the 10:02 data file


Loading…
Cancel
Save