From 048127c9d55652ced3d8f2e766cbf42767c1cc02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ch=C3=A9rifBALDE?= Date: Tue, 6 Sep 2022 21:50:55 +0200 Subject: [PATCH] 06/09/22 - 21h30 --- class_mgt.py | 4 ++ main.py | 12 ++++++ prj_common.py | 37 ++++++++++++++++- statistics.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ wrapper.py | 3 ++ 5 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 statistics.py diff --git a/class_mgt.py b/class_mgt.py index db36837..876f1b2 100644 --- a/class_mgt.py +++ b/class_mgt.py @@ -1993,6 +1993,10 @@ def get_class_by_metier(diction): # mycommon.myprint(str(retVal)) user = retVal RetObject.append(JSONEncoder().encode(user)) + # print(" 22222 ") + retVal_for_stat = retVal + retVal_for_stat['search_by_metier'] = str(my_metier) + mycommon.InsertStatistic(retVal_for_stat, "summary", mydata) return True, RetObject diff --git a/main.py b/main.py index 031cdc6..a1cd11a 100644 --- a/main.py +++ b/main.py @@ -27,6 +27,7 @@ import test_perso as TP import ela_factures_mgt as invoice import product_service as PS import ela_factures_mgt as factures +import statistics as Stat app = Flask(__name__) cors = CORS(app, resources={r"/foo": {"origins": "*"}}) @@ -1369,6 +1370,17 @@ def AutoamticCreateInvoice(): status, message = factures.AutoamticCreateInvoice() return jsonify(status=status, message=message) +""" +API de excuter une fonction de statistique +""" +@app.route('/myclass/api/GetStat_class_view/', methods=['GET','POST']) +@crossdomain(origin='*') +def GetStat_class_view(): + # On recupere le corps (payload) de la requete + payload = request.form.to_dict() + print(" ### payload = ", str(payload)) + status, message = Stat.GetStat_class_view() + return jsonify(status=status, message=message) diff --git a/prj_common.py b/prj_common.py index 48a3b0c..5aab9ab 100644 --- a/prj_common.py +++ b/prj_common.py @@ -1706,4 +1706,39 @@ def TryToDateYYYMMDD(mydate): return True, datetime.strptime(mydate, '%Y-%m-%d') except ValueError: - return False, False \ No newline at end of file + return False, False + + +""" +Cette fonction prend une formation (myclass) et l'insert dans +la table de statistique + - diction_class : les données de la formation + - type_view : type d'affichage ("basique", "detail", etc) + - user_location : les coordonnées du demandeur +""" +def InsertStatistic(diction_class, type_view, user_location): + try: + + mydata = {} + mydata['internal_url'] = diction_class['internal_url'] + mydata['date_update'] = datetime.now() + mydata['type_view'] = str(type_view) + + mydict_combined = {**diction_class, **mydata, **user_location} + mydict_combined['date_update'] = str(datetime.now()) + mydict_combined['type_view'] = "summary" + + + coll_name = MYSY_GV.dbname['user_recherche_result'] + ret_val = coll_name.insert_one(mydict_combined) + if (ret_val is False): + myprint( + str(inspect.stack()[0][3]) + " - WARNING : Impossbile d'inserer la formation " + str(diction_class['internal_url']) + " dans les statistiques") + + + return True + + except Exception as e: + exc_type, exc_obj, exc_tb = sys.exc_info() + print(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) + return False diff --git a/statistics.py b/statistics.py new file mode 100644 index 0000000..56eee50 --- /dev/null +++ b/statistics.py @@ -0,0 +1,107 @@ +''' +Ce fichier traite tout ce qui est liée aux requetes de statistique + +''' +import pymongo +from pymongo import MongoClient +import json +from bson import ObjectId +import re +from datetime import datetime +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 + + + +class JSONEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, ObjectId): + return str(o) + return json.JSONEncoder.default(self, o) + + +def GetStat_class_view(): + 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, + """ + + QUERY_BY = "jour" + SUBSTR = 0 + + if (QUERY_BY == 'heure'): + SUBSTR = 13 + elif( QUERY_BY == 'jour'): + SUBSTR = 10 + elif (QUERY_BY == 'mois'): + SUBSTR = 7 + + + + pipe2 = [{ + '$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" \ No newline at end of file diff --git a/wrapper.py b/wrapper.py index 086ce9e..e908701 100644 --- a/wrapper.py +++ b/wrapper.py @@ -212,6 +212,9 @@ def get_all_class(diction): user['id'] = str(val_tmp) insertObject.append(JSONEncoder().encode(user)) + #print(" 1111111 ") + mycommon.InsertStatistic(x, "summary",mydata) + tab_training_inzone.append(JSONEncoder().encode(user)) val_tmp = val_tmp + 1