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|
| 2 |
+
from pymongo import MongoClient
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from prefect import task
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
MONGODB_PASSWORD = os.getenv("MONGODB_PASSWORD")
|
| 9 |
+
|
| 10 |
+
blinded_connection_string = os.getenv("blinded_connection_string")
|
| 11 |
+
|
| 12 |
+
connection_string = blinded_connection_string.replace("<db_password>", MONGODB_PASSWORD)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@task
|
| 16 |
+
def generate_empty_well():
|
| 17 |
+
dbclient = MongoClient(connection_string)
|
| 18 |
+
db = dbclient["LCM-OT-2-SLD"]
|
| 19 |
+
collection = db["wells"]
|
| 20 |
+
rows = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
|
| 21 |
+
columns = [str(i) for i in range(1, 13)]
|
| 22 |
+
for row in rows:
|
| 23 |
+
for col in columns:
|
| 24 |
+
well = f"{row}{col}"
|
| 25 |
+
metadata = {
|
| 26 |
+
"well": well,
|
| 27 |
+
"status": "empty",
|
| 28 |
+
"project": "OT2"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
#send_data_to_mongodb(collection="wells", data=metadata)
|
| 32 |
+
query = {"well": well}
|
| 33 |
+
update_data = {"$set": metadata}
|
| 34 |
+
result = collection.update_one(query, update_data, upsert=True)
|
| 35 |
+
|
| 36 |
+
# close connection
|
| 37 |
+
dbclient.close()
|
| 38 |
+
|
| 39 |
+
@task
|
| 40 |
+
def update_used_wells(used_wells):
|
| 41 |
+
dbclient = MongoClient(connection_string)
|
| 42 |
+
db = dbclient["LCM-OT-2-SLD"]
|
| 43 |
+
collection = db["wells"]
|
| 44 |
+
|
| 45 |
+
for well in used_wells:
|
| 46 |
+
metadata = {
|
| 47 |
+
"well": well,
|
| 48 |
+
"status": "used",
|
| 49 |
+
"project": "OT2"
|
| 50 |
+
}
|
| 51 |
+
#send_data_to_mongodb(collection="wells", data=metadata)
|
| 52 |
+
query = {"well": well}
|
| 53 |
+
update_data = {"$set": metadata}
|
| 54 |
+
result = collection.update_one(query, update_data, upsert=True)
|
| 55 |
+
|
| 56 |
+
# close connection
|
| 57 |
+
dbclient.close()
|
| 58 |
+
|
| 59 |
+
@task
|
| 60 |
+
def find_unused_wells():
|
| 61 |
+
dbclient = MongoClient(connection_string)
|
| 62 |
+
db = dbclient["LCM-OT-2-SLD"]
|
| 63 |
+
collection = db["wells"]
|
| 64 |
+
query = {"status": "empty"}
|
| 65 |
+
response = list(collection.find(query))
|
| 66 |
+
df = pd.DataFrame(response)
|
| 67 |
+
|
| 68 |
+
# Extract the "well" column as a list
|
| 69 |
+
if "well" not in df.columns:
|
| 70 |
+
raise ValueError("The returned data does not contain the 'well' field.")
|
| 71 |
+
# Sort obtained list
|
| 72 |
+
def well_sort_key(well):
|
| 73 |
+
row = well[0] # A, B, C, ...
|
| 74 |
+
col = int(well[1:]) # 1, 2, ..., 12
|
| 75 |
+
return (row, col)
|
| 76 |
+
|
| 77 |
+
empty_wells = sorted(df["well"].tolist(), key=well_sort_key)
|
| 78 |
+
#print(empty_wells)
|
| 79 |
+
|
| 80 |
+
# close connection
|
| 81 |
+
dbclient.close()
|
| 82 |
+
|
| 83 |
+
# Check if there are any empty wells
|
| 84 |
+
if len(empty_wells) == 0:
|
| 85 |
+
raise ValueError("No empty wells found")
|
| 86 |
+
#print(empty_wells)
|
| 87 |
+
return empty_wells
|
| 88 |
+
|
| 89 |
+
@task
|
| 90 |
+
def save_result(result_data):
|
| 91 |
+
dbclient = MongoClient(connection_string)
|
| 92 |
+
db = dbclient["LCM-OT-2-SLD"]
|
| 93 |
+
#collection = db["MSE403_result"]
|
| 94 |
+
collection = db["test_result"]
|
| 95 |
+
result_data["timestamp"] = datetime.utcnow() # UTC time
|
| 96 |
+
insert_result = collection.insert_one(result_data)
|
| 97 |
+
inserted_id = insert_result.inserted_id
|
| 98 |
+
# close connection
|
| 99 |
+
dbclient.close()
|
| 100 |
+
return inserted_id
|
| 101 |
+
|
| 102 |
+
def get_student_quota(student_id):
|
| 103 |
+
with MongoClient(connection_string) as client:
|
| 104 |
+
db = client["LCM-OT-2-SLD"]
|
| 105 |
+
collection = db["student"]
|
| 106 |
+
student = collection.find_one({"student_id": student_id})
|
| 107 |
+
if student is None:
|
| 108 |
+
raise ValueError(f"Student ID '{student_id}' not found in the database.")
|
| 109 |
+
return student.get("quota", 0)
|
| 110 |
+
|
| 111 |
+
def decrement_student_quota(student_id):
|
| 112 |
+
dbclient = MongoClient(connection_string)
|
| 113 |
+
db = dbclient["LCM-OT-2-SLD"]
|
| 114 |
+
collection = db["student"]
|
| 115 |
+
|
| 116 |
+
student = collection.find_one({"student_id": student_id})
|
| 117 |
+
if not student:
|
| 118 |
+
return f"Student ID {student_id} not found."
|
| 119 |
+
if student.get("quota", 0) <= 0:
|
| 120 |
+
return f"Student ID {student_id} has no remaining quota."
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
result = collection.update_one(
|
| 124 |
+
{"student_id": student_id, "quota": {"$gt": 0}},
|
| 125 |
+
{"$inc": {"quota": -1}}
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if result.modified_count > 0:
|
| 129 |
+
return f"Student ID {student_id}'s quota update successfully."
|
| 130 |
+
else:
|
| 131 |
+
return f"Quota update failed for Student ID {student_id}."
|
| 132 |
+
|
| 133 |
+
def add_student_quota(student_id, quota):
|
| 134 |
+
"""
|
| 135 |
+
Adds a new student with a given quota.
|
| 136 |
+
:param student_id: The ID of the student.
|
| 137 |
+
:param quota: The initial quota for the student.
|
| 138 |
+
"""
|
| 139 |
+
dbclient = MongoClient(connection_string)
|
| 140 |
+
db = dbclient["LCM-OT-2-SLD"]
|
| 141 |
+
collection = db["student"]
|
| 142 |
+
student_data = {"student_id": student_id, "quota": quota}
|
| 143 |
+
collection.update_one({"student_id": student_id}, {"$set": student_data}, upsert=True)
|
| 144 |
+
dbclient.close()
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
if __name__ == "__main__":
|
| 148 |
+
generate_empty_well()
|
| 149 |
+
#find_unused_wells()
|
| 150 |
+
#test_id = "test"
|
| 151 |
+
#quota = 999
|
| 152 |
+
#add_student_quota(test_id, quota)
|
| 153 |
+
|
| 154 |
+
|