main.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import pymysql
  2. from app import app
  3. from db import mysql
  4. from flask import jsonify, request, render_template
  5. @app.route('/')
  6. def home():
  7. return render_template('xchart.html')
  8. @app.route('/xchart', methods=['POST'])
  9. def x_chart():
  10. conn = None
  11. cursor = None
  12. _json = request.json
  13. start_date = _json['start']
  14. end_date = _json['end']
  15. try:
  16. conn = mysql.connect()
  17. cursor = conn.cursor(pymysql.cursors.DictCursor)
  18. 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";
  19. param = (start_date, end_date)
  20. cursor.execute(sql, param)
  21. rows = cursor.fetchall()
  22. data = []
  23. for row in rows:
  24. data.append({'label':row['day_date'], 'value':int(row['total_visits'])})
  25. resp = jsonify(data)
  26. resp.status_code = 200
  27. return resp
  28. except Exception as e:
  29. print(e)
  30. finally:
  31. if cursor and conn:
  32. cursor.close()
  33. conn.close()
  34. if __name__ == "__main__":
  35. app.run()