-
Notifications
You must be signed in to change notification settings - Fork 0
/
DgraphInterface.py
265 lines (242 loc) · 8.86 KB
/
DgraphInterface.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
import pydgraph
import datetime
import json
from DgraphRecommendation import Person, config
class DgraphInterface:
def __init__(self):
# pygraph works on grpc which can read options for channels it covers
opts = [('grpc.max_receive_message_length', 16*1024*1024)]
# grpc external port
grpc_e = config.dgraph_settings["host"]+":9080"
http_e = config.dgraph_settings["host"]+":8080"
self.grpc_external = grpc_e
# http external port
self.http_external = http_e
self.client_stub = pydgraph.DgraphClientStub(grpc_e, options=opts)
self.client = pydgraph.DgraphClient(self.client_stub)
''' Set dgraph schema, do execute once or after test execution '''
def set_schema(self):
schema = """
id: string @index(exact) .
name: string @index(exact) .
follows: [uid] .
tracks: [uid] @reverse .
type Person {
id
follows
tracks
}
type Feature {
name
}
"""
return self.client.alter(pydgraph.Operation(schema=schema))
''' Drop whole DB, use only for test purposes '''
def drop_all(self):
return self.client.alter(pydgraph.Operation(drop_all=True))
''' Simply add feature to dgraph @:returns uid '''
def addFeature(self, feature: str) -> str:
res = ''
txn = self.client.txn()
try:
# create data
p = {
'uid': '_:newfeature',
'dgraph.type': 'Feature',
'name': feature
}
# Run mutation.
response = txn.mutate(set_obj=p)
# Commit transaction.
txn.commit()
res = response.uids['newfeature']
finally:
txn.discard()
return res
''' Simply add person to dgraph @:returns uid '''
def addPerson(self, person: Person) -> str:
res = ''
txn = self.client.txn()
try:
# create data
p = {
'uid': '_:newperson',
'dgraph.type': 'Person',
'id': f'{person.id}',
}
# Run mutation.
response = txn.mutate(set_obj=p)
# Commit transaction.
txn.commit()
res = response.uids['newperson']
finally:
txn.discard()
return res
def deleteNode(self, uid: str):
res = ''
txn = self.client.txn()
try:
p = {
'uid': f'{uid}'
}
response = txn.mutate(del_obj=p)
txn.commit()
res = response
finally:
txn.discard()
return res
''' Query to find feature by name @:returns uid of target or empty string '''
def find_feature_by_name(self, name: str) -> str:
query = """query all($a: string) {
find_feature(func: eq(name, $a)) {
uid
}
}"""
variables = {'$a': name}
res = self.client.txn(read_only=True).query(query, variables=variables)
ppl = json.loads(res.json)
if len(ppl['find_feature']) == 0:
return None
return ppl['find_feature'][0]['uid']
def getAllPersons(self) -> str:
query = """
query {
persons(func: has(id)) {
id
uid
tracks {
name
uid
}
follows {
id
uid
}
}
}
"""
res = self.client.txn(read_only=True).query(query)
ppl = json.loads(res.json)
return ppl
def getAllFeatures(self) -> str:
query = """
query {
features(func: has(name)) {
name
uid
}
}
"""
res = self.client.txn(read_only=True).query(query)
ppl = json.loads(res.json)
return ppl
''' Query to find person by id @:returns uid of target '''
def find_by_id(self, id: str) -> str:
query = """query all($a: string) {
find_person(func: eq(id, $a)) {
uid
}
}"""
variables = { '$a': id }
res = self.client.txn(read_only=True).query(query, variables=variables)
ppl = json.loads(res.json)
if len(ppl['find_person']) == 0:
return None
return ppl['find_person'][0]['uid']
''' Add follower to the person @:params person's and follower's uids '''
def addFollowerTo(self, person: str, follower: str):
txn = self.client.txn()
try:
setstatement = '<%s> <follows> <%s> .' % (follower, person)
mutation = txn.create_mutation(set_nquads=setstatement)
request = txn.create_request(mutations=[mutation], commit_now=True)
txn.do_request(request)
finally:
txn.discard()
''' Add feature to the user @:params person's and feature's uids '''
def addTracksTo(self, person: str, feature: str):
txn = self.client.txn()
try:
setstatement = '<%s> <tracks> <%s> .' % (person, feature)
mutation = txn.create_mutation(set_nquads=setstatement)
request = txn.create_request(mutations=[mutation], commit_now=True)
txn.do_request(request)
finally:
txn.discard()
'''
Add predictable edges to graph
'''
def addPredictableEdges(self, srcfile: str):
txn = self.client.txn()
try:
muts = [] # mutations
with open(srcfile, 'r') as f:
for line in f:
content = line.strip()
mutation = txn.create_mutation(set_nquads=content)
muts.append(mutation)
request = txn.create_request(mutations=muts, commit_now=True)
txn.do_request(request)
finally:
txn.discard()
'''
Remove all "predictable" edges from graph
'''
def remove_predictable(self, srcfile: str):
txn = self.client.txn()
try:
muts = [] # mutations
with open(srcfile, 'r') as f:
for line in f:
content = line.strip()
mutation = txn.create_mutation(del_nquads=content)
muts.append(mutation)
request = txn.create_request(mutations=muts, commit_now=True)
txn.do_request(request)
finally:
txn.discard()
''' Get numbers of nodes and edges in the graph: #persons, #features, #interpersons_connections, #feature_connections '''
def getNumbers(self) -> (int, int, int, int):
txn = self.client.txn()
query = """ query
{
amount_of_persons(func: type(Person)) {
number: count(uid)
}
amount_of_features(func: type(Feature)) {
number: count(uid)
}
var(func: has(follows)) {
u as count(follows)
}
amount_of_persons_connections() {
number: sum(val(u))
}
var(func: has(tracks)) {
v as count(tracks)
}
amount_of_feature_connections() {
number: sum(val(v))
}
}
"""
res = self.client.txn(read_only=True).query(query)
ppl = json.loads(res.json)
return int(ppl["amount_of_persons"][0]["number"]), int(ppl["amount_of_features"][0]["number"]), int(ppl["amount_of_persons_connections"][0]["number"]), int(ppl["amount_of_feature_connections"][0]["number"])
'''
Find k shortest pathes in dgraph between @arg str and @arg dst
'''
def get_k_shortest_pathes(self, k: int, src: str, dst: str):
query = """query all($src: string, $dst: string, $k: int) {
source as var(func: uid($src))
destination as var(func: uid($dst))
shortest(from: uid(source), to: uid(destination), numpaths: $k) {
follows
}
}
"""
variables = {'$src': src, '$dst': dst, '$k': str(k)}
res = self.client.txn(read_only=True).query(query, variables=variables)
ppl = json.loads(res.json)
latency = res.latency
return ppl, latency