#! /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