aboutsummaryrefslogtreecommitdiff
path: root/scripts/database.py
blob: 866439395af4c9c74c3658c87f290aae74c34e4f (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import datetime
import postgresql
import collections

import utils
import exceptions
from conf import conf
from conf import sf

def __git_describe__():
	return utils.callsubprocess('git_describe',
			conf.git_describe_cmd, False, True)[0]

def __git_commit__():
	return utils.callsubprocess('git_rev_parse',
			conf.git_commit_cmd, False, True)[0]

def __timestamp__():
	return datetime.datetime.now().strftime('%y-%m-%d-%H-%M-%S')

Config = collections.namedtuple('Config', 'id hash config') # Named tuple for configuration
Measure = collections.namedtuple('Measure', 'id conf_id output value') # Named tuple for measurement

class database:
	"Class used for accessing PostgreSQL project database."
	def __init__(self):
		self.db = postgresql.open(database = conf.db_database,
				user = conf.db_user,
				password = conf.db_password,
				host = conf.db_host,
				port = conf.db_port
				)
		# check if tables are present
		tables = ('toolsgit', 'configurations', 'measure')
		for tab in tables:
			val = self.db.prepare("""SELECT COUNT(*) FROM pg_class
							   WHERE relname = $1""")(tab)[0][0]
			if val < 1:
				raise exceptions.DatabaseUninitialized()

	def check_toolsgit(self):
		"Return id of toolsgit row. If missing, it is inserted"
		ds = __git_describe__()
		cm = __git_commit__()
		ps = self.db.prepare("""SELECT id FROM toolsgit
							  WHERE git_describe = $1 AND git_commit = $2
							  """)
		id = ps(ds, cm)
		if id:
			return id[0][0]
		ps = self.db.prepare("""INSERT INTO toolsgit
						   (git_describe, git_commit)
						   VALUES
						   ($1, $2);
						   """)
		ps(ds, cm)
		return self.check_toolsgit()

	def check_linuxgit(self):
		"Return id of linuxgit row. If missing, it is inserted."
		wd = os.getcwd()
		os.chdir(sf(conf.linux_sources))
		ds = __git_describe__()
		cm = __git_commit__()
		os.chdir(wd)
		ps = self.db.prepare("""SELECT id FROM linuxgit
							  WHERE git_describe = $1 AND git_commit = $2
							  """)
		id = ps(ds, cm)
		if id:
			return id[0][0]
		ps = self.db.prepare("""INSERT INTO linuxgit
						   (git_describe, git_commit)
						   VALUES
						   ($1, $2);
						   """)
		ps(ds, cm)
		return self.check_linuxgit()

	def add_configuration(self, hash, txtconfig, generator):
		"Add configuration to database."
		ps = self.db.prepare("""INSERT INTO configurations
								(hash, config, gtime, toolgit, linuxgit, generator)
								VALUES
								($1, $2, $3, $4, $5, $6);
								""")
		gt = self.check_toolsgit()
		lgt = self.check_linuxgit()
		tm = datetime.datetime.now()
		ps(hash, '\n'.join(txtconfig), tm, gt, lgt, generator)

	def get_configration(self, hash):
		"Return configration id for inserted hash."
		ps = self.db.prepare("""SELECT id, config FROM configurations
								WHERE hash = $1""")
		rtn = []
		for dt in ps(hash):
			rtn.append(Config(dt[0], hash, dt[1].split('\n')))
		return rtn

	def add_measure(self, output, conf_id, value = None):
		"Add measurement."
		ps = self.db.prepare("""INSERT INTO measure
								(conf, output, value, mtime, toolgit, linuxgit, measurement)
								VALUES
								($1, $2, $3, $4, $5, $6, $7);
								""")
		gt = self.check_toolsgit()
		lgt = self.check_linuxgit()
		tm = datetime.datetime.now()
		ps(conf_id, output, value, tm, gt, lgt, conf.measure_identifier)

	def update_measure(self, measure_id, value):
		"Update measured value"
		ps = self.db.prepare("""UPDATE measure SET
								(value) = ($2)
								WHERE
								id = $1;
								""")
		ps(measure_id, value)

	def get_measures(self, conf_id):
		"Get measures for configuration with conf_id id"
		ps = self.db.prepare("""SELECT id, output, value FROM measure
								WHERE conf = $1;
								""")
		rtn = []
		for dt in ps(conf_id):
			rtn.append(Measure(dt[0], conf_id, dt[1], dt[2]))
		return rtn

	def get_unmeasured(self):
		"Returns list of all unmeasured configurations."
		ps = self.db.prepare("""SELECT id, hash, config FROM configurations
								WHERE id NOT IN
								(SELECT conf FROM measure)
								""")
		rtn = []
		for dt in ps():
			rtn.append(Config(dt[0], dt[1], dt[2].split('\n')))
		return rtn

	def add_configsort(self, configopt):
		"Add configuration option to sorted list"
		ps = self.db.prepare("""INSERT INTO configopt
								(configopt) VALUES ($1)
								""")
		ps(configopt)

	def get_configsort(self):
		"Returns sorted list of all configuration options"
		ps = self.db.prepare("""SELECT id, configopt FROM configopt
								ORDER BY id ASC
								""")
		rtn = []
		itms = ps()
		for id, config in itms:
			rtn.append(config)
		return rtn