-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
executable file
·162 lines (119 loc) · 4.53 KB
/
app.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
#!/usr/bin/env python
import time
from flask import Flask, jsonify, request, render_template
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db/gridlock.db'
db = SQLAlchemy(app)
color_scheme = ''
class Service(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), unique=True, nullable=False)
def __init__(self, name):
self.name = name
def __repr__(self):
return '<Service %r>' % self.name
class Location(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), unique=True, nullable=False)
def __init__(self, name):
self.name = name
def __repr__(self):
return '<Location %r>' % self.name
class Status(db.Model):
id = db.Column(db.Integer, primary_key=True)
service_id = db.Column(db.Integer, db.ForeignKey('service.id'))
status = db.Column(db.Integer, nullable=False)
location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
env = db.Column(db.String(120), nullable=False)
timestamp = db.Column(db.String(120), nullable=False)
description = db.Column(db.Text, nullable=True)
def __init__(self,
service_id,
status,
location_id,
env,
timestamp,
description):
self.service_id = service_id
self.status = status
self.location_id = location_id
self.env = env
self.timestamp = timestamp
self.description = description
@app.route('/gridlock/api/v0.1/', methods=['PUT'])
def put_status():
service = request.json['service']
status = request.json['status']
location = request.json['location']
env = request.json['env']
timestamp = request.json['timestamp']
description = request.json['description']
s = Service.query.filter_by(name=service).first()
if s is None:
del s
s = Service(service)
db.session.add(s)
l = Location.query.filter_by(name=location).first()
if l is None:
del l
l = Location(location)
db.session.add(l)
stuff = Status(s.id, status, l.id, env, timestamp, description)
db.session.add(stuff)
db.session.commit()
return "OK", 201
@app.route('/', methods=['GET'])
def homepage():
service_list = {}
locs = []
services = Service.query.all()
locations = Location.query.all()
for location in locations:
locs.append(location.name)
for service in services:
service_list[service.name] = {}
for location in locations:
service_list[service.name][location.name] = []
statuses = Status.query.filter_by(service_id = service.id,
location_id = location.id).order_by(Status.timestamp.desc()).limit(1)
for status in statuses:
"""
if (time.time() - int(status.timestamp)) >= 600000:
state = 4
else:
state = status.status
"""
state = status.status
stats = {"status": state,
"timestamp": status.timestamp,
"description": status.description}
service_list[service.name][location.name] = state if state else 0
return render_template('index.html', data=service_list, locs=locs, rows=len(service_list), cols=len(locs))
@app.route('/detail', methods=['GET'])
def detail():
service = request.args.get('service')
location = request.args.get('location')
sid = Service.query.filter_by(name=service).first()
lid = Location.query.filter_by(name=location).first()
if request.args.get('offset'):
offset = request.args.get('offset')
else:
offset = 20
history = Status.query.filter_by(service_id=sid.id,
location_id=lid.id).order_by(Status.timestamp.desc()).limit(offset).all()
h_array = []
for h in history:
new_time = time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(float(h.timestamp)))
stats = {"status": h.status,
"timestamp": new_time,
"description": h.description}
h_array.append(stats)
return render_template('detail.html',
data=h_array,
location=location,
service=service)
if __name__ == '__main__':
db.create_all()
app.run(debug=True, passthrough_errors=True)