123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- from flask import Flask
- import base64
- from io import BytesIO
- from matplotlib.figure import Figure
- import json
- from flask import request, jsonify
- import logging
- from logging.handlers import RotatingFileHandler
- import time
- import signal as sig
- import sys, getopt
- app = Flask(__name__)
- app.debug = True
- curname = __name__
- logging.basicConfig(filename='/var/www/html/data/log/flask.log',
- format='%(levelname)10s %(asctime)s %(name)10s %(thread)d %(message)s',
- level=logging.DEBUG)
- logger = logging.getLogger('plot')
- from refill import ChannelCumPainter, ChannelCovPainter, ChannelCovSuccPainter
- from refill import MerchantCumRatioPainter, MerchantAmountPainter, MerchantCovRatioPainter, ChannelSpeedAnalyzePainter
- from refill import filter_chname, filter_cardtype, filter_mchids, get_channels, get_mchids
- from refill import NetcheckCovPainter, get_net_channels
- def parse_parmeter():
- end_time = request.args.get('end_time')
- end_time = None if end_time is None else int(end_time.strip())
- start_time = request.args.get('start_time')
- start_time = None if start_time is None else int(start_time.strip())
- card_types = request.args.get('card_types')
- card_types = filter_cardtype(card_types)
- spec = request.args.get('spec')
- if spec is not None:
- spec = int(spec.strip())
- filter_wave = request.args.get('filter_wave')
- if filter_wave is not None:
- filter_wave = int(filter_wave.strip())
- return start_time, end_time, card_types, spec, filter_wave
- def onError(ex):
- logger.error(ex)
- return jsonify({'state': 'fail'})
- @app.route('/plot/channels')
- def channels():
- try:
- logger.debug('start chratio')
- result = get_channels()
- return jsonify({'channels': result, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/ch_ratio')
- def ch_ratio():
- try:
- logger.debug('start chratio')
- start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
- chnames = request.args.get('chnames')
- chnames = filter_chname(chnames)
- only_all = request.args.get('only_all')
- painter = ChannelCumPainter(start_time=start_time, end_time=end_time, chnames=chnames, card_types=card_types, spec=spec,
- filter_wave=filter_wave)
- buf, ratios = painter.paint()
- data = base64.b64encode(buf.getbuffer()).decode("ascii")
- return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/ch_speed_ratio')
- def ch_speed_ratio():
- try:
- logger.debug('start ch_speed_ratio')
- start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
- chnames = request.args.get('chnames')
- chnames = filter_chname(chnames)
- period = request.args.get('period')
- period = 60 if period is None else int(period.strip())
- painter = ChannelSpeedAnalyzePainter(start_time=start_time, end_time=end_time,
- chnames=chnames, card_types=card_types, spec=spec,
- period=period)
- buf, ratios = painter.paint()
- data = base64.b64encode(buf.getbuffer()).decode("ascii")
- return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/ch_covratio')
- def ch_covratio():
- try:
- logger.debug('start ch_covratio')
- start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
- chnames = request.args.get('chnames')
- chnames = filter_chname(chnames)
- only_all = request.args.get('only_all')
- all_show_type = 0 if only_all is None else int(only_all)
- painter = ChannelCovPainter(start_time=start_time, end_time=end_time, chnames=chnames, card_types=card_types, spec=spec,
- filter_wave=filter_wave,all_show_type=all_show_type)
- buf, result = painter.paint()
- data = base64.b64encode(buf.getbuffer()).decode("ascii")
- return jsonify({'img': data, 'result': result, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/ch_covsuccs')
- def ch_covsuccs():
- try:
- logger.debug('start ch_covratio')
- start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
- chnames = request.args.get('chnames')
- chnames = filter_chname(chnames)
- painter = ChannelCovSuccPainter(start_time=start_time, end_time=end_time, chnames=chnames, card_types=card_types, spec=spec,
- filter_wave=filter_wave)
- buf, ratios = painter.paint()
- data = base64.b64encode(buf.getbuffer()).decode("ascii")
- return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/net_check')
- def net_check():
- try:
- logger.debug('start net_check')
- start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
- chnames = request.args.get('chnames')
- chnames = filter_chname(chnames)
- painter = NetcheckCovPainter(start_time=start_time, end_time=end_time, chnames=chnames, filter_wave=filter_wave)
- buf, ratios = painter.paint()
- data = base64.b64encode(buf.getbuffer()).decode("ascii")
- return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/net_channels')
- def net_channels():
- try:
- channels = get_net_channels()
- return jsonify(channels)
- except Exception as ex:
- logger.error(ex)
- return jsonify([])
- @app.route('/plot/mchids')
- def mchids():
- try:
- logger.debug('start mchids')
- result = get_mchids()
- return jsonify({'mchids': result, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/mch_ratio')
- def mch_ratio():
- try:
- logger.debug('start mchratio')
- start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
- mchids = request.args.get('mchids')
- mchids = filter_mchids(mchids)
- painter = MerchantCumRatioPainter(start_time=start_time, end_time=end_time, mchids=mchids, card_types=card_types, spec=spec,
- filter_wave=filter_wave)
- buf, ratios = painter.paint()
- data = base64.b64encode(buf.getbuffer()).decode("ascii")
- return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/mch_covratio')
- def mch_covratio():
- try:
- logger.debug('start mchratio')
- start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
- mchids = request.args.get('mchids')
- mchids = filter_mchids(mchids)
- painter = MerchantCovRatioPainter(start_time=start_time, end_time=end_time, mchids=mchids, card_types=card_types, spec=spec,
- filter_wave=filter_wave)
- buf, ratios = painter.paint()
- data = base64.b64encode(buf.getbuffer()).decode("ascii")
- return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- @app.route('/plot/mch_order_send')
- def mch_order_send():
- try:
- logger.debug('start mch_order_send')
- start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
- mchids = request.args.get('mchids')
- mchids = filter_mchids(mchids)
- painter = MerchantAmountPainter(start_time=start_time, end_time=end_time, mchids=mchids, card_types=card_types, spec=spec,
- filter_wave=filter_wave)
- buf, mchids = painter.paint()
- data = base64.b64encode(buf.getbuffer()).decode("ascii")
- return jsonify({'img': data, 'mchids': mchids, 'state': 'success'})
- except Exception as ex:
- return onError(ex)
- if __name__ == "__main__":
- debug_mode = False
- if debug_mode:
- app.run(debug=True, host='0.0.0.0', port=5000)
- else:
- from gevent import monkey
- from gevent.pywsgi import WSGIServer
- from gevent import signal as geventsig
- app.debug = False
- monkey.patch_all()
- http_server = WSGIServer(('0.0.0.0', 5000), app)
- geventsig.signal(sig.SIGTERM, lambda: http_server.stop())
- geventsig.signal(sig.SIGINT, lambda: http_server.stop())
- http_server.serve_forever()
|