Skip to content

Commit

Permalink
Merge pull request #15 from diggyk/master
Browse files Browse the repository at this point in the history
Improve labor output for client
  • Loading branch information
rra committed Jun 18, 2015
2 parents 2f1bba3 + b1445b6 commit ac68afa
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 75 deletions.
161 changes: 88 additions & 73 deletions bin/hermes
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import logging
import requests
from requests.packages import urllib3
import sys
import textwrap
from time import sleep
import traceback

Expand Down Expand Up @@ -98,6 +99,54 @@ def request_post(path, json):
return response


def print_labor(labor, fates):
"""Print the details of a labor to standard out
Args:
labor: the labor to print
fates: all the fates so we can give suggestions
"""
creation_time = parser.parse(labor["creationTime"])
creation_time = creation_time.replace(tzinfo=tz.tzutc())
creation_time = creation_time.astimezone(tz.tzlocal())
if labor.get("targetTime", None):
target_time = parser.parse(labor["targetTime"])
target_time = target_time.replace(tzinfo=tz.tzutc())
target_time = target_time.astimezone(tz.tzlocal())
target_time_str = "(due by {})".format(target_time)
else:
target_time_str = ""

print "[{}] {}: \n\tcreated on {} {} \n\tby event {} {} {}".format(
labor["id"],
labor["host"]["hostname"],
creation_time,
target_time_str,
labor["creationEvent"]["eventType"]["category"],
labor["creationEvent"]["eventType"]["state"],
labor["creationEvent"]["note"] or ""
)

if labor.get("questId", None):
print "\n\tPart of Quest [{}] created by {}".format(
labor["quest"]["id"],
labor["quest"]["creator"]
)
print "\t\"{}\"".format(textwrap.fill(
labor["quest"]["description"],
width=60, subsequent_indent="\t "
))

print "\n\tPossible Resolutions:"
for fate in fates:
if fate["creationEventTypeId"] == labor["creationEvent"]["eventType"]["id"]:
print "{}".format(textwrap.fill(
fate["description"], width=60,
initial_indent="\t* ", subsequent_indent="\t "
))
print ""


def list_event_types(args):
logging.debug("list_event_types()")

Expand Down Expand Up @@ -144,28 +193,23 @@ def create_event_type(args):
def list_fates(args):
logging.debug("list_fates()")

response = request_get("/api/v1/fates?limit=all")
response = request_get("/api/v1/fates?limit=all&expand=eventtypes")
fates = response.json()["fates"]

response = request_get("/api/v1/eventtypes?limit=all")
event_types = response.json()["eventTypes"]

print "FATES ([id] created by => completed by)"
print " * = intermediate\n"
for fate in fates:
creation_event = None
completion_event = None
for event_type in event_types:
if event_type["id"] == fate['creationEventTypeId']:
creation_event = event_type
if event_type["id"] == fate['completionEventTypeId']:
completion_event = event_type

print "[{}]{} {} => {}".format(
fate["id"],
"*" if fate["intermediate"] else " ",
creation_event["category"] + "-" + creation_event["state"],
completion_event["category"] + "-" + completion_event["state"]
(
fate["creationEventType"]["category"]
+ "-" + fate["creationEventType"]["state"]
),
(
fate["completionEventType"]["category"]
+ "-" + fate["completionEventType"]["state"]
)
)
print fate["description"]
print ""
Expand Down Expand Up @@ -337,62 +381,22 @@ def list_host_labors(args):
logging.debug("list_host_labors(%s)", args.hostname)

response = request_get(
"/api/v1/hosts/{}"
"?expand=labors&expand=events&expand=eventtypes&limit={}".format(
args.hostname,
args.limit
)
)

host = response.json()
labors = host["labors"]

print "OPEN LABORS FOR {} (up to {}):".format(host["hostname"], args.limit)
for labor in labors:
creation_time = parser.parse(labor["creationTime"])
creation_time = creation_time.replace(tzinfo=tz.tzutc())
creation_time = creation_time.astimezone(tz.tzlocal())
if "targetTime" in labor and labor["targetTime"]:
target_time = parser.parse(labor["targetTime"])
target_time = target_time.replace(tzinfo=tz.tzutc())
target_time = target_time.astimezone(tz.tzlocal())
else:
target_time = "whenever"

print "[{}] created on {} (due by {}) \n\tby event {} {} {}".format(
labor["id"],
creation_time,
target_time,
labor["creationEvent"]["eventType"]["category"],
labor["creationEvent"]["eventType"]["state"],
labor["creationEvent"]["note"] or ""
"/api/v1/labors/"
"?open=true&expand=hosts&expand=eventtypes&expand=events"
"&expand=quests&limit={}&hostname={}".format(
args.limit,
args.hostname
)
)

def print_labor(labor):
"""Print the details of a labor to standard out
labors = response.json()["labors"]

Args:
labor: the labor to print
"""
creation_time = parser.parse(labor["creationTime"])
creation_time = creation_time.replace(tzinfo=tz.tzutc())
creation_time = creation_time.astimezone(tz.tzlocal())
if labor.get("targetTime", None):
target_time = parser.parse(labor["targetTime"])
target_time = target_time.replace(tzinfo=tz.tzutc())
target_time = target_time.astimezone(tz.tzlocal())
else:
target_time = "whenever"
response = request_get("/api/v1/fates?limit=all&expand=eventtypes")
fates = response.json()["fates"]

