forked from roytuts/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (35 loc) · 1.08 KB
/
main.py
File metadata and controls
51 lines (35 loc) · 1.08 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
import pymysql
from app import app
from db import mysql
from flask import jsonify, request, render_template
@app.route('/')
def home():
return render_template('xchart.html')
@app.route('/xchart', methods=['POST'])
def x_chart():
conn = None
cursor = None
_json = request.json
start_date = _json['start']
end_date = _json['end']
try:
conn = mysql.connect()
cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = "SELECT SUM(no_of_visits) total_visits, DATE(access_date) day_date FROM site_log WHERE DATE(access_date) >= %s AND DATE(access_date) <= %s GROUP BY DATE(access_date) ORDER BY DATE(access_date) DESC";
param = (start_date, end_date)
cursor.execute(sql, param)
rows = cursor.fetchall()
data = []
for row in rows:
data.append({'label':row['day_date'], 'value':int(row['total_visits'])})
resp = jsonify(data)
resp.status_code = 200
return resp
except Exception as e:
print(e)
finally:
if cursor and conn:
cursor.close()
conn.close()
if __name__ == "__main__":
app.run()