File: gm-fingerprint_db.py

package info (click to toggle)
gnumed-server 21.11-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 45,408 kB
  • ctags: 3,891
  • sloc: sql: 1,210,640; python: 13,526; sh: 1,476; makefile: 19
file content (67 lines) | stat: -rwxr-xr-x 2,432 bytes parent folder | download | duplicates (2)
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 python

#==============================================================
# This script can be used to fingerprint a GNUmed database.
#
# usage: ./gm-fingerprint_db.py <database name> <gm-dbo password>
#
# author: Karsten Hilbert
# license: GPL v2 or later
#
#==============================================================

import sys
import psycopg2
import io

database = sys.argv[1]
passwd = sys.argv[2]
dsn = u'dbname=%s user=gm-dbo password=%s' % (database, passwd)
queries = [
	("SELECT pg_size_pretty(pg_database_size('%s'))" % database, "Size (DB)"),
	("SELECT md5(gm.concat_table_structure())", "Schema hash"),
	("SELECT setting FROM pg_settings WHERE name = 'server_version'", "Version (PG)"),
	("SELECT setting FROM pg_settings WHERE name = 'server_encoding'", "Encoding (PG)"),
	("SELECT setting FROM pg_settings WHERE name = 'lc_collate'", "LC_COLLATE (PG)"),
	("SELECT setting FROM pg_settings WHERE name = 'lc_ctype'", "LC_CTYPE (PG)"),
	("SELECT count(1) FROM dem.identity", "Patients"),
	("SELECT count(1) FROM clin.encounter", "Contacts"),
	("SELECT count(1) FROM clin.episode", "Episodes"),
	("SELECT count(1) FROM clin.health_issue", "Issues"),
	("SELECT count(1) FROM clin.test_result", "Results"),
	("SELECT count(1) FROM clin.vaccination", "Vaccinations"),
	("SELECT count(1) FROM blobs.doc_med", "Documents"),
	("SELECT count(1) FROM blobs.doc_obj", "Objects"),
	("SELECT count(1) FROM dem.org", "Organizations"),
	("SELECT count(1) FROM dem.org_unit", "Organizational units"),
	("SELECT max(modified_when) FROM audit.audit_fields", "Last .modified_when"),
	("SELECT max(audit_when) FROM audit.audit_trail", "Last .audit_when")
]

fname = u'gm_db-%s-fingerprint.log' % database
#==============================================================
outfile = io.open(fname, mode = 'wt', encoding = 'utf8')

outfile.write(u"Fingerprinting GNUmed database ...\n")
outfile.write(u"\n")
outfile.write(u"%20s: %s\n" % (u"Name (DB)", database))

conn = psycopg2.connect(dsn=dsn)
curs = conn.cursor()

for cmd, label in queries:
	curs.execute(cmd)
	rows = curs.fetchall()
	outfile.write(u"%20s: %s\n" % (label, rows[0][0]))

if len(sys.argv) > 3:
	if sys.argv[3] == '--with-dump':
		curs.execute('SELECT gm.concat_table_structure()')
		rows = curs.fetchall()
		outfile.write(u"\n%s\n" % rows[0][0])

curs.close()
conn.rollback()

outfile.close()
#==============================================================