Skip to content

Commit

Permalink
Merge pull request #236 from sarah256/recent-stories
Browse files Browse the repository at this point in the history
Add metadata for recents endpoint that specifies the id and timestamp keys
  • Loading branch information
sarah256 committed May 21, 2019
2 parents 07ea9c8 + 7ed2a1d commit 788030b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
5 changes: 2 additions & 3 deletions estuary/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,9 @@ def get_artifact_relationships(resource, uid, relationship):
@login_required
def get_recent_stories():
"""Get stories that were most recently updated, by their artifact type."""
nodes = get_recent_nodes()
nodes, meta = get_recent_nodes()
result = {
'data': nodes,
'metadata': {}
'metadata': meta
}

return jsonify(result)
25 changes: 16 additions & 9 deletions estuary/utils/recents.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ def get_recent_nodes():
"""
Get the most recent nodes of each node type.
:return: a dictionary with the keys as names of each node type, and
values of arrays of the most recents of each node
:rtype: dict
:return: a tuple with the first value as a dictionary with the keys as
names of each node type, and values of arrays of the most recents of
each node, and the second as metadata
:rtype: tuple
"""
label_dict = {
timestamp_dict = {
FreshmakerEvent.__label__: 'id',
BugzillaBug.__label__: 'modified_time',
DistGitCommit.__label__: 'commit_date',
KojiBuild.__label__: 'completion_time',
Advisory.__label__: 'update_date'
}

final_result = {}
for label, time_property in label_dict.items():
id_dict = {}
final_result_data = {}
final_result_metadata = {
'id_keys': id_dict,
'timestamp_keys': timestamp_dict
}
for label, time_property in timestamp_dict.items():
query = (
'MATCH (node:{label}) '
'WHERE node.{time_property} IS NOT NULL '
Expand All @@ -38,9 +43,11 @@ def get_recent_nodes():
).format(label=label, time_property=time_property)
results, _ = db.cypher_query(query)
for result in results:
node_results = final_result.setdefault(label, [])
node_results = final_result_data.setdefault(label, [])
# result is always a list of a single node
node = inflate_node(result[0])
node_results.append(node.serialized)
if node.__label__ not in id_dict:
id_dict[node.__label__] = node.unique_id_property

return final_result
return (final_result_data, final_result_metadata)
19 changes: 18 additions & 1 deletion tests/api/test_recents.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@

def test_get_recent_nodes(client):
"""Test the get_recent_nodes function."""
id_dict = {
FreshmakerEvent.__label__: 'id_',
BugzillaBug.__label__: 'id_',
DistGitCommit.__label__: 'hash_',
KojiBuild.__label__: 'id_',
Advisory.__label__: 'id_'
}
timestamp_dict = {
FreshmakerEvent.__label__: 'id',
BugzillaBug.__label__: 'modified_time',
DistGitCommit.__label__: 'commit_date',
KojiBuild.__label__: 'completion_time',
Advisory.__label__: 'update_date'
}
expected = {
'data': {
'Advisory': [
Expand Down Expand Up @@ -114,7 +128,10 @@ def test_get_recent_nodes(client):
}
]
},
'metadata': {}
'metadata': {
'id_keys': id_dict,
'timestamp_keys': timestamp_dict
}
}

BugzillaBug.get_or_create({
Expand Down
36 changes: 27 additions & 9 deletions tests/utils/test_recents.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,30 @@ def test_get_recent_nodes():
'id_': '77777'
})

ret = estuary.utils.recents.get_recent_nodes()

assert ret['Advisory'][0]['id'] == '66666'
assert ret['DistGitCommit'][0]['hash'] == '55555'
assert ret['FreshmakerEvent'][0]['id'] == '77777'
assert ret['KojiBuild'][0]['id'] == '44444'
assert ret['BugzillaBug'][0]['id'] == '22222'
assert ret['BugzillaBug'][1]['id'] == '33333'
assert ret['BugzillaBug'][2]['id'] == '11111'
nodes, meta = estuary.utils.recents.get_recent_nodes()

assert nodes['Advisory'][0]['id'] == '66666'
assert nodes['DistGitCommit'][0]['hash'] == '55555'
assert nodes['FreshmakerEvent'][0]['id'] == '77777'
assert nodes['KojiBuild'][0]['id'] == '44444'
assert nodes['BugzillaBug'][0]['id'] == '22222'
assert nodes['BugzillaBug'][1]['id'] == '33333'
assert nodes['BugzillaBug'][2]['id'] == '11111'

id_dict = {
FreshmakerEvent.__label__: 'id_',
BugzillaBug.__label__: 'id_',
DistGitCommit.__label__: 'hash_',
KojiBuild.__label__: 'id_',
Advisory.__label__: 'id_'
}
timestamp_dict = {
FreshmakerEvent.__label__: 'id',
BugzillaBug.__label__: 'modified_time',
DistGitCommit.__label__: 'commit_date',
KojiBuild.__label__: 'completion_time',
Advisory.__label__: 'update_date'
}

assert meta['id_keys'] == id_dict
assert meta['timestamp_keys'] == timestamp_dict

0 comments on commit 788030b

Please sign in to comment.