Skip to content

Commit

Permalink
Merge pull request #85 from diggyk/master
Browse files Browse the repository at this point in the history
Change labor ownership behavior
  • Loading branch information
leojli committed Nov 3, 2015
2 parents 3f70aee + da6151b commit f5c6f09
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 66 deletions.
10 changes: 5 additions & 5 deletions db/update_to_051.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ VALUES
(1,1,NULL,0,1,'Reboot or release the system'),
(2,2,1,0,1,'System rebooted'),
(3,3,NULL,0,1,'Release or acknowledge downtime'),
(4,4,3,0,1,'Perform maintenance'),
(4,4,3,1,0,'Perform maintenance'),
(5,5,4,1,0,'Maintenance completed'),
(6,6,3,0,1,'Acknowledge maintenance'),
(6,6,3,1,0,'Perform online maintenance'),
(7,5,6,1,0,'Maintenance completed'),
(8,7,3,0,1,'Maintenance cancelled'),
(9,7,4,0,1,'Maintenance cancelled'),
(10,7,6,0,1,'Maintenance cancelled');
(8,7,3,1,0,'Maintenance cancelled'),
(9,7,4,1,0,'Maintenance cancelled'),
(10,7,6,1,0,'Maintenance cancelled');

/*!40000 ALTER TABLE fates ENABLE KEYS */;
UNLOCK TABLES;
Expand Down
61 changes: 32 additions & 29 deletions hermes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def get_starting_fates(cls, session):
return Fate._starting_fates

