stats_prises.py 1.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Affiche des statistiques sur le câblage des prises des chambres
(CRANS ou CROUS)."""

import sys
import psycopg2
sys.path.append('/usr/scripts/gestion')
from affich_tools import tableau

conn = psycopg2.connect("user=crans dbname=switchs host=pgsql.adm.crans.org")

def compte_prises(reseau, batiment=''):
    """Compte les prises câblées sur reseau (crans ou crous)."""

    reseau = reseau.lower()
    cur = conn.cursor()
    if batiment == '':
        cur.execute("SELECT COUNT(prises) FROM prises WHERE " + reseau + "=True")
        n = cur.fetchone()[0]
    else:
        batiment = batiment.lower()
24 25 26 27
        if batiment == "g":
            cur.execute("SELECT COUNT(prises) FROM prises WHERE (batiment, " + reseau + ")=(%s, True) AND substring(chambre for 1) <> '0'", (batiment, ))
        else:
            cur.execute("SELECT COUNT(prises) FROM prises WHERE (batiment, " + reseau + ")=(%s, True)", (batiment, ))
28
        n = cur.fetchone()[0]
29

30 31
    return n

32 33 34
tot_crans = 0
tot_crous = 0

35
data = []
36
for bat in ['A', 'B', 'C', 'G', 'H', 'I', 'J', 'M']:
37 38
    n_crans = compte_prises('crans', bat)
    n_crous = compte_prises('crous', bat)
39 40
    tot_crans += n_crans
    tot_crous += n_crous
41 42 43
    frac_crans = 1.*n_crans/(n_crous + n_crans)
    data.append([bat, n_crans, n_crous, '%0.2f' % (100*frac_crans)])

44 45
data.append(["Total", tot_crans, tot_crous, '%0.2f' % (100.*n_crans/(n_crous+n_crans))])

46 47 48
print tableau(data, [u'Bât.', u'CRANS',
                     u'CROUS', u'% CRANS'],
              [7, 7, 7, 9], ['c', 'd', 'd', 'd'])