import pymongo
import datetime
import sys
sys.path.insert(0, '/home/bneron/Mobyle/mobyle_statistics')

from mobyle_statistics import *


client = pymongo.MongoClient('localhost', 27017, w=1, j=True)
db_name = 'mobyle_1'
db = client[db_name]
col = db.logs
start = datetime(2014, 1, 1)
stop = datetime(2015, 1, 1)

users = uniq_user(col, start=start, stop=stop)
pasteuriens = uniq_user(col, start=start, stop=stop, pasteurien=True, foreigner=False)
foreigners = uniq_user(col, start=start, stop=stop, pasteurien=False, foreigner=True)

jobs = count_jobs(col, start=start, stop=stop)
jobs_pa = count_jobs(col, start=start, stop=stop, pasteurien=True, foreigner=False)
jobs_fo = count_jobs(col, start=start, stop=stop, pasteurien=False, foreigner=True)

uc = user_count_per_service(col, start=start, stop=stop)
uc_fo = user_count_per_service(col, start=start, stop=stop, foreigner=True, pasteurien=False)
uc_pa = user_count_per_service(col, start=start, stop=stop, foreigner=False, pasteurien=True)

jc = jobs_count_per_service(col, start=start, stop=stop, pasteurien=True, foreigner=True)
jc_fo = jobs_count_per_service(col, start=start, stop=stop, pasteurien=False, foreigner=True)
jc_pa = jobs_count_per_service(col, start=start, stop=stop, pasteurien=True, foreigner=False)


job_count = {}
for p in jc:
    job_count[p['_id']] = {'all': p['count']}
for p in jc_fo:
    if p['_id'] in job_count:
        job_count[p['_id']]['for'] = p['count']
    else:
        job_count[p['_id']] = {'for': p['count']}
for p in jc_pa:
    if p['_id'] in job_count:
        job_count[p['_id']]['past'] = p['count']
    else:
        job_count[p['_id']] = {'past': p['count']}

user_count = {}
for s_name, count in uc.items():
    user_count[s_name] = {'all': len(count)}
for s_name, count in uc_fo.items():
    if s_name in user_count:
        user_count[s_name]['for'] = len(count)
    else:
        user_count[s_name] = {'for': len(count)}

for s_name, count in uc_pa.items():
    if s_name in user_count:
        user_count[s_name]['past'] = len(count)
    else:
        user_count[s_name] = {'past': len(count)}

#############################

print("Distinct users")
print("pasteuriens, {}".format(len(pasteuriens)))
print("foreigners, {}".format(len(foreigners)))
print("all, {}".format(len(users)))
print()
print("programs + workflows used")
print("pasteuriens, {}".format(len(uc_pa)))
print("foreigners, {}".format(len(uc_fo)))
print("all, {}".format(len(uc)))
print()
print("jobs launched")
print("pasteuriens, {}".format(jobs_pa))
print("foreigners, {}".format(jobs_fo))
print("all, {}".format(jobs))
print()
print("details of jobs launched")
print("service, jobs pasteuriens, jobs foreigners, jobs")

for item in sorted(job_count.items(), key=lambda x: x[1]['all'], reverse=True):
    print("{}, {}, {}, {}".format(item[0],
                                  item[1].get('past') if item[1].get('past') else 0,
                                  item[1].get('for') if item[1].get('for') else 0,
                                  item[1].get('all') if item[1].get('all') else 0
                                 )
          )
print()
print("Distinct users per service")
print("service, jobs pasteuriens, jobs foreigners, jobs")
for item in sorted(user_count.items(), key=lambda x: x[1]['all'], reverse=True):
    print("{}, {}, {}, {}".format(item[0],
                                  item[1].get('past') if item[1].get('past') else 0,
                                  item[1].get('for') if item[1].get('for') else 0,
                                  item[1].get('all') if item[1].get('all') else 0
                                 )
          )

@@ -8,16 +8,30 @@
 pymongo%0A
+from datetime 
 import d
@@ -38,16 +38,50 @@
 atetime%0A
+import urllib.request%0Aimport json%0A
 import s
@@ -178,9 +178,77 @@
 ort 
-*
+uniq_user, count_jobs, user_count_per_service, jobs_count_per_service
 %0A%0A%0Ac
@@ -2210,16 +2210,728 @@
 ount)%7D%0A%0A
+%0Aresp = urllib.request.urlopen(%22http://mobyle.pasteur.fr/cgi-bin/net_services.py%22)%0Aif resp.getcode() == 200:%0A    all_services = json.loads(resp.readline().decode('utf-8'))%0A    all_services = all_services.keys()%0A    unused_services = set(all_services) - set(job_count.keys())%0A    for service_name in unused_services:%0A        job_count%5Bservice_name%5D = %7B'all':0, 'for': 0, 'past': 0%7D%0A%0A    unused_services = set(all_services) - set(user_count.keys())%0A    for service_name in unused_services:%0A        user_count%5Bservice_name%5D = %7B'all':0 , 'for': 0, 'past': 0%7D%0A%0Aelse:%0A    print(%22request to Mobyle failed: %7B%7D: %7B%7D%22.format(resp.getcode(), resp.msg), file=sys.stderr)%0A    print(%22unused services will not take in account%22)%0A
 ########

add in stat service which were never used