-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (65 loc) · 1.69 KB
/
app.py
File metadata and controls
82 lines (65 loc) · 1.69 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
import os
from flask import Flask, jsonify
import sqlalchemy as sa
# web app
app = Flask(__name__)
# database engine
SQL_URI = sa.engine.URL.create(
drivername='postgresql',
username=os.getenv('PGUSER', ''),
password=os.getenv('PGPASSWORD', ''),
host=os.getenv('PGHOST', ''),
database=os.getenv('PGDATABASE', ''),
port=os.getenv('PGPORT', 5432),
)
engine = sa.create_engine(SQL_URI)
@app.route('/')
def index():
return 'Welcome to EQ Works 😎'
@app.route('/events/hourly')
def events_hourly():
return query_helper('''
SELECT date, hour, events
FROM public.hourly_events
ORDER BY date, hour
LIMIT 168;
''')
@app.route('/events/daily')
def events_daily():
return query_helper('''
SELECT date, SUM(events) AS events
FROM public.hourly_events
GROUP BY date
ORDER BY date
LIMIT 7;
''')
@app.route('/stats/hourly')
def stats_hourly():
return query_helper('''
SELECT date, hour, impressions, clicks, revenue
FROM public.hourly_stats
ORDER BY date, hour
LIMIT 168;
''')
@app.route('/stats/daily')
def stats_daily():
return query_helper('''
SELECT date,
SUM(impressions) AS impressions,
SUM(clicks) AS clicks,
SUM(revenue) AS revenue
FROM public.hourly_stats
GROUP BY date
ORDER BY date
LIMIT 7;
''')
@app.route('/poi')
def poi():
return query_helper('''
SELECT *
FROM public.poi;
''')
def query_helper(query):
with engine.connect() as conn:
result = conn.execute(query).fetchall()
return jsonify([dict(row.items()) for row in result])