Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# encoding: utf-8 

from __future__ import print_function, division, absolute_import 

 

from sqlalchemy.ext.declarative import declarative_base 

from sqlalchemy import (Column, Integer, String, LargeBinary, ForeignKey, Float, 

DateTime, UniqueConstraint) 

from sqlalchemy.orm import relationship, backref 

from datetime import datetime 

 

 

Base = declarative_base() 

 

 

def dbo_from_bunch(clz, bunch): 

dd = {k: v for (k, v) in bunch.items() if not k.startswith("_")} 

return clz(**dd) 

 

 

class SiteDbo(Base): 

 

__tablename__ = 'site' 

 

site_id = Column(Integer(), primary_key=True) 

name = Column(String(), unique=True, index=True, nullable=False) 

description = Column(String()) 

street = Column(String()) 

postcode = Column(String()) 

city = Column(String()) 

coord_x = Column(Float()) 

coord_y = Column(Float()) 

coord_z = Column(Float()) 

 

def __str__(self): 

return ("<SiteDbo name={name}, city={city}, x={coord_x}, y={coord_y}, " 

"z={coord_z}, description={description}").format(**vars(self)) 

 

 

class SignalDbo(Base): 

 

"""(formerly known as fact table) 

Contains the measured value of a given parameter taken a at a specific time, site 

""" 

 

__tablename__ = 'signal' 

 

signal_id = Column(Integer(), primary_key=True) 

value = Column(Float(), nullable=False) 

measurement_date = Column(DateTime(), nullable=False) 

parameter_id = Column(ForeignKey("parameter.parameter_id"), nullable=False) 

source_id = Column(ForeignKey("source.source_id"), nullable=False) 

site_id = Column(ForeignKey("site.site_id"), nullable=False) 

coord_x = Column(Float()) 

coord_y = Column(Float()) 

coord_z = Column(Float()) 

 

site = relationship("SiteDbo", backref=backref("signals")) 

source = relationship("SourceDbo", backref=backref("signals")) 

parameter = relationship("ParameterDbo", backref=backref("signals")) 

 

def __str__(self): 

return ("<SourceDbo signal_id={signal_id}, parameter_id={parameter_id}, " 

"value={value}, timestamp={timestamp}, site={site_id}").format(**vars(self)) 

 

 

class PictureDbo(Base): 

 

__tablename__ = 'picture' 

 

picture_id = Column(Integer(), primary_key=True) 

site_id = Column(ForeignKey("site.site_id"), nullable=False) 

filename = Column(String(), nullable=False) 

description = Column(String()) 

date = Column(DateTime()) 

data = Column(LargeBinary()) 

 

site = relationship("SiteDbo", backref=backref("pictures")) 

constraint = UniqueConstraint('site_id', 'filename') 

 

def __str__(self): 

return ("<PictureDbo filename={filename}, date={date}, " 

"description={description}").format(**vars(self)) 

 

 

class SourceDbo(Base): 

 

__tablename__ = 'source' 

 

source_id = Column(Integer(), primary_key=True) 

source_type_id = Column(ForeignKey("source_type.source_type_id"), nullable=False) 

site_id = Column(ForeignKey("site.site_id"), nullable=True) 

name = Column(String(), unique=True, index=True, nullable=False) 

description = Column(String()) 

serial = Column(String(), unique=True, nullable=True) 

manufacturer = Column(String()) 

manufacturing_date = Column(DateTime()) 

 

immutable = ("name", "serial") 

 

def __str__(self): 

return ("<SourceDbo name={name}, serial={serial}, " 

"description={description}").format(**vars(self)) 

 

 

class SourceTypeDbo(Base): 

 

__tablename__ = 'source_type' 

 

source_type_id = Column(Integer(), primary_key=True) 

name = Column(String(), unique=True, index=True, nullable=False) 

description = Column(String()) 

 

immutable = ("name") 

 

def __str__(self): 

return "<SourceTypeDbo name={name}, description={description}".format(**vars(self)) 

 

 

class SpecialValueDefinitionDbo(Base): 

 

__tablename__ = 'special_value_definition' 

 

special_value_definition_id = Column(Integer(), primary_key=True) 

source_type_id = Column(ForeignKey("source_type.source_type_id"), nullable=False) 

description = Column(String()) 

categorical_value = Column(String(), nullable=False) 

numeric_value = Column(Float(), nullable=False) 

 

def __str__(self): 

return ("<SpecialValueDefinitionDbo categorical_value={categorical_value}, " 

"numeric_value={numeric_value}").format(**vars(self)) 

 

 

class ParameterDbo(Base): 

 

__tablename__ = 'parameter' 

 

parameter_id = Column(Integer(), primary_key=True) 

name = Column(String(), unique=True, index=True, nullable=False) 

description = Column(String()) 

unit = Column(String(), nullable=False) 

 

immutable = ("name", "unit") 

 

def __str__(self): 

return "<ParameterDbo name={name}, unit={unit}>".format(**vars(self)) 

 

 

class ParameterAveragingDbo(Base): 

 

__tablename__ = 'parameter_averaging' 

 

parameter_averaging_id = Column(Integer(), primary_key=True) 

parameter_id = Column(ForeignKey("parameter.parameter_id"), nullable=False) 

source_id = Column(ForeignKey("source.source_id"), nullable=False) 

integration_length_x = Column(Float()) 

integration_length_y = Column(Float()) 

integration_angle = Column(Float()) 

integration_time = Column(Float()) 

 

constraint = UniqueConstraint('parameter_id', 'source_id') 

 

parameter = relationship("ParameterDbo", backref=backref("averaging")) 

source = relationship("SourceDbo", backref=backref("averaging")) 

 

 

class CommentDbo(Base): 

 

__tablename__ = 'comment' 

 

comment_id = Column(Integer(), primary_key=True) 

signal_id = Column(ForeignKey("signal.signal_id"), nullable=False) 

text = Column(String(), nullable=False) 

timestamp = Column(DateTime(), nullable=False, default=datetime.now()) 

author = Column(String()) 

 

signals = relationship("SignalDbo", backref=backref("comments")) 

 

 

class SignalQualityDbo(Base): 

 

__tablename__ = 'signal_quality' 

 

signal_quality_id = Column(Integer(), primary_key=True) 

signal_id = Column(ForeignKey("signal.signal_id"), nullable=False) 

quality_id = Column(ForeignKey("quality.quality_id"), nullable=False) 

timestamp = Column(DateTime(), nullable=False, default=datetime.now()) 

author = Column(String()) 

 

 

class QualityDbo(Base): 

 

__tablename__ = 'quality' 

 

quality_id = Column(Integer(), primary_key=True) 

flag = Column(String(), nullable=False) 

method = Column(String()) 

 

 

class ProjectDbo(Base): 

 

__tablename__ = 'project' 

 

project_id = Column(Integer(), primary_key=True) 

title = Column(String(), nullable=False) 

description = Column(String())