Skip to content

Commit

Permalink
Add slow_query case
Browse files Browse the repository at this point in the history
  • Loading branch information
dongweiming committed Aug 17, 2016
1 parent f0d175d commit d4cc8cb
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 13 deletions.
File renamed without changes.
36 changes: 36 additions & 0 deletions chapter11/section3/chart_styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# coding=utf-8
# From http://bit.ly/2beWXlj
import xlsxwriter

workbook = xlsxwriter.Workbook('chart_styles.xlsx')

# Show the styles for all of these chart types.
chart_types = ['column', 'area', 'line', 'pie']

for chart_type in chart_types:

# Add a worksheet for each chart type.
worksheet = workbook.add_worksheet(chart_type.title())
worksheet.set_zoom(30)
style_number = 1

# Create 48 charts, each with a different style.
for row_num in range(0, 90, 15):
for col_num in range(0, 64, 8):

chart = workbook.add_chart({'type': chart_type})
chart.add_series({'values': '=Data!$A$1:$A$6'})
chart.set_title({'name': 'Style %d' % style_number})
chart.set_legend({'none': True})
chart.set_style(style_number)

worksheet.insert_chart(row_num, col_num, chart)
style_number += 1

# Create a worksheet with data for the charts.
data_worksheet = workbook.add_worksheet('Data')
data = [10, 40, 50, 20, 10, 50]
data_worksheet.write_column('A1', data)
data_worksheet.hide()

workbook.close()
Binary file added chapter11/section3/chart_styles.xlsx
Binary file not shown.
File renamed without changes.
2 changes: 1 addition & 1 deletion chapter11/section3/pv_uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
chart2.set_title({'name': '上周活动页 UV'})
chart2.set_x_axis({'name': 'UV'})
chart2.set_y_axis({'name': '数量'})
chart2.set_style(42)
chart2.set_style(33)
worksheet2.insert_chart('A8', chart2, {'x_offset': 25, 'y_offset': 10})

workbook.close()
Binary file added chapter11/section3/pv_uv.xlsx
Binary file not shown.
11 changes: 0 additions & 11 deletions chapter11/section4/statistics.csv

This file was deleted.

2 changes: 1 addition & 1 deletion chapter3/section3/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
cur.execute("SELECT VERSION()")
ver = cur.fetchone()
print "Database version : %s " % ver
except MySQLdb.Error, e:
except MySQLdb.Error as e:
print "Error %d: %s" % (e.args[0], e.args[1])
exit(1)
finally:
Expand Down
53 changes: 53 additions & 0 deletions chapter3/section3/logger_slow_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# coding=utf-8
import logging
from logging.handlers import RotatingFileHandler

from flask import Flask, request, jsonify
from flask_sqlalchemy import get_debug_queries

from ext import db
from users import User

app = Flask(__name__)
app.config.from_object('config')
app.config['DATABASE_QUERY_TIMEOUT'] = 0.0001
app.config['SQLALCHEMY_RECORD_QUERIES'] = True
db.init_app(app)

formatter = logging.Formatter(
"[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s")
handler = RotatingFileHandler('slow_query.log', maxBytes=10000, backupCount=10)
handler.setLevel(logging.WARN)
handler.setFormatter(formatter)
app.logger.addHandler(handler)

with app.app_context():
db.drop_all()
db.create_all()


@app.route('/users', methods=['POST'])
def users():
username = request.form.get('name')

user = User(username)
print 'User ID: {}'.format(user.id)
db.session.add(user)
db.session.commit()

return jsonify({'id': user.id})


@app.after_request
def after_request(response):
for query in get_debug_queries():
if query.duration >= app.config['DATABASE_QUERY_TIMEOUT']:
app.logger.warn(
('\nContext:{}\nSLOW QUERY: {}\nParameters: {}\n'
'Duration: {}\n').format(query.context, query.statement,
query.parameters, query.duration))
return response


if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000)

0 comments on commit d4cc8cb

Please sign in to comment.