1
0

index.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. import json
  16. import re
  17. from flask import request, abort, g, render_template
  18. from rrd import app, config
  19. from rrd import corelib
  20. def get_api_endpoints(q, tags, page=1, limit=100):
  21. if not q and not tags:
  22. raise Exception("no query params given")
  23. h = {"Content-type": "application/json"}
  24. r = corelib.auth_requests("GET", config.API_ADDR + "/graph/endpoint?q=%s&limit=%d&page=%d&tags=%s" %(q, limit, page, tags), headers=h)
  25. if r.status_code != 200:
  26. raise Exception(r.text)
  27. j = sorted(r.json(), key=lambda x:x["endpoint"])
  28. return j
  29. @app.route("/")
  30. def index():
  31. q = request.args.get("q") or "."
  32. raw_tag = request.args.get("tags") or ""
  33. tags = ','.join(re.split('\s*,\s*', raw_tag))
  34. limit = int(request.args.get("limit") or 50)
  35. page = int(request.args.get("page") or 1)
  36. endpoint_objs = get_api_endpoints(q, tags, page, limit)
  37. return render_template("index.html", **locals())
  38. @app.route("/api/endpoints")
  39. def api_endpoints():
  40. ret = {
  41. "ok": False,
  42. "msg": "",
  43. "data": [],
  44. }
  45. q = request.args.get("q") or "."
  46. raw_tag = request.args.get("tags") or ""
  47. tags = ','.join(re.split('\s*,\s*', raw_tag))
  48. limit = int(request.args.get("limit") or 100)
  49. page = int(request.args.get("page") or 1)
  50. try:
  51. data = get_api_endpoints(q, tags, page, limit)
  52. ret['data'] = data
  53. ret['ok'] = True
  54. return json.dumps(ret)
  55. except Exception as e:
  56. abort(400, str(ret))
  57. @app.route("/api/counters", methods=["POST"])
  58. def api_get_counters():
  59. ret = {
  60. "ok": False,
  61. "msg": "",
  62. "data": [],
  63. }
  64. q = request.form.get("q") or ""
  65. limit = int(request.form.get("limit") or 50)
  66. page = int(request.form.get("page") or 1)
  67. eids = request.form.get("eids") or ""
  68. eids = eids and json.loads(eids) or []
  69. if not (eids or q):
  70. ret['msg'] = "no endpoints or counter given"
  71. return json.dumps(ret)
  72. h = {"Content-type": "application/json"}
  73. r = corelib.auth_requests("GET", config.API_ADDR + "/graph/endpoint_counter?eid=%s&metricQuery=%s&limit=%d&page=%d" %(",".join(eids), q, limit, page), headers=h)
  74. if r.status_code != 200:
  75. abort(400, r.text)
  76. j = r.json()
  77. counters_map = {}
  78. for x in j:
  79. counters_map[x['counter']] = [x['counter'], x['type'], x['step']]
  80. sorted_counters = sorted(counters_map.keys())
  81. sorted_values = [counters_map[x] for x in sorted_counters]
  82. ret['data'] = sorted_values
  83. ret['ok'] = True
  84. return json.dumps(ret)
  85. @app.route("/api/counters", methods=["DELETE"])
  86. def api_delete_counters():
  87. ret = {
  88. "ok": False,
  89. "msg": "",
  90. }
  91. endpoints = request.form.getlist("endpoints[]") or []
  92. counters = request.form.getlist("counters[]") or []
  93. if len(endpoints) == 0 or len(counters) == 0:
  94. ret['msg'] = "no endpoint and counter"
  95. return json.dumps(ret)
  96. h = {"Content-type": "application/json"}
  97. d = {
  98. "endpoints": endpoints,
  99. "counters": counters,
  100. }
  101. r = corelib.auth_requests("DELETE", config.API_ADDR + "/graph/counter", headers=h, data=json.dumps(d))
  102. if r.status_code != 200:
  103. abort(r.status_code, r.text)
  104. j = r.json()
  105. ret["ok"] = True
  106. ret["data"] = "%s counters affected" %j.get("affected_counter")
  107. return json.dumps(ret)
  108. @app.route("/api/endpoints", methods=["DELETE"])
  109. def api_delete_endpoints():
  110. ret = {
  111. "ok": False,
  112. "msg": "",
  113. }
  114. endpoints = request.form.getlist("endpoints[]") or []
  115. if len(endpoints) == 0:
  116. ret['msg'] = "no endpoint"
  117. return json.dumps(ret)
  118. h = {"Content-type": "application/json"}
  119. d = endpoints
  120. r = corelib.auth_requests("DELETE", config.API_ADDR + "/graph/endpoint", headers=h, data=json.dumps(d))
  121. if r.status_code != 200:
  122. abort(r.status_code, r.text)
  123. j = r.json()
  124. ret["ok"] = True
  125. ret["data"] = "%s counters affected, %s endpoints affected" %(j.get("affected_counter"), j.get("affected_endpoint"))
  126. return json.dumps(ret)