@@ -3,15 +3,25 @@ export copySession!
3
3
# Please be careful with these
4
4
# With great power comes great "Oh crap, I deleted everything..."
5
5
export clearSession!!, clearRobot!!, clearUser!!
6
- export createSession, createRobot, createUser
6
+ 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
@@ -20,22 +30,43 @@ function _convertNodeToDict(abstractNode::N)::Dict{String, Any} where N <: Abstr
20
30
cp = deepcopy (abstractNode)
21
31
data = length (cp. data) != 0 ? JSON2. write (cp. data) : " {}"
22
32
ser = JSON2. read (JSON2. write (abstractNode), Dict{String, Any})
23
- ser[" data" ] = data
33
+ ser[" data" ] = base64encode ( data)
24
34
return ser
25
35
end
26
36
27
37
# TODO : Refactor, #HACK :D (but it works!)
28
38
function _convertDictToSession (dict:: Dict{String, Any} ):: Session
29
- sessionData = JSON2. read (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" )
@@ -84,11 +115,127 @@ function createSession(dfg::CloudGraphsDFG, session::Session)::Session
84
115
return session
85
116
end
86
117
87
- function listSessions (dfg:: CloudGraphsDFG ):: Vector{Session}
118
+ """
119
+ $(SIGNATURES)
120
+ Shortcut method to create the user, robot, and session if it doesn't already exist.
121
+ """
122
+ function createDfgSessionIfNotExist (dfg:: CloudGraphsDFG ):: Session
123
+ strip (dfg. userId) == " " && error (" User ID is not populated in DFG." )
124
+ strip (dfg. robotId) == " " && error (" Robot ID is not populated in DFG." )
125
+ strip (dfg. sessionId) == " " && error (" Session ID is not populated in DFG." )
126
+ user = User (Symbol (dfg. userId), dfg. userId, " Description for $(dfg. userId) " , Dict {Symbol, String} ())
127
+ robot = Robot (Symbol (dfg. robotId), Symbol (dfg. userId), dfg. robotId, " Description for $(dfg. userId) :$(dfg. robotId) " , Dict {Symbol, String} ())
128
+ session = Session (Symbol (dfg. sessionId), Symbol (dfg. robotId), Symbol (dfg. userId), dfg. sessionId, " Description for $(dfg. userId) :$(dfg. robotId) :$(dfg. sessionId) " , Dict {Symbol, String} ())
129
+
130
+ _getNodeCount (dfg. neo4jInstance, [dfg. userId, " USER" ]) == 0 && createUser (dfg, user)
131
+ _getNodeCount (dfg. neo4jInstance, [dfg. userId, dfg. robotId, " ROBOT" ]) == 0 && createRobot (dfg, robot)
132
+ if _getNodeCount (dfg. neo4jInstance, [dfg. userId, dfg. robotId, dfg. sessionId, " SESSION" ]) == 0
133
+ return createSession (dfg, session)
134
+ else
135
+ return getSession (dfg)
136
+ end
137
+ end
138
+
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}
88
145
sessionNodes = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:SESSION:$(dfg. robotId) :$(dfg. userId) )" )
89
146
return map (s -> _convertDictToSession (Neo4j. getnodeproperties (s)), sessionNodes)
90
147
end
91
148
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) )" )
179
+ length (sessionNode) == 0 && return nothing
180
+ length (sessionNode) > 1 && error (" There look to be $(length (sessionNode)) sessions identified for $(sessionId) :$(robotId) :$(userId) " )
181
+ return _convertDictToSession (Neo4j. getnodeproperties (sessionNode[1 ]))
182
+ end
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
+
92
239
"""
93
240
$(SIGNATURES)
94
241
DANGER: Clears the whole session from the database.
@@ -156,18 +303,27 @@ DANGER: Copies the source to a new unique destination.
156
303
copySession! (sourceDFG:: CloudGraphsDFG ):: CloudGraphsDFG = copySession! (sourceDFG, nothing )
157
304
158
305
159
- getUserData (dfg:: CloudGraphsDFG ):: Dict{Symbol, String} = _getNodeProperty (dfg. neo4jInstance, [dfg. userId, " USER" ])
306
+ function getUserData (dfg:: CloudGraphsDFG ):: Dict{Symbol, String}
307
+ propVal = _getNodeProperty (dfg. neo4jInstance, [dfg. userId, " USER" ], " data" )
308
+ return JSON2. read (String (base64decode (propVal)), Dict{Symbol, String})
309
+ end
160
310
function setUserData (dfg:: CloudGraphsDFG , data:: Dict{Symbol, String} ):: Bool
161
- error (" Not implemented yet" )
162
- return true
311
+ count = _setNodeProperty (dfg. neo4jInstance, [dfg. userId, " USER" ], " data" , base64encode (JSON2. write (data)))
312
+ return count == 1
313
+ end
314
+ function getRobotData (dfg:: CloudGraphsDFG ):: Dict{Symbol, String}
315
+ propVal = _getNodeProperty (dfg. neo4jInstance, [dfg. userId, dfg. robotId, " ROBOT" ], " data" )
316
+ return JSON2. read (String (base64decode (propVal)), Dict{Symbol, String})
163
317
end
164
- getRobotData (dfg:: CloudGraphsDFG ):: Dict{Symbol, String} = _getNodeProperty (dfg. neo4jInstance, [dfg. userId, dfg. robotId, " ROBOT" ])
165
318
function setRobotData (dfg:: CloudGraphsDFG , data:: Dict{Symbol, String} ):: Bool
166
- error (" Not implemented yet" )
167
- return true
319
+ count = _setNodeProperty (dfg. neo4jInstance, [dfg. userId, dfg. robotId, " ROBOT" ], " data" , base64encode (JSON2. write (data)))
320
+ return count == 1
321
+ end
322
+ function getSessionData (dfg:: CloudGraphsDFG ):: Dict{Symbol, String}
323
+ propVal = _getNodeProperty (dfg. neo4jInstance, [dfg. userId, dfg. robotId, dfg. sessionId, " SESSION" ], " data" )
324
+ return JSON2. read (String (base64decode (propVal)), Dict{Symbol, String})
168
325
end
169
- getSessionData (dfg:: CloudGraphsDFG ):: Dict{Symbol, String} = _getNodeProperty (dfg. neo4jInstance, [dfg. userId, dfg. robotId, dfg. sessionId, " SESSION" ])
170
326
function setSessionData (dfg:: CloudGraphsDFG , data:: Dict{Symbol, String} ):: Bool
171
- error ( " Not implemented yet " )
172
- return true
327
+ count = _setNodeProperty (dfg . neo4jInstance, [dfg . userId, dfg . robotId, dfg . sessionId, " SESSION " ], " data " , base64encode (JSON2 . write (data)) )
328
+ return count == 1
173
329
end
0 commit comments