|
""" |
|
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 pymongo.errors import InvalidDocument |
|
from fairness.config.logger import CustomLogger |
|
from fairness.dao.WorkBench.databaseconnection import DataBase_WB |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
load_dotenv() |
|
|
|
log = CustomLogger() |
|
|
|
|
|
|
|
class Model: |
|
def __init__(self,db=None) -> None: |
|
if db is not None: |
|
log.info("inside model loop") |
|
self.ModelWorkBench= db |
|
else: |
|
ModelWorkBenchconnection = DataBase_WB() |
|
self.ModelWorkBench=ModelWorkBenchconnection.db |
|
|
|
|
|
self.collection = self.ModelWorkBench["Model"] |
|
|
|
def find(self,model_id: float): |
|
|
|
if model_id is None or not isinstance(model_id, float): |
|
raise ValueError("Model ID must be a non-empty float") |
|
|
|
|
|
try: |
|
result = self.collection.find_one({"ModelId": model_id}, {"_id": 0, "ModelName": 1, "ModelData": 1, 'ModelEndPoint': 1}) |
|
|
|
except Exception as e: |
|
raise ValueError(f"Invalid model ID {model_id}: {str(e)}") |
|
|
|
return result |
|
|
|
class ModelAttributes: |
|
|
|
|
|
def __init__(self,db=None) -> None: |
|
if db is not None: |
|
log.info("inside model attributesloop") |
|
self.ModelWorkBench= db |
|
else: |
|
ModelWorkBenchconnection = DataBase_WB() |
|
self.ModelWorkBench=ModelWorkBenchconnection.db |
|
self.collection = self.ModelWorkBench["ModelAttributes"] |
|
|
|
def find(self,model_attributes: list): |
|
|
|
if model_attributes is None or not isinstance(model_attributes, list): |
|
raise ValueError("Model attributes must be a non-empty list") |
|
|
|
|
|
if not model_attributes: |
|
raise ValueError("Model attributes must not be an empty list") |
|
|
|
|
|
try: |
|
|
|
model_attribute_ids = list(self.collection.find({"ModelAttributeName": {"$in": model_attributes}}, {"_id": 0, "ModelAttributeId": 1, "ModelAttributeName": 1})) |
|
|
|
|
|
model_attribute_ids.sort(key=lambda x: model_attributes.index(x['ModelAttributeName'])) |
|
|
|
|
|
|
|
|
|
|
|
if not model_attribute_ids: |
|
raise ValueError(f"Unable to found attribute id(s) from the given list: {model_attributes}") |
|
|
|
|
|
model_attribute_ids_values = [item['ModelAttributeId'] for item in model_attribute_ids] |
|
except Exception as e: |
|
|
|
raise ValueError(str(e)) |
|
|
|
return model_attribute_ids_values |
|
|
|
class ModelAttributeValues: |
|
|
|
|
|
def __init__(self,db=None) -> None: |
|
if db is not None: |
|
log.info("inside model loop") |
|
self.ModelWorkBench= db |
|
else: |
|
ModelWorkBenchconnection = DataBase_WB() |
|
self.ModelWorkBench=ModelWorkBenchconnection.db |
|
self.collection = self.ModelWorkBench["ModelAttributesValues"] |
|
|
|
def find(self,batch_id: float, model_id: float, model_attribute_ids: list): |
|
|
|
if model_id is None or not isinstance(model_id, float): |
|
raise ValueError("Model ID must be a non-empty float") |
|
|
|
|
|
try: |
|
if batch_id is not None: |
|
|
|
log.info("executing") |
|
model_attributes = list(self.collection.find({"ModelId": model_id, "ModelAttributeId": {"$in": model_attribute_ids}, "BatchId": batch_id, 'IsActive':'Y'}, {"_id": 0, "ModelAttributeValues": 1, "ModelAttributeId": 1})) |
|
log.info("execution completed") |
|
else: |
|
|
|
model_attributes = list(self.collection.find({"ModelId": model_id, "ModelAttributeId": {"$in": model_attribute_ids}, 'IsActive':'Y'}, {"_id": 0, "ModelAttributeValues": 1, "ModelAttributeId": 1})) |
|
|
|
if not model_attributes: |
|
raise ValueError(f"No records found for model_id: {model_id}. Please check the model_id.") |
|
|
|
|
|
model_attributes.sort(key=lambda x: model_attribute_ids.index(x['ModelAttributeId'])) |
|
|
|
|
|
model_attribute_values = [item['ModelAttributeValues'] for item in model_attributes] |
|
|
|
except Exception as e: |
|
raise |
|
|
|
return model_attribute_values |
|
|
|
def update(self,batch_id,model_id,model_attribute_id,value:dict): |
|
|
|
try: |
|
|
|
update_result= self.collection.update_one({'BatchId': batch_id, 'ModelId': model_id,'ModelAttributeId': model_attribute_id, 'IsActive':'Y'}, {'$set': value}) |
|
|
|
|
|
if not update_result.acknowledged: |
|
raise RuntimeError(f"Failed to update document with ModelId {model_id} and ModelAttributeId {model_attribute_id}") |
|
|
|
return update_result.acknowledged |
|
except InvalidDocument: |
|
raise ValueError(f"Document is not a valid document with ModelId {model_id} and ModelAttributeId {model_attribute_id}") |