app.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. from flask import Flask
  2. import base64
  3. from io import BytesIO
  4. from matplotlib.figure import Figure
  5. import json
  6. from flask import request, jsonify
  7. import logging
  8. from logging.handlers import RotatingFileHandler
  9. import time
  10. import signal as sig
  11. import sys, getopt
  12. app = Flask(__name__)
  13. app.debug = True
  14. curname = __name__
  15. logging.basicConfig(filename='/var/www/html/data/log/flask.log',
  16. format='%(levelname)10s %(asctime)s %(name)10s %(thread)d %(message)s',
  17. level=logging.DEBUG)
  18. logger = logging.getLogger('plot')
  19. from refill import ChannelCumPainter, ChannelCovPainter, ChannelCovSuccPainter
  20. from refill import MerchantCumRatioPainter, MerchantAmountPainter, MerchantCovRatioPainter, ChannelSpeedAnalyzePainter
  21. from refill import filter_chname, filter_cardtype, filter_mchids, get_channels, get_mchids
  22. from refill import NetcheckCovPainter, get_net_channels
  23. def parse_parmeter():
  24. end_time = request.args.get('end_time')
  25. end_time = None if end_time is None else int(end_time.strip())
  26. start_time = request.args.get('start_time')
  27. start_time = None if start_time is None else int(start_time.strip())
  28. card_types = request.args.get('card_types')
  29. card_types = filter_cardtype(card_types)
  30. spec = request.args.get('spec')
  31. if spec is not None:
  32. spec = int(spec.strip())
  33. filter_wave = request.args.get('filter_wave')
  34. if filter_wave is not None:
  35. filter_wave = int(filter_wave.strip())
  36. return start_time, end_time, card_types, spec, filter_wave
  37. def onError(ex):
  38. logger.error(ex)
  39. return jsonify({'state': 'fail'})
  40. @app.route('/plot/channels')
  41. def channels():
  42. try:
  43. logger.debug('start chratio')
  44. result = get_channels()
  45. return jsonify({'channels': result, 'state': 'success'})
  46. except Exception as ex:
  47. return onError(ex)
  48. @app.route('/plot/ch_ratio')
  49. def ch_ratio():
  50. try:
  51. logger.debug('start chratio')
  52. start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
  53. chnames = request.args.get('chnames')
  54. chnames = filter_chname(chnames)
  55. only_all = request.args.get('only_all')
  56. painter = ChannelCumPainter(start_time=start_time, end_time=end_time, chnames=chnames, card_types=card_types, spec=spec,
  57. filter_wave=filter_wave)
  58. buf, ratios = painter.paint()
  59. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  60. return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
  61. except Exception as ex:
  62. return onError(ex)
  63. @app.route('/plot/ch_speed_ratio')
  64. def ch_speed_ratio():
  65. try:
  66. logger.debug('start ch_speed_ratio')
  67. start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
  68. chnames = request.args.get('chnames')
  69. chnames = filter_chname(chnames)
  70. period = request.args.get('period')
  71. period = 60 if period is None else int(period.strip())
  72. painter = ChannelSpeedAnalyzePainter(start_time=start_time, end_time=end_time,
  73. chnames=chnames, card_types=card_types, spec=spec,
  74. period=period)
  75. buf, ratios = painter.paint()
  76. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  77. return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
  78. except Exception as ex:
  79. return onError(ex)
  80. @app.route('/plot/ch_covratio')
  81. def ch_covratio():
  82. try:
  83. logger.debug('start ch_covratio')
  84. start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
  85. chnames = request.args.get('chnames')
  86. chnames = filter_chname(chnames)
  87. only_all = request.args.get('only_all')
  88. all_show_type = 0 if only_all is None else int(only_all)
  89. painter = ChannelCovPainter(start_time=start_time, end_time=end_time, chnames=chnames, card_types=card_types, spec=spec,
  90. filter_wave=filter_wave,all_show_type=all_show_type)
  91. buf, result = painter.paint()
  92. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  93. return jsonify({'img': data, 'result': result, 'state': 'success'})
  94. except Exception as ex:
  95. return onError(ex)
  96. @app.route('/plot/ch_covsuccs')
  97. def ch_covsuccs():
  98. try:
  99. logger.debug('start ch_covratio')
  100. start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
  101. chnames = request.args.get('chnames')
  102. chnames = filter_chname(chnames)
  103. painter = ChannelCovSuccPainter(start_time=start_time, end_time=end_time, chnames=chnames, card_types=card_types, spec=spec,
  104. filter_wave=filter_wave)
  105. buf, ratios = painter.paint()
  106. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  107. return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
  108. except Exception as ex:
  109. return onError(ex)
  110. @app.route('/plot/net_check')
  111. def net_check():
  112. try:
  113. logger.debug('start net_check')
  114. start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
  115. chnames = request.args.get('chnames')
  116. chnames = filter_chname(chnames)
  117. painter = NetcheckCovPainter(start_time=start_time, end_time=end_time, chnames=chnames, filter_wave=filter_wave)
  118. buf, ratios = painter.paint()
  119. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  120. return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
  121. except Exception as ex:
  122. return onError(ex)
  123. @app.route('/plot/net_channels')
  124. def net_channels():
  125. try:
  126. channels = get_net_channels()
  127. return jsonify(channels)
  128. except Exception as ex:
  129. logger.error(ex)
  130. return jsonify([])
  131. @app.route('/plot/mchids')
  132. def mchids():
  133. try:
  134. logger.debug('start mchids')
  135. result = get_mchids()
  136. return jsonify({'mchids': result, 'state': 'success'})
  137. except Exception as ex:
  138. return onError(ex)
  139. @app.route('/plot/mch_ratio')
  140. def mch_ratio():
  141. try:
  142. logger.debug('start mchratio')
  143. start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
  144. mchids = request.args.get('mchids')
  145. mchids = filter_mchids(mchids)
  146. painter = MerchantCumRatioPainter(start_time=start_time, end_time=end_time, mchids=mchids, card_types=card_types, spec=spec,
  147. filter_wave=filter_wave)
  148. buf, ratios = painter.paint()
  149. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  150. return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
  151. except Exception as ex:
  152. return onError(ex)
  153. @app.route('/plot/mch_covratio')
  154. def mch_covratio():
  155. try:
  156. logger.debug('start mchratio')
  157. start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
  158. mchids = request.args.get('mchids')
  159. mchids = filter_mchids(mchids)
  160. painter = MerchantCovRatioPainter(start_time=start_time, end_time=end_time, mchids=mchids, card_types=card_types, spec=spec,
  161. filter_wave=filter_wave)
  162. buf, ratios = painter.paint()
  163. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  164. return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
  165. except Exception as ex:
  166. return onError(ex)
  167. @app.route('/plot/mch_order_send')
  168. def mch_order_send():
  169. try:
  170. logger.debug('start mch_order_send')
  171. start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
  172. mchids = request.args.get('mchids')
  173. mchids = filter_mchids(mchids)
  174. painter = MerchantAmountPainter(start_time=start_time, end_time=end_time, mchids=mchids, card_types=card_types, spec=spec,
  175. filter_wave=filter_wave)
  176. buf, mchids = painter.paint()
  177. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  178. return jsonify({'img': data, 'mchids': mchids, 'state': 'success'})
  179. except Exception as ex:
  180. return onError(ex)
  181. if __name__ == "__main__":
  182. debug_mode = False
  183. if debug_mode:
  184. app.run(debug=True, host='0.0.0.0', port=5000)
  185. else:
  186. from gevent import monkey
  187. from gevent.pywsgi import WSGIServer
  188. from gevent import signal as geventsig
  189. app.debug = False
  190. monkey.patch_all()
  191. http_server = WSGIServer(('0.0.0.0', 5000), app)
  192. geventsig.signal(sig.SIGTERM, lambda: http_server.stop())
  193. geventsig.signal(sig.SIGINT, lambda: http_server.stop())
  194. http_server.serve_forever()