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