Skip to content

Commit

Permalink
FIX #212, FIX #213
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Jul 30, 2019
1 parent 892a624 commit 9cfa17d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
36 changes: 33 additions & 3 deletions apstools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import xlrd
import zipfile

from .filewriters import _rebuild_scan_command


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -182,7 +184,7 @@ def itemizer(fmt, items):
return [fmt % k for k in items]


def list_recent_scans(num=20, keys=[], printing=True, db=None):
def list_recent_scans(num=20, keys=[], printing=True, show_command=False, db=None):
"""
make a table of the most recent scans
Expand All @@ -192,8 +194,20 @@ def list_recent_scans(num=20, keys=[], printing=True, db=None):
Make the table include the ``num`` most recent scans.
keys : [str] (default: ``[]``)
Include these additional keys from the start document.
Two special keys are supported:
* ``command`` : shows the scan command.
(note: This command is reconstructed from keys in the start
document so it will not be exactly as the user typed.
Also, it will be truncated so that it is no more than 40 characters.)
* ``exit_status`` : from the stop document
printing : bool (default: ``True``)
If True, print the table to stdout
show_command : bool (default: ``False``)
If True, show the (reconstructed) full command,
but truncate it to no more than 40 characters)
db : object (default: ``db`` from the IPython shell)
Instance of ``databroker.Broker()``
Expand Down Expand Up @@ -223,7 +237,10 @@ def list_recent_scans(num=20, keys=[], printing=True, db=None):
global_db = None
db = db or global_db

labels = "scan_id plan_name".split() + keys
if show_command:
labels = "scan_id command".split() + keys
else:
labels = "scan_id plan_name".split() + keys

table = pyRestTable.Table()
table.labels = "short_uid date/time".split() + labels
Expand All @@ -232,7 +249,20 @@ def list_recent_scans(num=20, keys=[], printing=True, db=None):
row = [
h.start["uid"][:7],
datetime.datetime.fromtimestamp(h.start['time']),
] + [h.start.get(k, "") for k in labels]
]
for k in labels:
if k == "command":
command = _rebuild_scan_command(h.start)
command = command[command.find(" "):].strip()
maxlen = 40
if len(command) > maxlen:
suffix = " ..."
command = command[:maxlen-len(suffix)] + suffix
row.append(command)
elif k == "exit_status":
row.append(h.stop.get(k, ""))
else:
row.append(h.start.get(k, ""))
table.addRow(row)

if printing:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ def test_show_ophyd_symbols(self):
if k not in wont_show:
self.assertTrue(k in rr, msg)
self.assertEqual(num, len(table.rows))

def test_list_recent_scans(self):
from tests.test_export_json import get_db
db = get_db()
headers = db(plan_name="count")
headers = list(headers)[0:1]
self.assertEqual(len(headers), 1)
table = APS_utils.list_recent_scans(
keys=["exit_status",],
show_command=True,
printing=False,
num=10,
db=db,
)
self.assertIsNotNone(table)
self.assertEqual(len(table.labels), 5, "asked for 2 extra columns (total 5)")
self.assertEqual(len(table.rows), 10, "asked for 10 rows")
self.assertLessEqual(len(table.rows[1][3]), 40, "command row should be 40 char or less")


def suite(*args, **kw):
Expand Down

0 comments on commit 9cfa17d

Please sign in to comment.