summaryrefslogtreecommitdiffstats
path: root/munin/publik_count_
blob: 37679a9a44b1711ff8c6caff9b4fff50b16fcd6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /usr/bin/env python
#%# family=auto
#%# capabilities=autoconf

import cPickle
import itertools
import os
import psycopg2
import sys

base_dir = None
for dirname in os.listdir('/var/lib/machines/'):
    if os.path.exists(os.path.join('/var/lib/machines/', dirname, 'var/lib/wcs')):
        base_dir = os.path.join('/var/lib/machines/', dirname, 'var/lib/wcs')
        break

if len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
    if base_dir:
        print 'yes'
    else:
        print 'no'
    sys.exit(0)

tenant = os.path.basename(sys.argv[0]).replace('publik_count_', '')

if len(sys.argv) == 2 and sys.argv[1] == 'config':
    print '''graph_title Count of forms for %s
graph_category entrouvert
total_count.label Total
total_user_count.label Front
total_logged_user_count.label Front by logged user
''' % tenant
    sys.exit(0)

for tenant_dir in itertools.chain(os.listdir(base_dir), os.listdir(os.path.join(base_dir, 'tenants'))):
    if tenant_dir != tenant:
        continue
    cfg = cPickle.load(open(os.path.join(base_dir, tenant, 'config.pck')))
    if not 'postgresql' in cfg:
        continue
    psql_cfg = {}
    for k, v in cfg['postgresql'].items():
        if v and isinstance(v, basestring):
            psql_cfg[k] = v
    pgconn = psycopg2.connect(**psql_cfg)
    cur = pgconn.cursor()
    cur.execute('''SELECT COUNT(*) from wcs_all_forms WHERE status != 'draft' ''')
    total_count = cur.fetchone()[0]

    cur.execute('''SELECT COUNT(*) from wcs_all_forms WHERE status != 'draft' AND backoffice_submission = FALSE''')
    total_user_count = cur.fetchone()[0]

    cur.execute('''SELECT COUNT(*) from wcs_all_forms WHERE status != 'draft' AND backoffice_submission = FALSE AND user_id is not NULL''')
    total_logged_user_count = cur.fetchone()[0]

    print 'total_count.value', total_count
    print 'total_user_count.value', total_user_count
    print 'total_logged_user_count.value', total_logged_user_count
    pgconn.close()
    break