scripts/stats_cableurs.py
Michel Blockelet a2aaafca9b Ajout -e a stats_cableurs
Ajout du switch '-e' au script stats_cableurs, ce switch permet de
compter aussi les mois de Novembre et Decembre dans le decompte ...

darcs-hash:20071210095914-af6e7-89596b5ab75caf0f6002556fa25710f6b397032c.gz
2007-12-10 10:59:14 +01:00

157 lines
5.1 KiB
Python
Executable file

#!/usr/bin/python
# -*- mode: python; coding: utf-8 -*-
#
# $Id: stats_cableurs.py,v 1.2 2007-09-29 17:50:09 dimino Exp $
#
# stats_cableurs.py
# -----------------
#
# Copyright (C) 2007 François Bobot <bobot@crans.org>,
# Jeremie Dimino <jeremie@dimino.org>,
# Michel Blockelet <blockelet@crans.org
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
import sys
sys.path.append("/usr/scripts/gestion/")
from ldap_crans import crans_ldap
from config import ann_scol
from affich_tools import cprint
import time
db = crans_ldap()
date_debut_ann_scol = time.mktime((ann_scol, 8, 1, 0, 0, 0, 0, 0, 0))
paiement_ann_scol = "paiement+%d" % ann_scol
def donne_cableur(mois,adherent):
u""" Cherche le cableur qui a inscrit ou réinscrit un adhérent. """
cableur = None
# Est-ce qu'on recherche une inscription ou une reinscription?
if adherent.dateInscription() < date_debut_ann_scol:
action = 'reinscription'
action_filtre = paiement_ann_scol
else:
action = 'inscription'
action_filtre = 'inscription'
for hist in adherent.historique():
# On decoupe pour avoir un truc utilisable
champ = hist.replace(',', '').replace(': ', '').split(' ')
# On inspecte la date
date = champ[0].split('/')
if int(date[1]) in mois and int(date[2]) == ann_scol:
# Maintenant on regarde si l'action recherchee est ici
if action_filtre in champ[3:]:
return action, champ[2]
return action, None
def calcul_score(mois):
u""" Calcul le score de tous les cableurs ayant inscrit ou
réinscrit quelqu'un pour l'année en cours. """
liste = db.search("paiement=2007")['adherent']
score = {}
for adherent in liste:
action, cableur = donne_cableur(mois,adherent)
if cableur:
if not score.has_key(cableur):
score[cableur] = { 'inscription': 0,
'reinscription': 0 }
score[cableur][action] += 1
# On calcul le score total pour chaque cableur
for s in score.values():
s['total'] = s['inscription'] + s['reinscription']
return score
def classe(score={}):
u""" Retourne la liste des câbleurs classé par score total décroisant. """
cableurs = score.keys()
cableurs.sort(lambda x,y : cmp(score[x]['total'],score[y]['total']), reverse=True)
return cableurs
if __name__ == "__main__":
nb_affiche = 10
mois = [8,9,10]
if len(sys.argv) > 1:
if sys.argv[1] == '-e':
mois = [8,9,10,11]
if len(sys.argv) > 2:
try:
nb_affiche = int(sys.argv[2])
except:
nb_affiche = 10
else:
try:
nb_affiche = int(sys.argv[1])
except:
nb_affiche = 10
score = calcul_score(mois)
classement = classe(score)
if nb_affiche > 0:
classement = classement[0:nb_affiche]
# On cherche les noms des câbleurs parceque c'est quand même mieux
nom_reel = {}
for cableur in classement:
nom_reel[cableur] =db.search('uid=%s' % cableur)['adherent'][0].Nom()
# Calcul des statistiques
total_inscription = 0
total_reinscription = 0
total = 0
for s in score.values():
total_inscription += s['inscription']
total_reinscription += s['reinscription']
total += s['total']
cprint(u"""Statistiques globales:
- inscriptions: %(inscription)d
- réinscription: %(reinscription)d
- total: %(total)d
""" % { 'inscription': total_inscription,
'reinscription': total_reinscription,
'total': total }, newline=False)
# Calcul la longueur du nom le plus long
long_max = reduce(lambda m,c : max(m, len(c)), nom_reel.values(), len('cableur'))
colonnes = [('inscription', u'inscription'),
('reinscription', u'réinscription'),
('total', u'score total')]
# Titres des colonnes
ligne = "%-*s" % (long_max, u'câbleur')
for type, nom in colonnes:
ligne += " | %s" % nom
cprint(ligne)
# Ligne pour délimiter
ligne = ''.center(long_max, '-')
for type, nom in colonnes:
ligne += "-+-%s" % ''.center(len(nom), '-')
cprint(ligne)
# Statiqtiques par câbleur
for cableur in classement:
score_cableur = score[cableur]
ligne = "%-*s" % (long_max, nom_reel[cableur])
for type, nom in colonnes:
ligne += " | %*d" % (len(nom), score_cableur[type])
cprint(ligne)