@classmethod
def question_the_fates(cls, session, events, quest=None):
def question_the_fates(cls, session, events, quest=None, starting_fates=None):
"""Look through the Fates and see if we need to create or close
Labors based on these Events.
Expand All @@ -716,7 +716,8 @@ def question_the_fates(cls, session, events, quest=None):

# Get all the fates, in various categories, for easy reference
all_fates = Fate.get_all_fates(session)
starting_fates = Fate.get_starting_fates(session)
if not starting_fates:
starting_fates = Fate.get_starting_fates(session)

# Query the database for open labors for hosts of which we have an event
open_labors = (
Expand Down Expand Up @@ -779,11 +780,6 @@ def question_the_fates(cls, session, events, quest=None):
# subsequent labors to see if the labor should be
# for the quest creator, server owner, or both.
if fate["precedes_ids"]:
designations = Fate.get_designations(
all_fates,
fate["precedes_ids"]
)

new_labor_dict = {
"host_id": host.id,
"starting_labor_id": (
Expand All @@ -796,8 +792,8 @@ def question_the_fates(cls, session, events, quest=None):
"quest_id": (
labor.quest.id if labor.quest else None
),
"for_creator": designations["for_creator"],
"for_owner": designations["for_owner"],
"for_creator": fate["for_creator"],
"for_owner": fate["for_owner"],
}
if new_labor_dict not in all_new_labors:
all_new_labors.append(new_labor_dict)
Expand All @@ -810,21 +806,6 @@ def question_the_fates(cls, session, events, quest=None):
session.flush()
session.commit()

@classmethod
def get_designations(cls, fates, ids):
for_owner = False
for_creator = False

for fate in fates.values():
if fate['id'] in ids:
for_owner = for_owner or fate["for_owner"]
for_creator = for_creator or fate["for_creator"]

return {
"for_owner": for_owner,
"for_creator": for_creator
}

def href(self, base_uri):
"""Create an HREF value for this object
Expand Down Expand Up @@ -966,7 +947,7 @@ def create(
return event

@classmethod
def create_many(cls, session, events, tx, quest=None):
def create_many(cls, session, events, tx, quest=None, fates=None):
"""Create multiple Events
Args:
Expand All @@ -975,6 +956,7 @@ def create_many(cls, session, events, tx, quest=None):
tx: transaction id tied to these bulk creations
quest: optional if events tied to quests
flush: indicate if we should flush after we are done
fate: the explicit list of fates of use when evaluating for labor creations
"""
log.debug("Event.create_many()")

Expand All @@ -991,7 +973,9 @@ def create_many(cls, session, events, tx, quest=None):
hook.on_event(event)

# refer to fates to see if these events should close or open any labors
Fate.question_the_fates(session, events, quest=quest)
Fate.question_the_fates(
session, events, quest=quest, starting_fates=fates
)

def href(self, base_uri):
"""Create an HREF value for this object
Expand Down Expand Up @@ -1063,8 +1047,8 @@ class Quest(Model):

@classmethod
def create(
cls, session, creator, hosts, creation_event_type, target_time=None,
create=True, description=None
cls, session, creator, hosts, creation_event_type,
target_time=None, create=True, description=None, fate_id=None
):
"""Create a new Quest.
Expand Down Expand Up @@ -1096,6 +1080,11 @@ def create(
if creation_event_type is None:
raise exc.ValidationError("Quest must have an EventType")

if fate_id:
fate = session.query(Fate).id(fate_id)
else:
fate = None

try:
quest = cls(
creator=creator, description=description,
Expand All @@ -1118,7 +1107,21 @@ def create(
"event_type_id": creation_event_type.id,
"tx": quest.id
})
Event.create_many(session, events_to_create, quest.id, quest=quest)
if fate:
Event.create_many(
session,
events_to_create,
quest.id,
quest=quest,
fates=[fate]
)
else:
Event.create_many(
session,
events_to_create,
quest.id,
quest=quest
)
else:
open_labors = (
session.query(Labor).filter(
Expand Down
2 changes: 1 addition & 1 deletion hermes/webapp/src/templates/fateViewer.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div ng-controller="FateCtrl as fateCtrl">
<div id="fatesView" style="width: 100%; height: 600px; border: 1px #000 dotted; overflow: scroll"></div>
<div id="fatesView" style="width: 100%; height: 200px;"></div>
</div>
4 changes: 2 additions & 2 deletions tests/api_tests/data/set1/fates.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"fate4": {
"creationEventTypeId": 4,
"followsId": 3,
"forCreator": true,
"forOwner": false,
"description": "A system that needs maintenance made ready before maintenance can occur."
},
"fate5": {
"creationEventTypeId": 5,
"followsId": 4,
"forCreator": true,
"forOwner": false,
"description": "Maintenance must be performed on a system that is prepped."
}
}
14 changes: 7 additions & 7 deletions tests/api_tests/test_quests.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def test_quest_lifecycle(sample_data1_server):

target_time = datetime.utcnow() + timedelta(days=7)

# Create a quest
# Create a quest with system-maintenance required
assert_created(
client.create(
"/quests",
Expand Down Expand Up @@ -468,8 +468,8 @@ def test_quest_lifecycle(sample_data1_server):
"creationEventTypeId": 4,
"description": "A system that needs maintenance made ready before maintenance can occur.",
"followsId": 3,
"forCreator": False,
"forOwner": True,
"forOwner": False,
"forCreator": True,
"id": 4,
"precedesIds": [5],
},
Expand All @@ -496,8 +496,8 @@ def test_quest_lifecycle(sample_data1_server):
"creationEventTypeId": 4,
"description": "A system that needs maintenance made ready before maintenance can occur.",
"followsId": 3,
"forCreator": False,
"forOwner": True,
"forOwner": False,
"forCreator": True,
"id": 4,
"precedesIds": [5],
},
Expand All @@ -524,8 +524,8 @@ def test_quest_lifecycle(sample_data1_server):
"creationEventTypeId": 4,
"description": "A system that needs maintenance made ready before maintenance can occur.",
"followsId": 3,
"forCreator": False,
"forOwner": True,
"forOwner": False,
"forCreator": True,
"id": 4,
"precedesIds": [5],
},
Expand Down
8 changes: 6 additions & 2 deletions tests/model_tests/test_labors.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,13 @@ def test_longer_chain(sample_data2):
labors = sample_data2.query(Labor).all()
assert len(labors) == 0

# system-maintenance audit
event_type_a = sample_data2.query(EventType).get(1)
# system-maintenance needed
event_type_b = sample_data2.query(EventType).get(2)
# system-maintenance ready
event_type_c = sample_data2.query(EventType).get(3)
# system-maintenance completed
event_type_d = sample_data2.query(EventType).get(4)

host = sample_data2.query(Host).get(1)
Expand Down Expand Up @@ -307,14 +311,14 @@ def test_longer_chain(sample_data2):
assert len(labors) == 1
assert len(host.labors) == 2
assert labors[0].starting_labor_id == starting_labor_id
assert labors[0].for_creator is True
assert labors[0].for_creator is False

event_c = Event.create(sample_data2, host, "system", event_type_c)
labors = Labor.get_open_unacknowledged(sample_data2)
assert len(labors) == 1
assert len(host.labors) == 3
assert labors[0].starting_labor_id == starting_labor_id
assert labors[0].for_creator is False
assert labors[0].for_creator is True

# This last event closes the final labor but does not create a new labor
event_d = Event.create(sample_data2, host, "system", event_type_d)
Expand Down
16 changes: 5 additions & 11 deletions tests/model_tests/test_quests.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,10 @@ def test_complex_chaining1(sample_data2):

assert labor1.host == hosts[0]
assert labor2.host == hosts[1]
# True b/c of Fate #2
assert labor1.for_owner is True
# True b/c of Fate #4
assert labor1.for_creator is True
# True b/c of Fate #2
assert labor1.for_creator is False
assert labor2.for_owner is True
# True b/c of Fate #4
assert labor2.for_creator is True
assert labor2.for_creator is False
assert labor1.creation_event == event1
assert labor2.creation_event == event2

Expand Down Expand Up @@ -539,10 +535,8 @@ def test_complex_chaining3(sample_data2):
)
assert bravo_quest.get_open_labors().all()[0].creation_event == event1
assert bravo_quest.get_open_labors().all()[0].starting_labor_id == hosts[0].events[0].id
# True b/c of Fate #2
assert bravo_quest.get_open_labors().all()[0].for_owner is True
# True b/c of Fate #4
assert bravo_quest.get_open_labors().all()[0].for_creator is True
assert bravo_quest.get_open_labors().all()[0].for_creator is False
assert len(bravo_quest.labors) == 2

# Now we progress the bravo quest again by throwing sys-maint-ready
Expand All @@ -553,8 +547,8 @@ def test_complex_chaining3(sample_data2):
)
assert bravo_quest.get_open_labors().all()[0].creation_event == event1b
assert bravo_quest.get_open_labors().all()[0].starting_labor_id == hosts[0].events[0].id
assert bravo_quest.get_open_labors().all()[0].for_owner is True
assert bravo_quest.get_open_labors().all()[0].for_creator is False
assert bravo_quest.get_open_labors().all()[0].for_owner is False
assert bravo_quest.get_open_labors().all()[0].for_creator is True
assert len(bravo_quest.labors) == 3

