-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
107 lines (87 loc) · 3.1 KB
/
dashboard.py
File metadata and controls
107 lines (87 loc) · 3.1 KB
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
#!/usr/bin/env python2.7
import json
import datetime
import psycopg2
from psycopg2.extras import DictCursor
from flask import Flask, g, request, jsonify, config, render_template
from db import * # database query functions
from utils import * # utility and calendar functions
app = Flask(__name__)
app.config.from_object('settings')
@app.before_request
def db_connect():
g.conn = psycopg2.connect(app.config['DB'])
@app.after_request
def db_commit(rsp):
g.conn.commit()
return rsp
@app.route('/')
@app.route('/page/<int:page>')
def index(page=1):
return render_template('index.html',
counts=({'date':str(x['date']), 'count':x['count']}
for x in
get_counts(get_start_current_month(),
get_start_next_month(),
'day')),
page=page,
pagecount=get_report_count(),
reports=get_reports(page-1))
### Reporting API
@app.route('/report/<int:id>', methods=['GET'])
def load_report(id):
c = g.conn.cursor(cursor_factory=DictCursor)
c.execute("select * from reports where id = %s", [id])
row = c.fetchone()
c.close()
g.conn.commit()
if row is None:
return jsonify({'error': 'not found'}), 404
data = row.copy()
return jsonify(**data)
@app.route('/report', methods=['POST'])
def report():
"""Writes a JSON 451 report to the database."""
f = request.json
c = g.conn.cursor()
try:
c.execute("""
insert into reports
(url, creator, version, status, status_text, blocked_by, date, created)
values
(%s,%s,%s,%s,%s,%s,%s, now())
returning id as id
""",
[f['url'], f['creator'], f['version'], f['status'], f['statusText'], f['blockedBy'], f['date']]
)
newrecord = c.fetchone()
app.logger.info("Created record %s for %s", newrecord[0], f['url'])
c.close()
g.conn.commit()
return jsonify(id=newrecord[0]), 201
except psycopg2.Error as exc:
app.logger.warn("Database error: %s", repr(exc))
g.conn.rollback()
return jsonify({'error':repr(exc)}), 400
### Summary views
@app.route('/view/rate')
@app.route('/view/rate/<interval>')
@app.route('/view/rate/<interval>/<start>')
@app.route('/view/rate/<interval>/<start>/<end>')
def report_data_rate(start=None, end=None, interval='day'):
if interval == 'day':
if start is None:
start = get_start_current_month()
elif interval == 'month':
if start is None:
start = get_start_current_year()
else:
return jsonify({'error': 'unsupported interval'}), 400
if end is None:
today = datetime.date.today()
end = get_start_next_month # start of next month
data = get_counts(start, end, interval)
g.conn.commit()
return jsonify(data=data)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)