diff --git a/main.py b/main.py index a1cd11a..c8a9796 100644 --- a/main.py +++ b/main.py @@ -1383,6 +1383,19 @@ def GetStat_class_view(): 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__': print(" debut api") diff --git a/statistics.py b/statistics.py index 56eee50..2cf13f9 100644 --- a/statistics.py +++ b/statistics.py @@ -18,7 +18,7 @@ from pymongo import ReturnDocument import GlobalVariable as MYSY_GV from math import isnan import GlobalVariable as MYSY_GV - +from datetime import timedelta class JSONEncoder(json.JSONEncoder): @@ -104,4 +104,84 @@ def GetStat_class_view(): 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" \ No newline at end of file + 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"