# Now we progress the charlie quest with the system-reboot-completed event
Expand Down
8 changes: 4 additions & 4 deletions tests/sample_data/sample_data1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ VALUES

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

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

INSERT INTO fates
VALUES
(3,4,1, 0, 1, 'A release finishes labors');
(3,4,1, 0, 1, 'System released');

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

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

INSERT INTO fates
VALUES
Expand Down
10 changes: 5 additions & 5 deletions tests/sample_data/sample_data2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ VALUES

INSERT INTO fates
VALUES
(3,3,2, 1, 0, 'Maintenance is needed so machine must be made ready.');
(3,3,2, 1, 0, 'Perform maintenance');

INSERT INTO fates
VALUES
(4,4,3, 0, 1, 'System is ready for maintenance might need work done.');
(4,4,3, 1, 0, 'Maintenance completed');

INSERT INTO fates
VALUES
(5,6,2, 0, 1, 'Maintenance is needed but a reboot might work.');
(5,6,2, 0, 1, 'System rebooted to finish maintenance');

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

INSERT INTO fates
VALUES
(7,6, 6, 0, 1, 'Reboot the system.');
(7,6, 6, 0, 1, 'Restart puppet');

INSERT INTO fates
VALUES
(8,7,7, 0, 1, 'Reboot was completed so puppet must be restarted.');
(8,7,7, 1, 0, 'Puppet restarted');

INSERT INTO hosts
VALUES (1, 'example.dropbox.com');
Expand Down

0 comments on commit f5c6f09

Please sign in to comment.