Skip to content

Commit

Permalink
Merge pull request #82 from diggyk/fates_refactor
Browse files Browse the repository at this point in the history
Fates refactor
  • Loading branch information
jathanism committed Oct 22, 2015
2 parents 398717d + 7a8ce9d commit 175e55e
Show file tree
Hide file tree
Showing 18 changed files with 346 additions and 269 deletions.
25 changes: 13 additions & 12 deletions bin/hermes
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,12 @@ def list_quests(args):
logging.debug("list_quests()")

response = request_get(
"/api/v1/quests?filterClosed=true&limit=all&expand=labors"
"/api/v1/quests?filterClosed=true&limit=all&progressInfo=true"
)
quests = response.json()["quests"]

print "OPEN QUESTS:"
print "\t* Labors: unstarted/in-progress/completed/total\n\n"
for quest in quests:
if args.user and quest["creator"] != args.user:
continue
Expand All @@ -656,16 +657,10 @@ def list_quests(args):
target_time = target_time.astimezone(tz.tzlocal())
print"\tTarget Completion: {}".format(target_time)

labors_remaining = 0
for labor in quest["labors"]:
if labor["completionTime"] is None:
labors_remaining += 1

total_labors = len(quest["labors"])
print "\n\t{:.2%} complete. {} total labors. {} remain open.\n".format(
(total_labors - labors_remaining)/total_labors,
total_labors,
labors_remaining
print "\n\t{}% complete. Labors: {}/{}/{}/{}\n".format(
quest["percentComplete"], quest["unstartedLabors"],
quest["inprogressLabors"], quest["completedLabors"],
quest["totalLabors"]
)


Expand All @@ -684,7 +679,14 @@ def show_quest(args):
remaining = []
remaining_types = {}
completed = []
total_labors = 0
total_unstarted = 0
for labor in quest["labors"]:
if labor["startingLaborId"] is None:
total_labors += 1
if labor["completionTime"] is None:
total_unstarted += 1

if len(labor["host"]["hostname"]) > max_width:
max_width = len(labor["host"]["hostname"])

Expand Down Expand Up @@ -743,7 +745,6 @@ def show_quest(args):
labor["creationEvent"]["eventType"]["state"]
)

total_labors = len(quest["labors"])
labors_remaining = len(remaining)
labors_completed = len(completed)
if labors_remaining:
Expand Down
33 changes: 33 additions & 0 deletions db/update_to_05.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
TRUNCATE TABLE `fates`;
ALTER TABLE `fates` DROP FOREIGN KEY `fates_ibfk_2`;
ALTER TABLE `fates` DROP `completion_type_id`;

INSERT INTO fates
VALUES
(1,1,NULL, 0, 1, 'Reboot or release the system.');

INSERT INTO fates
VALUES
(2,2,1, 0, 1, 'A reboot finishes labors.');

INSERT INTO fates
VALUES
(3,3,NULL, 0, 1, 'Release or acknowledge downtime');

INSERT INTO fates
VALUES
(4,4,3, 0, 1, 'Perform maintenance');

INSERT INTO fates
VALUES
(5,5,4, 1, 0, 'Maintenance completed');

ALTER TABLE `labors` ADD `fate_id` INT(11) NOT NULL DEFAULT 0 AFTER `starting_labor_id`;

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
)
63 changes: 10 additions & 53 deletions hermes/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,6 @@ def post(self):
Content-Type: application/json
{
"creationEventTypeId": 1,
"completionEventTypeId": 2,
"description": "This is a fate",
"follows_id": 1,
"for_creator": true,
Expand All @@ -1197,14 +1196,13 @@ def post(self):
"href": "/api/v1/fates/3",
"id": 3,
"creationEventTypeId": 1,
"completionEventTypeId": 2,
"follows": 1,
"follows_id": 1,
"precedes_ids": [],
"for_creator": true,
"description": "This is a fate"
}
:reqjson int creationEventTypeId: the ID of the EventType that triggers this Fate
:regjson int completionEventTypeId: the ID of the EventType that closes Labors created by this Fate
:regjson int follows: (*optional*) The ID of the Fate this Fate must come after, or null
:regjson string description: (*optional*) The human readable description this Fate
Expand All @@ -1221,7 +1219,6 @@ def post(self):

try:
creation_event_type_id = self.jbody["creationEventTypeId"]
completion_event_type_id = self.jbody["completionEventTypeId"]
follows_id = self.jbody.get("follows_id")
for_creator = self.jbody.get("for_creator", False)
for_owner = self.jbody.get("for_owner", True)
Expand All @@ -1241,17 +1238,9 @@ def post(self):
self.write_error(400, message="Bad creation event type")
return

completion_event_type = (
self.session.query(EventType).get(completion_event_type_id)
)

if completion_event_type is None:
self.write_error(400, message="Bad event type")
return

try:
fate = Fate.create(
self.session, creation_event_type, completion_event_type,
self.session, creation_event_type,
follows_id=follows_id, for_creator=for_creator,
for_owner=for_owner,
description=description
Expand Down Expand Up @@ -1294,8 +1283,8 @@ def get(self):
"id": 1,
"href": "/api/v1/fates/1",
"creationEventTypeId": 1,
"completionEventType": 2,
"follows_id": null,
"precedes_ids": [],
"for_creator": 0,
"precedes_ids": [3, 5],
"description": "This is a fate",
Expand Down Expand Up @@ -1355,8 +1344,8 @@ def get(self, id):
"id": 1,
"href": "/api/v1/fates/1",
"creationEventTypeId": 1,
"completionEventType": 2,
"follows_id": null,
"precedes_ids": [],
"for_creator": false,
"for_owner": true,
"description": string,
Expand Down Expand Up @@ -1408,8 +1397,8 @@ def put(self, id):
"id": 3,
"href": "/api/v1/fates/3",
"creationEventTypeId": 1,
"completionEventType": 2,
"follows_id": 1,
"precedes_id": [],
"for_creator": false,
"for_owner": true
"description": "New desc"
Expand Down Expand Up @@ -2090,23 +2079,7 @@ def get(self):
expand=set(expand)
)
if progress_info:
labor_count = self.session.query(Labor).filter(
Labor.quest_id == quest.id
).count()
open_labors_count = self.session.query(Labor).filter(
and_(
Labor.quest_id == quest.id,
Labor.completion_time == None
)
).count()

percent_complete = round(
(labor_count - open_labors_count) / labor_count * 100,
2
)
quest_json['totalLabors'] = labor_count
quest_json['openLabors'] = open_labors_count
quest_json['percentComplete'] = percent_complete
quest_json = quest.calcuate_progress(quest_json)
quests_json.append(quest_json)

json = {
Expand Down Expand Up @@ -2170,31 +2143,15 @@ def get(self, id):
if not quest:
raise exc.NotFound("No such Quest {} found".format(id))

json = quest.to_dict(
out = quest.to_dict(
base_uri=self.href_prefix, expand=set(expand),
only_open_labors=only_open_labors
)

if progress_info:
labor_count = self.session.query(Labor).filter(
Labor.quest_id == quest.id
).count()
open_labors_count = self.session.query(Labor).filter(
and_(
Labor.quest_id == quest.id,
Labor.completion_time == None
)
).count()
out = quest.calcuate_progress(out)

percent_complete = round(
(labor_count - open_labors_count) / labor_count * 100,
2
)
json['totalLabors'] = labor_count
json['openLabors'] = open_labors_count
json['percentComplete'] = percent_complete

self.success(json)
self.success(out)

def put(self, id):
"""**Update a Quest**
Expand Down

0 comments on commit 175e55e

Please sign in to comment.