-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-function.py
37 lines (29 loc) · 1.46 KB
/
cloud-function.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
#This code defines a Google Cloud Function named prune_graph. Its primary goal is to maintain the efficiency and relevance of the object graph that your robot is building and using for navigation and decision-making.
from google.cloud import storage
from datetime import datetime, timedelta
import networkx as nx
import json
def prune_graph(data, context):
"""Cloud Function to prune stale nodes and edges from the object graph."""
# 1. Access graph data
storage_client = storage.Client()
bucket = storage_client.bucket('your-bucket-name') # Replace with your bucket name
blob = bucket.blob('graph_data.json') # Replace with your file name
graph_data = json.loads(blob.download_as_string())
# 2. Identify stale nodes and edges
threshold = timedelta(hours=24) # Adjust the threshold as needed
now = datetime.utcnow()
graph = nx.node_link_graph(graph_data)
stale_nodes = []
for node_id, node_data in graph.nodes(data=True):
if 'timestamp' in node_data and now - datetime.fromisoformat(node_data['timestamp']) > threshold:
stale_nodes.append(node_id)
# Similarly, identify stale edges (if needed)
# ...
# 3. Delete stale nodes and edges
graph.remove_nodes_from(stale_nodes)
# ... (Remove stale edges if identified)
# 4. Update the graph data
updated_graph_data = nx.node_link_data(graph)
blob.upload_from_string(json.dumps(updated_graph_data))
print("Graph pruned successfully!")