Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding stats #63

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Code/libs/mongo_db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,59 @@ def get_df(self, db_name: str, collection_name: str, query: dict):
print("Exception in getting meps documents in Mongo" + str(e))
return {"ged_insert_status": "Exception in getting meps documents in Mongo" + str(e)}

def get_df_grouped_by_month(self,
db_name: str,
collection_name: str,
query: dict,
date_start: datetime.datetime,
date_end: datetime.datetime):

def get_first_day_of_next_month(date):
""" Retourne le premier jour du mois suivant pour une date donnée. """
if date.month == 12:
return datetime.datetime(date.year + 1, 1, 1)
else:
return datetime.datetime(date.year, date.month + 1, 1)

def get_last_day_of_month(date):
""" Retourne le dernier jour d'un mois pour une date donnée. """
next_month_start = get_first_day_of_next_month(date)
return next_month_start - datetime.timedelta(days=1)

if date_start is None:
date_start = datetime.datetime(2010, 1, 1)
if date_end is None:
date_end = datetime.datetime.now()


client = self.__mongo_client__
db = client[db_name]
collection = db[collection_name]
cumulated_data = {};
try:

current_date = get_first_day_of_next_month(date_start)
dates = []

while current_date < date_end.replace(tzinfo=None):
start_of_month = current_date
end_of_month = get_last_day_of_month(current_date)
dates.append((start_of_month, end_of_month))
current_date = get_first_day_of_next_month(current_date)

for date in dates:
query['Date'] = {'$gte': date[0], '$lte': date[1]}
data = list(collection.find(query, {'_id': False}))
df = pd.DataFrame(data)

if 'Date' in df.columns:
df['Date'] = df['Date'].dt.strftime('%d/%m/%Y')

if data:
cumulated_data[date[0].strftime('%Y-%m-%d')] = df

return cumulated_data

except Exception as e:
print("Exception in getting meps documents in Mongo" + str(e))
return {"ged_insert_status": "Exception in getting meps documents in Mongo" + str(e)}
21 changes: 17 additions & 4 deletions Code/public_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Roles(str, Enum):
print(e)

ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
ACCESS_TOKEN_EXPIRE_MINUTES = 1440
JSON_EXTENSION = ".json"


Expand Down Expand Up @@ -615,9 +615,22 @@ def wild_card(word_to_search: str) :
query['Date'] = {"$lte": end_date}

try:
df = mongo_handler.get_df(db_name=db_name, collection_name=collection_name, query=query)

return meps_handler.get_stats(df)
df = mongo_handler.get_df(db_name=db_name,
collection_name=collection_name,
query=query)

dfs_grouped_by_month = mongo_handler.get_df_grouped_by_month(db_name=db_name,
collection_name=collection_name,
query=query,
date_start=start_date,
date_end=end_date)
dfs_grouped_by_month_stats = {}
for date in dfs_grouped_by_month.keys():
dfs_grouped_by_month_stats[date] = meps_handler.get_stats(dfs_grouped_by_month[date])

global_stats = meps_handler.get_stats(df)
global_stats['meps_stats_grouped_by_month'] = dfs_grouped_by_month_stats
return global_stats

except Exception as e:
print("get_meps_stats : " + str(e), flush=True)
Expand Down
Loading