@@ -7,11 +7,21 @@ export createSession, createRobot, createUser, createDfgSessionIfNotExist
7
7
export existsSession, existsRobot, existsUser
8
8
export getSession, getRobot, getUser
9
9
export updateSession, updateRobot, updateUser
10
- export listSessions, listRobots, listUsers
10
+ export lsSessions, lsRobots, lsUsers
11
+
12
+ global _invalidIds = [" USER" , " ROBOT" , " SESSION" , " VARIABLE" , " FACTOR" , " ENVIRONMENT" , " PPE" , " BIGDATA" ]
13
+ global _validLabelRegex = r" ^[a-zA-Z]\w *$"
14
+
15
+ function _isValid (id:: Union{Symbol, String} ):: Bool
16
+ if typeof (id) == Symbol
17
+ id = String (id)
18
+ end
19
+ return all (t -> t != uppercase (id), _invalidIds) && match (_validLabelRegex, id) != nothing
20
+ end
11
21
12
22
function _isValid (abstractNode:: N ):: Bool where N <: AbstractCGNode
13
- invalidIds = [ " USER " , " ROBOT " , " SESSION " , " VARIABLE " , " FACTOR " , " ENVIRONMENT " ]
14
- return all (t -> t != uppercase (String (abstractNode . id)), invalidIds)
23
+ id = String (abstractNode . id)
24
+ return all (t -> t != uppercase (id), _invalidIds) && match (_validLabelRegex, id) != nothing
15
25
end
16
26
17
27
# Fastest way I can think to convert the data into a dict
26
36
27
37
# TODO : Refactor, #HACK :D (but it works!)
28
38
function _convertDictToSession (dict:: Dict{String, Any} ):: Session
29
- sessionData = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
39
+ data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
30
40
session = Session (
31
41
Symbol (dict[" id" ]),
32
42
Symbol (dict[" robotId" ]),
33
43
Symbol (dict[" userId" ]),
34
44
dict[" name" ],
35
45
dict[" description" ],
36
- sessionData )
46
+ data )
37
47
return session
38
48
end
49
+ # TODO : Refactor, #HACK :D (but it works!)
50
+ function _convertDictToRobot (dict:: Dict{String, Any} ):: Robot
51
+ data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
52
+ robot = Robot (
53
+ Symbol (dict[" id" ]),
54
+ Symbol (dict[" userId" ]),
55
+ dict[" name" ],
56
+ dict[" description" ],
57
+ data)
58
+ return robot
59
+ end
60
+ # TODO : Refactor, #HACK :D (but it works!)
61
+ function _convertDictToUser (dict:: Dict{String, Any} ):: User
62
+ data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
63
+ user = User (
64
+ Symbol (dict[" id" ]),
65
+ dict[" name" ],
66
+ dict[" description" ],
67
+ data)
68
+ return user
69
+ end
39
70
40
71
function createUser (dfg:: CloudGraphsDFG , user:: User ):: User
41
72
Symbol (dfg. userId) != user. id && error (" DFG user ID must match user's ID" )
@@ -105,18 +136,106 @@ function createDfgSessionIfNotExist(dfg::CloudGraphsDFG)::Session
105
136
end
106
137
end
107
138
108
- function listSessions (dfg:: CloudGraphsDFG ):: Vector{Session}
139
+ """
140
+ $(SIGNATURES)
141
+ List all sessions for the specified DFG's robot and user.
142
+ Returns nothing if it isn't found.
143
+ """
144
+ function lsSessions (dfg:: CloudGraphsDFG ):: Vector{Session}
109
145
sessionNodes = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:SESSION:$(dfg. robotId) :$(dfg. userId) )" )
110
146
return map (s -> _convertDictToSession (Neo4j. getnodeproperties (s)), sessionNodes)
111
147
end
112
148
113
- function getSession (dfg:: CloudGraphsDFG ):: Union{Nothing, Session}
114
- sessionNode = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:SESSION:$(dfg. sessionId) :$(dfg. robotId) :$(dfg. userId) )" )
149
+ """
150
+ $(SIGNATURES)
151
+ List all robots for the specified DFG's user.
152
+ Returns nothing if it isn't found.
153
+ """
154
+ function lsRobots (dfg:: CloudGraphsDFG ):: Vector{Robot}
155
+ robotNodes = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:ROBOT:$(dfg. userId) )" )
156
+ return map (s -> _convertDictToRobot (Neo4j. getnodeproperties (s)), robotNodes)
157
+ end
158
+
159
+ """
160
+ $(SIGNATURES)
161
+ List all users.
162
+ Returns nothing if it isn't found.
163
+ """
164
+ function lsUsers (dfg:: CloudGraphsDFG ):: Vector{User}
165
+ userNodes = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:USER)" )
166
+ return map (s -> _convertDictToUser (Neo4j. getnodeproperties (s)), userNodes)
167
+ end
168
+
169
+ """
170
+ $(SIGNATURES)
171
+ Get a session specified by userId:robotId:sessionId.
172
+ Returns nothing if it isn't found.
173
+ """
174
+ function getSession (dfg:: CloudGraphsDFG , userId:: Symbol , robotId:: Symbol , sessionId:: Symbol ):: Union{Session, Nothing}
175
+ ! _isValid (userId) && error (" Can't receive session with user ID '$(userId) '." )
176
+ ! _isValid (robotId) && error (" Can't receive session with robot ID '$(robotId) '." )
177
+ ! _isValid (sessionId) && error (" Can't receive session with session ID '$(sessionId) '." )
178
+ sessionNode = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:SESSION:$(sessionId) :$(robotId) :$(userId) )" )
115
179
length (sessionNode) == 0 && return nothing
116
- length (sessionNode) > 1 && error (" There look to be $(length (sessionNode)) sessions identified for $(dfg . sessionId) :$(dfg . robotId) :$(dfg . userId) " )
180
+ length (sessionNode) > 1 && error (" There look to be $(length (sessionNode)) sessions identified for $(sessionId) :$(robotId) :$(userId) " )
117
181
return _convertDictToSession (Neo4j. getnodeproperties (sessionNode[1 ]))
118
182
end
119
183
184
+ """
185
+ $(SIGNATURES)
186
+ Get the session specified by the DFG object.
187
+ Returns nothing if it isn't found.
188
+ """
189
+ function getSession (dfg:: CloudGraphsDFG ):: Union{Nothing, Session}
190
+ return getSession (dfg, Symbol (dfg. userId), Symbol (dfg. robotId), Symbol (dfg. sessionId))
191
+ end
192
+
193
+ """
194
+ $(SIGNATURES)
195
+ Get a robot specified by userId:robotId.
196
+ Returns nothing if it isn't found.
197
+ """
198
+ function getRobot (dfg:: CloudGraphsDFG , userId:: Symbol , robotId:: Symbol ):: Union{Robot, Nothing}
199
+ ! _isValid (userId) && error (" Can't receive session with user ID '$(userId) '." )
200
+ ! _isValid (robotId) && error (" Can't receive session with robot ID '$(robotId) '." )
201
+ robotNode = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:ROBOT:$(robotId) :$(userId) )" )
202
+ length (robotNode) == 0 && return nothing
203
+ length (robotNode) > 1 && error (" There look to be $(length (robotNode)) robots identified for $(robotId) :$(userId) " )
204
+ return _convertDictToRobot (Neo4j. getnodeproperties (robotNode[1 ]))
205
+ end
206
+
207
+ """
208
+ $(SIGNATURES)
209
+ Get the robot specified by the DFG object.
210
+ Returns nothing if it isn't found.
211
+ """
212
+ function getRobot (dfg:: CloudGraphsDFG ):: Union{Nothing, Robot}
213
+ return getRobot (dfg, Symbol (dfg. userId), Symbol (dfg. robotId))
214
+ end
215
+
216
+ """
217
+ $(SIGNATURES)
218
+ Get a user specified by userId.
219
+ Returns nothing if it isn't found.
220
+ """
221
+ function getUser (dfg:: CloudGraphsDFG , userId:: Symbol ):: Union{User, Nothing}
222
+ ! _isValid (userId) && error (" Can't receive session with user ID '$(userId) '." )
223
+ userNode = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:USER:$(userId) )" )
224
+ length (userNode) == 0 && return nothing
225
+ length (userNode) > 1 && error (" There look to be $(length (userNode)) robots identified for $(userId) " )
226
+ return _convertDictToUser (Neo4j. getnodeproperties (userNode[1 ]))
227
+ end
228
+
229
+ """
230
+ $(SIGNATURES)
231
+ Get the user specified by the DFG object.
232
+ Returns nothing if it isn't found.
233
+ """
234
+ function getUser (dfg:: CloudGraphsDFG ):: Union{Nothing, User}
235
+ return getUser (dfg, Symbol (dfg. userId))
236
+ end
237
+
238
+
120
239
"""
121
240
$(SIGNATURES)
122
241
DANGER: Clears the whole session from the database.
0 commit comments