Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Last active June 4, 2019 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbengfort/2c859845f329ba9edc3ce96cd2d4522f to your computer and use it in GitHub Desktop.
Save bbengfort/2c859845f329ba9edc3ce96cd2d4522f to your computer and use it in GitHub Desktop.
BTrDB Helpers for common operations
import os
import time
import btrdb
from tabulate import tabulate
db = btrdb.connect("api.research.predictivegrid.com:4411", "48BE21A7A13661B1EED5AB87")
def list_collection(prefix):
table = [
(s.uuid, s.collection, s.name)
for s in db.streams_in_collection(prefix)
]
print(tabulate(table, tablefmt="simple"))
def rename_collection(src, dst):
count = 0
for stream in db.streams_in_collection(src, is_collection_prefix=False):
stream.update(collection=dst)
count += 1
print(f"updated {count} streams from {src} to {dst}")
def count_points(s, latest_version=False):
version = s.version() if latest_version else 0
# NOTE: Specifying latest_version will only count points that have been
# linked and stored, avoiding points buffered in the PQM.
# Use version=0 to get the most up to date count possible.
windows = s.aligned_windows(
start=0, end=(1<<61), pointwidth=61, version=version
)
return sum([
window[0].count for window in windows
])
def data_rate(uuid):
stream = db.stream_from_uuid(uuid)
print(f" {stream.collection}/{stream.name}")
prev = count_points(stream)
last = time.time()
print(f" starting data rate at {prev:,} points")
while True:
try:
current = count_points(stream)
if current > prev:
print(f"+{current-prev: >4} in {time.time()-last:0.3f} seconds | {current:,}")
last = time.time()
prev = current
time.sleep(1)
except KeyboardInterrupt:
print("stopping data rate detection")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment