Skip to content

Commit f6acdd7

Browse files
authored
Add %reset_graph line magic (#610)
* Add ResetGraph magic for Neptune Analytics * Bump botocore and boto3 reqs * Update SkipSnapshot behavior * update changelog
1 parent 7f56fc8 commit f6acdd7

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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+
- Added `%reset_graph` line magic ([Link to PR](https://github.com/aws/graph-notebook/pull/610))
67

78
## Release 4.3.1 (June 3, 2024)
89

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ numpy<1.24.0
2525
nest_asyncio>=1.5.5,<=1.5.6
2626

2727
# requirements for testing
28-
botocore~=1.21.49
29-
boto3~=1.18.49
28+
botocore~=1.34.74
29+
boto3~=1.34.74
3030
pytest==6.2.5
3131
parameterized==0.8.1

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def get_version():
7878
'jupyter-contrib-nbextensions<=0.7.0',
7979
'widgetsnbextension<=3.6.1',
8080
'jupyter==1.0.0',
81-
'botocore>=1.21.49',
82-
'boto3>=1.18.49',
81+
'botocore>=1.34.74',
82+
'boto3>=1.34.74',
8383
'ipython>=7.16.1,<=8.10.0',
8484
'neo4j>=4.4.9,<5.0.0',
8585
'rdflib==7.0.0',

src/graph_notebook/magics/graph_magic.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,39 @@ def on_button_cancel_clicked(b):
16011601
logger.info(f'got the response {res}')
16021602
return res
16031603

1604+
@line_magic
1605+
@needs_local_scope
1606+
@display_exceptions
1607+
@neptune_graph_only
1608+
def graph_reset(self, line, local_ns: dict = None):
1609+
self.reset_graph(line, local_ns)
1610+
1611+
@line_magic
1612+
@needs_local_scope
1613+
@display_exceptions
1614+
@neptune_graph_only
1615+
def reset_graph(self, line, local_ns: dict = None):
1616+
parser = argparse.ArgumentParser()
1617+
parser.add_argument('-ns', '--no-skip-snapshot', action='store_true', default=False,
1618+
help='Creates a final graph snapshot before the graph data is deleted.')
1619+
parser.add_argument('--silent', action='store_true', default=False, help="Display no output.")
1620+
parser.add_argument('--store-to', type=str, default='', help='Store query result to this variable.')
1621+
args = parser.parse_args(line.split())
1622+
1623+
try:
1624+
graph_id = self.client.get_graph_id()
1625+
res = self.client.reset_graph(graph_id=graph_id, no_skip_snapshot=args.no_skip_snapshot)
1626+
if not args.silent:
1627+
print(f"ResetGraph call submitted successfully for graph ID [{graph_id}]. Please note that the graph "
1628+
f"may take several minutes to become available again.\n")
1629+
print(json.dumps(res, indent=2, default=str))
1630+
store_to_ns(args.store_to, res, local_ns)
1631+
except Exception as e:
1632+
if not args.silent:
1633+
print("Received an error when attempting graph reset:")
1634+
print(e)
1635+
store_to_ns(args.store_to, e, local_ns)
1636+
16041637
@magic_variables
16051638
@line_magic
16061639
@needs_local_scope

src/graph_notebook/neptune/client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
from urllib.parse import urlparse, urlunparse
1313
from SPARQLWrapper import SPARQLWrapper
1414
from boto3 import Session
15+
from boto3 import client as boto3_client
1516
from botocore.session import Session as botocoreSession
1617
from botocore.auth import SigV4Auth
1718
from botocore.awsrequest import AWSRequest
19+
from botocore.exceptions import ClientError
1820
from gremlin_python.driver import client, serializer
1921
from gremlin_python.driver.protocol import GremlinServerError
2022
from gremlin_python.driver.aiohttp.transport import AiohttpTransport
@@ -202,6 +204,8 @@ def __init__(self, host: str, port: int = DEFAULT_PORT,
202204

203205
self._http_session = None
204206

207+
self.neptune_graph_client = boto3_client(service_name='neptune-graph', region_name=self.region)
208+
205209
@property
206210
def host(self):
207211
if self.proxy_host != '':
@@ -242,6 +246,11 @@ def get_uri_with_port(self, use_websocket=False, use_proxy=False):
242246
uri = f'{protocol}://{uri_host}:{uri_port}'
243247
return uri
244248

249+
def get_graph_id(self):
250+
graph_host = self.host
251+
graph_id = graph_host.split('.')[0]
252+
return graph_id
253+
245254
def sparql_query(self, query: str, headers=None, explain: str = '', path: str = '') -> requests.Response:
246255
if headers is None:
247256
headers = {}
@@ -569,6 +578,17 @@ def perform_reset(self, token: str) -> requests.Response:
569578
res = self._http_session.send(req, verify=self.ssl_verify)
570579
return res
571580

581+
def reset_graph(self, graph_id: str = '', no_skip_snapshot: bool = False) -> dict:
582+
try:
583+
res = self.neptune_graph_client.reset_graph(
584+
graphIdentifier=graph_id,
585+
skipSnapshot=(not no_skip_snapshot)
586+
)
587+
return res
588+
except ClientError as e:
589+
logger.debug(f"Reset Graph call failed with service exception: {e}")
590+
raise e
591+
572592
def dataprocessing_start(self, s3_input_uri: str, s3_output_uri: str, **kwargs) -> requests.Response:
573593
data = {
574594
'inputDataS3Location': s3_input_uri,

0 commit comments

Comments
 (0)