07/09/22 - 16h30
parent
048127c9d5
commit
999c945449
13
main.py
13
main.py
|
@ -1383,6 +1383,19 @@ def GetStat_class_view():
|
||||||
return jsonify(status=status, message=message)
|
return jsonify(status=status, message=message)
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
API de excuter une fonction de statistique sur la semaine ecoulée
|
||||||
|
"""
|
||||||
|
@app.route('/myclass/api/GetStat_class_view_lastweek/', methods=['GET','POST'])
|
||||||
|
@crossdomain(origin='*')
|
||||||
|
def GetStat_class_view_lastweek():
|
||||||
|
# On recupere le corps (payload) de la requete
|
||||||
|
payload = request.form.to_dict()
|
||||||
|
print(" ### payload = ", str(payload))
|
||||||
|
status, message = Stat.GetStat_class_view_lastweek()
|
||||||
|
return jsonify(status=status, message=message)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(" debut api")
|
print(" debut api")
|
||||||
|
|
|
@ -18,7 +18,7 @@ from pymongo import ReturnDocument
|
||||||
import GlobalVariable as MYSY_GV
|
import GlobalVariable as MYSY_GV
|
||||||
from math import isnan
|
from math import isnan
|
||||||
import GlobalVariable as MYSY_GV
|
import GlobalVariable as MYSY_GV
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
|
||||||
class JSONEncoder(json.JSONEncoder):
|
class JSONEncoder(json.JSONEncoder):
|
||||||
|
@ -105,3 +105,83 @@ def GetStat_class_view():
|
||||||
exc_type, exc_obj, exc_tb = sys.exc_info()
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
||||||
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
||||||
return False, "Impossible de recuperer les stat"
|
return False, "Impossible de recuperer les stat"
|
||||||
|
|
||||||
|
|
||||||
|
def GetStat_class_view_lastweek():
|
||||||
|
try:
|
||||||
|
# collection
|
||||||
|
collection = MYSY_GV.dbname["user_recherche_result"]
|
||||||
|
|
||||||
|
"""
|
||||||
|
Pour avoir une requete qui groupe par jour, ou jour-heure
|
||||||
|
on doit jouer avec le
|
||||||
|
"date" : { $substr: [ "$date_update", 0, 25 ] } ==> ou 25 et la largeur de la coupure,
|
||||||
|
"""
|
||||||
|
|
||||||
|
thisweek = datetime.today() - timedelta(days=0)
|
||||||
|
thisweek_format = thisweek.strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
QUERY_BY = "jour"
|
||||||
|
SUBSTR = 0
|
||||||
|
|
||||||
|
if (QUERY_BY == 'heure'):
|
||||||
|
SUBSTR = 13
|
||||||
|
elif (QUERY_BY == 'jour'):
|
||||||
|
SUBSTR = 10
|
||||||
|
elif (QUERY_BY == 'mois'):
|
||||||
|
SUBSTR = 7
|
||||||
|
|
||||||
|
pipe2 = [
|
||||||
|
{'$match': {'date_update': {'$gt': str(thisweek_format)},
|
||||||
|
'owner':'ism'
|
||||||
|
}},
|
||||||
|
{
|
||||||
|
'$group': {
|
||||||
|
'_id': {
|
||||||
|
"INTERNAL URL": "$internal_url",
|
||||||
|
"OWNER": "$owner",
|
||||||
|
"Date_view": {"$substr": ["$date_update", 0, SUBSTR]},
|
||||||
|
},
|
||||||
|
'count': {'$count': {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$sort": {"count": -1}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
insertObject = []
|
||||||
|
for result in collection.aggregate(pipe2):
|
||||||
|
tmp_val = {}
|
||||||
|
|
||||||
|
# tab_training_id.append(str(result["_id"]))
|
||||||
|
# print(result['_id'])
|
||||||
|
|
||||||
|
if ("Date_view" in result['_id'].keys()):
|
||||||
|
if result['_id']['Date_view']:
|
||||||
|
print("Date_view = " + str(result['_id']['Date_view']))
|
||||||
|
tmp_val['Date_view'] = str(result['_id']['Date_view'])
|
||||||
|
|
||||||
|
if ("OWNER" in result['_id'].keys()):
|
||||||
|
if result['_id']['OWNER']:
|
||||||
|
print("Owner = " + str(result['_id']['OWNER']))
|
||||||
|
tmp_val['owner'] = str(result['_id']['OWNER'])
|
||||||
|
|
||||||
|
if ("INTERNAL URL" in result['_id'].keys()):
|
||||||
|
if result['_id']['INTERNAL URL']:
|
||||||
|
print("INTERNAL URL = " + str(result['_id']['INTERNAL URL']))
|
||||||
|
tmp_val['internal_url'] = str(result['_id']['INTERNAL URL'])
|
||||||
|
|
||||||
|
print("Nombre = " + str(result['count']))
|
||||||
|
tmp_val['nb_view'] = str(result['count'])
|
||||||
|
insertObject.append(JSONEncoder().encode(tmp_val))
|
||||||
|
|
||||||
|
# print(result)
|
||||||
|
|
||||||
|
return True, insertObject
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
||||||
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
||||||
|
return False, "Impossible de recuperer les stat"
|
||||||
|
|
Loading…
Reference in New Issue