06/09/22 - 21h30

master
ChérifBALDE 2022-09-06 21:50:55 +02:00 committed by cherif
parent ff17dda434
commit 048127c9d5
5 changed files with 162 additions and 1 deletions

View File

@ -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

12
main.py
View File

@ -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)

View File

@ -1706,4 +1706,39 @@ def TryToDateYYYMMDD(mydate):
return True, datetime.strptime(mydate, '%Y-%m-%d')
except ValueError:
return False, False
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

107
statistics.py Normal file
View File

@ -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"

View File

@ -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