print "[{}] {}: \n\tcreated on {} (due by {}) \n\tby event {} {} {}".format(
labor["id"],
labor["host"]["hostname"],
creation_time,
target_time,
labor["creationEvent"]["eventType"]["category"],
labor["creationEvent"]["eventType"]["state"],
labor["creationEvent"]["note"] or ""
)
print "OPEN LABORS FOR {} (up to {}):".format(args.hostname, args.limit)
for labor in labors:
print_labor(labor, fates)


def list_host_labors_monitoring(args):
Expand All @@ -409,12 +413,13 @@ def list_host_labors_monitoring(args):
response = request_get(
"/api/v1/labors/"
"?open=true&expand=hosts&expand=eventtypes&expand=events"
"&limit=all&hostname={}".format(
"&expand=quests&limit=all&hostname={}".format(
args.hostname
)
)
json = response.json()
labors = json["labors"]

except HermesException as exc:
# FIXME -- add error codes to Hermes API and test for that
if exc.message.startswith("Error: No host"):
Expand All @@ -433,14 +438,17 @@ def list_host_labors_monitoring(args):
traceback.print_exc(file=sys.stdout)
sys.exit(3)

response = request_get("/api/v1/fates?limit=all&expand=eventtypes")
fates = response.json()["fates"]

if labors:
print "WARNING: {} open labor{} for this host".format(
len(labors),
"s" if len(labors) > 1 else ""
)
print ""
for labor in labors:
print_labor(labor)
print_labor(labor, fates)
print ""
print "For more details, login to shelby and run:"
print "hermes host labors {}".format(args.hostname)
Expand All @@ -452,7 +460,7 @@ def list_host_labors_monitoring(args):
def list_labors(args):
logging.debug("list_labors()")

url = "/api/v1/labors/?limit={}&open=true&expand=events&expand=hosts&expand=eventtypes".format(args.limit)
url = "/api/v1/labors/?limit={}&open=true&expand=events&expand=hosts&expand=eventtypes&expand=quests".format(args.limit)

if args.query:
url += "&hostQuery={}".format(args.query)
Expand All @@ -463,9 +471,12 @@ def list_labors(args):
labors = response.json()["labors"]
total_labors = response.json()["totalLabors"]

response = request_get("/api/v1/fates?limit=all&expand=eventtypes")
fates = response.json()["fates"]

print "OPEN LABORS ({} of {}):".format(len(labors), total_labors)
for labor in labors:
print_labor(labor)
print_labor(labor, fates)


def create_event(args):
Expand Down Expand Up @@ -525,10 +536,14 @@ def list_quests(args):
embark_time = parser.parse(quest["embarkTime"])
embark_time = embark_time.replace(tzinfo=tz.tzutc())
embark_time = embark_time.astimezone(tz.tzlocal())
print "[{}] {} by {}.\n\tEmbarked on {}".format(
quest["id"], quest["description"], quest["creator"],
print "[{}] by {}.\n\tEmbarked on {}".format(
quest["id"], quest["creator"],
embark_time
)
print "\t\"{}\"".format(textwrap.fill(
quest["description"],
width=60, subsequent_indent="\t "
))
if quest["targetTime"]:
target_time = parser.parse(quest["targetTime"])
target_time = target_time.replace(tzinfo=tz.tzutc())
Expand All @@ -541,7 +556,7 @@ def list_quests(args):
labors_remaining += 1

total_labors = len(quest["labors"])
print "{:.2%} complete. {} total labors. {} remain open.\n".format(
print "\n\t{:.2%} complete. {} total labors. {} remain open.\n".format(
(total_labors - labors_remaining)/total_labors,
total_labors,
labors_remaining
Expand Down
22 changes: 20 additions & 2 deletions hermes/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,8 @@ def get(self):
:query int limit: (*optional*) Limit result to N resources.
:query int offset: (*optional*) Skip the first N resources.
:query string expand: (*optional*) supports eventtypes
:statuscode 200: The request was successful.
:statuscode 401: The request was made without being logged in.
"""
Expand All @@ -1171,11 +1173,23 @@ def get(self):
offset, limit, expand = self.get_pagination_values()
hosts, total = self.paginate_query(fates, offset, limit)

fates_json = []
for fate in fates.all():
fate_json = fate.to_dict(API_VER)
if "eventtypes" in expand:
fate_json["creationEventType"] = (
fate.creation_event_type.to_dict(API_VER)
)
fate_json["completionEventType"] = (
fate.completion_event_type.to_dict(API_VER)
)
fates_json.append(fate_json)

json = {
"limit": limit,
"offset": offset,
"totalFates": total,
"fates": [fate.to_dict(API_VER) for fate in fates.all()],
"fates": fates_json,
}

self.success(json)
Expand Down Expand Up @@ -1379,7 +1393,7 @@ def get(self):
:query string userQuest: (*optional*) the user quest to send to the plugin to come up with the list of hostnames
:query boolean open: if true, filter Labors to those still open
:query int questId: the id of the quest we want to filter by
:query string expand: (*optional*) supports hosts, eventtypes, events
:query string expand: (*optional*) supports hosts, eventtypes, events, quests
:query int limit: (*optional*) Limit result to N resources.
:query int offset: (*optional*) Skip the first N resources.
Expand Down Expand Up @@ -1460,6 +1474,10 @@ def get(self):
labor_dicts = []
for labor in labors:
labor_dict = labor.to_dict(API_VER)
if "quests" in expand:
labor_dict["quest"] = (
labor.quest.to_dict(API_VER) if labor.quest else None
)
if "hosts" in expand:
labor_dict["host"] = labor.host.to_dict(API_VER)
if "events" in expand:
Expand Down

0 comments on commit ac68afa

Please sign in to comment.