alarm.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #-*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from flask import jsonify, render_template, request, g, abort
  16. from rrd import app
  17. from rrd.model.portal.alarm import Event, EventCase
  18. import json
  19. @app.route("/portal/alarm-dash/case")
  20. def alarm_dash_case_get():
  21. event_cases = []
  22. limit = int(request.args.get("limit") or 10)
  23. page = int(request.args.get("p") or 1)
  24. endpoint_q = request.args.get("endpoint_q") or ""
  25. metric_q = request.args.get("metric_q") or ""
  26. status = request.args.get("status") or ""
  27. cases, total = EventCase.query(page, limit, endpoint_q, metric_q, status)
  28. return render_template("portal/alarm/case.html", **locals())
  29. @app.route("/portal/alarm-dash/case/event")
  30. def alarm_dash_event_get():
  31. limit = int(request.args.get("limit") or 10)
  32. page = int(request.args.get("p") or 1)
  33. case_id = request.args.get("case_id")
  34. if not case_id:
  35. abort(400, "no case id")
  36. _cases = EventCase.select_vs(where='id=%s', params=[case_id], limit=1)
  37. if len(_cases) == 0:
  38. abort(400, "no such case where id=%s" %case_id)
  39. case = _cases[0]
  40. case_events, total = Event.query(event_caseId=case_id, page=page, limit=limit)
  41. return render_template("portal/alarm/case_events.html", **locals())
  42. @app.route("/portal/alarm-dash/case/delete", methods=['POST'])
  43. def alarm_dash_case_delete():
  44. ret = {
  45. "msg": "",
  46. }
  47. ids = request.form.get("ids") or ""
  48. ids = ids.split(",") or []
  49. if not ids:
  50. ret['msg'] = "no case ids"
  51. return json.dumps(ret)
  52. holders = []
  53. for x in ids:
  54. holders.append("%s")
  55. placeholder = ','.join(holders)
  56. where = 'id in (' + placeholder + ')'
  57. params = ids
  58. EventCase.delete(where=where, params=params)
  59. for x in ids:
  60. Event.delete(where='event_caseId=%s', params=[x])
  61. return json.dumps(ret)
  62. @app.route("/portal/alarm-dash/case/event/delete", methods=['POST'])
  63. def alarm_dash_case_event_delete():
  64. ret = {
  65. "msg": "",
  66. }
  67. ids = request.form.get("ids") or ""
  68. ids = ids.split(",") or []
  69. if not ids:
  70. ret['msg'] = "no case ids"
  71. return json.dumps(ret)
  72. holders = []
  73. for x in ids:
  74. holders.append("%s")
  75. placeholder = ','.join(holders)
  76. where = 'id in (' + placeholder + ')'
  77. params = ids
  78. Event.delete(where=where, params=params)
  79. return json.dumps(ret)