566 lines
23 KiB
Python
566 lines
23 KiB
Python
"""
|
|
Ce fichier contient les requetes utilisées dans les tableaux de bord des session
|
|
"""
|
|
import dateutil
|
|
import pymongo
|
|
from pymongo import MongoClient
|
|
import json
|
|
from bson import ObjectId
|
|
import re
|
|
from datetime import datetime, timezone
|
|
import prj_common as mycommon
|
|
import secrets
|
|
import inspect
|
|
import sys, os
|
|
import csv
|
|
import pandas as pd
|
|
from pymongo import ReturnDocument
|
|
import GlobalVariable as MYSY_GV
|
|
from math import isnan
|
|
import GlobalVariable as MYSY_GV
|
|
from datetime import timedelta
|
|
from datetime import timedelta
|
|
|
|
|
|
"""
|
|
Recuperation des sessions avec le taux de remplissage
|
|
"""
|
|
def Get_Qery_List_Session_Data(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'session_start_date', 'session_end_date' ]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', ]
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
filt_session_start_date = ""
|
|
if ("session_start_date" in diction.keys() and diction['session_start_date']):
|
|
filt_session_start_date = str(diction['session_start_date'])[0:10]
|
|
local_status = mycommon.CheckisDate(filt_session_start_date)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le filtre : 'date de debut de session' n'est pas au format jj/mm/aaaa.")
|
|
return False, " Le filtre : 'date de debut de session' n'est pas au format jj/mm/aaaa."
|
|
|
|
filt_session_end_date = ""
|
|
if ("session_end_date" in diction.keys() and diction['session_end_date']):
|
|
filt_session_end_date = str(diction['session_end_date'])[0:10]
|
|
local_status = mycommon.CheckisDate(filt_session_end_date)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le filtre : 'date de fin de session' n'est pas au format jj/mm/aaaa.")
|
|
return False, " Le filtre : 'date de fin de session' n'est pas au format jj/mm/aaaa."
|
|
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
qery = [{'partner_owner_recid':str(my_partner['recid'])}, {'code_session':1, 'session_etape':1, 'class_internal_url':1, 'distantiel':1,
|
|
'presentiel':1, 'nb_participant':1, 'formateur_id':1,
|
|
'date_debut':1, 'date_fin':1}]
|
|
|
|
|
|
print(" #### qery = ", qery)
|
|
for retval in MYSY_GV.dbname['session_formation'].find({'partner_owner_recid':str(my_partner['recid'])}, {'code_session':1, 'session_etape':1, 'class_internal_url':1, 'distantiel':1,
|
|
'presentiel':1, 'nb_participant':1, 'formateur_id':1, 'date_debut':1, 'date_fin':1}):
|
|
user = retval
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
|
|
nb_participant = "1"
|
|
if ("nb_participant" in retval.keys()):
|
|
nb_participant = retval['nb_participant']
|
|
|
|
user['nb_participant'] = nb_participant
|
|
|
|
## Recuperation du nombre d'inscrits
|
|
Count_Inscrit = MYSY_GV.dbname['inscription'].count_documents(
|
|
{'session_id': str(retval['_id']), 'class_internal_url': str(retval['class_internal_url']),
|
|
'status': "1"})
|
|
user['nb_inscrit'] = str(Count_Inscrit)
|
|
|
|
## Recuperation du nombre de preinscrits
|
|
Count_Preinscrit = MYSY_GV.dbname['inscription'].count_documents(
|
|
{'session_id': str(retval['_id']), 'class_internal_url': str(retval['class_internal_url']),
|
|
'status': "0"})
|
|
|
|
user['nb_preinscrit'] = str(Count_Preinscrit)
|
|
|
|
taux_disponibilite = "100"
|
|
taux_remplissage = "0"
|
|
if (str(nb_participant) != "0"):
|
|
taux_remplissage = (round(int(mycommon.tryInt(Count_Inscrit)) / int(mycommon.tryInt(nb_participant)),
|
|
2))*100
|
|
|
|
taux_disponibilite = 100 - taux_remplissage
|
|
|
|
|
|
|
|
user['taux_remplissage'] = str(taux_remplissage)
|
|
user['taux_remplissageColor'] = "green"
|
|
|
|
user['taux_disponibilite'] = str(taux_disponibilite)
|
|
user['taux_disponibiliteColor'] = "yellow"
|
|
|
|
|
|
if (filt_session_start_date and filt_session_end_date):
|
|
# Si on a un filtre sur la date debut et de fin de session
|
|
if ((datetime.strptime(str(retval['date_debut'][0:10]).strip(), '%d/%m/%Y') >= datetime.strptime(
|
|
str(filt_session_start_date).strip(), '%d/%m/%Y'))
|
|
and
|
|
(datetime.strptime(str(retval['date_fin'][0:10]).strip(),
|
|
'%d/%m/%Y') <= datetime.strptime(str(filt_session_end_date).strip(),
|
|
'%d/%m/%Y'))
|
|
):
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
|
|
elif (filt_session_start_date):
|
|
# Si on a un filtre uniquement sur la date de debut de session
|
|
if ((datetime.strptime(str(retval['date_debut'][0:10]).strip(), '%d/%m/%Y') >= datetime.strptime(
|
|
str(filt_session_start_date).strip(), '%d/%m/%Y'))):
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
|
|
elif (filt_session_end_date):
|
|
# Si on a un filtre uniquement sur la date de fin session
|
|
if ((datetime.strptime(str(retval['date_fin'][0:10]).strip(), '%d/%m/%Y') <= datetime.strptime(
|
|
str(filt_session_end_date).strip(), '%d/%m/%Y'))):
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
|
|
else:
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
|
|
|
|
#RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
#print(" #### RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer les données "
|
|
|
|
|
|
"""
|
|
Recuperation des sessions par formateur sur une periode
|
|
"""
|
|
|
|
def Get_Qery_Session_By_Trainer_By_Periode(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'session_start_date', 'session_end_date' ]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'session_start_date', 'session_end_date' ]
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
filt_session_start_date = ""
|
|
if ("session_start_date" in diction.keys() and diction['session_start_date']):
|
|
filt_session_start_date = str(diction['session_start_date'])[0:10]
|
|
local_status = mycommon.CheckisDate(filt_session_start_date)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le filtre : 'date de debut de session' n'est pas au format jj/mm/aaaa.")
|
|
return False, " Le filtre : 'date de debut de session' n'est pas au format jj/mm/aaaa."
|
|
|
|
filt_session_end_date = ""
|
|
if ("session_end_date" in diction.keys() and diction['session_end_date']):
|
|
filt_session_end_date = str(diction['session_end_date'])[0:10]
|
|
local_status = mycommon.CheckisDate(filt_session_end_date)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le filtre : 'date de fin de session' n'est pas au format jj/mm/aaaa.")
|
|
return False, " Le filtre : 'date de fin de session' n'est pas au format jj/mm/aaaa."
|
|
|
|
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
filt_session_start_date_ISODATE = datetime.strptime(str(filt_session_start_date), '%d/%m/%Y')
|
|
filt_session_end_date_ISODATE = datetime.strptime(str(filt_session_end_date), '%d/%m/%Y')
|
|
|
|
|
|
|
|
qery = [{'partner_owner_recid':str(my_partner['recid'])}, {'code_session':1, 'session_etape':1, 'class_internal_url':1, 'distantiel':1,
|
|
'presentiel':1, 'nb_participant':1, 'formateur_id':1,
|
|
'date_debut':1, 'date_fin':1}]
|
|
|
|
qery_match = {'$and': [ {"partner_owner_recid":str(my_partner['recid']) }, {"valide":'1'}, {"formateur_id": { "$exists": 'true' } }, { 'mysy_date_debut_session': { '$gte': filt_session_start_date_ISODATE, '$lte': filt_session_end_date_ISODATE } } , ]}
|
|
|
|
pipe_qry = ([
|
|
{"$addFields": {
|
|
"mysy_date_debut_session": {
|
|
'$dateFromString': {
|
|
'dateString': '$date_debut',
|
|
'format': "%d/%m/%Y"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
|
|
{'$match': qery_match},
|
|
{'$lookup': {
|
|
'from': 'ressource_humaine',
|
|
"let": {'formateur_id': "$formateur_id", 'partner_owner_recid': '$partner_owner_recid'},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$formateur_id",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$partner_recid", '$$partner_owner_recid']}
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
],
|
|
'as': 'ressource_humaine'
|
|
}
|
|
},
|
|
{'$group': {
|
|
'_id': {
|
|
"Formateur_id": "$formateur_id",
|
|
"Formateur_nom": "$ressource_humaine.nom",
|
|
"Formateur_prenom": "$ressource_humaine.prenom",
|
|
"mois_session": {'$substr': ["$date_debut", 3, 2]},
|
|
"annee_session": {'$substr': ["$date_debut", 6, 4]},
|
|
},
|
|
'count': {'$count': {}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
'$sort': {'count': -1}
|
|
},
|
|
|
|
])
|
|
|
|
|
|
|
|
print(" ### Get_Qery_Session_By_Trainer_By_Periode ici pipe_qry = ", pipe_qry)
|
|
|
|
|
|
for retval in MYSY_GV.dbname['session_formation'].aggregate(pipe_qry):
|
|
|
|
user = {}
|
|
user["Formateur_id"] = retval['_id']['Formateur_id']
|
|
if( len(retval['_id']['Formateur_nom']) > 0 ):
|
|
user["Formateur_nom"] = retval['_id']['Formateur_nom'][0]
|
|
else:
|
|
user["Formateur_nom"] = ""
|
|
|
|
if (len(retval['_id']['Formateur_nom']) > 0):
|
|
user["Formateur_prenom"] = retval['_id']['Formateur_prenom'][0]
|
|
else:
|
|
user["Formateur_prenom"] = ""
|
|
|
|
user["mois_session"] = retval['_id']['mois_session']
|
|
user["annee_session"] = retval['_id']['annee_session']
|
|
user["nb"] = retval['count']
|
|
|
|
if( str(user["Formateur_nom"]).strip() == "" and str(user["Formateur_prenom"]).strip() == ""):
|
|
user["Formateur_nom_prenom_mois_annee"] = str(user["annee_session"]) + "/" + str(user["mois_session"]) + " - Inconnu "
|
|
else:
|
|
user["Formateur_nom_prenom_mois_annee"] = str(user["annee_session"]) + "/" + str(
|
|
user["mois_session"]) + " - " + str(user["Formateur_nom"]) + " " + str(user["Formateur_prenom"])
|
|
|
|
val_tmp = val_tmp + 1
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
#print(" #### RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer les données "
|
|
|
|
|
|
"""
|
|
Repartition du pourcentage de session par formateur
|
|
"""
|
|
def Get_Qery_Session_Repartition_Session_By_Trainer_By_Periode(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'session_start_date', 'session_end_date' ]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'session_start_date', 'session_end_date' ]
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
filt_session_start_date = ""
|
|
if ("session_start_date" in diction.keys() and diction['session_start_date']):
|
|
filt_session_start_date = str(diction['session_start_date'])[0:10]
|
|
local_status = mycommon.CheckisDate(filt_session_start_date)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le filtre : 'date de debut de session' n'est pas au format jj/mm/aaaa.")
|
|
return False, " Le filtre : 'date de debut de session' n'est pas au format jj/mm/aaaa."
|
|
|
|
filt_session_end_date = ""
|
|
if ("session_end_date" in diction.keys() and diction['session_end_date']):
|
|
filt_session_end_date = str(diction['session_end_date'])[0:10]
|
|
local_status = mycommon.CheckisDate(filt_session_end_date)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le filtre : 'date de fin de session' n'est pas au format jj/mm/aaaa.")
|
|
return False, " Le filtre : 'date de fin de session' n'est pas au format jj/mm/aaaa."
|
|
|
|
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
filt_session_start_date_ISODATE = datetime.strptime(str(filt_session_start_date), '%d/%m/%Y')
|
|
filt_session_end_date_ISODATE = datetime.strptime(str(filt_session_end_date), '%d/%m/%Y')
|
|
|
|
|
|
|
|
qery_match = {'$and': [{"partner_owner_recid":str(my_partner['recid']) }, {"valide":'1'}, { 'mysy_date_debut_session': { '$gte': filt_session_start_date_ISODATE, '$lte': filt_session_end_date_ISODATE } } , ]}
|
|
|
|
# Recuperation du nombre total de session sur la periode
|
|
pipe_qry_nb_record = ([
|
|
{"$addFields": {
|
|
"mysy_date_debut_session": {
|
|
'$dateFromString': {
|
|
'dateString': '$date_debut',
|
|
'format': "%d/%m/%Y"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{'$match': qery_match},
|
|
|
|
{'$group': {'_id': 'null', 'nb_record': {'$sum': 1}}},
|
|
{'$project': {'_id': 0}}
|
|
])
|
|
|
|
|
|
|
|
nb_total_session = 0
|
|
Nb_Record_RetObject = []
|
|
for retval in MYSY_GV.dbname['session_formation'].aggregate(pipe_qry_nb_record):
|
|
|
|
if( "nb_record" in retval.keys() and retval['nb_record']):
|
|
nb_total_session = mycommon.tryInt(retval['nb_record'])
|
|
|
|
print(" #### nb_total_session = ",str(nb_total_session))
|
|
if(nb_total_session == 0 ):
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Aucune session de formation valide ")
|
|
return False, " Aucune session de formation valide "
|
|
|
|
pipe_qry = ([
|
|
{"$addFields": {
|
|
"mysy_date_debut_session": {
|
|
'$dateFromString': {
|
|
'dateString': '$date_debut',
|
|
'format': "%d/%m/%Y"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
|
|
{'$match': qery_match},
|
|
{'$lookup': {
|
|
'from': 'ressource_humaine',
|
|
"let": {'formateur_id': "$formateur_id", 'partner_owner_recid': '$partner_owner_recid'},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$formateur_id",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$partner_recid", '$$partner_owner_recid']}
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
],
|
|
'as': 'ressource_humaine'
|
|
}
|
|
},
|
|
{'$group': {
|
|
'_id': {
|
|
"Formateur_id": "$formateur_id",
|
|
"Formateur_nom": "$ressource_humaine.nom",
|
|
"Formateur_prenom": "$ressource_humaine.prenom",
|
|
|
|
},
|
|
'count': {'$count': {}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
'$sort': {'count': -1}
|
|
},
|
|
|
|
])
|
|
|
|
|
|
|
|
print(" ### Get_Qery_Session_Repartition_Session_By_Trainer_By_Periode ici pipe_qry = ", pipe_qry)
|
|
|
|
|
|
for retval in MYSY_GV.dbname['session_formation'].aggregate(pipe_qry):
|
|
print(" ### retval = ", retval)
|
|
if( "Formateur_id" in retval['_id'].keys() ):
|
|
user = {}
|
|
user["Formateur_id"] = retval['_id']['Formateur_id']
|
|
if( len(retval['_id']['Formateur_nom']) > 0 ):
|
|
user["Formateur_nom"] = retval['_id']['Formateur_nom'][0]
|
|
else:
|
|
user["Formateur_nom"] = ""
|
|
|
|
if (len(retval['_id']['Formateur_nom']) > 0):
|
|
user["Formateur_prenom"] = retval['_id']['Formateur_prenom'][0]
|
|
else:
|
|
user["Formateur_prenom"] = ""
|
|
|
|
|
|
user["nb"] = retval['count']
|
|
|
|
user["pourcentage"] = str(round(mycommon.tryInt( str(user["nb"] )) / nb_total_session, 2))
|
|
|
|
if( str(user["Formateur_nom"]).strip() == "" and str(user["Formateur_prenom"]).strip() == ""):
|
|
user["Formateur_nom_prenom"] = "Inconnu "
|
|
user["id"] = "Inconnu "
|
|
else:
|
|
user["Formateur_nom_prenom"] = str(user["Formateur_nom"]) + " " + str(user["Formateur_prenom"])
|
|
user["id"] = str(user["Formateur_nom"]) + " " + str(user["Formateur_prenom"])
|
|
|
|
user["label"] = user["id"]
|
|
user["value"] = retval['count']
|
|
|
|
val_tmp = val_tmp + 1
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
#print(" #### RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer les données "
|