File size: 9,237 Bytes
073ab01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from astrapy import DataAPIClient

xforce_bcl_endpoint="https://b3cc3888-098f-45f7-9087-260ed43b742c-us-east-2.apps.astra.datastax.com"
xforce_bcl_db_token="AstraCS:dRZIxPSXJqqWPPgJjMpoqnrJ:999c021256da787c45f0309e589c9e99884c6559a9337d09de9fa15cd88843dc"




#Initialize the client
client = DataAPIClient(xforce_bcl_db_token)
db = client.get_database_by_api_endpoint(xforce_bcl_endpoint)
coll="bcl"
ie="individual_experiments"
app="app"
memory="memory"
history="history"
applayer="applayer"
filtered_candidates="filtered_candidates"
ooda="ooda"



class DatabaseEngine():
    
    def __init__(self):
        pass

    def Insert(self,data):
        
        collection=db.get_collection(coll)
        result = collection.insert_one(data)
        return result


    def Insert_IE(self,data):
        try:
            collection=db.get_collection(ie)
            collection.insert_one(data)
            return True
            
        except Exception as e:
            return False
        

    def InsertMemory(self,data):
        try:
            collection=db.get_collection(memory)
            collection.insert_one(data)
            return True
        except Exception as e:
            return False
            

    
    def Fetch_IE(self,bcl_id):
        
        collection=db.get_collection(ie)
        documents=collection.find({"bcl_id":bcl_id})
        released_docs=[]
        for doc in documents:
            released_docs.append(doc)
        return released_docs
        


    def FetchMemory(self,bcl_id):
        collection=db.get_collection(memory)
        document=collection.find_one({"bcl_id":bcl_id})
        return document


    def UpdateMemory(self,bcl_id,operation):
        collection=db.get_collection(memory)
        update_result_a = collection.update_one(
            {"bcl_id": bcl_id},
            {"$push": {"executed_operations":operation}}
        )

    #UPDATED 
    def UpdateProject(self,projectid,plan):
        collection=db.get_collection(coll)
        update_result_a = collection.update_one(
            {"project.project_id": projectid},
            {"$push": {"plans":plan}}
        )
    
        
    def CheckEmpty(self,id):
        
        collection=db.get_collection(history)
        cursor=collection.find({"bcl_id":id})
        data=list(cursor)
        if not data:
            return True
        else:
            return False




    def CheckEmptyAppLayer(self,id):
        
        collection=db.get_collection(applayer)
        cursor=collection.find({"bcl_id":id})
        data=list(cursor)
        if not data:
            return True
        else:
            return False

    

    def CheckEmptyOps(self,id):
        collection=db.get_collection(memory)
        cursor=collection.find({"bcl_id":id})
        data=list(cursor)
        if not data:
            return True
        else:
            return False



    def CheckEmptyProjects(self,id):
        collection=db.get_collection(coll)
        cursor=collection.find({"bcl_id":id})
        data=list(cursor)
        if not data:
            return True
        else:
            return False
            

    def Insert_Conversation(self,data):
        collection=db.get_collection(history)
        collection.insert_one(data)

    def Update_Conversation(self,bcl_id,data):
        collection=db.get_collection(history)
        update_result_a = collection.update_one(
            {"bcl_id": bcl_id},
            {"$push": {"messages": {"$each": data}}}
        )


            
    def FetchConversation(self,id):
        collection=db.get_collection(history)
        document=collection.find_one({"bcl_id":id})
        return document
    




    def Insert_AppLayer(self,data):
        collection=db.get_collection(applayer)
        collection.insert_one(data)
        

    def Update_AppLayer(self,bcl_id,data):
        collection=db.get_collection(applayer)
        update_result_a = collection.update_one(
            {"bcl_id": bcl_id},
            {"$push": {"messages": {"$each": data}}}
        )


            
    def Fetch_AppLayer(self,id):
        collection=db.get_collection(applayer)
        document=collection.find_one({"bcl_id":id})
        return document

    

    
    def Update_Status(self,project_id,bcl_id):
        
        try:
            collection=db.get_collection(coll)
             # This is the query to find the correct document AND the specific plan.
            filter_query = {
                "project.project_id": project_id,
                "project.plans.bcl_id": bcl_id
                }

            update_operation = {
                "$set": {
                "project.plans.$.status": "done"
                }
            }

            collection.update_one(filter_query, update_operation)

            #collection.update_one(filter_query, update_operation)
            return True
            
        except Exception as e:
            return False


        
    def Find_User(self,uid):
        collection = db.get_collection(coll)
        cursor = collection.find({"user_id":uid})
        
        documents=[]
        
        for document in cursor:

            
            bcl_id=document.get("bcl_id")
            status=document.get("status")
            newdoc={"id":bcl_id,"status":status}
            
            documents.append(newdoc)
                
        return documents
        
    def Find_Task(self,id):
        
        collection = db.get_collection(coll)
        cursor = collection.find({"bcl_id":id})
        
        documents=[]
        
        for document in cursor:
            documents.append(document)

        
        return documents


    def Fetch_Status(self,bcl_id):
        
        collection=db.get_collection(coll)
        document=collection.find_one({"bcl_id":bcl_id})
        
        return {"status":document["status"]}
        
    '''
    def Fetch_Projects(self,user_id):
        
        collection=db.get_collection(coll)
        cursor=collection.find({"user_id":user_id})
        documents=[]
        for document in cursor:
            
            projectid=document.get("project_id")
            status=document.get("status")
            documents.append({"project":projectid,"status":status})
            
        return documents
    '''



    def Fetch_Projects(self, user_id):
        #Get the correct collection from the database
        collection = db.get_collection(coll)

        #Find the document for the given user_id
        document = collection.find_one({"user_id": user_id})
    
        #Initialize a list to hold the project details
        projects = []

        #Check if a document was found and if it contains a 'project' field
        if document and "project" in document:
        # Get the project dictionary from the document
            project_data = document["project"]

        # Get the project_id from the project dictionary
            project_id = project_data.get("project_id")

        # Get the list of plans from the project data
        #plans = project_data.get("plans", [])

        # Iterate over each plan to extract its status and include the project_id
        #for plan in plans:
            projects.append(project_id)

        return projects


    def RegisterUser(self,data):
        
        collection=db.get_collection(app)
        collection.insert_one(data)


    def UpdateUser(self,uid,data):
        try:
            collection=db.get_collection(app)
        
            collection.update_one(
                {"username": uid},
                {"$push": {"projects": data}}
            )
            return True
        except Exception as e:
            return False



    def InsertFiltered(self,data):
        
        collection=db.get_collection(filtered_candidates)
        collection.insert_one(data)


    def FetchFiltered(self,bcl_id):
        collection=db.get_collection(filtered_candidates)
        document=collection.find_one({"bcl_id":bcl_id})
        return document


    def CheckEmptyFiltered(self,id):
        collection=db.get_collection(filtered_candidates)
        cursor=collection.find({"bcl_id":id})
        data=list(cursor)
        if not data:
            return True
        else:
            return False


    def UpdateFiltered(self,bcl_id,data):
        collection=db.get_collection(filtered_candidates)
        update_result_a = collection.update_one(
            {"bcl_id": bcl_id},
            {"$push": {"candidates": {"$each": data}}}
        )


    def Insert_OODA(self,data):
        collection=db.get_collection(ooda)
        collection.insert_one(data)

    def update_OODA(self,bcl_id,data):
        collection=db.get_collection(ooda)
        update_result_a=collection.update_one(
            {"bcl_id":bcl_id},
            {"$push":{"ooda_loop":{"$each":data}}}
        )


    def CheckEmptyOODA(self,id):
        collection=db.get_collection(ooda)
        cursor=collection.find({"bcl_id":id})
        data=list(cursor)
        if not data:
            return True
        else:
            return False

    
    def FetchOODA(self,bcl_id):
        collection=db.get_collection(ooda)
        document=collection.find_one({"bcl_id":bcl_id})
        return document