def send_hour_report(): report = hour_report() call_detail_yestoday = report[0] call_detail_today = report[1] Utils.log(call_detail_today['time_key'] + '报告准备发送') title = '六合一API'+call_detail_today['time_key']+'调用概览\n' for call_stat_today in call_detail_today['call_stat']: call = call_stat_today['call'] call_num = call_stat_today['call_num'] succeed_rate = call_stat_today.get('succeed_rate',0) yestoday_same_call = get_yestoday_same_call(call,call_detail_yestoday) if yestoday_same_call is not None: print(yestoday_same_call) if call_num == 0 : title += str(call) + ' 接口调用数量异常今日单小时 ' + str(call_num) + ' 昨日单小时 ' + str(yestoday_same_call['call_num']) + '\n' elif yestoday_same_call.get('call_num',0) != 0 and int(abs(call_num - yestoday_same_call['call_num']) * 100 / call_num) > 30 and (call_num - yestoday_same_call['call_num'])<0: title +=str(call)+' 接口调用数量异常今日单小时 '+str(call_num)+' 昨日单小时 '+str(yestoday_same_call['call_num'])+'\n' if succeed_rate == 0: title += str(call) + ' 接口调用通过率异常今日单小时调用成功率 ' + str(succeed_rate) + ' 昨日单小时调用成功率' + str(yestoday_same_call['succeed_rate']) + '\n' elif yestoday_same_call.get('succeed_rate','')!='' and int(abs(succeed_rate - yestoday_same_call['succeed_rate']) * 100 / succeed_rate) >10 and (succeed_rate - yestoday_same_call['succeed_rate'])<0: title += str(call)+' 接口调用通过率异常今日单小时调用成功率 '+str(succeed_rate)+' 昨日单小时调用成功率'+str(yestoday_same_call['succeed_rate'])+'\n' # Mail.html_send(subject='六合一API'+call_detail_today['time_key']+'调用概览', msg=MIMEText(html_report(report,title), _subtype='html', _charset='utf-8'),to=['*****@*****.**', '*****@*****.**', '*****@*****.**']) Utils.log(call_detail_today['time_key']+'邮件发送成功')
def day_report(): day_timestamp_duration = 24 * 60 * 60 * 1000 today_begin_timestamp = DateTimeUtil.get_current_day_begin_timestamp() #yestoday_begin_timestamp = today_begin_timestamp - day_timestamp_duration two_day_ago_begin_timestamp = today_begin_timestamp - (2 * day_timestamp_duration) ranges = [] for i in range(int((today_begin_timestamp - two_day_ago_begin_timestamp) / day_timestamp_duration)): ranges_detail = {} ranges_detail['from'] = two_day_ago_begin_timestamp + i * day_timestamp_duration ranges_detail['to'] = two_day_ago_begin_timestamp + (i + 1) * day_timestamp_duration ranges_detail['key'] = DateTimeUtil.timestamp_to_strtime((two_day_ago_begin_timestamp + i * day_timestamp_duration),'%Y-%m-%d') ranges.append(ranges_detail) Utils.log(str(ranges)) return AppLog.get_range_general(two_day_ago_begin_timestamp, today_begin_timestamp, ranges)
def test_job(): Utils.log('heart beat')
def send_day_report(): Utils.log('六合一每天报告开始') Mail.html_send(subject="六合一每天API调用概览", msg=MIMEText(html_report(day_report(),"六合一每天API调用概览"), _subtype='html', _charset='utf-8'),to=['*****@*****.**']) Utils.log('六合一每天邮件发送成功')
# Mail.html_send(subject='六合一API'+call_detail_today['time_key']+'调用概览', msg=MIMEText(html_report(report,title), _subtype='html', _charset='utf-8'),to=['*****@*****.**', '*****@*****.**', '*****@*****.**']) Utils.log(call_detail_today['time_key']+'邮件发送成功') def get_yestoday_same_call(call,call_detail_yestoday): for call_stat_yestoday in call_detail_yestoday['call_stat']: if call_stat_yestoday['call'] == call: return call_stat_yestoday def test_job(): Utils.log('heart beat') if __name__ == '__main__': # 挂载任务 Utils.log('start') #print(json.dumps(hour_report(), ensure_ascii=False)) scheduler = Scheduler.get_sched() #send_hour_report() scheduler.add_job(func=send_day_report, trigger='cron',hour=2,minute=18) scheduler.add_job(func=test_job, trigger='interval', minutes=1) scheduler.add_job(func=send_hour_report, trigger='interval', hours=1) scheduler.start() while True: time.sleep(10)
def get_timestamp_range_general(cls, gte, lte): report = {} report['gte'] = gte report['lte'] = lte es = Utils.get_es_client() query = { "query": { "bool": { "filter": { "range": { "request.baseRequest.timeStamp": { "gte": gte, "lte": lte } } } } }, "aggs": { "call_terms": { "terms": { "field": "request.baseRequest.call.keyword", "size": 100 }, "aggs": { "terms_returnCode": { "terms": { "field": "response.returnCode.keyword", "size": 100 } } } } } } try: res = es.search(index=cls.index, doc_type=cls.index, body=query) except Exception as e: res = None print(' 查询失败 ', e) if res == None: return buckets = res['aggregations']['call_terms']['buckets'] print(type(buckets)) call_stat_list = [] for bucket in buckets: call_detail = {} return_detail_list = [] call_detail['call'] = bucket['key'] call_detail['call_num'] = bucket['doc_count'] returncode_buckets = bucket['terms_returnCode']['buckets'] for returncode_bucket in returncode_buckets: return_detail = {} return_detail['return_code'] = returncode_bucket['key'] return_detail['return_msg'] = RETURN_CODE_MSG_ENUM_DICT.get( str(returncode_bucket['key']), 'UnKnow') return_detail['return_num'] = returncode_bucket['doc_count'] return_detail_list.append(return_detail) call_detail['return_stat'] = return_detail_list call_stat_list.append(call_detail) report['call_stat'] = call_stat_list return report
def get_range_general(cls, gte, lte, ranges): es = Utils.get_es_client() query = { "query": { "bool": { "filter": { "range": { "request.baseRequest.timeStamp": { "gte": gte, "lte": lte } } } } }, "aggs": { "timestamp_range": { "range": { "field": "request.baseRequest.timeStamp", "keyed": "true", "ranges": ranges }, "aggs": { "call_terms": { "terms": { "field": "request.baseRequest.call.keyword", "size": 100 }, "aggs": { "terms_returnCode": { "terms": { "field": "response.returnCode.keyword", "size": 100 } } } } } } } } Utils.log(str(query)) try: res = es.search(index=cls.index, doc_type=cls.index, body=query) except Exception as e: Utils.log('查询失败 ' + str(e)) res = None if res == None: return buckets = res['aggregations']['timestamp_range']['buckets'] time_key_report_list = [] for k, v in buckets.items(): hour_report = {} hour_report['time_key'] = k hour_report['from'] = v['from'] hour_report['to'] = v['to'] hour_report['doc_count'] = v['doc_count'] call_stat_list = [] call_stat_buckets = v['call_terms']['buckets'] for bucket in call_stat_buckets: call_detail = {} return_detail_list = [] call_detail['call'] = bucket['key'] call_detail['call_num'] = bucket['doc_count'] returncode_buckets = bucket['terms_returnCode']['buckets'] for returncode_bucket in returncode_buckets: return_detail = {} return_detail['return_code'] = returncode_bucket['key'] return_detail[ 'return_msg'] = RETURN_CODE_MSG_ENUM_DICT.get( str(returncode_bucket['key']), 'UnKnow') return_detail['return_num'] = str( returncode_bucket['doc_count']) + ' (' + str( int( int(returncode_bucket['doc_count']) * 100 / int(bucket['doc_count']))) + '%)' return_detail_list.append(return_detail) call_detail['return_stat'] = return_detail_list call_stat_list.append(call_detail) hour_report['call_stat'] = call_stat_list time_key_report_list.append(hour_report) return time_key_report_list