File size: 10,199 Bytes
38d6a33 |
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 |
"""
Copyright 2024 Infosys Ltd.”
Use of this source code is governed by MIT license that can be found in the LICENSE file or at
MIT license https://opensource.org/licenses/MIT
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from fairness.config.logger import CustomLogger
from fairness.dao.WorkBench.databaseconnection import DataBase_WB
from fairness.Telemetry.Telemetry_call import SERVICE_UPLOAD_FILE_METADATA
from fastapi import HTTPException
from dotenv import load_dotenv
load_dotenv()
log = CustomLogger()
# Create a MongoDB database instance
ModelWorkBenchconnection = DataBase_WB()
ModelWorkBench=ModelWorkBenchconnection.db
class SampleDataset:
# Access the "Model" collection in the Common Workbench MongoDB database
def __init__(self,db=None) -> None:
if db is not None:
log.info("inside sampleDATASET loop")
self.ModelWorkBench= db
else:
ModelWorkBenchconnection = DataBase_WB()
self.ModelWorkBench=ModelWorkBenchconnection.db
# Access the "Model" collection in the Common Workbench MongoDB database
self.collection = ModelWorkBench["Dataset"]
def find(self,Sample_Id: float):
# Check if model_id is not None and is a string
if Dataset_Id is None or not isinstance(Sample_Id, float):
raise HTTPException(status_code=500, detail="Data ID must be a non-empty float")
# Try to find the model by model_id in the database
result = self.collection.find_one({"SampleData": Sample_Id}, {"_id": 0, "DataId": 1, "SampleData": 0})
if result is None:
raise HTTPException(status_code=500, detail="Data ID not found")
return result
class Dataset:
# Access the "Model" collection in the Common Workbench MongoDB database
def __init__(self,db=None) -> None:
if db is not None:
log.info("inside DATASET loop")
self.ModelWorkBench= db
else:
ModelWorkBenchconnection = DataBase_WB()
self.ModelWorkBench=ModelWorkBenchconnection.db
# Access the "Model" collection in the Common Workbench MongoDB database
self.collection = self.ModelWorkBench["Dataset"]
def find(self,Dataset_Id: float):
# Check if model_id is not None and is a string
if Dataset_Id is None or not isinstance(Dataset_Id, float):
raise HTTPException(status_code=500, detail="Data ID must be a non-empty float")
# Try to find the model by model_id in the database
result = self.collection.find_one({"DataId": Dataset_Id}, {"_id": 0, "DataSetName": 1, "SampleData": 1})
if result is None:
raise HTTPException(status_code=500, detail="Data ID not found")
return result
def findFile(self,file_Id: float):
# Check if model_id is not None and is a string
if file_Id is None or not isinstance(file_Id, float):
raise HTTPException(status_code=500, detail="Data ID must be a non-empty float")
# Try to find the model by model_id in the database
log.info(f"file_Id{file_Id}")
result = self.collection.find_one({"SampleData": file_Id}, {"_id": 0, "DataId": 1})
if result is None:
raise HTTPException(status_code=500, detail="Data ID not found")
return result
class DataAttributes:
# Access the "DataAttributes" collection in the Common Workbench MongoDB database
def __init__(self, db=None) -> None:
if db is not None:
log.info("inside dataattributes loop")
self.ModelWorkBench= db
else:
ModelWorkBenchconnection = DataBase_WB()
self.ModelWorkBench=ModelWorkBenchconnection.db
# Access the "DataAttributes" collection in the Common Workbench MongoDB database
self.collection = self.ModelWorkBench["DataAttributes"]
def find(self,dataset_attributes: list):
# Check if dataset_attributes is not None and is a string
if dataset_attributes is None or not isinstance(dataset_attributes, list):
raise HTTPException(status_code=500, detail="Dataset attribute(s) names must be a non-empty list")
# Check if dataset_attributes is an empty list
if not dataset_attributes:
raise HTTPException(status_code=500, detail="Dataset attribute(s) names must be a non-empty list")
# Try to find the dataset_attributes in the database
# Query the database for the dataset attributes
dataset_attribute_ids = list(self.collection.find({"DataAttributeName": {"$in": dataset_attributes}}, {"_id": 0, "DataAttributeId": 1, "DataAttributeName": 1}))
# Sort dataset_attribute_ids based on the order of dataset_attributes
dataset_attribute_ids.sort(key=lambda x: dataset_attributes.index(x['DataAttributeName']))
# Get all values of each dictionary in dataset_attribute_ids
dataset_attribute_ids_values = [list(d.values())[0] for d in dataset_attribute_ids]
# Check if the query returned any results
if not dataset_attribute_ids:
raise HTTPException(status_code=500, detail="No dataset attributes found")
return dataset_attribute_ids_values
class DataAttributeValues:
def __init__(self,db=None) -> None:
if db is not None:
log.info("inside dataattribute values loop")
self.ModelWorkBench= db
else:
ModelWorkBenchconnection = DataBase_WB()
self.ModelWorkBench=ModelWorkBenchconnection.db
self.collection = self.ModelWorkBench["DataAttributesValues"]
def find(self,dataset_id: float, dataset_attribute_ids: list, batch_id: float):
# Check if dataset_id is not None and is a string
if dataset_id is None or not isinstance(dataset_id, float):
raise HTTPException(status_code=500, detail="Data ID must be a non-empty float")
log.info(f"{dataset_attribute_ids}dataset_attribute_ids")
# Try to find the dataset by dataset_id in the database
# Query the database for the dataset with the given ID and DataAttributeId
dataset_attributes_result = list(self.collection.find({"DataId": dataset_id, "DataAttributeId": {"$in": dataset_attribute_ids}, "BatchId": batch_id}, {"_id": 0, "DataAttributeValues": 1, "DataAttributeId": 1}))
#dataset_attributes_result = list(DataAttributeValues.collection.find({"DataId": dataset_id, "DataAttributeId": {"$in": dataset_attribute_ids}, "IsActive": IsActive}, {"_id": 0, "DataAttributeValues": 1, "DataAttributeId": 1}))
# Sort dataset_attributes_result based on the order of dataset_attribute_ids
dataset_attributes_result.sort(key=lambda x: dataset_attribute_ids.index(x['DataAttributeId']))
# Check if the query returned any results
if not dataset_attributes_result:
raise HTTPException(status_code=500, detail="No dataset attributes found")
# Extract the ModelAttributeId values from the query results
data_attribute_values = [item['DataAttributeValues'] for item in dataset_attributes_result]
return data_attribute_values
def update(self,dataset_id, value:dict):
log.info(f"{dataset_id}data set value")
#update the document with the given collection
update_result = self.collection.update_many({"DataId": dataset_id}, {"$set": value})
log.info("success")
#check if the update was acknowledged
if not update_result.acknowledged:
raise RuntimeError(f"Failed to update document with batchId {dataset_id}")
return update_result.acknowledged
def checkValue(self,dataset_id: float, dataset_attribute_ids: list, batch_id: float):
# Check if dataset_id is not None and is a string
if dataset_id is None or not isinstance(dataset_id, float):
raise HTTPException(status_code=500, detail="No dataset found")
log.info(f"{dataset_attribute_ids}dataset_attribute_ids")
# Try to find the dataset by dataset_id in the database
# Query the database for the dataset with the given ID and DataAttributeId
dataset_attributes_result = list(self.collection.find({"DataId": dataset_id, "DataAttributeId": {"$in": dataset_attribute_ids}, "BatchId": batch_id}, {"_id": 0, "DataAttributeValues": 1, "DataAttributeId": 1}))
#dataset_attributes_result = list(DataAttributeValues.collection.find({"DataId": dataset_id, "DataAttributeId": {"$in": dataset_attribute_ids}, "IsActive": IsActive}, {"_id": 0, "DataAttributeValues": 1, "DataAttributeId": 1}))
# Sort dataset_attributes_result based on the order of dataset_attribute_ids
dataset_attributes_result.sort(key=lambda x: dataset_attribute_ids.index(x['DataAttributeId']))
# Check if the query returned any results
if not dataset_attributes_result:
return False
# Extract the ModelAttributeId values from the query results
return True |