Skip to content

Commit f8f6ef1

Browse files
authored
Add %get_import_task line magic (#668)
* Add %get_import_task line magic * update changelog
1 parent 25a8b92 commit f8f6ef1

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

ChangeLog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
Starting with v1.31.6, this file will contain a record of major features and updates made in each release of graph-notebook.
44

55
## Upcoming
6+
67
- Upgraded Neo4j Bolt driver to v5.x ([Link to PR](https://github.com/aws/graph-notebook/pull/682))
8+
- Added `%get_import_task` line magic ([Link to PR](https://github.com/aws/graph-notebook/pull/668))
79

810
## Release 4.5.2 (August 15, 2024)
911

1012
- New Neptune Analytics notebooks - openCypher over RDF ([Link to PR](https://github.com/aws/graph-notebook/pull/672))
1113
- Path: 02-Neptune-Analytics > 04-OpenCypher-Over-RDF
1214
- Updated OC-RDF samples to use `%load` magic, and pull from regional S3 buckets ([Link to PR](https://github.com/aws/graph-notebook/pull/676))
1315
- Added regional S3 bucket mappings to Neptune CloudFormation template ([Link to PR](https://github.com/aws/graph-notebook/pull/664))
14-
- Enabled n-triples data for `%load` with Neptune Analytics ([PR #1](https://github.com/aws/graph-notebook/pull/671)) ( ([PR #2](https://github.com/aws/graph-notebook/pull/675)))
16+
- Enabled n-triples data for `%load` with Neptune Analytics ([PR #1](https://github.com/aws/graph-notebook/pull/671)) ([PR #2](https://github.com/aws/graph-notebook/pull/675))
1517
- Removed unused options from `%load`([Link to PR](https://github.com/aws/graph-notebook/pull/662))
1618
- Made EncryptionKey optional in Neptune CloudFormation template ([Link to PR](https://github.com/aws/graph-notebook/pull/663))
1719
- Fixed unintended type coercion in results table with missing/null values ([Link to PR](https://github.com/aws/graph-notebook/pull/679))

src/graph_notebook/magics/graph_magic.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,44 @@ def create_graph_snapshot(self, line='', local_ns: dict = None):
16031603
print(e)
16041604
store_to_ns(args.store_to, e, local_ns)
16051605

1606+
@line_magic
1607+
@needs_local_scope
1608+
@display_exceptions
1609+
@neptune_graph_only
1610+
def get_import_task(self, line='', local_ns: dict = None):
1611+
parser = argparse.ArgumentParser()
1612+
parser.add_argument('-ti', '--task-identifier', type=str, default='',
1613+
help="The unique identifier of an import task.")
1614+
parser.add_argument('--include-metadata', action='store_true', default=False,
1615+
help="Display the response metadata if it is available.")
1616+
parser.add_argument('--silent', action='store_true', default=False, help="Display no output.")
1617+
parser.add_argument('--store-to', type=str, default='', help='store query result to this variable')
1618+
args = parser.parse_args(line.split())
1619+
1620+
task_id_regex = "t-[a-z0-9]{10}"
1621+
if args.task_identifier == '':
1622+
print("Please provide an import task ID using the -ti or --task-identifier parameter.")
1623+
return
1624+
if not re.match(fr"^{task_id_regex}$", args.task_identifier):
1625+
print(f"Import task ID must satisfy the regular expression pattern: {task_id_regex}")
1626+
return
1627+
1628+
try:
1629+
res = self.client.get_import_task(task_id=args.task_identifier)
1630+
if not args.include_metadata:
1631+
res.pop('ResponseMetadata', None)
1632+
if not args.silent:
1633+
print(json.dumps(res, indent=2, default=str))
1634+
store_to_ns(args.store_to, res, local_ns)
1635+
except Exception as e:
1636+
if not args.silent:
1637+
if "ResourceNotFoundException" in str(e):
1638+
print(f"Unable to import task with ID: {args.task_identifier}")
1639+
else:
1640+
print("Encountered an error when attempting to retrieve the specified import task:\n")
1641+
print(e)
1642+
store_to_ns(args.store_to, e, local_ns)
1643+
16061644
@line_magic
16071645
@needs_local_scope
16081646
@display_exceptions

src/graph_notebook/neptune/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,16 @@ def create_graph_snapshot(self, graph_id: str = '', snapshot_name: str = '', tag
680680
logger.debug(f"CreateGraphSnapshot call failed with service exception: {e}")
681681
raise e
682682

683+
def get_import_task(self, task_id: str = '') -> dict:
684+
try:
685+
res = self.neptune_graph_client.get_import_task(
686+
taskIdentifier=task_id
687+
)
688+
return res
689+
except ClientError as e:
690+
logger.debug(f"GetGraph call failed with service exception: {e}")
691+
raise e
692+
683693
def dataprocessing_start(self, s3_input_uri: str, s3_output_uri: str, **kwargs) -> requests.Response:
684694
data = {
685695
'inputDataS3Location': s3_input_uri,

0 commit comments

Comments
 (0)