app.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. from flask import Flask,Response
  2. from DBSession import session_maker
  3. from model import *
  4. from sqlalchemy.sql import func
  5. from utils import *
  6. import atexit
  7. from aliyunsdkcore.client import AcsClient
  8. from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
  9. from apscheduler.schedulers.background import BackgroundScheduler
  10. import json
  11. from model_longvideo import produce_video_task,produce_video_project
  12. from DBSession_longvideo import session_maker_longvideo
  13. from prometheus_client import Gauge,Counter, generate_latest
  14. from prometheus_client.core import CollectorRegistry
  15. from scheduler_jobs import interface_info_count
  16. from ex_response import ex_response
  17. import time
  18. import threading
  19. import logging
  20. logging.basicConfig(level=logging.INFO)
  21. app = Flask(__name__)
  22. registry = CollectorRegistry(auto_describe=False)
  23. # 定义后台执行调度器
  24. scheduler = BackgroundScheduler()
  25. scheduler.add_job(func=update_request_url_list, trigger="interval", seconds=300)
  26. scheduler.add_job(produce_video_ratio_cnt, 'cron', hour='00', minute='03')
  27. scheduler.start()
  28. atexit.register(lambda: scheduler.shutdown())
  29. client = AcsClient('LTAI4GBWbFvvXoXsSVBe1o9f', 'kRAikWitb4kDxaAyBqNrmLmllMEDO3', 'cn-hangzhou')
  30. healthcheck_status = Gauge("healthcheck_status", "ipaddress", ['server_name', 'ipaddress'], registry=registry)
  31. url_http_avgtime = Gauge("url_http_times_avgs", "url of avgs", ['appType', 'url'], registry=registry)
  32. url_http_qps = Gauge("url_http_qps", "url of qps", ['appType','url'], registry=registry)
  33. url_http_expendtime_summary = Gauge("url_http_expendtime_summary", "expendtime summary", ['appType', 'url', 'duration'], registry=registry)
  34. url_http_error_code_cnt = Gauge("url_http_error_code_cnt", "error code", ['appType', 'error_code'], registry=registry)
  35. probe_http_status_code = Gauge("http_status_code", 'h5',['server_name'], registry=registry)
  36. probe_http_total_time = Gauge("http_total_time", 'h5',['server_name'], registry=registry)
  37. probe_http_dns_time = Gauge("http_dns_time", 'h5',['server_name'], registry=registry)
  38. probe_http_connect_time = Gauge("http_connect_time", 'h5',['server_name'], registry=registry)
  39. probe_http_pretransfer_time = Gauge("http_pretransfer_time", 'h5',['server_name'], registry=registry)
  40. probe_http_first_byte_time = Gauge("http_first_byte_time", 'h5',['server_name'], registry=registry)
  41. slb_http_status_code = Gauge("slb_http_status_code", 'slb', ['server_name', 'status'], registry=registry)
  42. #视频合成
  43. produce_video_task_count = Gauge("produce_video_task_count", 'status', ['status'], registry=registry)
  44. produce_video_task_rate = Gauge("produce_video_task_rate", 'rate', ['rate'], registry=registry)
  45. produce_video_task_total = Gauge("produce_video_task_total", 'total', ['total_cnt'], registry=registry)
  46. produce_video_task_sucess = Gauge("produce_video_task_sucess", 'sucess', ['sucess_cnt'], registry=registry)
  47. produce_video_task_ratio = Gauge("produce_video_task_ratio", 'produce_video_ratio', ['ratio'], registry=registry)
  48. produce_video_tts_count = Gauge("produce_video_tts_count", 'success', ['tts_channel'], registry=registry)
  49. produce_video_tts_ratio = Gauge("produce_video_tts_ratio", 'ratio', ['channel'], registry=registry)
  50. logs_app_recommend_log_cnt_300 = Gauge("logs_app_recommend_log_null_cnt_300", "null cnt", ['cnt'], registry=registry)
  51. logs_app_recommend_log_cnt_all = Counter("logs_app_recommend_log_null_cnt_all", "all cnt", ['cnt'], registry=registry)
  52. @app.route('/update')
  53. def update():
  54. request = DescribeInstancesRequest()
  55. request.set_accept_format('json')
  56. request.set_PageSize(100)
  57. # request.set_InstanceNetworkType("vpc")
  58. request.set_Tags([
  59. {
  60. "Key": "ecs"
  61. }
  62. ])
  63. response = client.do_action_with_exception(request)
  64. instance_info = json.loads(response)
  65. intances_list_del()
  66. count = len(instance_info["Instances"]["Instance"])
  67. for i in range(len(instance_info["Instances"]["Instance"])):
  68. instance_id = instance_info["Instances"]["Instance"][i]["InstanceId"]
  69. if instance_info["Instances"]["Instance"][i]["InstanceNetworkType"] == "vpc":
  70. ipaddr = instance_info["Instances"]["Instance"][i]["VpcAttributes"]["PrivateIpAddress"]["IpAddress"][0]
  71. if instance_info["Instances"]["Instance"][i]["InstanceNetworkType"] == "classic":
  72. ipaddr = instance_info["Instances"]["Instance"][i]["InnerIpAddress"]["IpAddress"][0]
  73. server_name = instance_info["Instances"]["Instance"][i]["Tags"]["Tag"][0]["TagValue"]
  74. status = instance_info["Instances"]["Instance"][i]["Status"]
  75. instance_name = instance_info["Instances"]["Instance"][i]["HostName"]
  76. if status == "Running":
  77. status = 1
  78. instance_insert(instance_id, ipaddr, instance_name, server_name, status)
  79. return "更新完成"
  80. @app.route('/app/healthcheck/metrics')
  81. def app_healthcheck():
  82. threads = []
  83. with session_maker() as session:
  84. instance_infos = session.query(InstanceList).filter(InstanceList.server_name=="longvideoapi.prod").all()
  85. for index in range(len(instance_infos)):
  86. ipaddr = instance_infos[index].ipadd
  87. server_name = instance_infos[index].server_name
  88. http_code = healthcheck(ipaddr, server_name)
  89. instance_id = instance_infos[index].instance_id
  90. healthcheck_status.labels(instance_id, server_name, ipaddr).set(http_code)
  91. return Response(generate_latest(registry),mimetype="text/plain")
  92. @app.route('/app/qps/metrics')
  93. def qps_avgtime_count():
  94. threads = []
  95. with session_maker() as session:
  96. intface_infos = session.query(IntfaceList.interface_url).all()
  97. app_type = session.query(app_info.app_type).all()
  98. for i in range(len(intface_infos)):
  99. for index in range(len(app_type)):
  100. url = intface_infos[i].interface_url
  101. appType = app_type[index].app_type
  102. url_avgtime = count_avg_time(appType, url)
  103. url_qps = count_qps(appType, url)
  104. url_http_avgtime.labels(appType, url).set(url_avgtime)
  105. url_http_qps.labels(appType, url).set(url_qps)
  106. return Response(generate_latest(registry),mimetype="text/plain")
  107. @app.route('/h5/metrics')
  108. def h5_healthcheck():
  109. # curl_respon = ex_response(
  110. share_h5 = "share_h5"
  111. download_h5 = "download_h5"
  112. share_h5_url = "https://longvideoh5.piaoquantv.com/core/share?shareSource=customerMessage&fromAppType=0&qrAppType=0&versionCode=321&shareUid=12463024&shareMachineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&h5WxrootPageSource=vlog-pages___category&videoId=2689415&isRecommendShare=1&h5ShareId=backend493cd67dd28f4ee395781d59881567211625976055926&shareDepth=0&state=#"
  113. download_h5_url = "https://longvideoh5.piaoquantv.com/dist_1_3_4/upload?accessToken=fe8914eb2e99d1fe8ddaa2f753f5ec613eb2dfbb&versionCode=323&galleryId=0&fileType=2&machineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&platform=devtools&system=iOS%2010.0.1&appType=0&appId=wx89e7eb06478361d7&pageSource=vlog-pages%2Fwebview&loginUid=12463024&machineInfo=%7B%22sdkVersion%22%3A%222.4.1%22,%22brand%22%3A%22devtools%22,%22language%22%3A%22zh_CN%22,%22model%22%3A%22iPhone%20X%22,%22platform%22%3A%22devtools%22,%22system%22%3A%22iOS%2010.0.1%22,%22weChatVersion%22%3A%228.0.5%22,%22screenHeight%22%3A812,%22screenWidth%22%3A375,%22windowHeight%22%3A730,%22windowWidth%22%3A375,%22softVersion%22%3A%224.1.168%22%7D&wxHelpPagePath=%2Fpackage-my%2Fhelp-feedback%2Fhelp-feedback&transaction=2065ff98-6f27-4f09-c9eb-d366c99dd5d5&videoBarrageSwitch=true&addMusic=1&eventId=0&fromActivityId=0&sessionId=1626833289618-583a312d-81cd-62f9-cdd4-cf914c682d55&subSessionId=1626833289618-583a312d-81cd-62f9-cdd4-cf914c682d55&projectId=&entranceType=#wechat_redirec"
  114. shar_h5_curl_response = ex_response(share_h5_url)
  115. share_h5_url_info = shar_h5_curl_response.getinfo()
  116. download_h5_curl_response = ex_response(download_h5_url)
  117. download_h5_url_info = download_h5_curl_response.getinfo()
  118. probe_http_status_code.labels(share_h5).set(share_h5_url_info["http_code"])
  119. probe_http_status_code.labels(download_h5).set(download_h5_url_info["http_code"])
  120. probe_http_total_time.labels("share_h5").set(share_h5_url_info["total_time"]*1000)
  121. probe_http_total_time.labels("download_h5").set(download_h5_url_info["total_time"]*1000)
  122. probe_http_dns_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  123. probe_http_dns_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  124. probe_http_connect_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  125. probe_http_connect_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  126. probe_http_pretransfer_time.labels("share_h5").set(share_h5_url_info["pretransfer_time"]*1000)
  127. probe_http_pretransfer_time.labels("download_h5").set(download_h5_url_info["pretransfer_time"]*1000)
  128. probe_http_first_byte_time.labels("share_h5").set(share_h5_url_info["first_byte_time"]*1000)
  129. probe_http_first_byte_time.labels("download_h5").set(download_h5_url_info["first_byte_time"]*1000)
  130. return Response(generate_latest(registry), mimetype="text/plain")
  131. @app.route('/slbStatusCode/metrics')
  132. def slb_request_status_metric():
  133. svc_name = {'longvideoapi', 'clip', 'speed'}
  134. for name in svc_name:
  135. res = slb_status_code_count(name)
  136. if res:
  137. for i in range(len(res)):
  138. status = res[i]['status']
  139. cnt = float(res[i]['cnt'])
  140. slb_http_status_code.labels(name, status).set(cnt)
  141. return Response(generate_latest(registry), mimetype="text/plain")
  142. @app.route('/metrics')
  143. def all_metric():
  144. """视频合成类metrics"""
  145. """视频合成成功率"""
  146. current_app.logger.info("测试")
  147. time_stamp = int(time.time())
  148. end_time = int(datetime.datetime.fromtimestamp(time_stamp).strftime('%Y%m%d%H%M%S')) * 1000000000
  149. start_time = int(datetime.datetime.fromtimestamp(time_stamp-300).strftime('%Y%m%d%H%M%S')) * 1000000000
  150. print(start_time, end_time)
  151. query_sql = ("select totalCount , (successCount+processingCount1), round((successCount + processingCount1)/totalCount * 100,2), processingCount ,processingCount1, processingCount2 from "
  152. "(select count(*) as totalCount,"
  153. "sum(case when produce_status in (5,6,7,8) then 1 else 0 end) as successCount,"
  154. "sum(case when produce_status = 99 then 1 else 0 end) as failCount , "
  155. "sum(case when produce_status in(0,1,2,3,4) then 1 else 0 end) as processingCount,"
  156. "sum(case when produce_status in(0,1,2,3,4) and (rate < 0.7 or rate is null) then 1 else 0 end) processingCount1,"
  157. "sum(case when produce_status in(0,1,2,3,4) and rate >= 0.7 then 1 else 0 end) processingCount2 from "
  158. "(select t1.project_id, t1.produce_status, round((t2.last_connect_timestamp - t1.submit_timestamp) / (t1.video_duration/1000), 1) as rate from produce_video_project t1 "
  159. "left join produce_video_project_connect_time t2 on t1.project_id = t2.project_id "
  160. "where t1.project_id > %s and t1.project_id < %s and t1.app_type not in (1,13,15)) s1) ss1" %(start_time ,end_time)
  161. )
  162. res = db_query(query_sql)
  163. if res[0] is not None:
  164. total_cnt = res[0]
  165. else:
  166. total_cnt = 0
  167. if res[1] is not None:
  168. success_cnt = res[1]
  169. else:
  170. success_cnt = 0
  171. if total_cnt == 0:
  172. rate = 100
  173. else:
  174. rate = round((success_cnt/total_cnt), 2)*100
  175. if res[3] is not None:
  176. process_cnt = res[3]
  177. else:
  178. process_cnt = 0
  179. if res[4] is not None:
  180. process_cnt_1 = res[4]
  181. else:
  182. process_cnt_1 = 0
  183. if res[5] is not None:
  184. process_cnt_2 = res[5]
  185. else:
  186. process_cnt_2 = 0
  187. produce_video_task_rate.labels("produce_video_task_rate").set(rate)
  188. produce_video_task_total.labels('total_cnt').set(total_cnt)
  189. produce_video_task_sucess.labels('success_cnt').set(success_cnt)
  190. produce_video_task_count.labels('process_cnt').set(process_cnt)
  191. produce_video_task_count.labels('process_cnt<0.7').set(process_cnt_1)
  192. produce_video_task_count.labels('process_cnt>0.7').set(process_cnt_2)
  193. res = produce_video_task_cnt()
  194. if res:
  195. if res[0]:
  196. total_cnt = res[0]
  197. produce_video_task_count.labels("total").set(total_cnt)
  198. if res[1]:
  199. success_cnt = res[1]
  200. produce_video_task_count.labels("success").set(success_cnt)
  201. if res[2]:
  202. fail_cnt = res[2]
  203. produce_video_task_count.labels("fail").set(fail_cnt)
  204. res = produce_video_ratio_cnt()
  205. if res[1]:
  206. r1 = res[1]
  207. else:
  208. r1 = 0
  209. produce_video_task_ratio.labels("<0.5").set(r1)
  210. if res[2]:
  211. r2 = res[2]
  212. else:
  213. r2 = 0
  214. produce_video_task_ratio.labels("0.5-0.7").set(r2)
  215. if res[3]:
  216. r3 = res[3]
  217. else:
  218. r3 = 0
  219. produce_video_task_ratio.labels("0.7-1.0").set(r3)
  220. if res[4]:
  221. r4 = res[4]
  222. else:
  223. r4 = 0
  224. produce_video_task_ratio.labels("1.0-1.5").set(r4)
  225. if res[5]:
  226. r5 = res[5]
  227. else:
  228. r5 = 0
  229. produce_video_task_ratio.labels("1.5-2.0").set(r5)
  230. if res[6]:
  231. r6 = res[6]
  232. else:
  233. r6 = 0
  234. produce_video_task_ratio.labels(">2.0").set(r6)
  235. # sql = "select v1 as 平均合成耗时,v2 as 平均视频时长, round(v2/v1,1) as 时长耗时比 from (select avg(produce_done_timestamp - submit_timestamp) as v1, avg(video_duration/1000) as v2 from produce_video_project where project_id > {} and project_id < {} and app_type not in (13,15) and produce_status in (5,6,7,8)) as t1".format(start_time, end_time)
  236. # #tts
  237. # res = db_query(sql)
  238. # if res[2] is not None:
  239. # produce_video_task_count.labels("video_progress").set(res[2])
  240. # else:
  241. # produce_video_task_count.labels("video_progress").set(0)
  242. res = logs_tts_count("aliyun",1)
  243. if res is not None:
  244. aliyun_success = int(res[0]["count"])
  245. else:
  246. aliyun_success = 0
  247. produce_video_tts_count.labels("aliyun_success").set(aliyun_success)
  248. res = logs_tts_count("aliyun", 0)
  249. if res is not None:
  250. aliyun_fail = int(res[0]["count"])
  251. else:
  252. aliyun_fail = 0
  253. produce_video_tts_count.labels("aliyun_fail").set(aliyun_fail)
  254. res = logs_tts_count("azure", 1)
  255. if res is not None:
  256. azure_success = int(res[0]["count"])
  257. else:
  258. azure_success = 0
  259. produce_video_tts_count.labels("azure_success").set(azure_success)
  260. res = logs_tts_count("azure", 0)
  261. if res is not None:
  262. azure_fail = int(res[0]["count"])
  263. else:
  264. azure_fail = 0
  265. produce_video_tts_count.labels("azure_fail").set(azure_fail)
  266. """5m计算"""
  267. res = logs_tts_ratio("aliyun", 1)
  268. if res is not None:
  269. aliyun_success = int(res[0]["count"])
  270. else:
  271. aliyun_success = 0
  272. produce_video_tts_count.labels("aliyun_success_5m").set(aliyun_success)
  273. res = logs_tts_ratio("aliyun", 0)
  274. if res is not None:
  275. aliyun_fail = int(res[0]["count"])
  276. else:
  277. aliyun_fail = 0
  278. produce_video_tts_count.labels("aliyun_fail_5m").set(aliyun_fail)
  279. res = logs_tts_ratio("azure", 1)
  280. if res is not None:
  281. azure_success = int(res[0]["count"])
  282. else:
  283. azure_success = 0
  284. produce_video_tts_count.labels("azure_success_5m").set(azure_success)
  285. res = logs_tts_ratio("azure", 0)
  286. if res is not None:
  287. azure_fail = int(res[0]["count"])
  288. else:
  289. azure_fail = 0
  290. produce_video_tts_count.labels("azure_fail_5m").set(azure_fail)
  291. aliyun_cnt = aliyun_success + aliyun_fail
  292. azure_cnt = azure_success + azure_fail
  293. aliyun_ratio = round((aliyun_success/aliyun_cnt)*100,4)
  294. azure_ratio = round((azure_success/azure_cnt)*100, 4)
  295. produce_video_tts_ratio.labels("aliyun").set(aliyun_ratio)
  296. produce_video_tts_ratio.labels("azure").set(azure_ratio)
  297. #当日负载均衡http_code
  298. svc_name = {'longvideoapi', 'clip', 'speed', 'commonapi'}
  299. for name in svc_name:
  300. res = slb_status_code_count(name)
  301. if res:
  302. for i in range(len(res)):
  303. status = res[i]['status']
  304. cnt = float(res[i]['cnt'])
  305. slb_http_status_code.labels(name, status).set(cnt)
  306. #h5 healthcheck
  307. share_h5 = "share_h5"
  308. download_h5 = "download_h5"
  309. share_h5_url = "https://longvideoh5.piaoquantv.com/core/share?shareSource=customerMessage&fromAppType=0&qrAppType=0&versionCode=321&shareUid=12463024&shareMachineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&h5WxrootPageSource=vlog-pages___category&videoId=2689415&isRecommendShare=1&h5ShareId=backend493cd67dd28f4ee395781d59881567211625976055926&shareDepth=0&state=#"
  310. download_h5_url = "https://longvideoh5.piaoquantv.com/dist_1_3_4/upload?accessToken=fe8914eb2e99d1fe8ddaa2f753f5ec613eb2dfbb&versionCode=323&galleryId=0&fileType=2&machineCode=weixin_openid_o0w175fPwp8yrtOGihYJhvnT9Ag4&platform=devtools&system=iOS%2010.0.1&appType=0&appId=wx89e7eb06478361d7&pageSource=vlog-pages%2Fwebview&loginUid=12463024&machineInfo=%7B%22sdkVersion%22%3A%222.4.1%22,%22brand%22%3A%22devtools%22,%22language%22%3A%22zh_CN%22,%22model%22%3A%22iPhone%20X%22,%22platform%22%3A%22devtools%22,%22system%22%3A%22iOS%2010.0.1%22,%22weChatVersion%22%3A%228.0.5%22,%22screenHeight%22%3A812,%22screenWidth%22%3A375,%22windowHeight%22%3A730,%22windowWidth%22%3A375,%22softVersion%22%3A%224.1.168%22%7D&wxHelpPagePath=%2Fpackage-my%2Fhelp-feedback%2Fhelp-feedback&transaction=2065ff98-6f27-4f09-c9eb-d366c99dd5d5&videoBarrageSwitch=true&addMusic=1&eventId=0&fromActivityId=0&sessionId=1626833289618-583a312d-81cd-62f9-cdd4-cf914c682d55&subSessionId=1626833289618-583a312d-81cd-62f9-cdd4-cf914c682d55&projectId=&entranceType=#wechat_redirec"
  311. shar_h5_curl_response = ex_response(share_h5_url)
  312. share_h5_url_info = shar_h5_curl_response.getinfo()
  313. download_h5_curl_response = ex_response(download_h5_url)
  314. download_h5_url_info = download_h5_curl_response.getinfo()
  315. probe_http_status_code.labels(share_h5).set(share_h5_url_info["http_code"])
  316. probe_http_status_code.labels(download_h5).set(download_h5_url_info["http_code"])
  317. probe_http_total_time.labels("share_h5").set(share_h5_url_info["total_time"]*1000)
  318. probe_http_total_time.labels("download_h5").set(download_h5_url_info["total_time"]*1000)
  319. probe_http_dns_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  320. probe_http_dns_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  321. probe_http_connect_time.labels("share_h5").set(share_h5_url_info["dns_time"]*1000)
  322. probe_http_connect_time.labels("download_h5").set(download_h5_url_info["dns_time"]*1000)
  323. probe_http_pretransfer_time.labels("share_h5").set(share_h5_url_info["pretransfer_time"]*1000)
  324. probe_http_pretransfer_time.labels("download_h5").set(download_h5_url_info["pretransfer_time"]*1000)
  325. probe_http_first_byte_time.labels("share_h5").set(share_h5_url_info["first_byte_time"]*1000)
  326. probe_http_first_byte_time.labels("download_h5").set(download_h5_url_info["first_byte_time"]*1000)
  327. #推荐业务
  328. res_null_cnt = count_recommend_null()
  329. logs_app_recommend_log_cnt_300.labels("recommend").set(res_null_cnt)
  330. # # logs_app_recommend_log_cnt_all.labels("recommend").inc(1)
  331. # logs_app_recommend_log_cnt_all.inc(res_null_cnt)
  332. # """ 所有服务health_check """
  333. return Response(generate_latest(registry), mimetype="text/plain")
  334. @app.route('/qps_avgtime/metrics')
  335. def qps_avgtime_metrics():
  336. """统计接口QPM, RT时间,RT时间分布 """
  337. res, url = app_openapi_qps_avgtime_count()
  338. qps = int(res.body[0]["cnt"])
  339. avgtime = int(float(res.body[0]["avg_time"]))
  340. app_type_temp = 1
  341. url_http_qps.labels(app_type_temp, url).set(qps)
  342. url_http_avgtime.labels(app_type_temp, url).set(avgtime)
  343. app_type = ['1', '0', '4', '5', '6', '12', '13', '15']
  344. for i in range(len(app_type)):
  345. appType = app_type[i]
  346. res = count_qps_avgtime(appType)
  347. for i in range(len(res.body)):
  348. url = res.body[i]["requestUri"]
  349. qps = int(res.body[i]["cnt"])
  350. avgtime = int(float(res.body[i]["avg_time"]))
  351. url_http_qps.labels(appType, url).set(qps)
  352. url_http_avgtime.labels(appType, url).set(avgtime)
  353. res = count_rt_less_time_count(appType, 0, 200)
  354. for i in range(len(res.body)):
  355. url = res.body[i]["requestUri"]
  356. count = res.body[i]["cnt"]
  357. url_http_expendtime_summary.labels(appType, url, "0-200").set(count)
  358. res = count_rt_less_time_count(appType, 200, 500)
  359. for i in range(len(res.body)):
  360. url = res.body[i]["requestUri"]
  361. count = res.body[i]["cnt"]
  362. url_http_expendtime_summary.labels(appType, url, "200-300").set(count)
  363. res = count_rt_less_time_count(appType, 500, 1000)
  364. for i in range(len(res.body)):
  365. url = res.body[i]["requestUri"]
  366. count = res.body[i]["cnt"]
  367. url_http_expendtime_summary.labels(appType, url, "500-1000").set(count)
  368. res = count_rt_less_time_count(appType, 1000, 10000)
  369. for i in range(len(res.body)):
  370. url = res.body[i]["requestUri"]
  371. count = res.body[i]["cnt"]
  372. url_http_expendtime_summary.labels(appType, url, ">1000").set(count)
  373. """接口error统计"""
  374. res = error_cnt(appType)
  375. for i in range(len(res.body)):
  376. cnt = res.body[i]["cnt"]
  377. error_code = res.body[i]["resultCode"]
  378. url_http_error_code_cnt.labels(appType,error_code).set(cnt)
  379. return Response(generate_latest(registry), mimetype="text/plain")
  380. @app.route('/svc/healthcheck')
  381. def svc_healthcheck():
  382. sql = "select ipadd ,server_name from instance_list where status=1"
  383. res = db_query_devlop(sql)
  384. if res is not None:
  385. for i in range(len(res)):
  386. ipaddr = res[i][0]
  387. svc_name = res[i][1]
  388. print(ipaddr, svc_name)
  389. http_code = healthcheck(ipaddr, svc_name)
  390. print(http_code)
  391. healthcheck_status.labels(svc_name, ipaddr ).set(http_code)
  392. return Response(generate_latest(registry), mimetype="text/plain")
  393. def produce_video_data():
  394. start_time = (int(time.strftime("%Y%m%d%H%M", time.localtime())) - 30) * 100000000000
  395. end_time = (int(time.strftime("%Y%m%d%H%M", time.localtime()))) * 100000000000
  396. sum_sql = ("select count(*) as totalCount from"
  397. "(select t1.project_id, t1.produce_status "
  398. "from produce_video_project t1 "
  399. "left join produce_video_project_connect_time t2 on t1.project_id = t2.project_id "
  400. " where t1.project_id > %s and t1.project_id < %s and t1.app_type not in (1,13,15)) s1) ss1"%(start_time,end_time)
  401. )
  402. if __name__ == '__main__':
  403. #app.run()
  404. app.run(host='192.168.201.1', port=9091)
  405. app.run()