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