#!/bin/bash /usr/scripts/python.sh # -*- coding: utf-8 -*- # # stats.py # ----------------- # # Copyright (C) 2013,2014 Raphaël-David Lasseri , # # 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, re, datetime, string import argparse from lc_ldap import shortcuts from gestion.config import ann_scol ### Appels à LDAP et tri initial sur l'année en cours db = shortcuts.lc_ldap_readonly() adherents=db.search(u'(&(paiement=%s)(aid=*))' % (ann_scol), sizelimit=2000) cableurs= db.search(u'(|(droits=cableur)(droits=nounou))') scores=[] historique=[] #### On prends les historiques de tout les adhérents def parse_historique(ligne): u"""Parse une ligne d'historique et renvoie [ligne parsée],action du cableur, date de l'action""" champ = ligne.value.replace(',','').replace(':','').split(' ') sdate = champ[0].split('/') date = datetime.date(int(sdate[2]),int(sdate[1]),int(sdate[0])) champ_action=champ[4] return champ,champ_action,date def actions_cableurs(): u"""Renvoie l'historique de tous les adherents et tri en fonction des actions éffectuées.""" for adherent in adherents: histo=adherent.get('historique',None) for j in range (0,len(histo)): champ=parse_historique(histo[j])[0] champ_action=parse_historique(histo[j])[1] date=parse_historique(histo[j])[2] if ((champ_action==u'inscription' or champ_action==u'paiement+'+str(ann_scol)) or len(champ)>5 and (champ[5]==u'paiement+'+str(ann_scol) or champ[5]==u'inscription')) and date > datetime.date(ann_scol,8,1) : historique.append(champ) return historique #### On parse l'historique et on trie def score_cableurs(): u"""Calcul le score de tout les câbleurs en fonction des actions effectuées """ for cableur in cableurs: inscriptions=reinscriptions=0 nom=cableur.get(u'nom',None)[0].value prenom=cableur.get(u'prenom',None)[0].value uid=cableur.get(u'uid',None)[0].value for l in range (0,len(historique)): histo_uid=historique[l][2] histo_action=historique[l][4] if histo_uid==uid and histo_action==u'inscription': inscriptions=inscriptions+1 if histo_uid==uid and (histo_action==u'paiement+'+str(ann_scol) or (len(historique[l])>5 and (historique[l][5]==u'paiement+'+str(ann_scol)))): reinscriptions=reinscriptions+1 score = 2*inscriptions+reinscriptions scores.append([prenom+' '+nom,score,inscriptions,reinscriptions]) return scores ### Tri par score def sort_by_score(): u"""Tri la liste des câbleurs par ordre de score décroissant de score""" return score_cableurs().sort(key=lambda x:int(x[1]),reverse=True) def sort_by_inverse_score(): u"""Tri la liste des câbleurs par ordre de score croissant de score""" return score_cableurs().sort(key=lambda x:int(x[1])) def cableurs_utiles(): u"""Renvoi le nombre de cableurs ayant un score non nul""" useless_cableurs=0 for k in range(0,len(cableurs)): if (scores[k][1]==0): useless_cableurs=useless_cableurs+1 return len(cableurs)-useless_cableurs #### Affichage ou x est le nombre de câbleurs à afficher def show_all(x): u"""Tableau fait main pour un effet plus visuel""" print '\033[0m ' + '\033[4m|',' '*8,u'Câbleur',' '*6,'|',u'Score',' |',u'Inscriptions',' ','|',u'Réinscriptions','\033[0m' for k in range(0,x): if k%2==01: print '\033[92m',string.ljust('|'+unicode((scores[k][0])),25), string.ljust('|'+str(scores[k][1]),8), string.ljust('|'+str(scores[k][2]),16),string.ljust('|'+str(scores[k][3]),16),'\033[0m' else: print '\033[0m',string.ljust('|'+unicode((scores[k][0])),25), string.ljust('|'+str(scores[k][1]),8), string.ljust('|'+str(scores[k][2]),16),string.ljust('|'+str(scores[k][3]),16),'\033[0m' def show_all_no_color(x): u"""Tableau fait main pour un effet plus visuel sans couleur""" print '\033[0m ' + '\033[4m|',' '*8,u'Câbleur',' '*6,'|',u'Score',' |',u'Inscriptions',' ','|',u'Réinscriptions','\033[0m' for k in range(0,x): print '\033[0m',string.ljust('|'+unicode((scores[k][0])),25), string.ljust('|'+str(scores[k][1]),8), string.ljust('|'+str(scores[k][2]),16),string.ljust('|'+str(scores[k][3]),16),'\033[0m' #### On définit le Parser if __name__ == "__main__": actions_cableurs() parser = argparse.ArgumentParser() parser.add_argument("-a", "--all", help="Affiche les scores de tout les câbleurs", action="store_true") parser.add_argument("-t", "--top", help="Affiche seulement les meilleurs câbleurs", action="store_true") parser.add_argument("-s", "--scores", help="Affiche seulement les câbleurs ayant un score non nul", action="store_true") parser.add_argument("-m", "--menage", help="Affiche seulement les câbleurs ayant un score nul", action="store_true") parser.add_argument("-a_nc", "--ncall", help="Affiche les scores de tout les câbleurs (sans couleur)", action="store_true") parser.add_argument("-t_nc", "--nctop", action="store_true") parser.add_argument("-s_nc", "--ncscores", action="store_true") parser.add_argument("-m_nc", "--ncmenage", action="store_true") args = parser.parse_args() if args.all: sort_by_score() show_all(len(cableurs)) elif args.scores: sort_by_score() show_all(cableurs_utiles()) elif args.menage: sort_by_inverse_score() show_all(len(cableurs)-cableurs_utiles()) elif args.top: sort_by_score() show_all(5) elif args.ncall: sort_by_score() show_all_no_color(len(cableurs)) elif args.ncscores: sort_by_score() show_all_no_color(cableurs_utiles()) elif args.ncmenage: sort_by_inverse_score() show_all_no_color(len(cableurs)-cableurs_utiles()) elif args.nctop: sort_by_score() show_all_no_color(5) else: sort_by_score() show_all(10)