forked from amundsen-io/amundsen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneo4j_proxy.py
2229 lines (1909 loc) · 100 KB
/
neo4j_proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import logging
import re
import textwrap
import time
from random import randint
from typing import (Any, Dict, Iterable, List, Optional, Tuple, # noqa: F401
Union, no_type_check)
import neo4j
from amundsen_common.entity.resource_type import ResourceType, to_resource_type
from amundsen_common.models.api import health_check
from amundsen_common.models.dashboard import DashboardSummary
from amundsen_common.models.feature import Feature, FeatureWatermark
from amundsen_common.models.generation_code import GenerationCode
from amundsen_common.models.lineage import Lineage, LineageItem
from amundsen_common.models.popular_table import PopularTable
from amundsen_common.models.table import (Application, Badge, Column,
ProgrammaticDescription, Reader,
ResourceReport, Source, SqlJoin,
SqlWhere, Stat, Table, TableSummary,
Tag, TypeMetadata, User, Watermark)
from amundsen_common.models.user import User as UserEntity
from amundsen_common.models.user import UserSchema
from beaker.cache import CacheManager
from beaker.util import parse_cache_config_options
from flask import current_app, has_app_context
from neo4j import GraphDatabase, Record, Transaction # noqa: F401
from neo4j.api import (SECURITY_TYPE_SECURE,
SECURITY_TYPE_SELF_SIGNED_CERTIFICATE, parse_neo4j_uri)
from neo4j.exceptions import ClientError
from metadata_service import config
from metadata_service.entity.dashboard_detail import \
DashboardDetail as DashboardDetailEntity
from metadata_service.entity.dashboard_query import \
DashboardQuery as DashboardQueryEntity
from metadata_service.entity.description import Description
from metadata_service.entity.tag_detail import TagDetail
from metadata_service.exception import NotFoundException
from metadata_service.proxy.base_proxy import BaseProxy
from metadata_service.proxy.statsd_utilities import timer_with_counter
from metadata_service.util import UserResourceRel
_CACHE = CacheManager(**parse_cache_config_options({'cache.type': 'memory'}))
# Expire cache every 11 hours + jitter
_GET_POPULAR_RESOURCES_CACHE_EXPIRY_SEC = 11 * 60 * 60 + randint(0, 3600)
CREATED_EPOCH_MS = 'publisher_created_epoch_ms'
LAST_UPDATED_EPOCH_MS = 'publisher_last_updated_epoch_ms'
PUBLISHED_TAG_PROPERTY_NAME = 'published_tag'
LOGGER = logging.getLogger(__name__)
def execute_statement(tx: Transaction, stmt: str, params: dict = None) -> List[Record]:
"""
Executes statement against Neo4j. If execution fails, it rollsback and raises exception.
"""
LOGGER.debug('Executing statement: %s with params %s', stmt, params)
result = tx.run(stmt, parameters=params)
return [record for record in result]
def get_single_record(records_list: List[Record]) -> Record:
"""
Helper method to get single item from _execute_cypher_query return when only one item is expected.
Emulates neo4j's Result.single() behavior.
"""
records_list_length = len(records_list)
if records_list_length > 1:
LOGGER.warning(f'There are {records_list_length} records in this result but only 1 was expected')
try:
return records_list[0]
except IndexError as e:
return None
class Neo4jProxy(BaseProxy):
"""
A proxy to Neo4j (Gateway to Neo4j)
"""
def __init__(self, *,
host: str,
port: int = 7687,
user: str = 'neo4j',
password: str = '',
num_conns: int = 50,
max_connection_lifetime_sec: int = 100,
encrypted: bool = False,
validate_ssl: bool = False,
database_name: str = neo4j.DEFAULT_DATABASE,
**kwargs: dict) -> None:
"""
There's currently no request timeout from client side where server
side can be enforced via "dbms.transaction.timeout"
By default, it will set max number of connections to 50 and connection time out to 10 seconds.
:param endpoint: neo4j endpoint
:param num_conns: number of connections
:param max_connection_lifetime_sec: max lifetime the connection can have when it comes to reuse. In other
words, connection lifetime longer than this value won't be reused and closed on garbage collection. This
value needs to be smaller than surrounding network environment's timeout.
:param database_name: the neo4j database to be queried if different from the default
"""
endpoint = f'{host}:{port}'
self._database_name = database_name
driver_args = {
'uri': endpoint,
'max_connection_lifetime': max_connection_lifetime_sec,
'auth': (user, password),
'connection_timeout': 10,
'max_connection_pool_size': num_conns,
}
# if URI scheme not secure set `trust`` and `encrypted` arguments for the driver
# https://neo4j.com/docs/api/python-driver/current/api.html#uri
_, security_type, _ = parse_neo4j_uri(uri=endpoint)
if security_type not in [SECURITY_TYPE_SELF_SIGNED_CERTIFICATE, SECURITY_TYPE_SECURE]:
trust = neo4j.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES if validate_ssl else neo4j.TRUST_ALL_CERTIFICATES
default_security_conf = {'trust': trust, 'encrypted': encrypted}
driver_args.update(default_security_conf)
self._driver = GraphDatabase.driver(**driver_args)
def health(self) -> health_check.HealthCheck:
"""
Runs one or more series of checks on the service. Can also
optionally return additional metadata about each check (e.g.
latency to database, cpu utilization, etc.).
"""
checks = {}
try:
# dbms.cluster.overview() is only available for enterprise neo4j users
cluster_overview = self._execute_cypher_query(statement='CALL dbms.cluster.overview()', param_dict={})
checks = dict(cluster_overview[0])
checks['overview_enabled'] = True
status = health_check.OK
except ClientError:
checks = {'overview_enabled': False}
status = health_check.OK # Can connect to database but plugin is not available
except Exception:
status = health_check.FAIL
final_checks = {f'{type(self).__name__}:connection': checks}
return health_check.HealthCheck(status=status, checks=final_checks)
@timer_with_counter
def get_table(self, *, table_uri: str) -> Table:
"""
:param table_uri: Table URI
:return: A Table object
"""
cols, last_neo4j_record = self._exec_col_query(table_uri)
readers = self._exec_usage_query(table_uri)
wmk_results, table_writer, table_apps, timestamp_value, owners, tags, source, \
badges, prog_descs, resource_reports = self._exec_table_query(table_uri)
joins, filters = self._exec_table_query_query(table_uri)
table = Table(database=last_neo4j_record['db']['name'],
cluster=last_neo4j_record['clstr']['name'],
schema=last_neo4j_record['schema']['name'],
name=last_neo4j_record['tbl']['name'],
tags=tags,
badges=badges,
description=self._safe_get(last_neo4j_record, 'tbl_dscrpt', 'description'),
columns=cols,
owners=owners,
table_readers=readers,
watermarks=wmk_results,
table_writer=table_writer,
table_apps=table_apps,
last_updated_timestamp=timestamp_value,
source=source,
is_view=self._safe_get(last_neo4j_record, 'tbl', 'is_view'),
programmatic_descriptions=prog_descs,
common_joins=joins,
common_filters=filters,
resource_reports=resource_reports
)
return table
@timer_with_counter
def _exec_col_query(self, table_uri: str) -> Tuple:
# Return Value: (Columns, Last Processed Record)
column_level_query = textwrap.dedent("""
MATCH (db:Database)-[:CLUSTER]->(clstr:Cluster)-[:SCHEMA]->(schema:Schema)
-[:TABLE]->(tbl:Table {key: $tbl_key})-[:COLUMN]->(col:Column)
OPTIONAL MATCH (tbl)-[:DESCRIPTION]->(tbl_dscrpt:Description)
OPTIONAL MATCH (col:Column)-[:DESCRIPTION]->(col_dscrpt:Description)
OPTIONAL MATCH (col:Column)-[:STAT]->(stat:Stat)
OPTIONAL MATCH (col:Column)-[:HAS_BADGE]->(badge:Badge)
OPTIONAL MATCH (col:Column)-[:TYPE_METADATA]->(Type_Metadata)-[:SUBTYPE *0..]->(tm:Type_Metadata)
OPTIONAL MATCH (tm:Type_Metadata)-[:DESCRIPTION]->(tm_dscrpt:Description)
OPTIONAL MATCH (tm:Type_Metadata)-[:HAS_BADGE]->(tm_badge:Badge)
WITH db, clstr, schema, tbl, tbl_dscrpt, col, col_dscrpt, collect(distinct stat) as col_stats,
collect(distinct badge) as col_badges,
{node: tm, description: tm_dscrpt, badges: collect(distinct tm_badge)} as tm_results
RETURN db, clstr, schema, tbl, tbl_dscrpt, col, col_dscrpt, col_stats, col_badges,
collect(distinct tm_results) as col_type_metadata
ORDER BY col.sort_order;""")
tbl_col_neo4j_records = self._execute_cypher_query(
statement=column_level_query, param_dict={'tbl_key': table_uri})
cols = []
last_neo4j_record = None
for tbl_col_neo4j_record in tbl_col_neo4j_records:
# Getting last record from this for loop as Neo4j's result's random access is O(n) operation.
col_stats = []
for stat in tbl_col_neo4j_record['col_stats']:
col_stat = Stat(
stat_type=stat['stat_type'],
stat_val=stat['stat_val'],
start_epoch=int(float(stat['start_epoch'])),
end_epoch=int(float(stat['end_epoch']))
)
col_stats.append(col_stat)
column_badges = self._make_badges(tbl_col_neo4j_record['col_badges'])
col_type_metadata = self._get_type_metadata(tbl_col_neo4j_record['col_type_metadata'])
last_neo4j_record = tbl_col_neo4j_record
col = Column(name=tbl_col_neo4j_record['col']['name'],
description=self._safe_get(tbl_col_neo4j_record, 'col_dscrpt', 'description'),
col_type=tbl_col_neo4j_record['col']['col_type'],
sort_order=int(tbl_col_neo4j_record['col']['sort_order']),
stats=col_stats,
badges=column_badges,
type_metadata=col_type_metadata)
cols.append(col)
if not cols:
raise NotFoundException('Table URI( {table_uri} ) does not exist'.format(table_uri=table_uri))
return sorted(cols, key=lambda item: item.sort_order), last_neo4j_record
def _get_type_metadata(self, type_metadata_results: List) -> Optional[TypeMetadata]:
"""
Generates a TypeMetadata object for a column. All columns will have at least
one associated type metadata node if the ComplexTypeTransformer is configured
to transform table metadata. Otherwise, there will be no type metadata found
and this will return quickly.
:param type_metadata_results: A list of type metadata values for a column
:return: a TypeMetadata object
"""
# If there are no Type_Metadata nodes, type_metadata_results will have
# one object with an empty node value
if len(type_metadata_results) > 0 and type_metadata_results[0]['node'] is not None:
sorted_type_metadata = sorted(type_metadata_results, key=lambda x: x['node']['key'])
else:
return None
type_metadata_nodes: Dict[str, TypeMetadata] = {}
type_metadata_children: Dict[str, Dict] = {}
for tm in sorted_type_metadata:
tm_node = tm['node']
description = self._safe_get(tm, 'description', 'description')
sort_order = self._safe_get(tm_node, 'sort_order') or 0
badges = self._safe_get(tm, 'badges')
# kind refers to the general type of the TypeMetadata, such as "array" or "map",
# while data_type refers to the entire type such as "array<int>" or "map<string, string>"
type_metadata = TypeMetadata(kind=tm_node['kind'], name=tm_node['name'], key=tm_node['key'],
description=description, data_type=tm_node['data_type'],
sort_order=sort_order, badges=self._make_badges(badges) if badges else [])
# type_metadata_nodes maps each type metadata path to its corresponding TypeMetadata object
tm_key_regex = re.compile(
r'(?P<db>\w+):\/\/(?P<cluster>\w+)\.(?P<schema>\w+)\/(?P<tbl>\w+)\/(?P<col>\w+)\/type\/(?P<tm_path>.*)'
)
tm_key_match = tm_key_regex.search(type_metadata.key)
if tm_key_match is None:
LOGGER.error(f'Could not retrieve the type metadata path from key {type_metadata.key}')
continue
tm_path = tm_key_match.group('tm_path')
type_metadata_nodes[tm_path] = type_metadata
# type_metadata_children is a nested dict where each type metadata node name
# maps to a dict of its children's names
split_key_list = tm_path.split('/')
tm_name = split_key_list.pop()
node_children = self._safe_get(type_metadata_children, *split_key_list)
if node_children is not None:
node_children[tm_name] = {}
else:
LOGGER.error(f'Could not construct the dict of children for type metadata key {type_metadata.key}')
# Iterate over the temporary children dict to create the proper TypeMetadata structure
result = self._build_type_metadata_structure('', type_metadata_children, type_metadata_nodes)
return result[0] if len(result) > 0 else None
def _build_type_metadata_structure(self, prev_path: str, tm_children: Dict, tm_nodes: Dict) -> List[TypeMetadata]:
type_metadata = []
for node_name, children in tm_children.items():
curr_path = f'{prev_path}/{node_name}' if prev_path else node_name
tm = tm_nodes.get(curr_path)
if tm is None:
LOGGER.error(f'Could not find expected type metadata object at type metadata path {curr_path}')
continue
if len(children) > 0:
tm.children = self._build_type_metadata_structure(curr_path, children, tm_nodes)
type_metadata.append(tm)
if len(type_metadata) > 1:
type_metadata.sort(key=lambda x: x.sort_order)
return type_metadata
@timer_with_counter
def _exec_usage_query(self, table_uri: str) -> List[Reader]:
# Return Value: List[Reader]
usage_query = textwrap.dedent("""\
MATCH (user:User)-[read:READ]->(table:Table {key: $tbl_key})
RETURN user.email as email, read.read_count as read_count, table.name as table_name
ORDER BY read.read_count DESC LIMIT 5;
""")
usage_neo4j_records = self._execute_cypher_query(statement=usage_query,
param_dict={'tbl_key': table_uri})
readers = [] # type: List[Reader]
for usage_neo4j_record in usage_neo4j_records:
reader_data = self._get_user_details(user_id=usage_neo4j_record['email'])
reader = Reader(user=self._build_user_from_record(record=reader_data),
read_count=usage_neo4j_record['read_count'])
readers.append(reader)
return readers
@timer_with_counter
def _exec_table_query(self, table_uri: str) -> Tuple:
"""
Queries one Cypher record with watermark list, Application,
,timestamp, owner records and tag records.
"""
# Return Value: (Watermark Results, Table Writer, Last Updated Timestamp, owner records, tag records)
table_level_query = textwrap.dedent("""\
MATCH (tbl:Table {key: $tbl_key})
OPTIONAL MATCH (wmk:Watermark)-[:BELONG_TO_TABLE]->(tbl)
OPTIONAL MATCH (app_producer:Application)-[:GENERATES]->(tbl)
OPTIONAL MATCH (app_consumer:Application)-[:CONSUMES]->(tbl)
OPTIONAL MATCH (tbl)-[:LAST_UPDATED_AT]->(t:Timestamp)
OPTIONAL MATCH (owner:User)<-[:OWNER]-(tbl)
OPTIONAL MATCH (tbl)-[:TAGGED_BY]->(tag:Tag{tag_type: $tag_normal_type})
OPTIONAL MATCH (tbl)-[:HAS_BADGE]->(badge:Badge)
OPTIONAL MATCH (tbl)-[:SOURCE]->(src:Source)
OPTIONAL MATCH (tbl)-[:DESCRIPTION]->(prog_descriptions:Programmatic_Description)
OPTIONAL MATCH (tbl)-[:HAS_REPORT]->(resource_reports:Report)
RETURN collect(distinct wmk) as wmk_records,
collect(distinct app_producer) as producing_apps,
collect(distinct app_consumer) as consuming_apps,
t.last_updated_timestamp as last_updated_timestamp,
collect(distinct owner) as owner_records,
collect(distinct tag) as tag_records,
collect(distinct badge) as badge_records,
src,
collect(distinct prog_descriptions) as prog_descriptions,
collect(distinct resource_reports) as resource_reports
""")
table_records = self._execute_cypher_query(statement=table_level_query,
param_dict={'tbl_key': table_uri,
'tag_normal_type': 'default'})
table_records = get_single_record(table_records)
wmk_results = []
wmk_records = table_records['wmk_records']
for record in wmk_records:
if record['key'] is not None:
watermark_type = record['key'].split('/')[-2]
wmk_result = Watermark(watermark_type=watermark_type,
partition_key=record['partition_key'],
partition_value=record['partition_value'],
create_time=record['create_time'])
wmk_results.append(wmk_result)
tags = []
if table_records.get('tag_records'):
tag_records = table_records['tag_records']
for record in tag_records:
tag_result = Tag(tag_name=record['key'],
tag_type=record['tag_type'])
tags.append(tag_result)
# this is for any badges added with BadgeAPI instead of TagAPI
badges = self._make_badges(table_records.get('badge_records'))
table_writer, table_apps = self._create_apps(table_records['producing_apps'], table_records['consuming_apps'])
timestamp_value = table_records['last_updated_timestamp']
owner_record = []
for owner in table_records.get('owner_records', []):
owner_data = self._get_user_details(user_id=owner['email'])
owner_record.append(self._build_user_from_record(record=owner_data))
src = None
if table_records['src']:
src = Source(source_type=table_records['src']['source_type'],
source=table_records['src']['source'])
prog_descriptions = self._extract_programmatic_descriptions_from_query(
table_records.get('prog_descriptions', [])
)
resource_reports = self._extract_resource_reports_from_query(table_records.get('resource_reports', []))
return wmk_results, table_writer, table_apps, timestamp_value, owner_record,\
tags, src, badges, prog_descriptions, resource_reports
@timer_with_counter
def _exec_table_query_query(self, table_uri: str) -> Tuple:
"""
Queries one Cypher record with results that contain information about queries
and entities (e.g. joins, where clauses, etc.) associated to queries that are executed
on the table.
"""
# Return Value: (Watermark Results, Table Writer, Last Updated Timestamp, owner records, tag records)
table_query_level_query = textwrap.dedent("""
MATCH (tbl:Table {key: $tbl_key})
OPTIONAL MATCH (tbl)-[:COLUMN]->(col:Column)-[COLUMN_JOINS_WITH]->(j:Join)
OPTIONAL MATCH (j)-[JOIN_OF_COLUMN]->(col2:Column)
OPTIONAL MATCH (j)-[JOIN_OF_QUERY]->(jq:Query)-[:HAS_EXECUTION]->(exec:Execution)
WITH tbl, j, col, col2,
sum(coalesce(exec.execution_count, 0)) as join_exec_cnt
ORDER BY join_exec_cnt desc
LIMIT 5
WITH tbl,
COLLECT(DISTINCT {
join: {
joined_on_table: {
database: case when j.left_table_key = $tbl_key
then j.right_database
else j.left_database
end,
cluster: case when j.left_table_key = $tbl_key
then j.right_cluster
else j.left_cluster
end,
schema: case when j.left_table_key = $tbl_key
then j.right_schema
else j.left_schema
end,
name: case when j.left_table_key = $tbl_key
then j.right_table
else j.left_table
end
},
joined_on_column: col2.name,
column: col.name,
join_type: j.join_type,
join_sql: j.join_sql
},
join_exec_cnt: join_exec_cnt
}) as joins
WITH tbl, joins
OPTIONAL MATCH (tbl)-[:COLUMN]->(col:Column)-[USES_WHERE_CLAUSE]->(whr:Where)
OPTIONAL MATCH (whr)-[WHERE_CLAUSE_OF]->(wq:Query)-[:HAS_EXECUTION]->(whrexec:Execution)
WITH tbl, joins,
whr, sum(coalesce(whrexec.execution_count, 0)) as where_exec_cnt
ORDER BY where_exec_cnt desc
LIMIT 5
RETURN tbl, joins,
COLLECT(DISTINCT {
where_clause: whr.where_clause,
where_exec_cnt: where_exec_cnt
}) as filters
""")
query_records = self._execute_cypher_query(statement=table_query_level_query, param_dict={'tbl_key': table_uri})
table_query_records = get_single_record(query_records)
joins = self._extract_joins_from_query(table_query_records.get('joins', [{}]))
filters = self._extract_filters_from_query(table_query_records.get('filters', [{}]))
return joins, filters
def _extract_programmatic_descriptions_from_query(self, raw_prog_descriptions: dict) -> list:
prog_descriptions = []
for prog_description in raw_prog_descriptions:
source = prog_description['description_source']
if source is None:
LOGGER.error("A programmatic description with no source was found... skipping.")
else:
prog_descriptions.append(ProgrammaticDescription(source=source, text=prog_description['description']))
prog_descriptions.sort(key=lambda x: x.source)
return prog_descriptions
def _extract_resource_reports_from_query(self, raw_resource_reports: dict) -> list:
resource_reports = []
for resource_report in raw_resource_reports:
name = resource_report.get('name')
if name is None:
LOGGER.error("A report with no name found... skipping.")
else:
resource_reports.append(ResourceReport(name=name, url=resource_report['url']))
parsed_reports = current_app.config['RESOURCE_REPORT_CLIENT'](resource_reports) \
if current_app.config['RESOURCE_REPORT_CLIENT'] else resource_reports
parsed_reports.sort(key=lambda x: x.name)
return parsed_reports
def _extract_joins_from_query(self, joins: List[Dict]) -> List[Dict]:
valid_joins = []
for join in joins:
join_data = join['join']
if all(join_data.values()):
new_sql_join = SqlJoin(join_sql=join_data['join_sql'],
join_type=join_data['join_type'],
joined_on_column=join_data['joined_on_column'],
joined_on_table=TableSummary(**join_data['joined_on_table']),
column=join_data['column'])
valid_joins.append(new_sql_join)
return valid_joins
def _extract_filters_from_query(self, filters: List[Dict]) -> List[Dict]:
return_filters = []
for filt in filters:
filter_where = filt.get('where_clause')
if filter_where:
return_filters.append(SqlWhere(where_clause=filter_where))
return return_filters
@no_type_check
def _safe_get(self, dct, *keys):
"""
Helper method for getting value from nested dict. This also works either key does not exist or value is None.
:param dct:
:param keys:
:return:
"""
for key in keys:
dct = dct.get(key)
if dct is None:
return None
return dct
@timer_with_counter
def _execute_cypher_query(self, *,
statement: str,
param_dict: Dict[str, Any]) -> List[Record]:
"""
Execute Cypher queries using managed read transactions
"""
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug('Executing Cypher query: {statement} with params {params}: '.format(statement=statement,
params=param_dict))
start = time.time()
try:
with self._driver.session(database=self._database_name) as session:
return session.read_transaction(execute_statement, statement, param_dict)
finally:
# TODO: Add support on statsd
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug('Cypher query execution elapsed for {} seconds'.format(time.time() - start))
# noinspection PyMethodMayBeStatic
def _make_badges(self, badges: Iterable) -> List[Badge]:
"""
Generates a list of Badges objects
:param badges: A list of badges of a table, column, or type_metadata
:return: a list of Badge objects
"""
_badges = []
for badge in badges:
_badges.append(Badge(badge_name=badge["key"], category=badge["category"]))
return _badges
@timer_with_counter
def get_resource_description(self, *,
resource_type: ResourceType,
uri: str) -> Description:
"""
Get the resource description based on the uri. Any exception will propagate back to api server.
:param resource_type:
:param id:
:return:
"""
description_query = textwrap.dedent("""
MATCH (n:{node_label} {{key: $key}})-[:DESCRIPTION]->(d:Description)
RETURN d.description AS description;
""".format(node_label=resource_type.name))
result = self._execute_cypher_query(statement=description_query,
param_dict={'key': uri})
result = get_single_record(result)
return Description(description=result['description'] if result else None)
@timer_with_counter
def get_table_description(self, *,
table_uri: str) -> Union[str, None]:
"""
Get the table description based on table uri. Any exception will propagate back to api server.
:param table_uri:
:return:
"""
return self.get_resource_description(resource_type=ResourceType.Table, uri=table_uri).description
@timer_with_counter
def get_type_metadata_description(self, *,
type_metadata_key: str) -> Union[str, None]:
"""
Get the type_metadata description based on its key. Any exception will propagate back to api server.
:param type_metadata_key:
:return:
"""
return self.get_resource_description(resource_type=ResourceType.Type_Metadata,
uri=type_metadata_key).description
@timer_with_counter
def put_resource_description(self, *,
resource_type: ResourceType,
uri: str,
description: str) -> None:
"""
Update resource description with one from user
:param uri: Resource uri (key in Neo4j)
:param description: new value for resource description
"""
# start neo4j transaction
desc_key = uri + '/_description'
upsert_desc_query = textwrap.dedent("""
MERGE (u:Description {key: $desc_key})
on CREATE SET u={description: $description, key: $desc_key}
on MATCH SET u={description: $description, key: $desc_key}
""")
upsert_desc_tab_relation_query = textwrap.dedent("""
MATCH (n1:Description {{key: $desc_key}}), (n2:{node_label} {{key: $key}})
MERGE (n2)-[r2:DESCRIPTION]->(n1)
RETURN n1.key, n2.key
""".format(node_label=resource_type.name))
start = time.time()
try:
tx = self._driver.session(database=self._database_name).begin_transaction()
tx.run(upsert_desc_query, {'description': description,
'desc_key': desc_key})
result = tx.run(upsert_desc_tab_relation_query, {'desc_key': desc_key,
'key': uri})
if not result.single():
raise NotFoundException(f'Failed to update the description as resource {uri} does not exist')
# end neo4j transaction
tx.commit()
except Exception as e:
LOGGER.exception('Failed to execute update process')
if not tx.closed():
tx.rollback()
# propagate exception back to api
raise e
finally:
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug('Update process elapsed for {} seconds'.format(time.time() - start))
@timer_with_counter
def put_table_description(self, *,
table_uri: str,
description: str) -> None:
"""
Update table description with one from user
:param table_uri: Table uri (key in Neo4j)
:param description: new value for table description
"""
self.put_resource_description(resource_type=ResourceType.Table,
uri=table_uri,
description=description)
@timer_with_counter
def put_type_metadata_description(self, *,
type_metadata_key: str,
description: str) -> None:
"""
Update type_metadata description with one from user
:param type_metadata_key:
:param description:
"""
self.put_resource_description(resource_type=ResourceType.Type_Metadata,
uri=type_metadata_key,
description=description)
@timer_with_counter
def get_column_description(self, *,
table_uri: str,
column_name: str) -> Union[str, None]:
"""
Get the column description based on table uri. Any exception will propagate back to api server.
:param table_uri:
:param column_name:
:return:
"""
column_description_query = textwrap.dedent("""
MATCH (tbl:Table {key: $tbl_key})-[:COLUMN]->(c:Column {name: $column_name})-[:DESCRIPTION]->(d:Description)
RETURN d.description AS description;
""")
result = self._execute_cypher_query(statement=column_description_query,
param_dict={'tbl_key': table_uri, 'column_name': column_name})
column_descrpt = get_single_record(result)
column_description = column_descrpt['description'] if column_descrpt else None
return column_description
@timer_with_counter
def put_column_description(self, *,
table_uri: str,
column_name: str,
description: str) -> None:
"""
Update column description with input from user
:param table_uri:
:param column_name:
:param description:
:return:
"""
column_uri = table_uri + '/' + column_name # type: str
desc_key = column_uri + '/_description'
upsert_desc_query = textwrap.dedent("""
MERGE (u:Description {key: $desc_key})
on CREATE SET u={description: $description, key: $desc_key}
on MATCH SET u={description: $description, key: $desc_key}
""")
upsert_desc_col_relation_query = textwrap.dedent("""
MATCH (n1:Description {key: $desc_key}), (n2:Column {key: $column_key})
MERGE (n2)-[r2:DESCRIPTION]->(n1)
RETURN n1.key, n2.key
""")
start = time.time()
try:
tx = self._driver.session(database=self._database_name).begin_transaction()
tx.run(upsert_desc_query, {'description': description,
'desc_key': desc_key})
result = tx.run(upsert_desc_col_relation_query, {'desc_key': desc_key,
'column_key': column_uri})
if not result.single():
raise NotFoundException(f'Failed to update the table {table_uri} column '
f'{column_uri} description as either table or column does not exist')
# end neo4j transaction
tx.commit()
except Exception as e:
LOGGER.exception('Failed to execute update process')
if not tx.closed():
tx.rollback()
# propagate error to api
raise e
finally:
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug('Update process elapsed for {} seconds'.format(time.time() - start))
@timer_with_counter
def add_owner(self, *,
table_uri: str,
owner: str) -> None:
"""
Update table owner informations.
1. Do a create if not exists query of the owner(user) node.
2. Do a upsert of the owner/owned_by relation.
:param table_uri:
:param owner:
:return:
"""
self.add_resource_owner(uri=table_uri,
resource_type=ResourceType.Table,
owner=owner)
@timer_with_counter
def add_resource_owner(self, *,
uri: str,
resource_type: ResourceType,
owner: str) -> None:
"""
Update table owner informations.
1. Do a create if not exists query of the owner(user) node.
2. Do a upsert of the owner/owned_by relation.
:param table_uri:
:param owner:
:return:
"""
create_owner_query = textwrap.dedent("""
MERGE (u:User {key: $user_email})
on CREATE SET u={email: $user_email, key: $user_email}
""")
upsert_owner_relation_query = textwrap.dedent("""
MATCH (n1:User {{key: $user_email}}), (n2:{resource_type} {{key: $res_key}})
MERGE (n1)-[r1:OWNER_OF]->(n2)-[r2:OWNER]->(n1)
RETURN n1.key, n2.key
""".format(resource_type=resource_type.name))
try:
tx = self._driver.session(database=self._database_name).begin_transaction()
# upsert the node
tx.run(create_owner_query, {'user_email': owner})
result = tx.run(upsert_owner_relation_query, {'user_email': owner,
'res_key': uri})
if not result.single():
raise RuntimeError('Failed to create relation between '
'owner {owner} and resource {uri}'.format(owner=owner,
uri=uri))
tx.commit()
except Exception as e:
if not tx.closed():
tx.rollback()
# propagate the exception back to api
raise e
@timer_with_counter
def delete_owner(self, *,
table_uri: str,
owner: str) -> None:
"""
Delete the owner / owned_by relationship.
:param table_uri:
:param owner:
:return:
"""
self.delete_resource_owner(uri=table_uri,
resource_type=ResourceType.Table,
owner=owner)
@timer_with_counter
def delete_resource_owner(self, *,
uri: str,
resource_type: ResourceType,
owner: str) -> None:
"""
Delete the owner / owned_by relationship.
:param table_uri:
:param owner:
:return:
"""
delete_query = textwrap.dedent("""
MATCH (n1:User{{key: $user_email}}), (n2:{resource_type} {{key: $res_key}})
OPTIONAL MATCH (n1)-[r1:OWNER_OF]->(n2)
OPTIONAL MATCH (n2)-[r2:OWNER]->(n1)
DELETE r1,r2
""".format(resource_type=resource_type.name))
try:
tx = self._driver.session(database=self._database_name).begin_transaction()
tx.run(delete_query, {'user_email': owner,
'res_key': uri})
except Exception as e:
# propagate the exception back to api
if not tx.closed():
tx.rollback()
raise e
finally:
tx.commit()
@timer_with_counter
def add_badge(self, *,
id: str,
badge_name: str,
category: str = '',
resource_type: ResourceType) -> None:
LOGGER.info('New badge {} for id {} with category {} '
'and resource type {}'.format(badge_name, id, category, resource_type.name))
validation_query = \
'MATCH (n:{resource_type} {{key: $key}}) return n'.format(resource_type=resource_type.name)
upsert_badge_query = textwrap.dedent("""
MERGE (u:Badge {key: $badge_name})
on CREATE SET u={key: $badge_name, category: $category}
on MATCH SET u={key: $badge_name, category: $category}
""")
upsert_badge_relation_query = textwrap.dedent("""
MATCH(n1:Badge {{key: $badge_name, category: $category}}),
(n2:{resource_type} {{key: $key}})
MERGE (n1)-[r1:BADGE_FOR]->(n2)-[r2:HAS_BADGE]->(n1)
RETURN n1.key, n2.key
""".format(resource_type=resource_type.name))
try:
tx = self._driver.session(database=self._database_name).begin_transaction()
tbl_result = tx.run(validation_query, {'key': id})
if not tbl_result.single():
raise NotFoundException('id {} does not exist'.format(id))
tx.run(upsert_badge_query, {'badge_name': badge_name,
'category': category})
result = tx.run(upsert_badge_relation_query, {'badge_name': badge_name,
'key': id,
'category': category})
if not result.single():
raise RuntimeError('failed to create relation between '
'badge {badge} and resource {resource} of resource type '
'{resource_type} MORE {q}'.format(badge=badge_name,
resource=id,
resource_type=resource_type,
q=upsert_badge_relation_query))
tx.commit()
except Exception as e:
LOGGER.error(e)
if not tx.closed():
tx.rollback()
raise e
@timer_with_counter
def delete_badge(self, id: str,
badge_name: str,
category: str,
resource_type: ResourceType) -> None:
# TODO for some reason when deleting it will say it was successful
# even when the badge never existed to begin with
LOGGER.info('Delete badge {} for id {} with category {}'.format(badge_name, id, category))
# only deletes relationshop between badge and resource
delete_query = textwrap.dedent("""
MATCH (b:Badge {{key:$badge_name, category:$category}})-
[r1:BADGE_FOR]->(n:{resource_type} {{key: $key}})-[r2:HAS_BADGE]->(b) DELETE r1,r2
""".format(resource_type=resource_type.name))
try:
tx = self._driver.session(database=self._database_name).begin_transaction()
tx.run(delete_query, {'badge_name': badge_name,
'key': id,
'category': category})
tx.commit()
except Exception as e:
# propagate the exception back to api
if not tx.closed():
tx.rollback()
raise e
@timer_with_counter
def get_badges(self) -> List:
LOGGER.info('Get all badges')
query = textwrap.dedent("""
MATCH (b:Badge) RETURN b as badge
""")
records = self._execute_cypher_query(statement=query,
param_dict={})
results = []