Skip to content

Commit

Permalink
Merge pull request #83 from diggyk/fates_refactor
Browse files Browse the repository at this point in the history
Finished refactor
  • Loading branch information
jathanism committed Oct 28, 2015
2 parents 175e55e + ad70e52 commit 2ec429f
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 115 deletions.
86 changes: 28 additions & 58 deletions bin/hermes
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def print_labor(labor, fates):
else:
target_time_str = ""

print "[{}] {}: \n\tcreated on {} {} \n\tby event {} {}".format(
print "({}) {}: \n\tcreated on {} {} \n\tby event {} {}".format(
labor["id"],
labor["host"]["hostname"],
creation_time,
Expand All @@ -190,7 +190,10 @@ def print_labor(labor, fates):

print "\n\tPossible Resolutions:"
for fate in fates:
if fate["creationEventTypeId"] == labor["creationEvent"]["eventType"]["id"]:
if (
fate["creationEventTypeId"]
== labor["creationEvent"]["eventType"]["id"]
):
print "{}".format(textwrap.fill(
fate["description"], width=60,
initial_indent="\t* ", subsequent_indent="\t "
Expand All @@ -216,7 +219,7 @@ def list_event_types(args):
)
for event_type in event_types["eventTypes"]:
print "{}{}{}".format(
str.ljust("[" + str(event_type["id"]) + "]", max_length + 3),
str.ljust("(" + str(event_type["id"]) + ")", max_length + 3),
str.ljust(str(event_type["category"]), 20),
str.ljust(str(event_type["state"]), 20)
)
Expand Down Expand Up @@ -247,25 +250,22 @@ def list_fates(args):
response = request_get("/api/v1/fates?limit=all&expand=eventtypes")
fates = response.json()["fates"]

print "FATES: \n[id] created by => completed by\n"
print "FATES: \n(id) created by => completed by\n"
for fate in fates:
if not fate.get("follows_id"):
created_by_str = (
if not fate.get("precedesIds"):
print "({}) {}".format(
fate["id"],
fate["creationEventType"]["category"] + " "
+ fate["creationEventType"]["state"]
)
else:
created_by_str = (
"(follows Fate {})".format(fate["follows_id"])
)
print "[{}] {} => {}".format(
fate["id"],
created_by_str,
(
fate["completionEventType"]["category"]
+ " " + fate["completionEventType"]["state"]
print "({}) {} => {}".format(
fate["id"],
fate["creationEventType"]["category"] + " "
+ fate["creationEventType"]["state"],
fate["precedesIds"]
)
)

print textwrap.fill(
fate["description"], width=60,
initial_indent="\t", subsequent_indent="\t"
Expand All @@ -281,18 +281,11 @@ def create_fate(args):
"--creation-category and creation-state."
)
return
if not args.xt and not (args.xc and args.xs):
print (
"To create a fate, you must specify either --completion-type,\nor "
"--completion-category and completion-state."
)
return

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

creation_event_type = None
completion_event_type = None

for event_type in event_types:
if args.cc == event_type["category"] \
Expand All @@ -301,24 +294,13 @@ def create_fate(args):
elif args.ct == str(event_type["id"]):
creation_event_type = event_type

if args.xc == event_type["category"] \
and args.xs == event_type["state"]:
completion_event_type = event_type
elif args.xt == str(event_type["id"]):
completion_event_type = event_type

if not creation_event_type:
print "Creation event type not found."
return

if not completion_event_type:
print "Completion event type not found."
return

json = {
"creationEventTypeId": creation_event_type["id"],
"completionEventTypeId": completion_event_type["id"],
"follows_id": args.follows_id,
"followsId": args.follows_id,
"description": args.description
}

Expand Down Expand Up @@ -643,7 +625,7 @@ 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(
print "({}) by {}.\n\tEmbarked on {}".format(
quest["id"], quest["creator"],
embark_time
)
Expand All @@ -667,7 +649,7 @@ def list_quests(args):
def show_quest(args):
logging.debug("show_quest(%d)", args.quest_id)
url = (
"/api/v1/quests/{}?filterClosed=true&limit=all"
"/api/v1/quests/{}?filterClosed=true&limit=all&progressInfo=true"
"&expand=labors&expand=hosts&expand=events&expand=eventtypes".format(
args.quest_id
)
Expand Down Expand Up @@ -745,19 +727,20 @@ def show_quest(args):
labor["creationEvent"]["eventType"]["state"]
)

labors_remaining = len(remaining)
labors_completed = len(completed)
if labors_remaining:
if remaining:
print "\n\tOPEN LABORS BY TYPE:"
for type in remaining_types.iterkeys():
print "\t\t{}: {}".format(
type,
remaining_types[type]
)
print "\n\t{:.2%} complete. {} total labors. {} remain open.\n".format(
labors_completed/total_labors,
total_labors,
labors_remaining
print "\n\t{}% complete. {} not started, {} in progress, \n\t" \
"{} completed, and {} total labors\n".format(
quest["percentComplete"],
quest["unstartedLabors"],
quest["inprogressLabors"],
quest["completedLabors"],
quest["totalLabors"]
)
else:
if args.completed_only or args.all:
Expand Down Expand Up @@ -927,22 +910,9 @@ def parse_cli_args():
"-ct", "--creation-type", help="The ID of the creation type",
dest="ct", type=str
)
fate_create_subparser.add_argument(
"-xc", "--completion-category",
help="The category of the completion event type",
dest="xc", type=str
)
fate_create_subparser.add_argument(
"-xs", "--completion-state", help="The state of the completion event type",
dest="xs", type=str
)
fate_create_subparser.add_argument(
"-xt", "--completion-type", help="The ID of the completion event type",
dest="xt", type=str
)
fate_create_subparser.add_argument(
"--follows", type=int, default=None, dest="follows_id",
help="This Fate follows the Fate identified by ID here."
help="This Fate follows the Fate identified by ID."
)
fate_create_subparser.add_argument(
"--description", help="The human readable description of this fate",
Expand Down
14 changes: 11 additions & 3 deletions db/update_to_05.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ UPDATE `labors` SET `fate_id`=1;
ALTER TABLE `labors` ADD FOREIGN KEY `ix_labors_fate_id` (`fate_id`) REFERENCES FATES(`id`);


update `labors` l SET `fate_id`=(
select f.id from `events` e, `event_types` et, `fates` f where l.creation_event_id = e.id and e.event_type_id = et.id and f.creation_type_id = et.id
)
UPDATE `labors` l
SET `fate_id` = (
SELECT f.id
FROM `events` e, `event_types` et, `fates` f
WHERE l.creation_event_id = e.id AND e.event_type_id = et.id AND
f.creation_type_id = et.id
);

INSERT INTO fates
VALUES
(6,4,1, 0, 1, 'A release finishes labors');
46 changes: 23 additions & 23 deletions hermes/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,8 +1180,8 @@ def post(self):
{
"creationEventTypeId": 1,
"description": "This is a fate",
"follows_id": 1,
"for_creator": true,
"followsId": 1,
"forCreator": true,
}
**Example response:**
Expand All @@ -1196,9 +1196,9 @@ def post(self):
"href": "/api/v1/fates/3",
"id": 3,
"creationEventTypeId": 1,
"follows_id": 1,
"precedes_ids": [],
"for_creator": true,
"followsId": 1,
"precedesIds": [],
"forCreator": true,
"description": "This is a fate"
}
Expand All @@ -1219,9 +1219,9 @@ def post(self):

try:
creation_event_type_id = self.jbody["creationEventTypeId"]
follows_id = self.jbody.get("follows_id")
for_creator = self.jbody.get("for_creator", False)
for_owner = self.jbody.get("for_owner", True)
follows_id = self.jbody.get("followsId")
for_creator = self.jbody.get("forCreator", False)
for_owner = self.jbody.get("forOwner", True)
description = self.jbody["description"]
except KeyError as err:
raise exc.BadRequest(
Expand Down Expand Up @@ -1283,10 +1283,10 @@ def get(self):
"id": 1,
"href": "/api/v1/fates/1",
"creationEventTypeId": 1,
"follows_id": null,
"precedes_ids": [],
"for_creator": 0,
"precedes_ids": [3, 5],
"followsId": null,
"precedesIds": [],
"forCreator": 0,
"precedesIds": [3, 5],
"description": "This is a fate",
},
...
Expand Down Expand Up @@ -1344,10 +1344,10 @@ def get(self, id):
"id": 1,
"href": "/api/v1/fates/1",
"creationEventTypeId": 1,
"follows_id": null,
"precedes_ids": [],
"for_creator": false,
"for_owner": true,
"followsId": null,
"precedesIds": [],
"forCreator": false,
"forOwner": true,
"description": string,
}
Expand Down Expand Up @@ -1382,7 +1382,7 @@ def put(self, id):
{
"description": "New desc",
"follows_id": 1
"followsId": 1
}
**Example response:**
Expand All @@ -1397,10 +1397,10 @@ def put(self, id):
"id": 3,
"href": "/api/v1/fates/3",
"creationEventTypeId": 1,
"follows_id": 1,
"precedes_id": [],
"for_creator": false,
"for_owner": true
"followsId": 1,
"precedesId": [],
"forCreator": false,
"forOwner": true
"description": "New desc"
}
Expand All @@ -1427,8 +1427,8 @@ def put(self, id):
try:
if "description" in self.jbody:
fate = fate.update(description=self.jbody["description"])
if "follows_id" in self.jbody:
fate = fate.update(follows_id=self.jbody['follows_id'])
if "followsId" in self.jbody:
fate = fate.update(follows_id=self.jbody['followsId'])

except IntegrityError as err:
raise exc.Conflict(str(err.orig))
Expand Down
4 changes: 2 additions & 2 deletions hermes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,8 @@ def to_dict(self, base_uri=None, expand=None):
out = {
"id": self.id,
"creationEventTypeId": self.creation_type_id,
"follows_id": self.follows_id,
"precedes_ids": [labor.id for labor in self.precedes],
"followsId": self.follows_id,
"precedesIds": [labor.id for labor in self.precedes],
"forCreator": self.for_creator,
"forOwner": self.for_owner,
"description": self.description,
Expand Down
8 changes: 6 additions & 2 deletions hermes/webapp/src/js/controllers/fateCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@
addNodeLabel(vm.paper, settings, node);
}

vm.paper.setViewBox(0, 0, vm.paper.width, vm.paper.height, true);
vm.panZoom = vm.paper.panzoom({ initialZoom: 1, initialPosition: { x: 0, y: 0} });
vm.paper.setViewBox(0, 0, vm.paper.width + 500, vm.paper.height + 500, true);
vm.panZoom = vm.paper.panzoom({
initialZoom: 1,
initialPosition: { x: 0, y: 0},
maxZoom: 1,
});
vm.panZoom.enable();
}

Expand Down
6 changes: 3 additions & 3 deletions hermes/webapp/src/js/services/hermesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,22 @@
// first, let's graph out the nodes and the edges,
// but we won't add positions until afterwards
for (var x in fates) {
if (!fates[x]["follows_id"]) {
if (!fates[x]["followsId"]) {
parseFate(fates, fates[x])
}
}

function parseFate(fates, fate) {
var rootId;
if (!fate["follows_id"]) {
if (!fate["followsId"]) {
rootId = createRootNode(fate["creationEventType"]);
} else {
rootId = createChildNode(fate["creationEventType"], fate["id"])
}

var children = [];
for (var x in fates) {
if (fate['precedes_ids'].indexOf(fates[x]['id']) != -1) {
if (fate['precedesIds'].indexOf(fates[x]['id']) != -1) {
children.push(parseFate(fates, fates[x]));
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/api_tests/data/set1/fates.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"fate2": {
"creationEventTypeId": 2,
"follows_id": 1,
"followsId": 1,
"description": "A system that needs a reboot can be cleared by rebooting the machine."
},
"fate3": {
Expand All @@ -14,14 +14,14 @@
},
"fate4": {
"creationEventTypeId": 4,
"follows_id": 3,
"followsId": 3,
"description": "A system that needs maintenance made ready before maintenance can occur."
},
"fate5": {
"creationEventTypeId": 5,
"follows_id": 4,
"for_creator": true,
"for_owner": false,
"followsId": 4,
"forCreator": true,
"forOwner": false,
"description": "Maintenance must be performed on a system that is prepped."
}
}

0 comments on commit 2ec429f

Please sign in to comment.