Skip to content

Commit

Permalink
Search entity updated logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyadinesh0412 committed Dec 22, 2023
1 parent 4c22a3e commit b64bf1e
Showing 1 changed file with 56 additions and 53 deletions.
109 changes: 56 additions & 53 deletions urgent_data_metrics/imp_project_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
config.read(config_path[0] + "/config.ini")
sys.path.append(config.get("COMMON", "cloud_module_path"))

from cloud import MultiCloud
# from cloud import MultiCloud

cloud_init = MultiCloud()
# cloud_init = MultiCloud()
formatter = logging.Formatter('%(asctime)s - %(levelname)s')

successLogger = logging.getLogger('success log')
Expand Down Expand Up @@ -186,7 +186,8 @@ def searchEntities(url,ids_list):
}
})
response = requests.request("POST", url, headers=headers, data=payload)

delta_ids = []
entity_name_mapping = {}
if response.status_code == 200:
# convert the response to dictionary
response = response.json()
Expand All @@ -203,60 +204,61 @@ def searchEntities(url,ids_list):

# check with the input data to make sure there are no missing data from loc search
delta_ids = list(set(ids_list) - set(ids_from_api))

# if there are missing data , fetch the data from mongo
if len(delta_ids) > 0 :
# aggregate mongo query to fetch data from mongo
delta_loc = projectsCollec.aggregate([
{
"$match": {
"userProfile.userLocations": {
"$elemMatch": {
"id": {
"$in": delta_ids
}
else :
delta_ids = ids_list
# if there are missing data , fetch the data from mongo
if len(delta_ids) > 0 :
# aggregate mongo query to fetch data from mongo
delta_loc = projectsCollec.aggregate([
{
"$match": {
"userProfile.userLocations": {
"$elemMatch": {
"id": {
"$in": delta_ids
}
}
}
},
{
"$unwind": "$userProfile.userLocations"
},
{
"$match": {
"userProfile.userLocations.id": {
"$in": delta_ids
}
}
},
{
"$sort": {
"createdAt": -1
}
},
{
"$group": {
"_id": "$userProfile.userLocations.id",
"mostRecentDocument": { "$first": "$$ROOT" }
}
},
{
"$replaceRoot": { "newRoot": "$mostRecentDocument" }
},
{
"$project": {
"_id": 1,
"userProfile.userLocations": 1
}
},
{
"$unwind": "$userProfile.userLocations"
},
{
"$match": {
"userProfile.userLocations.id": {
"$in": delta_ids
}
}
])

# add delta entities to master variable
for index in delta_loc:
entity_name_mapping[index['userProfile']["userLocations"]['id']] = index['userProfile']["userLocations"]['name']
return entity_name_mapping
else :
},
{
"$sort": {
"createdAt": -1
}
},
{
"$group": {
"_id": "$userProfile.userLocations.id",
"mostRecentDocument": { "$first": "$$ROOT" }
}
},
{
"$replaceRoot": { "newRoot": "$mostRecentDocument" }
},
{
"$project": {
"_id": 1,
"userProfile.userLocations": 1
}
}
])
# add delta entities to master variable
for index in delta_loc:
entity_name_mapping[index['userProfile']["userLocations"]['id']] = index['userProfile']["userLocations"]['name']
return entity_name_mapping
else:
return False

except Exception as e:
errorLogger.error(e,exc_info=True)

Expand Down Expand Up @@ -366,7 +368,7 @@ def searchEntities(url,ids_list):
os.rename(f'{path}' + f'{result[0]}', f'{path}' + 'data.csv')

# Uploading file to Cloud
cloud_init.upload_to_cloud(blob_Path = blob_path, local_Path = local_path, file_Name = 'data.csv')
# cloud_init.upload_to_cloud(blob_Path = blob_path, local_Path = local_path, file_Name = 'data.csv')

print("file got uploaded to Cloud.")
print("DONE")
Expand All @@ -377,4 +379,5 @@ def searchEntities(url,ids_list):
except Exception as e:
# Log the custom error message along with exception information
error_message = "API error: {}".format(e)
errorLogger.error(error_message, exc_info=True)
errorLogger.error(error_message, exc_info=True)

0 comments on commit b64bf1e

Please sign in to comment.