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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from astrapy import DataAPIClient
|
| 2 |
+
|
| 3 |
+
xforce_bcl_endpoint="https://b3cc3888-098f-45f7-9087-260ed43b742c-us-east-2.apps.astra.datastax.com"
|
| 4 |
+
xforce_bcl_db_token="AstraCS:dRZIxPSXJqqWPPgJjMpoqnrJ:999c021256da787c45f0309e589c9e99884c6559a9337d09de9fa15cd88843dc"
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
#Initialize the client
|
| 10 |
+
client = DataAPIClient(xforce_bcl_db_token)
|
| 11 |
+
db = client.get_database_by_api_endpoint(xforce_bcl_endpoint)
|
| 12 |
+
coll="bcl"
|
| 13 |
+
ie="individual_experiments"
|
| 14 |
+
app="app"
|
| 15 |
+
memory="memory"
|
| 16 |
+
history="history"
|
| 17 |
+
applayer="applayer"
|
| 18 |
+
filtered_candidates="filtered_candidates"
|
| 19 |
+
ooda="ooda"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class DatabaseEngine():
|
| 24 |
+
|
| 25 |
+
def __init__(self):
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
def Insert(self,data):
|
| 29 |
+
|
| 30 |
+
collection=db.get_collection(coll)
|
| 31 |
+
result = collection.insert_one(data)
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def Insert_IE(self,data):
|
| 36 |
+
try:
|
| 37 |
+
collection=db.get_collection(ie)
|
| 38 |
+
collection.insert_one(data)
|
| 39 |
+
return True
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return False
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def InsertMemory(self,data):
|
| 46 |
+
try:
|
| 47 |
+
collection=db.get_collection(memory)
|
| 48 |
+
collection.insert_one(data)
|
| 49 |
+
return True
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return False
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def Fetch_IE(self,bcl_id):
|
| 56 |
+
|
| 57 |
+
collection=db.get_collection(ie)
|
| 58 |
+
documents=collection.find({"bcl_id":bcl_id})
|
| 59 |
+
released_docs=[]
|
| 60 |
+
for doc in documents:
|
| 61 |
+
released_docs.append(doc)
|
| 62 |
+
return released_docs
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def FetchMemory(self,bcl_id):
|
| 67 |
+
collection=db.get_collection(memory)
|
| 68 |
+
document=collection.find_one({"bcl_id":bcl_id})
|
| 69 |
+
return document
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def UpdateMemory(self,bcl_id,operation):
|
| 73 |
+
collection=db.get_collection(memory)
|
| 74 |
+
update_result_a = collection.update_one(
|
| 75 |
+
{"bcl_id": bcl_id},
|
| 76 |
+
{"$push": {"executed_operations":operation}}
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
#UPDATED
|
| 80 |
+
def UpdateProject(self,projectid,plan):
|
| 81 |
+
collection=db.get_collection(coll)
|
| 82 |
+
update_result_a = collection.update_one(
|
| 83 |
+
{"project.project_id": projectid},
|
| 84 |
+
{"$push": {"plans":plan}}
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def CheckEmpty(self,id):
|
| 89 |
+
|
| 90 |
+
collection=db.get_collection(history)
|
| 91 |
+
cursor=collection.find({"bcl_id":id})
|
| 92 |
+
data=list(cursor)
|
| 93 |
+
if not data:
|
| 94 |
+
return True
|
| 95 |
+
else:
|
| 96 |
+
return False
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def CheckEmptyAppLayer(self,id):
|
| 102 |
+
|
| 103 |
+
collection=db.get_collection(applayer)
|
| 104 |
+
cursor=collection.find({"bcl_id":id})
|
| 105 |
+
data=list(cursor)
|
| 106 |
+
if not data:
|
| 107 |
+
return True
|
| 108 |
+
else:
|
| 109 |
+
return False
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def CheckEmptyOps(self,id):
|
| 114 |
+
collection=db.get_collection(memory)
|
| 115 |
+
cursor=collection.find({"bcl_id":id})
|
| 116 |
+
data=list(cursor)
|
| 117 |
+
if not data:
|
| 118 |
+
return True
|
| 119 |
+
else:
|
| 120 |
+
return False
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def CheckEmptyProjects(self,id):
|
| 125 |
+
collection=db.get_collection(coll)
|
| 126 |
+
cursor=collection.find({"bcl_id":id})
|
| 127 |
+
data=list(cursor)
|
| 128 |
+
if not data:
|
| 129 |
+
return True
|
| 130 |
+
else:
|
| 131 |
+
return False
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def Insert_Conversation(self,data):
|
| 135 |
+
collection=db.get_collection(history)
|
| 136 |
+
collection.insert_one(data)
|
| 137 |
+
|
| 138 |
+
def Update_Conversation(self,bcl_id,data):
|
| 139 |
+
collection=db.get_collection(history)
|
| 140 |
+
update_result_a = collection.update_one(
|
| 141 |
+
{"bcl_id": bcl_id},
|
| 142 |
+
{"$push": {"messages": {"$each": data}}}
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def FetchConversation(self,id):
|
| 148 |
+
collection=db.get_collection(history)
|
| 149 |
+
document=collection.find_one({"bcl_id":id})
|
| 150 |
+
return document
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def Insert_AppLayer(self,data):
|
| 157 |
+
collection=db.get_collection(applayer)
|
| 158 |
+
collection.insert_one(data)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def Update_AppLayer(self,bcl_id,data):
|
| 162 |
+
collection=db.get_collection(applayer)
|
| 163 |
+
update_result_a = collection.update_one(
|
| 164 |
+
{"bcl_id": bcl_id},
|
| 165 |
+
{"$push": {"messages": {"$each": data}}}
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def Fetch_AppLayer(self,id):
|
| 171 |
+
collection=db.get_collection(applayer)
|
| 172 |
+
document=collection.find_one({"bcl_id":id})
|
| 173 |
+
return document
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def Update_Status(self,project_id,bcl_id):
|
| 179 |
+
|
| 180 |
+
try:
|
| 181 |
+
collection=db.get_collection(coll)
|
| 182 |
+
# This is the query to find the correct document AND the specific plan.
|
| 183 |
+
filter_query = {
|
| 184 |
+
"project.project_id": project_id,
|
| 185 |
+
"project.plans.bcl_id": bcl_id
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
update_operation = {
|
| 189 |
+
"$set": {
|
| 190 |
+
"project.plans.$.status": "done"
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
collection.update_one(filter_query, update_operation)
|
| 195 |
+
|
| 196 |
+
#collection.update_one(filter_query, update_operation)
|
| 197 |
+
return True
|
| 198 |
+
|
| 199 |
+
except Exception as e:
|
| 200 |
+
return False
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def Find_User(self,uid):
|
| 205 |
+
collection = db.get_collection(coll)
|
| 206 |
+
cursor = collection.find({"user_id":uid})
|
| 207 |
+
|
| 208 |
+
documents=[]
|
| 209 |
+
|
| 210 |
+
for document in cursor:
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
bcl_id=document.get("bcl_id")
|
| 214 |
+
status=document.get("status")
|
| 215 |
+
newdoc={"id":bcl_id,"status":status}
|
| 216 |
+
|
| 217 |
+
documents.append(newdoc)
|
| 218 |
+
|
| 219 |
+
return documents
|
| 220 |
+
|
| 221 |
+
def Find_Task(self,id):
|
| 222 |
+
|
| 223 |
+
collection = db.get_collection(coll)
|
| 224 |
+
cursor = collection.find({"bcl_id":id})
|
| 225 |
+
|
| 226 |
+
documents=[]
|
| 227 |
+
|
| 228 |
+
for document in cursor:
|
| 229 |
+
documents.append(document)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
return documents
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def Fetch_Status(self,bcl_id):
|
| 236 |
+
|
| 237 |
+
collection=db.get_collection(coll)
|
| 238 |
+
document=collection.find_one({"bcl_id":bcl_id})
|
| 239 |
+
|
| 240 |
+
return {"status":document["status"]}
|
| 241 |
+
|
| 242 |
+
'''
|
| 243 |
+
def Fetch_Projects(self,user_id):
|
| 244 |
+
|
| 245 |
+
collection=db.get_collection(coll)
|
| 246 |
+
cursor=collection.find({"user_id":user_id})
|
| 247 |
+
documents=[]
|
| 248 |
+
for document in cursor:
|
| 249 |
+
|
| 250 |
+
projectid=document.get("project_id")
|
| 251 |
+
status=document.get("status")
|
| 252 |
+
documents.append({"project":projectid,"status":status})
|
| 253 |
+
|
| 254 |
+
return documents
|
| 255 |
+
'''
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
def Fetch_Projects(self, user_id):
|
| 260 |
+
#Get the correct collection from the database
|
| 261 |
+
collection = db.get_collection(coll)
|
| 262 |
+
|
| 263 |
+
#Find the document for the given user_id
|
| 264 |
+
document = collection.find_one({"user_id": user_id})
|
| 265 |
+
|
| 266 |
+
#Initialize a list to hold the project details
|
| 267 |
+
projects = []
|
| 268 |
+
|
| 269 |
+
#Check if a document was found and if it contains a 'project' field
|
| 270 |
+
if document and "project" in document:
|
| 271 |
+
# Get the project dictionary from the document
|
| 272 |
+
project_data = document["project"]
|
| 273 |
+
|
| 274 |
+
# Get the project_id from the project dictionary
|
| 275 |
+
project_id = project_data.get("project_id")
|
| 276 |
+
|
| 277 |
+
# Get the list of plans from the project data
|
| 278 |
+
#plans = project_data.get("plans", [])
|
| 279 |
+
|
| 280 |
+
# Iterate over each plan to extract its status and include the project_id
|
| 281 |
+
#for plan in plans:
|
| 282 |
+
projects.append(project_id)
|
| 283 |
+
|
| 284 |
+
return projects
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def RegisterUser(self,data):
|
| 288 |
+
|
| 289 |
+
collection=db.get_collection(app)
|
| 290 |
+
collection.insert_one(data)
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def UpdateUser(self,uid,data):
|
| 294 |
+
try:
|
| 295 |
+
collection=db.get_collection(app)
|
| 296 |
+
|
| 297 |
+
collection.update_one(
|
| 298 |
+
{"username": uid},
|
| 299 |
+
{"$push": {"projects": data}}
|
| 300 |
+
)
|
| 301 |
+
return True
|
| 302 |
+
except Exception as e:
|
| 303 |
+
return False
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
def InsertFiltered(self,data):
|
| 308 |
+
|
| 309 |
+
collection=db.get_collection(filtered_candidates)
|
| 310 |
+
collection.insert_one(data)
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
def FetchFiltered(self,bcl_id):
|
| 314 |
+
collection=db.get_collection(filtered_candidates)
|
| 315 |
+
document=collection.find_one({"bcl_id":bcl_id})
|
| 316 |
+
return document
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
def CheckEmptyFiltered(self,id):
|
| 320 |
+
collection=db.get_collection(filtered_candidates)
|
| 321 |
+
cursor=collection.find({"bcl_id":id})
|
| 322 |
+
data=list(cursor)
|
| 323 |
+
if not data:
|
| 324 |
+
return True
|
| 325 |
+
else:
|
| 326 |
+
return False
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def UpdateFiltered(self,bcl_id,data):
|
| 330 |
+
collection=db.get_collection(filtered_candidates)
|
| 331 |
+
update_result_a = collection.update_one(
|
| 332 |
+
{"bcl_id": bcl_id},
|
| 333 |
+
{"$push": {"candidates": {"$each": data}}}
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
def Insert_OODA(self,data):
|
| 338 |
+
collection=db.get_collection(ooda)
|
| 339 |
+
collection.insert_one(data)
|
| 340 |
+
|
| 341 |
+
def update_OODA(self,bcl_id,data):
|
| 342 |
+
collection=db.get_collection(ooda)
|
| 343 |
+
update_result_a=collection.update_one(
|
| 344 |
+
{"bcl_id":bcl_id},
|
| 345 |
+
{"$push":{"ooda_loop":{"$each":data}}}
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
def CheckEmptyOODA(self,id):
|
| 350 |
+
collection=db.get_collection(ooda)
|
| 351 |
+
cursor=collection.find({"bcl_id":id})
|
| 352 |
+
data=list(cursor)
|
| 353 |
+
if not data:
|
| 354 |
+
return True
|
| 355 |
+
else:
|
| 356 |
+
return False
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
def FetchOODA(self,bcl_id):
|
| 360 |
+
collection=db.get_collection(ooda)
|
| 361 |
+
document=collection.find_one({"bcl_id":bcl_id})
|
| 362 |
+
return document
|