Unverified Commit 55c0fb65 authored by Tao Feng's avatar Tao Feng Committed by GitHub

Update the query publish tag to match entity (#221)

parent 824c9e48
...@@ -73,14 +73,14 @@ class Neo4jSearchDataExtractor(Extractor): ...@@ -73,14 +73,14 @@ class Neo4jSearchDataExtractor(Extractor):
# 2. add more fields once we have in the graph; 3. change mode to generic once add more support for dashboard # 2. add more fields once we have in the graph; 3. change mode to generic once add more support for dashboard
DEFAULT_NEO4J_DASHBOARD_CYPHER_QUERY = textwrap.dedent( DEFAULT_NEO4J_DASHBOARD_CYPHER_QUERY = textwrap.dedent(
""" """
MATCH (db:Dashboard) MATCH (dashboard:Dashboard)
OPTIONAL MATCH (db)-[:DASHBOARD_OF]->(dbg:Dashboardgroup) OPTIONAL MATCH (dashboard)-[:DASHBOARD_OF]->(dbg:Dashboardgroup)
OPTIONAL MATCH (db)-[:DESCRIPTION]->(db_descr:Description) OPTIONAL MATCH (dashboard)-[:DESCRIPTION]->(db_descr:Description)
OPTIONAL MATCH (dbg)-[:DESCRIPTION]->(dbg_descr:Description) OPTIONAL MATCH (dbg)-[:DESCRIPTION]->(dbg_descr:Description)
{publish_tag_filter} {publish_tag_filter}
with db, dbg, db_descr, dbg_descr with dashboard, dbg, db_descr, dbg_descr
where db.name is not null where dashboard.name is not null
return dbg.name as dashboard_group, db.name as dashboard_name, return dbg.name as dashboard_group, dashboard.name as dashboard_name,
coalesce(db_descr.description, '') as description, coalesce(db_descr.description, '') as description,
coalesce(dbg.description, '') as dashboard_group_description, coalesce(dbg.description, '') as dashboard_group_description,
'mode' as product, 'mode' as product,
...@@ -102,12 +102,12 @@ class Neo4jSearchDataExtractor(Extractor): ...@@ -102,12 +102,12 @@ class Neo4jSearchDataExtractor(Extractor):
Initialize Neo4jExtractor object from configuration and use that for extraction Initialize Neo4jExtractor object from configuration and use that for extraction
""" """
self.conf = conf self.conf = conf
self.entity = conf.get_string(Neo4jSearchDataExtractor.ENTITY_TYPE, default='table').lower()
# extract cypher query from conf, if specified, else use default query # extract cypher query from conf, if specified, else use default query
if Neo4jSearchDataExtractor.CYPHER_QUERY_CONFIG_KEY in conf: if Neo4jSearchDataExtractor.CYPHER_QUERY_CONFIG_KEY in conf:
self.cypher_query = conf.get_string(Neo4jSearchDataExtractor.CYPHER_QUERY_CONFIG_KEY) self.cypher_query = conf.get_string(Neo4jSearchDataExtractor.CYPHER_QUERY_CONFIG_KEY)
else: else:
entity_type = conf.get_string(Neo4jSearchDataExtractor.ENTITY_TYPE, default='table').lower() default_query = Neo4jSearchDataExtractor.DEFAULT_QUERY_BY_ENTITY[self.entity]
default_query = Neo4jSearchDataExtractor.DEFAULT_QUERY_BY_ENTITY[entity_type]
self.cypher_query = self._add_publish_tag_filter(conf.get_string(JOB_PUBLISH_TAG, ''), self.cypher_query = self._add_publish_tag_filter(conf.get_string(JOB_PUBLISH_TAG, ''),
cypher_query=default_query) cypher_query=default_query)
...@@ -148,5 +148,8 @@ class Neo4jSearchDataExtractor(Extractor): ...@@ -148,5 +148,8 @@ class Neo4jSearchDataExtractor(Extractor):
if not publish_tag: if not publish_tag:
publish_tag_filter = '' publish_tag_filter = ''
else: else:
publish_tag_filter = """WHERE table.published_tag = '{}'""".format(publish_tag) if not hasattr(self, 'entity'):
self.entity = 'table'
publish_tag_filter = """WHERE {entity}.published_tag = '{tag}'""".format(entity=self.entity,
tag=publish_tag)
return cypher_query.format(publish_tag_filter=publish_tag_filter) return cypher_query.format(publish_tag_filter=publish_tag_filter)
...@@ -5,6 +5,7 @@ from pyhocon import ConfigFactory ...@@ -5,6 +5,7 @@ from pyhocon import ConfigFactory
from databuilder import Scoped from databuilder import Scoped
from databuilder.extractor.neo4j_extractor import Neo4jExtractor from databuilder.extractor.neo4j_extractor import Neo4jExtractor
from databuilder.extractor.neo4j_search_data_extractor import Neo4jSearchDataExtractor from databuilder.extractor.neo4j_search_data_extractor import Neo4jSearchDataExtractor
from databuilder.publisher.neo4j_csv_publisher import JOB_PUBLISH_TAG
class TestNeo4jExtractor(unittest.TestCase): class TestNeo4jExtractor(unittest.TestCase):
...@@ -42,6 +43,29 @@ class TestNeo4jExtractor(unittest.TestCase): ...@@ -42,6 +43,29 @@ class TestNeo4jExtractor(unittest.TestCase):
self.assertEqual(extractor.cypher_query, Neo4jSearchDataExtractor self.assertEqual(extractor.cypher_query, Neo4jSearchDataExtractor
.DEFAULT_NEO4J_DASHBOARD_CYPHER_QUERY.format(publish_tag_filter='')) .DEFAULT_NEO4J_DASHBOARD_CYPHER_QUERY.format(publish_tag_filter=''))
def test_default_search_query_with_tag(self):
# type: (Any) -> None
with patch.object(Neo4jExtractor, '_get_driver'):
extractor = Neo4jSearchDataExtractor()
conf = ConfigFactory.from_dict({
'extractor.search_data.extractor.neo4j.{}'.format(Neo4jExtractor.GRAPH_URL_CONFIG_KEY):
'test-endpoint',
'extractor.search_data.extractor.neo4j.{}'.format(Neo4jExtractor.NEO4J_AUTH_USER):
'test-user',
'extractor.search_data.extractor.neo4j.{}'.format(Neo4jExtractor.NEO4J_AUTH_PW):
'test-passwd',
'extractor.search_data.{}'.format(Neo4jSearchDataExtractor.ENTITY_TYPE):
'dashboard',
'extractor.search_data.{}'.format(JOB_PUBLISH_TAG):
'test-date',
})
extractor.init(Scoped.get_scoped_conf(conf=conf,
scope=extractor.get_scope()))
self.assertEqual(extractor.cypher_query,
Neo4jSearchDataExtractor.DEFAULT_NEO4J_DASHBOARD_CYPHER_QUERY.format
(publish_tag_filter="""WHERE dashboard.published_tag = 'test-date'"""))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment