-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCreateDBsabrs.py
354 lines (314 loc) · 23.7 KB
/
CreateDBsabrs.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
"""
Module to create the database structure for dbprocessing
@author: Brian Larsen
@organization: LANL
@contact: [email protected]
@version: V1: 24-Mar-2011 (BAL)
"""
from __future__ import division # may not be needed but start with it
import os
import sqlalchemy
from sqlalchemy import schema, types
from sqlalchemy.engine import create_engine
from sqlalchemy.sql import func
from dbprocessing import DButils
class dbprocessing_db(object):
"""
Main workhorse class for the CreateDB module
"""
def __init__(self, create=True):
self.user = os.environ["PGUSER"]
self.password = ''
self.db_name = os.environ["PGDATABASE"]
self.dbIsOpen = False
if create:
self.createDB()
def init_db(self, user, password, db, host='localhost', port=5432):
url = "postgresql://{0}:{1}@{2}:{3}/{4}"
url = url.format(user, password, host, port, db)
self.engine = create_engine(url, echo=False, encoding='utf-8')
self.metadata = sqlalchemy.MetaData()
self.metadata.reflect(bind=self.engine)
def createDB(self):
"""
Step through and create the DB structure, relationships and constraints
**Note that order matters here, have to define a Table before you can link to it**
TODO this can/should all be redone using the new syntax and relations
see: http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html# for
some examples.
NOTE: if one stops using sqlite then change file_id, logging_id and file_logging_id
to BigIntegers (sqlite doesn't know BigInteger)
"""
self.init_db(self.user, self.password, self.db_name)
metadata = self.metadata
data_table = schema.Table('mission', metadata,
schema.Column('mission_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False),
schema.Column('mission_name', types.String(20), nullable=False, unique=True),
schema.Column('rootdir', types.String(150), nullable=False, ),
schema.Column('incoming_dir', types.String(150), nullable=False, ),
schema.Column('codedir', types.String(150), nullable=True, ),
schema.Column('inspectordir', types.String(150), nullable=True, ),
schema.Column('errordir', types.String(150), nullable=True, ),
extend_existing=True)
data_table = schema.Table('satellite', metadata,
schema.Column('satellite_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False),
schema.Column('satellite_name', types.String(20), nullable=False), # hmm long enough?
schema.Column('mission_id', types.Integer,
schema.ForeignKey('mission.mission_id'), nullable=False, ),
schema.UniqueConstraint('satellite_name', 'mission_id', name='unique_pairs_satellite'),
extend_existing=True)
data_table = schema.Table('instrument', metadata,
schema.Column('instrument_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False),
schema.Column('instrument_name', types.String(20), nullable=False),
# hmm long enough?
schema.Column('satellite_id', types.Integer,
schema.ForeignKey('satellite.satellite_id'), nullable=False, ),
schema.UniqueConstraint('instrument_name', 'satellite_id',
name='unique_pairs_instrument'),
extend_existing=True)
data_table = schema.Table('product', metadata,
schema.Column('product_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False, index=True),
schema.Column('product_name', types.String(100), nullable=False, index=True),
# hmm long enough?
schema.Column('instrument_id', types.Integer,
schema.ForeignKey('instrument.instrument_id'), nullable=False, ),
schema.Column('relative_path', types.String(100), nullable=False), # hmm long enough?
schema.Column('level', types.Float, nullable=False),
schema.Column('format', types.Text, nullable=False), # hmm long enough?
schema.Column('product_description', types.Text, nullable=True), # hmm long enough?
schema.UniqueConstraint('product_name', 'instrument_id', 'relative_path',
name='unique_triplet_product'),
extend_existing=True)
data_table = schema.Table('instrumentproductlink', metadata,
schema.Column('instrument_id', types.Integer,
schema.ForeignKey('instrument.instrument_id'), nullable=False),
schema.Column('product_id', types.Integer,
schema.ForeignKey('product.product_id'), nullable=False),
schema.PrimaryKeyConstraint('instrument_id', 'product_id'),
extend_existing=True)
data_table = schema.Table('process', metadata,
schema.Column('process_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False, index=True),
schema.Column('process_name', types.String(50), nullable=False), # hmm long enough?
schema.Column('output_product', types.Integer,
schema.ForeignKey('product.product_id'), nullable=True, index=True),
schema.Column('output_timebase', types.String(10), nullable=True, index=True),
schema.Column('extra_params', types.Text, nullable=True),
schema.UniqueConstraint('process_name', 'output_product'),
extend_existing=True)
data_table = schema.Table('productprocesslink', metadata,
schema.Column('process_id', types.Integer,
schema.ForeignKey('process.process_id'), nullable=False),
schema.Column('input_product_id', types.Integer,
schema.ForeignKey('product.product_id'), nullable=False),
schema.Column('optional', types.Boolean, nullable=False),
# schema.Column('yesterday', types.Integer, nullable=False),
# schema.Column('tomorrow', types.Integer, nullable=False),
schema.PrimaryKeyConstraint('process_id', 'input_product_id'),
extend_existing=True)
data_table = schema.Table('file', metadata,
# this was a bigint, sqlalchemy doesn't seem to like this... think here
schema.Column('file_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False, index=True),
schema.Column('filename', types.String(250), nullable=False, unique=True, index=True),
schema.Column('utc_file_date', types.Date, nullable=True, index=True),
schema.Column('utc_start_time', types.DateTime, nullable=True, index=True),
schema.Column('utc_stop_time', types.DateTime, nullable=True, index=True),
schema.Column('data_level', types.Float, nullable=False, index=True),
schema.Column('interface_version', types.SmallInteger, nullable=False),
schema.Column('quality_version', types.SmallInteger, nullable=False),
schema.Column('revision_version', types.SmallInteger, nullable=False),
schema.Column('verbose_provenance', types.Text, nullable=True),
schema.Column('check_date', types.DateTime, nullable=True),
schema.Column('quality_comment', types.Text, nullable=True),
schema.Column('caveats', types.Text, nullable=True),
schema.Column('file_create_date', types.DateTime, nullable=False),
schema.Column('met_start_time', types.Float, nullable=True),
schema.Column('met_stop_time', types.Float, nullable=True),
schema.Column('exists_on_disk', types.Boolean, nullable=False),
schema.Column('quality_checked', types.Boolean, nullable=True, default=False),
schema.Column('product_id', types.Integer,
schema.ForeignKey('product.product_id'), nullable=False),
schema.Column('shasum', types.String(40), nullable=True),
schema.Column('process_keywords', types.Text, nullable=True),
schema.CheckConstraint('utc_stop_time is not NULL OR met_stop_time is not NULL'),
schema.CheckConstraint('utc_start_time is not NULL OR met_start_time is not NULL'),
schema.CheckConstraint('met_start_time <= met_stop_time'), # in case of one entry
schema.CheckConstraint('utc_start_time <= utc_stop_time'), # in case of one entry
schema.CheckConstraint('interface_version >= 1'),
schema.UniqueConstraint('utc_file_date',
'product_id',
'interface_version',
'quality_comment',
'revision_version', name='Unique file tuple'),
extend_existing=True)
schema.Index('ix_file_big', data_table.columns['filename'],
data_table.columns['utc_file_date'],
data_table.columns['utc_start_time'],
data_table.columns['utc_stop_time'], unique=True
)
data_table = schema.Table('unixtime', metadata,
schema.Column('file_id', types.Integer,
schema.ForeignKey('file.file_id'), primary_key=True, index=True),
schema.Column('unix_start', types.Integer, index=True),
schema.Column('unix_stop', types.Integer, index=True),
schema.CheckConstraint('unix_start <= unix_stop'),
)
data_table = schema.Table('filefilelink', metadata,
schema.Column('source_file', types.Integer,
schema.ForeignKey('file.file_id'), nullable=False, index=True),
schema.Column('resulting_file', types.Integer,
schema.ForeignKey('file.file_id'), nullable=False, index=True),
schema.PrimaryKeyConstraint('source_file', 'resulting_file'),
schema.CheckConstraint('source_file <> resulting_file'),
# TODO this is supposed to be more general than !=
extend_existing=True)
data_table = schema.Table('code', metadata,
schema.Column('code_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False, index=True),
schema.Column('filename', types.String(250), nullable=False, unique=False),
schema.Column('relative_path', types.String(100), nullable=False),
schema.Column('code_start_date', types.Date, nullable=False),
schema.Column('code_stop_date', types.Date, nullable=False),
schema.Column('code_description', types.Text, nullable=False),
schema.Column('process_id', types.Integer,
schema.ForeignKey('process.process_id'), nullable=False, index=True),
schema.Column('interface_version', types.SmallInteger, nullable=False),
schema.Column('quality_version', types.SmallInteger, nullable=False),
schema.Column('revision_version', types.SmallInteger, nullable=False),
schema.Column('output_interface_version', types.SmallInteger, nullable=False),
schema.Column('active_code', types.Boolean, nullable=False, default=False),
schema.Column('date_written', types.Date, nullable=False),
schema.Column('shasum', types.String(40), nullable=True),
schema.Column('newest_version', types.Boolean, nullable=False),
schema.Column('arguments', types.Text, nullable=True),
schema.Column('ram', types.Float, nullable=True), # amanount of ram used in Gigs
schema.Column('cpu', types.SmallInteger, nullable=True), # number of cpus used
schema.CheckConstraint('code_start_date <= code_stop_date'),
schema.CheckConstraint('interface_version >= 1'),
schema.CheckConstraint('output_interface_version >= 1'),
extend_existing=True
)
data_table = schema.Table('processqueue', metadata,
schema.Column('file_id', types.Integer,
schema.ForeignKey('file.file_id'),
primary_key=True, nullable=False, unique=True, index=True),
schema.Column('version_bump', types.SmallInteger, nullable=True),
schema.Column('instrument_id', types.Integer,
schema.ForeignKey('instrument.instrument_id'), nullable=False),
schema.CheckConstraint('version_bump is NULL or version_bump < 3'),
extend_existing=True
)
data_table = schema.Table('filecodelink', metadata,
schema.Column('resulting_file', types.Integer,
schema.ForeignKey('file.file_id'), nullable=False),
schema.Column('source_code', types.Integer,
schema.ForeignKey('code.code_id'), nullable=False),
schema.PrimaryKeyConstraint('resulting_file', 'source_code'),
extend_existing=True
)
data_table = schema.Table('release', metadata,
schema.Column('file_id', types.Integer,
schema.ForeignKey('file.file_id'), nullable=False, ),
schema.Column('release_num', types.String(20), nullable=False),
schema.PrimaryKeyConstraint('file_id', 'release_num'),
extend_existing=True
)
data_table = schema.Table('processpidlink', metadata,
schema.Column('ppl_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False),
schema.Column('pid', types.Integer, nullable=True),
schema.Column('hostname', types.String(100), nullable=True),
schema.Column('process_id', types.Integer,
schema.ForeignKey('process.process_id'), nullable=True),
schema.Column('currentlyprocessing', types.Boolean, nullable=True, default='f'),
schema.Column('start_time', types.DateTime, nullable=True, default=func.now()),
schema.Column('end_time', types.DateTime, nullable=True, default=func.now())
)
data_table = schema.Table('logging', metadata,
schema.Column('logging_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False),
schema.Column('currently_processing', types.Boolean, nullable=False, default=False),
schema.Column('pid', types.Integer, nullable=True),
schema.Column('processing_start_time', types.DateTime, nullable=False),
# might have to be a TIMESTAMP
schema.Column('processing_end_time', types.DateTime, nullable=True),
schema.Column('comment', types.Text, nullable=True),
schema.Column('mission_id', types.Integer,
schema.ForeignKey('mission.mission_id'), nullable=False),
schema.Column('user', types.String(30), nullable=False),
schema.Column('hostname', types.String(100), nullable=False),
# schema.PrimaryKeyConstraint('logging_id'),
schema.CheckConstraint('processing_start_time < processing_end_time'),
extend_existing=True
)
data_table = schema.Table('logging_file', metadata,
schema.Column('logging_file_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False),
schema.Column('logging_id', types.Integer,
schema.ForeignKey('logging.logging_id'), nullable=False),
schema.Column('file_id', types.Integer,
schema.ForeignKey('file.file_id'), nullable=False),
schema.Column('code_id', types.Integer,
schema.ForeignKey('code.code_id'), nullable=False),
schema.Column('comments', types.Text, nullable=True),
# schema.PrimaryKeyConstraint('logging_file_id'),
extend_existing=True
)
data_table = schema.Table('inspector', metadata,
schema.Column('inspector_id', types.Integer, autoincrement=True, primary_key=True,
nullable=False, index=True),
schema.Column('filename', types.String(250), nullable=False, unique=False),
schema.Column('relative_path', types.String(250), nullable=False),
schema.Column('description', types.Text, nullable=False),
schema.Column('interface_version', types.SmallInteger, nullable=False),
schema.Column('quality_version', types.SmallInteger, nullable=False),
schema.Column('revision_version', types.SmallInteger, nullable=False),
schema.Column('output_interface_version', types.SmallInteger, nullable=False),
schema.Column('active_code', types.Boolean, nullable=False, default=False,
index=True),
schema.Column('date_written', types.Date, nullable=False),
schema.Column('shasum', types.String(40), nullable=True),
schema.Column('newest_version', types.Boolean, nullable=False, index=True),
schema.Column('arguments', types.Text, nullable=True),
schema.Column('product', types.Integer,
schema.ForeignKey('product.product_id'), nullable=False),
schema.CheckConstraint('interface_version >= 1'),
schema.CheckConstraint('output_interface_version >= 1'),
extend_existing=True
)
# TODO move this out so that the user chooses the db type
# engine = create_engine('postgres:///' + self.filename, echo=False)
# metadata.bind = engine
metadata.create_all(checkfirst=True, bind=self.engine)
def addMission(self, filename):
"""utility to add a mission"""
self.dbu = DButils.DButils(filename)
self.mission = self.dbu.addMission('rbsp', os.path.join('/', 'n', 'space_data', 'cda', 'rbsp'))
def addSatellite(self):
"""add satellite utility"""
self.satellite = self.dbu.addSatellite('rbspa') # 1
self.satellite = self.dbu.addSatellite('rbspb') # 2
def addInstrument(self):
"""addInstrument utility"""
self.instrument = self.dbu.addInstrument('hope', 1)
self.instrument = self.dbu.addInstrument('hope', 2)
self.instrument = self.dbu.addInstrument('rept', 1)
self.instrument = self.dbu.addInstrument('rept', 2)
self.instrument = self.dbu.addInstrument('mageis', 1)
self.instrument = self.dbu.addInstrument('mageis', 2)
if __name__ == "__main__":
# usage = "usage: %prog [options] filename"
# parser = OptionParser(usage=usage)
# (options, args) = parser.parse_args()
# if len(args) != 1:
# parser.error("incorrect number of arguments")
# filename = os.path.abspath(args[0])
# if os.path.isfile(filename):
# parser.error("file: {0} exists will not overwrite".format(filename))
db = dbprocessing_db()