summaryrefslogtreecommitdiffstats
path: root/munin/publik_recent_count
blob: 51bc78f8553fe2b47ce21fd0fc7601ccd6f4940b (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
61
62
63
64
65
66
67
#! /usr/bin/env python3
#%# family=auto
#%# capabilities=autoconf

import pickle
import datetime
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)

if len(sys.argv) == 2 and sys.argv[1] == 'config':
    print('''graph_title Count of recently submitted forms
graph_category entrouvert
count_5m.label Count (5 minutes)
count_1h.label Count (1 hour)
count_1d.label Count (k) (1 day)
''')
    sys.exit(0)

total_5m = 0
total_1h = 0
total_1d = 0

time_5m = datetime.datetime.now() - datetime.timedelta(minutes=5)
time_1h = datetime.datetime.now() - datetime.timedelta(hours=1)
time_1d = datetime.datetime.now() - datetime.timedelta(days=1)

for tenant in os.listdir(base_dir):
    if tenant in ('collectstatic', 'scripts', 'skeletons', 'spooler'):
        continue
    if tenant.endswith('.invalid'):
        continue
    if not os.path.isdir(os.path.join(base_dir, tenant)):
        continue
    cfg = pickle.load(open(os.path.join(base_dir, tenant, 'config.pck'), 'rb'), encoding='utf-8')
    if 'postgresql' not in cfg:
        continue
    psql_cfg = {}
    for k, v in cfg['postgresql'].items():
        if v and isinstance(v, str):
            psql_cfg[k] = v
    pgconn = psycopg2.connect(**psql_cfg)
    try:
        for var, time in zip(('total_5m', 'total_1h', 'total_1d'), (time_5m, time_1h, time_1d)):
            cur = pgconn.cursor()
            cur.execute('''SELECT COUNT(*) from wcs_all_forms WHERE status != 'draft' AND receipt_time > %(time)s''', {'time': time})
            locals()[var] += cur.fetchone()[0]
    except psycopg2.ProgrammingError:
        pass
    pgconn.close()

print('count_5m.value %s' % total_5m)
print('count_1h.value %s' % total_1h)
print('count_1d.value %s' % (total_1d / 1000))