|
""" |
|
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() |
|
|
|
|
|
ModelWorkBenchconnection = DataBase_WB() |
|
ModelWorkBench=ModelWorkBenchconnection.db |
|
|
|
|
|
class SampleDataset: |
|
|
|
|
|
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 |
|
|
|
|
|
self.collection = ModelWorkBench["Dataset"] |
|
|
|
def find(self,Sample_Id: float): |
|
|
|
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") |
|
|
|
|
|
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: |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
self.collection = self.ModelWorkBench["Dataset"] |
|
|
|
def find(self,Dataset_Id: float): |
|
|
|
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") |
|
|
|
|
|
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): |
|
|
|
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") |
|
|
|
|
|
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: |
|
|
|
|
|
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 |
|
|
|
|
|
self.collection = self.ModelWorkBench["DataAttributes"] |
|
|
|
def find(self,dataset_attributes: list): |
|
|
|
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") |
|
|
|
|
|
if not dataset_attributes: |
|
raise HTTPException(status_code=500, detail="Dataset attribute(s) names must be a non-empty list") |
|
|
|
|
|
|
|
dataset_attribute_ids = list(self.collection.find({"DataAttributeName": {"$in": dataset_attributes}}, {"_id": 0, "DataAttributeId": 1, "DataAttributeName": 1})) |
|
|
|
|
|
dataset_attribute_ids.sort(key=lambda x: dataset_attributes.index(x['DataAttributeName'])) |
|
|
|
|
|
dataset_attribute_ids_values = [list(d.values())[0] for d in dataset_attribute_ids] |
|
|
|
|
|
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): |
|
|
|
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") |
|
|
|
|
|
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.sort(key=lambda x: dataset_attribute_ids.index(x['DataAttributeId'])) |
|
|
|
|
|
if not dataset_attributes_result: |
|
raise HTTPException(status_code=500, detail="No dataset attributes found") |
|
|
|
|
|
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_result = self.collection.update_many({"DataId": dataset_id}, {"$set": value}) |
|
log.info("success") |
|
|
|
|
|
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): |
|
|
|
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") |
|
|
|
|
|
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.sort(key=lambda x: dataset_attribute_ids.index(x['DataAttributeId'])) |
|
|
|
|
|
if not dataset_attributes_result: |
|
return False |
|
|
|
|
|
return True |