From dced3258e0d5805eeeaf7e8a40f855f01139aae2 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Wed, 13 Jan 2016 17:11:17 +0100 Subject: [PATCH] clarified names around df-wrappers: include "fill" in function and class --- file_helpers.py | 13 ++++++++----- test/file_helper_unittest.py | 4 ++-- test_helpers.py | 40 +++++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/file_helpers.py b/file_helpers.py index 06566d7..96acbb7 100644 --- a/file_helpers.py +++ b/file_helpers.py @@ -38,7 +38,7 @@ def cd(path): DF_CMD = ['/usr/bin/df', '--no-sync', '--portability'] -class FilesystemStats: +class FilesystemFillState: """ representation of 1 line of the 'df' command has fields filesystem, size, used, available, capacity, mount_point @@ -55,11 +55,14 @@ class FilesystemStats: capacity = None mount_point = None + def __str__(self): + return '[Filesystem {0} mounted at {1}: {2}% used]' \ + .format(self.name, self.mount_point, self.capacity) -def get_disc_stats(): - """ get stats on all filesystems +def get_filesystem_fill_states(): + """ get fill state on all filesystems - parses the output of cmd 'df', returns list of FilesystemStats + parses the output of cmd 'df', returns list of :py:class:`FilesystemFillState` """ # call @@ -111,7 +114,7 @@ def get_disc_stats(): # create result result = [] for line in out[1:]: - stats = FilesystemStats() + stats = FilesystemFillState() stats.name = line[ : separator_cols[0]].strip() stats.size = int(line[separator_cols[0] : separator_cols[1]].strip()) stats.used = int(line[separator_cols[1] : separator_cols[2]].strip()) diff --git a/test/file_helper_unittest.py b/test/file_helper_unittest.py index db672f0..7dbdb3f 100644 --- a/test/file_helper_unittest.py +++ b/test/file_helper_unittest.py @@ -32,9 +32,9 @@ class FileHelperTester(unittest.TestCase): def test_disc_stats(self): - """ tests get_disc_stats """ + """ tests get_filesystem_fill_states """ - stats = file_helpers.get_disc_stats() + stats = file_helpers.get_filesystem_fill_states() # check number code, out, err = call_and_capture(file_helpers.DF_CMD) diff --git a/test_helpers.py b/test_helpers.py index 8c95e0d..fec0164 100644 --- a/test_helpers.py +++ b/test_helpers.py @@ -9,7 +9,7 @@ tests import contextlib from threading import Thread from time import sleep -from file_helpers import get_disc_stats +from file_helpers import get_filesystem_fill_states from datetime import datetime as dt from buffers import LogarithmicBuffer from itertools import tee @@ -29,26 +29,32 @@ class DiscFillCheckerThread(Thread): self.decision_function = decision_function # remember relevant disc stats at start - bufs = dict((stat.name, LogarithmicBuffer(5)) for stat in stats) - now = dt.now() - for stat in stats: - bufs[stat.name].add((now, stat.available)) + self._bufs = {} + for fs_state in get_filesystem_fill_states(): + self._internal_state_buffer(fs_state) + + def _internal_state_buffer(self, fs_state): + """ update internal stats buffer, returns all estims + + internal helper called from __init__ and run + """ + buf = None + try: + buf = self._bufs[fs_state.name] + except KeyError: + # new file system -- need to create a new buffer + buf = LogarithmicBuffer(5) + self._bufs[fs_state.name] = buf + + buf.add((dt.now(), float(fs_state.available))) + return buf.get_all() def run(self): while True: sleep(self.interval) - stats = get_disc_stats() - now = dt.now() - for stat in stats: - buf = None - try: - buf = bufs[stat.name] - except KeyError: - # new file system -- need to create a new buffer - buf = LogarithmicBuffer(5) - - buf.add((now, float(stat.available))) - fill_states = buf.get_all() + fs_states = get_filesystem_fill_states() + for fs_state in fs_states: + fill_states = self._internal_state_buffer(fs_state) # estimate time until full (i.e. when available == 0) times_until_empty = \ -- 1.7.1