-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
304 lines (225 loc) · 11.5 KB
/
model.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
"""Models and database functions for Dev Program Dashboard project."""
from flask_sqlalchemy import SQLAlchemy
# This is the connection to the PostgreSQL database; we're getting this through
# the Flask-SQLAlchemy helper library. On this, we can find the `session`
# object, where we do most of our interactions (like committing, etc.)
db = SQLAlchemy()
##############################################################################
# Model definitions
class Customer(db.Model):
"""Customer properties."""
__tablename__ = "customer"
customer_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
username = db.Column(db.String(128), nullable=False)
first_name = db.Column(db.String(128), nullable=False)
last_name = db.Column(db.String(128), nullable=False)
email = db.Column(db.String(128), nullable=False)
city = db.Column(db.String(128), nullable=False)
state = db.Column(db.String(128), nullable=True)
country = db.Column(db.String(128), nullable=False)
sub_status = db.Column(db.String(128), nullable=False)
sub_level = db.Column(db.String(128), nullable=True)
revenue = db.Column(db.Numeric, nullable=True)
sub_start = db.Column(db.DateTime, nullable=True)
sub_end = db.Column(db.DateTime, nullable=True)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Customer customer_id={} username={} sub_status={} sub_level={} sub_start={} sub_end={}>".format(self.customer_id,
self.username,
self.sub_status,
self.sub_level,
self.sub_start,
self.sub_end)
class Environment(db.Model):
"""Environment (production/staging) an API operates in."""
__tablename__ = "environment"
env_id = db.Column(db.Integer, primary_key=True)
env_name = db.Column(db.String(64), nullable=False, unique=True)
env_base_url = db.Column(db.String(128), nullable=False, unique=True)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Environment env_id={} env_name={} env_base_url={}>".format(self.env_name,
self.env_name,
self.env_base_url)
class API(db.Model):
"""API provided by a company."""
__tablename__ = "api"
__table_args__ = (db.UniqueConstraint('api_name', 'env_id', 'version',
name='_api_env_version'),)
api_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
api_name = db.Column(db.String(64), nullable=False)
env_id = db.Column(db.Integer,
db.ForeignKey("environment.env_id"),
nullable=False)
version = db.Column(db.String(10), nullable=False)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<API api_id={} api_name={} env_id={} version={}>".format(self.api_id,
self.api_name,
self.env_id,
self.version)
# Define relationship to call
call = db.relationship("Call",
backref=db.backref("api",
order_by=api_id))
environment = db.relationship("Environment",
backref=db.backref("api",
order_by=env_id))
class Call(db.Model):
"""A call from a company's API."""
__tablename__ = "call"
call_id = db.Column(db.Integer, primary_key=True)
call_name = db.Column(db.String(128))
env_id = db.Column(db.Integer,
db.ForeignKey("environment.env_id"),
nullable=False)
api_id = db.Column(db.Integer,
db.ForeignKey("api.api_id"),
nullable=False)
endpoint = db.Column(db.String(128))
method = db.Column(db.String(10))
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Call call_id={} call_name={} api_id={} endpoint={} method={}>".format(self.call_id,
self.call_name,
self.api_id,
self.endpoint,
self.method)
class Developer(db.Model):
"""Developer properties."""
__tablename__ = "developer"
dev_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
username = db.Column(db.String(128), nullable=False)
first_name = db.Column(db.String(128), nullable=False)
last_name = db.Column(db.String(128), nullable=False)
email = db.Column(db.String(128), nullable=False)
company = db.Column(db.String(128), nullable=False)
city = db.Column(db.String(128), nullable=False)
state = db.Column(db.String(128), nullable=True)
country = db.Column(db.String(128), nullable=False)
dev_type = db.Column(db.String(128), nullable=False)
dev_key = db.Column(db.String(128), nullable=True)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Developer dev_id={} username={} company={} dev_type={}>".format(self.dev_id,
self.username,
self.company,
self.dev_type,
self.dev_key)
class Application(db.Model):
"""Application properties."""
__tablename__ = "application"
app_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
app_name = db.Column(db.String(128), nullable=False)
app_type = db.Column(db.String(128), nullable=False)
dev_id = db.Column(db.Integer,
db.ForeignKey("developer.dev_id"),
nullable=False)
application_key = db.Column(db.String(128), nullable=False)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Application app_id={} app_name={} app_type={}>".format(self.app_id,
self.app_name,
self.app_type)
developer = db.relationship("Developer",
backref=db.backref("application",
order_by=dev_id))
class Request(db.Model):
"""The container for an individual API request to the server."""
__tablename__ = "request"
request_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
call_id = db.Column(db.Integer,
db.ForeignKey("call.call_id"),
nullable=False)
method = db.Column(db.String(128))
app_id = db.Column(db.Integer,
db.ForeignKey("application.app_id"),
nullable=False)
key_type = db.Column(db.String(128))
response_code = db.Column(db.String(128), nullable=False)
response_time = db.Column(db.Numeric, nullable=False)
date = db.Column(db.DateTime, nullable=False)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Request request_id={} call_id={} key_type={} response_code={} response_time={} date={}>".format(self.request_id,
self.call_id,
self.key_type,
self.response_code,
self.response_time,
self.date)
call = db.relationship("Call",
backref=db.backref("request",
order_by=call_id))
application = db.relationship("Application",
backref=db.backref("request",
order_by=app_id))
class App_Used(db.Model):
"""Application/customer relationships."""
__tablename__ = "app_used"
use_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
app_id = db.Column(db.Integer,
db.ForeignKey("application.app_id"),
nullable=False)
customer_id = db.Column(db.Integer,
db.ForeignKey("customer.customer_id"),
nullable=False)
use_start = db.Column(db.DateTime, nullable=False)
use_end = db.Column(db.DateTime, nullable=True)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<App_Used use_id={} app_id={} customer_id={}>".format(self.use_id,
self.app_id,
self.customer_id)
application = db.relationship("Application",
backref=db.backref("app_used",
order_by=app_id))
customer = db.relationship("Customer",
backref=db.backref("app_used",
order_by=customer_id))
class Agg_Request(db.Model):
"""The the aggregated stats for each call."""
__tablename__ = "agg_request"
aggr_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
call_id = db.Column(db.Integer,
db.ForeignKey("call.call_id"),
nullable=False)
env_id = db.Column(db.Integer,
db.ForeignKey("environment.env_id"),
nullable=False)
success_count = db.Column(db.Integer, nullable=False)
fail_count = db.Column(db.Integer, nullable=False)
total_responses = db.Column(db.Integer, nullable=False)
avg_response_time = db.Column(db.Numeric, nullable=False)
date = db.Column(db.DateTime, nullable=False)
def __repr__(self):
"""Provide helpful representation when printed."""
return "<Agg_Request aggr_id={} call_id={} success_count={} fail_count={} total_responses={} avg_response_time={} date={}>".format(self.aggr_id,
self.call_id,
self.success_count,
self.fail_count,
self.total_responses,
self.avg_response_time,
self.date)
call = db.relationship("Call",
backref=db.backref("agg_request",
order_by=call_id))
environment = db.relationship("Environment",
backref=db.backref("agg_request",
order_by=env_id))
##############################################################################
# Helper functions
def connect_to_db(app):
"""Connect the database to the Flask app."""
# Configure to use the PstgreSQL database
# Uncomment to connect to test DB
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///test_dashboard'
# Uncomment to connect to production DB
# app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///dashboard'
db.app = app
db.init_app(app)
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
connect_to_db(app)
print "Connected to DB."