Browse Source

Add database size to "nilmtool.py info" output.

git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@10968 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/bxinterval-last
Jim Paris 11 years ago
parent
commit
ff4e934bef
6 changed files with 45 additions and 1 deletions
  1. +1
    -0
      nilmdb/__init__.py
  2. +4
    -0
      nilmdb/client.py
  3. +2
    -1
      nilmdb/cmdline/info.py
  4. +30
    -0
      nilmdb/du.py
  5. +6
    -0
      nilmdb/server.py
  6. +2
    -0
      tests/test_cmdline.py

+ 1
- 0
nilmdb/__init__.py View File

@@ -13,3 +13,4 @@ import layout
import serializer
import timestamper
import interval
import du

+ 4
- 0
nilmdb/client.py View File

@@ -43,6 +43,10 @@ class Client(object):
"""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 stream_list(self, path = None, layout = None):
params = {}
if path is not None:


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

@@ -17,4 +17,5 @@ def cmd_info(self):
printf("Client library version: %s\n", self.client.client_version)
printf("Server version: %s\n", self.client.version())
printf("Server URL: %s\n", self.client.geturl())
printf("Server database: %s\n", self.client.dbpath())
printf("Server database path: %s\n", self.client.dbpath())
printf("Server database size: %s\n", self.client.dbsize())

+ 30
- 0
nilmdb/du.py View File

@@ -0,0 +1,30 @@
import nilmdb
import os
from math import log

def sizeof_fmt(num):
"""Human friendly file size"""
unit_list = zip(['bytes', 'kiB', 'MiB', 'GiB', 'TiB'], [0, 0, 1, 2, 2])
if num > 1:
exponent = min(int(log(num, 1024)), len(unit_list) - 1)
quotient = float(num) / 1024**exponent
unit, num_decimals = unit_list[exponent]
format_string = '{:.%sf} {}' % (num_decimals)
return format_string.format(quotient, unit)
if num == 0: # pragma: no cover
return '0 bytes'
if num == 1: # pragma: no cover
return '1 byte'

def du_bytes(path):
"""Like du -sb, returns total size of path in bytes."""
size = os.path.getsize(path)
if os.path.isdir(path):
for file in os.listdir(path):
filepath = os.path.join(path, file)
size += du_bytes(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))

+ 6
- 0
nilmdb/server.py View File

@@ -55,6 +55,12 @@ class Root(NilmApp):
def dbpath(self):
return self.db.get_basepath()

# /dbsize
@cherrypy.expose
@cherrypy.tools.json_out()
def dbsize(self):
return nilmdb.du.du(self.db.get_basepath())

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



+ 2
- 0
tests/test_cmdline.py View File

@@ -167,6 +167,8 @@ class TestCmdline(object):
self.ok("info")
self.contain("Server URL: http://localhost:12380/")
self.contain("Server version: " + test_server.version)
self.contain("Server database path")
self.contain("Server database size")

def test_cmdline_03_createlist(self):
# Basic stream tests, like those in test_client.


Loading…
Cancel
Save