-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from sarah256/recent-stories
Create function to get most recent nodes from the database
- Loading branch information
Showing
6 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# SPDX-License-Identifier: GPL-3.0+ | ||
|
||
from __future__ import unicode_literals | ||
|
||
from neomodel import db | ||
from estuary.models.koji import KojiBuild | ||
from estuary.models.bugzilla import BugzillaBug | ||
from estuary.models.distgit import DistGitCommit | ||
from estuary.models.errata import Advisory | ||
from estuary.models.freshmaker import FreshmakerEvent | ||
|
||
from estuary import log | ||
from estuary.utils.general import inflate_node | ||
|
||
|
||
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 | ||
""" | ||
label_dict = { | ||
FreshmakerEvent.__label__: 'id', | ||
BugzillaBug.__label__: 'modified_time', | ||
DistGitCommit.__label__: 'commit_date', | ||
KojiBuild.__label__: 'completion_time', | ||
Advisory.__label__: 'update_date' | ||
} | ||
|
||
query = 'CALL apoc.cypher.runMany(\'' | ||
for label, time_property in label_dict.items(): | ||
query += ( | ||
'\n MATCH ({label}:{label}) RETURN {label} ' | ||
'ORDER BY {label}.{time_property} DESC LIMIT 5;' | ||
).format(label=label, time_property=time_property) | ||
query = '{0}\'\n, {{}}, {{statistics: False, timeout: 15}});'.format(query) | ||
log.debug('Querying Neo4j with:\n{0}'.format(query)) | ||
|
||
final_result = {} | ||
results, _ = db.cypher_query(query) | ||
for result in [result[1] for result in results]: | ||
for label, raw_node in result.items(): | ||
node_results = final_result.setdefault(label, []) | ||
node = inflate_node(raw_node) | ||
node_results.append(node.serialized) | ||
|
||
return final_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# SPDX-License-Identifier: GPL-3.0+ | ||
|
||
from __future__ import unicode_literals | ||
from datetime import datetime | ||
|
||
import estuary.utils.recents | ||
from estuary.models.koji import KojiBuild | ||
from estuary.models.bugzilla import BugzillaBug | ||
from estuary.models.distgit import DistGitCommit | ||
from estuary.models.errata import Advisory | ||
from estuary.models.freshmaker import FreshmakerEvent | ||
|
||
|
||
def test_get_recent_nodes(): | ||
"""Test the get_recent_nodes function.""" | ||
BugzillaBug.get_or_create({ | ||
'id_': '11111', | ||
'modified_time': datetime(2017, 4, 26, 11, 44, 38) | ||
}) | ||
BugzillaBug.get_or_create({ | ||
'id_': '22222', | ||
'modified_time': datetime(2017, 6, 26, 11, 44, 38) | ||
}) | ||
BugzillaBug.get_or_create({ | ||
'id_': '33333', | ||
'modified_time': datetime(2017, 5, 26, 11, 44, 38) | ||
}) | ||
KojiBuild.get_or_create({ | ||
'id_': '44444', | ||
'completion_time': datetime(2017, 5, 27, 11, 44, 38) | ||
}) | ||
DistGitCommit.get_or_create({ | ||
'hash_': '55555', | ||
'commit_date': datetime(2017, 5, 2, 11, 44, 38) | ||
}) | ||
Advisory.get_or_create({ | ||
'id_': '66666', | ||
'update_date': datetime(2017, 5, 30, 11, 44, 38) | ||
}) | ||
FreshmakerEvent.get_or_create({ | ||
'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' |