Skip to content

Commit

Permalink
Add a function to calculate wait time between each artifact and expos…
Browse files Browse the repository at this point in the history
…e it to the metadata in the REST API
  • Loading branch information
sarah256 committed Jun 27, 2019
1 parent 389cb90 commit 7640c1d
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 313 deletions.
2 changes: 2 additions & 0 deletions estuary/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def _get_partial_story(results, reverse=False):
rv['meta']['story_related_nodes_backward'] = [0]
rv['meta']['requested_node_index'] = 0
rv['meta']['story_type'] = story_manager.__class__.__name__[:-12].lower()
rv['meta']['wait_times'] = [0]
rv['data'][0]['resource_type'] = item.__label__
rv['data'][0]['display_name'] = item.display_name
rv['data'][0]['timeline_timestamp'] = item.timeline_timestamp
Expand Down Expand Up @@ -231,6 +232,7 @@ def _get_partial_stories(results, reverse=False):
rv['meta']['story_related_nodes_backward'] = [0]
rv['meta']['requested_node_index'] = 0
rv['meta']['story_type'] = story_manager.__class__.__name__[:-12].lower()
rv['meta']['wait_times'] = [0]
rv['data'][0]['resource_type'] = item.__label__
rv['data'][0]['display_name'] = item.display_name
rv['data'][0]['timeline_timestamp'] = item.timeline_timestamp
Expand Down
58 changes: 57 additions & 1 deletion estuary/utils/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys

from neomodel import db
from datetime import datetime

from estuary.error import ValidationError
from estuary.models.koji import ContainerKojiBuild, KojiBuild, ModuleKojiBuild
Expand Down Expand Up @@ -144,6 +145,60 @@ def get_sibling_nodes_count(self, results, reverse=False):
return correlated_nodes[::-1]
return correlated_nodes

def get_wait_times(self, results):
"""
Get the wait time between two different artifacts.
:param list results: contains inflated results from Neo4j
:return: list of wait time ints in order of the pipeline (oldest to newest)
:rtype: list
:raises RuntimeError: if results has less than 2 elements
"""
len_story = len(results)
if len_story < 2:
raise RuntimeError('This function can\'t be called with one or zero elements')

# Some services do not have a real completion time because they perform a single action
# that takes a negligible amount of time
completion_times = {
'BugzillaBug': 'creation_time',
'DistGitCommit': 'commit_date',
'Advisory': 'created_at',
'ContainerAdvisory': 'created_at',
# Although Freshmaker has a duration, we need to see how long it takes to trigger a
# ContainerKojiBuild from when it started
'FreshmakerEvent': 'time_created',
'KojiBuild': 'completion_time',
'ModuleKojiBuild': 'completion_time',
'ContainerKojiBuild': 'completion_time'
}

wait_times = [None for i in range(len_story - 1)]

for index in range(len_story - 1):
artifact = results[index]
next_artifact = results[index + 1]
property_name = completion_times[artifact.__label__]
completion_time = getattr(artifact, property_name)
if not completion_time or not next_artifact.timeline_timestamp:
continue

next_artifact_start_time = datetime.strptime(next_artifact.timeline_timestamp,
'%Y-%m-%dT%H:%M:%SZ')
# Remove timezone info so that both are offset naive and thus able to be subtracted
next_artifact_start_time = next_artifact_start_time.replace(tzinfo=None)
completion_time = completion_time.replace(tzinfo=None)

# Ensure that the artifacts are sequential
if completion_time > next_artifact_start_time:
continue

# Find the time between when the current artifact completes and the next one starts
wait_time = next_artifact_start_time - completion_time
wait_times[index] = wait_time.seconds

return wait_times

def get_sibling_nodes(self, siblings_node_label, story_node, count=False):
"""
Return sibling nodes with the label siblings_node_label that are related to story_node.
Expand Down Expand Up @@ -212,7 +267,8 @@ def format_story_results(self, results, requested_item):
'story_related_nodes_backward': list(
self.get_sibling_nodes_count(results, reverse=True)),
'requested_node_index': requested_node_index,
'story_type': self.__class__.__name__[:-12].lower()
'story_type': self.__class__.__name__[:-12].lower(),
'wait_times': self.get_wait_times(results)
}
}

Expand Down

0 comments on commit 7640c1d

Please sign in to comment.