Skip to content

Commit 1eef9db

Browse files
Merge pull request #384 from trustgraph-ai/feature/entity-contexts-import-export
Entity contexts import/export
2 parents 7d90696 + 0507f10 commit 1eef9db

File tree

4 files changed

+171
-5
lines changed

4 files changed

+171
-5
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
import asyncio
3+
import queue
4+
import uuid
5+
6+
from ... schema import EntityContexts
7+
from ... base import Subscriber
8+
9+
from . serialize import serialize_entity_contexts
10+
11+
class EntityContextsExport:
12+
13+
def __init__(
14+
self, ws, running, pulsar_client, queue, consumer, subscriber
15+
):
16+
17+
self.ws = ws
18+
self.running = running
19+
self.pulsar_client = pulsar_client
20+
self.queue = queue
21+
self.consumer = consumer
22+
self.subscriber = subscriber
23+
24+
async def destroy(self):
25+
self.running.stop()
26+
await self.ws.close()
27+
28+
async def receive(self, msg):
29+
# Ignore incoming info from websocket
30+
pass
31+
32+
async def run(self):
33+
34+
subs = Subscriber(
35+
client = self.pulsar_client, topic = self.queue,
36+
consumer_name = self.consumer, subscription = self.subscriber,
37+
schema = EntityContexts
38+
)
39+
40+
await subs.start()
41+
42+
id = str(uuid.uuid4())
43+
q = await subs.subscribe_all(id)
44+
45+
while self.running.get():
46+
try:
47+
48+
resp = await asyncio.wait_for(q.get(), timeout=0.5)
49+
await self.ws.send_json(serialize_entity_contexts(resp))
50+
51+
except TimeoutError:
52+
continue
53+
54+
except queue.Empty:
55+
continue
56+
57+
except Exception as e:
58+
print(f"Exception: {str(e)}", flush=True)
59+
break
60+
61+
await subs.unsubscribe_all(id)
62+
63+
await subs.stop()
64+
65+
await self.ws.close()
66+
self.running.stop()
67+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
import asyncio
3+
import uuid
4+
from aiohttp import WSMsgType
5+
6+
from ... schema import Metadata
7+
from ... schema import EntityContexts, EntityContext
8+
from ... base import Publisher
9+
10+
from . serialize import to_subgraph, to_value
11+
12+
class EntityContextsImport:
13+
14+
def __init__(
15+
self, ws, running, pulsar_client, queue
16+
):
17+
18+
self.ws = ws
19+
self.running = running
20+
21+
self.publisher = Publisher(
22+
pulsar_client, topic = queue, schema = EntityContexts
23+
)
24+
25+
async def start(self):
26+
await self.publisher.start()
27+
28+
async def destroy(self):
29+
self.running.stop()
30+
31+
if self.ws:
32+
await self.ws.close()
33+
34+
await self.publisher.stop()
35+
36+
async def receive(self, msg):
37+
38+
data = msg.json()
39+
40+
elt = EntityContexts(
41+
metadata=Metadata(
42+
id=data["metadata"]["id"],
43+
metadata=to_subgraph(data["metadata"]["metadata"]),
44+
user=data["metadata"]["user"],
45+
collection=data["metadata"]["collection"],
46+
),
47+
entities=[
48+
EntityContext(
49+
entity=to_value(ent["entity"]),
50+
context=ent["context"],
51+
)
52+
for ent in data["entities"]
53+
]
54+
)
55+
56+
await self.publisher.send(None, elt)
57+
58+
async def run(self):
59+
60+
while self.running.get():
61+
await asyncio.sleep(0.5)
62+
63+
if self.ws:
64+
await self.ws.close()
65+
66+
self.ws = None
67+

trustgraph-flow/trustgraph/gateway/dispatch/manager.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
from . triples_export import TriplesExport
2424
from . graph_embeddings_export import GraphEmbeddingsExport
2525
from . document_embeddings_export import DocumentEmbeddingsExport
26+
from . entity_contexts_export import EntityContextsExport
2627

2728
from . triples_import import TriplesImport
2829
from . graph_embeddings_import import GraphEmbeddingsImport
2930
from . document_embeddings_import import DocumentEmbeddingsImport
31+
from . entity_contexts_import import EntityContextsImport
3032

3133
from . mux import Mux
3234

@@ -57,12 +59,14 @@
5759
"triples": TriplesExport,
5860
"graph-embeddings": GraphEmbeddingsExport,
5961
"document-embeddings": DocumentEmbeddingsExport,
62+
"entity-contexts": EntityContextsExport,
6063
}
6164

6265
import_dispatchers = {
6366
"triples": TriplesImport,
6467
"graph-embeddings": GraphEmbeddingsImport,
6568
"document-embeddings": DocumentEmbeddingsImport,
69+
"entity-contexts": EntityContextsImport,
6670
}
6771

6872
class DispatcherWrapper:
@@ -146,11 +150,17 @@ async def process_flow_import(self, ws, running, params):
146150

147151
intf_defs = self.flows[flow]["interfaces"]
148152

149-
if kind not in intf_defs:
153+
# FIXME: The -store bit, does it make sense?
154+
if kind == "entity-contexts":
155+
int_kind = kind + "-load"
156+
else:
157+
int_kind = kind + "-store"
158+
159+
if int_kind not in intf_defs:
150160
raise RuntimeError("This kind not supported by flow")
151161

152162
# FIXME: The -store bit, does it make sense?
153-
qconfig = intf_defs[kind + "-store"]
163+
qconfig = intf_defs[int_kind]
154164

155165
id = str(uuid.uuid4())
156166
dispatcher = import_dispatchers[kind](
@@ -179,11 +189,16 @@ async def process_flow_export(self, ws, running, params):
179189

180190
intf_defs = self.flows[flow]["interfaces"]
181191

182-
if kind not in intf_defs:
192+
# FIXME: The -store bit, does it make sense?
193+
if kind == "entity-contexts":
194+
int_kind = kind + "-load"
195+
else:
196+
int_kind = kind + "-store"
197+
198+
if int_kind not in intf_defs:
183199
raise RuntimeError("This kind not supported by flow")
184200

185-
# FIXME: The -store bit, does it make sense?
186-
qconfig = intf_defs[kind + "-store"]
201+
qconfig = intf_defs[int_kind]
187202

188203
id = str(uuid.uuid4())
189204
dispatcher = export_dispatchers[kind](

trustgraph-flow/trustgraph/gateway/dispatch/serialize.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ def serialize_graph_embeddings(message):
6363
],
6464
}
6565

66+
def serialize_entity_contexts(message):
67+
return {
68+
"metadata": {
69+
"id": message.metadata.id,
70+
"metadata": serialize_subgraph(message.metadata.metadata),
71+
"user": message.metadata.user,
72+
"collection": message.metadata.collection,
73+
},
74+
"entities": [
75+
{
76+
"context": entity.context,
77+
"entity": serialize_value(entity.entity),
78+
}
79+
for entity in message.entities
80+
],
81+
}
82+
6683
def serialize_document_embeddings(message):
6784
return {
6885
"metadata": {

0 commit comments

Comments
 (0)