Skip to content

Commit

Permalink
Merge pull request #222 from sarah256/recent-stories
Browse files Browse the repository at this point in the history
Create function to get most recent nodes from the database
  • Loading branch information
mprahl committed Feb 18, 2019
2 parents 3f37b22 + d1209b2 commit 707bee2
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion estuary/models/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BugzillaBug(EstuaryStructuredNode):
classification = StringProperty()
creation_time = DateTimeProperty()
id_ = UniqueIdProperty(db_property='id')
modified_time = DateTimeProperty()
modified_time = DateTimeProperty(index=True)
priority = StringProperty()
# Called product_name in case we want to use product as a relationship later on
product_name = StringProperty()
Expand Down
2 changes: 1 addition & 1 deletion estuary/models/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DistGitCommit(EstuaryStructuredNode):
"""Definition of a dist-git commit in Neo4j."""

author_date = DateTimeProperty()
commit_date = DateTimeProperty()
commit_date = DateTimeProperty(index=True)
hash_ = UniqueIdProperty(db_property='hash')
log_message = StringProperty()
author = RelationshipTo('.user.User', 'AUTHORED_BY', cardinality=ZeroOrOne)
Expand Down
2 changes: 1 addition & 1 deletion estuary/models/errata.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Advisory(EstuaryStructuredNode):
state = StringProperty()
status_time = DateTimeProperty()
synopsis = StringProperty()
update_date = DateTimeProperty()
update_date = DateTimeProperty(index=True)
assigned_to = RelationshipTo('.user.User', 'ASSIGNED_TO', cardinality=ZeroOrOne)
attached_bugs = RelationshipTo('.bugzilla.BugzillaBug', 'ATTACHED')
attached_builds = RelationshipTo('.koji.KojiBuild', 'ATTACHED')
Expand Down
2 changes: 1 addition & 1 deletion estuary/models/koji.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class KojiBuild(EstuaryStructuredNode):

advisories = RelationshipFrom('.errata.Advisory', 'ATTACHED')
commit = RelationshipTo('.distgit.DistGitCommit', 'BUILT_FROM', cardinality=ZeroOrOne)
completion_time = DateTimeProperty()
completion_time = DateTimeProperty(index=True)
creation_time = DateTimeProperty()
epoch = StringProperty()
extra = StringProperty()
Expand Down
49 changes: 49 additions & 0 deletions estuary/utils/recents.py
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
52 changes: 52 additions & 0 deletions tests/utils/test_recents.py
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'

0 comments on commit 707bee2

Please sign in to comment.