Skip to content

Commit

Permalink
Rename Watchers to Inspectors
Browse files Browse the repository at this point in the history
Address usnistgov#28
  • Loading branch information
wd15 committed Jul 12, 2016
1 parent 1a0d354 commit b36055d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
File renamed without changes.
26 changes: 13 additions & 13 deletions corrcli/watchers/watcher.py → corrcli/inspectors/inspector.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
"""Abstract base class for all watchers
"""Abstract base class for all inspectors
"""
import abc


class Watcher(object):
"""Abstract base class for all watchers.
class Inspector(object):
"""Abstract base class for all inspectors.
A watcher queries a process for metadata about that process.
A inspector queries a process for metadata about that process.
Attributes:
pid: the process ID to watch
pid: the process ID to inspector
schema_dict: mapping from the observed data to the output data
"""
__metaclass__ = abc.ABCMeta
schema_dict = {}
def __init__(self, pid):
"""Instantiate a Watcher.
"""Instantiate an Inspector.
Args:
pid: the process ID to watch
pid: the process ID to inspect
"""
self.pid = pid

@abc.abstractmethod
def watch(self, data_dict=None):
def inspect(self, data_dict=None):
"""Gather data about a process.
Data is aggregated from the observed process data and the
Expand All @@ -51,7 +51,7 @@ def get_observed_dict(self):
def ini_data_dict(self, data_dict):
"""Initialize the passed in data dictionary.
Update the data to have the keys associated with the watcher
Update the data to have the keys associated with the inspector
object even if nothing is observed about the process.
Args:
Expand All @@ -77,11 +77,11 @@ def update_data_dict(self, observed_dict, data_dict):
Returns:
the data dictionary updated with the observed data
>>> watcher = DummyWatcher(1)
>>> inspector = DummyInspector(1)
>>> observed_dict = {'value1' : 'observation1',
... 'value2' : 'observation2'}
>>> data_dict = {}
>>> actual = watcher.update_data_dict(observed_dict, data_dict)
>>> actual = inspector.update_data_dict(observed_dict, data_dict)
>>> test = {'key2': 'observation2_test', 'key1': 'observation1'}
>>> assert test == actual
Expand All @@ -95,7 +95,7 @@ def update_data_dict(self, observed_dict, data_dict):
return data_dict


class DummyWatcher(Watcher):
class DummyInspector(Inspector):
"""Used for testing only
"""
schema_dict = {'key1' : 'value1',
Expand All @@ -104,5 +104,5 @@ class DummyWatcher(Watcher):
def get_observed_dict(self): # pragma: no cover
pass

def watch(self, data_dict=None): # pragma: no cover
def inspect(self, data_dict=None): # pragma: no cover
pass
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
"""Gather data about the platform.
"""
import platform
from .watcher import Watcher
from .inspector import Inspector


class PlatformWatcher(Watcher):
class PlatformInspector(Inspector):
"""Gather data about the platform
>>> keys = list(PlatformWatcher(None).watch().keys())
>>> keys = list(PlatformInspector(None).inspect().keys())
>>> keys.sort()
>>> print(keys)
['node_name', 'platform']
Attributes:
pid: the process ID to watch
pid: the process ID to inspector
schema_dict: mapping from the observed data to the output data
watch_dict: the cached observed data
inspector_dict: the cached observed data
"""
schema_dict = {'node_name' : 'node',
'platform' : 'platform'}

def __init__(self, pid):
super(PlatformWatcher, self).__init__(pid)
super(PlatformInspector, self).__init__(pid)
self.observed_dict = {}
for value in self.schema_dict.values():
self.observed_dict[value] = getattr(platform, value)()

def get_observed_dict(self):
return self.observed_dict

def watch(self, data_dict=None):
def inspect(self, data_dict=None):
observed_dict = self.get_observed_dict()
data_dict = self.ini_data_dict(data_dict)
return self.update_data_dict(observed_dict, data_dict)
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
"""Watcher that uses psutil to gather proces data.
"""Inspector that uses psutil to gather proces data.
"""
import psutil
from .watcher import Watcher
from .inspector import Inspector

class ProcessWatcher(Watcher):
"""Watcher that uses psutil to gather data.
class ProcessInspector(Inspector):
"""Inspector that uses psutil to gather data.
A process is returned with 'status' finished if it can't be found.
>>> large_int = 100000000000000
>>> watcher = ProcessWatcher(large_int)
>>> print(watcher.watch()['status'])
>>> inspector = ProcessInspector(large_int)
>>> print(inspector.inspect()['status'])
finished
Attributes:
pid: the process ID to watch
pid: the process ID to inspect
schema_dict: the mapping from psutil to the CoRR schema
"""
Expand All @@ -33,7 +32,7 @@ class ProcessWatcher(Watcher):
'cmdline' : 'cmdline'}


def watch(self, data_dict=None):
def inspect(self, data_dict=None):
observed_dict = self.get_observed_dict()
data_dict = self.ini_data_dict(data_dict)
if observed_dict is not None:
Expand Down
22 changes: 11 additions & 11 deletions corrcli/job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import psutil

from .watchers.process_watcher import ProcessWatcher
from .watchers.platform_watcher import PlatformWatcher
from .inspectors.process_inspector import ProcessInspector
from .inspectors.platform_inspector import PlatformInspector
from .commands.cli import DEFAULT_WRITE_REFRESH_RATE, DEFAULT_WATCH_REFRESH_RATE
from .commands.config import parse_config
from .stores.file_store import FileStore
Expand All @@ -17,11 +17,11 @@
class JobManager(object):
"""Job manager for a single process.
Aggregates data from multiple process watchers and writes it to
Aggregates data from multiple process inspectors and writes it to
the data stores.
Attributes:
watchers: a list of watcher objects
inspectors: a list of inspector objects
label: the unique label associated with the job
stores: store objects to store the job records
data_dict: the dictionary to gather the data
Expand All @@ -31,12 +31,12 @@ def __init__(self, pid, config_dir):
"""Instantiate a JobManager.
Args:
pid: the process ID to watch
pid: the process ID to inspect
config_dir: the CoRR configuration directory
"""
self.watchers = [ProcessWatcher(pid),
PlatformWatcher(pid)]
self.inspectors = [ProcessInspector(pid),
PlatformInspector(pid)]
self.label = str(uuid.uuid4().hex)
self.stores = [FileStore(self.label, config_dir)]
self.data_dict = self.initialize_data(pid, config_dir)
Expand All @@ -46,7 +46,7 @@ def initialize_data(self, pid, config_dir):
"""Intialize the data.
Args:
pid: the process ID to watch
pid: the process ID to inspect
config_dir: the CoRR configuration directory
Returns:
Expand All @@ -65,12 +65,12 @@ def initialize_data(self, pid, config_dir):
return data_dict

def update_data(self):
"""Update the data with the watchers.
"""Update the data with the inspectors.
"""
self.data_dict['update_time'] = str(datetime.datetime.now())
for watcher in self.watchers:
self.data_dict = watcher.watch(self.data_dict)
for inspector in self.inspectors:
self.data_dict = inspector.inspect(self.data_dict)

def write_data(self):
"""Write the data to the record stores
Expand Down

0 comments on commit b36055d

Please sign in to comment.