screen.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 copy
  17. import json
  18. from flask import render_template, abort, request, url_for, redirect, g
  19. from flask_babel import gettext
  20. import time
  21. import datetime
  22. from rrd import app
  23. from rrd.model.screen import DashboardScreen
  24. from rrd.model.graph import DashboardGraph
  25. from rrd import consts
  26. from rrd.utils.graph_urls import generate_graph_urls
  27. from rrd import config
  28. @app.route("/screen", methods=["GET", "POST"])
  29. def dash_screens():
  30. top_screens = DashboardScreen.gets_by_pid(pid='0') or []
  31. top_screens = sorted(top_screens, key=lambda x:x.name)
  32. return render_template("screen/index.html", **locals())
  33. @app.route("/screen/<int:sid>/delete")
  34. def dash_screen_delete(sid):
  35. screen = DashboardScreen.get(sid)
  36. if not screen:
  37. abort(404, "no such screen")
  38. DashboardScreen.remove(sid)
  39. return redirect("/screen")
  40. @app.route("/screen/<int:sid>/edit", methods=["GET", "POST"])
  41. def dash_screen_edit(sid):
  42. screen = DashboardScreen.get(sid)
  43. if not screen:
  44. abort(404, "no such screen")
  45. if request.method == "POST":
  46. screen_name = request.form.get("screen_name").strip()
  47. screen.update(name=screen_name)
  48. return redirect("/screen/%s" %screen.id)
  49. else:
  50. return render_template("screen/edit.html", **locals())
  51. @app.route("/screen/<int:sid>/clone", methods=["GET", "POST"])
  52. def dash_screen_clone(sid):
  53. screen = DashboardScreen.get(sid)
  54. if not screen:
  55. abort(404, "no such screen")
  56. if request.method == "POST":
  57. screen_name = request.form.get("screen_name").strip()
  58. with_graph = request.form.get("with_graph")
  59. new_s = DashboardScreen.add(screen.pid, screen_name)
  60. if not new_s:
  61. abort(404, gettext("screen create fail"))
  62. if with_graph:
  63. old_graphs = DashboardGraph.gets_by_screen_id(sid)
  64. for o in old_graphs:
  65. DashboardGraph.add(o.title, o.hosts, o.counters, new_s.id,
  66. o.timespan, o.graph_type, o.method, o.position)
  67. return redirect("/screen/%s" %new_s.id)
  68. else:
  69. return render_template("screen/clone.html", **locals())
  70. @app.route("/graph/<int:gid>/delete")
  71. def dash_graph_delete(gid):
  72. graph = DashboardGraph.get(gid)
  73. if not graph:
  74. abort(404, "no such graph")
  75. DashboardGraph.remove(gid)
  76. return redirect("/screen/" + graph.screen_id)
  77. @app.route("/screen/<int:sid>")
  78. def dash_screen(sid):
  79. start = request.args.get("start")
  80. end = request.args.get("end")
  81. top_screens = DashboardScreen.gets_by_pid(pid=0)
  82. top_screens = sorted(top_screens, key=lambda x:x.name)
  83. screen = DashboardScreen.get(sid)
  84. if not screen:
  85. abort(404, "no screen")
  86. if str(screen.pid) == '0':
  87. sub_screens = DashboardScreen.gets_by_pid(pid=sid)
  88. sub_screens = sorted(sub_screens, key=lambda x:x.name)
  89. return render_template("screen/top_screen.html", **locals())
  90. pscreen = DashboardScreen.get(screen.pid)
  91. sub_screens = DashboardScreen.gets_by_pid(pid=screen.pid)
  92. sub_screens = sorted(sub_screens, key=lambda x:x.name)
  93. graphs = DashboardGraph.gets_by_screen_id(screen.id)
  94. all_graphs = []
  95. for graph in graphs:
  96. all_graphs.extend(generate_graph_urls(graph, start, end) or [])
  97. all_graphs = sorted(all_graphs, key=lambda x: (x.position, x.id))
  98. return render_template("screen/screen.html", **locals())
  99. @app.route("/screen/embed/<int:sid>")
  100. def dash_screen_embed(sid):
  101. start = request.args.get("start")
  102. end = request.args.get("end")
  103. screen = DashboardScreen.get(sid)
  104. if not screen:
  105. abort(404, "no screen")
  106. if screen.pid == '0':
  107. abort(404, "top screen")
  108. graphs = DashboardGraph.gets_by_screen_id(screen.id)
  109. all_graphs = []
  110. for graph in graphs:
  111. all_graphs.extend(generate_graph_urls(graph, start, end) or [])
  112. all_graphs = sorted(all_graphs, key=lambda x: (x.position, x.id))
  113. return render_template("screen/screen_embed.html", **locals())
  114. @app.route("/screen/add", methods=["GET", "POST"])
  115. def dash_screen_add():
  116. if request.method == "POST":
  117. name = request.form.get("screen_name").strip()
  118. pid = request.form.get("pid", '0')
  119. screen = DashboardScreen.add(pid, name)
  120. return redirect("/screen/%s" % screen.id)
  121. else:
  122. pid = request.args.get("pid", '0')
  123. try:
  124. screen = DashboardScreen.get(pid)
  125. except:
  126. screen = None
  127. return render_template("screen/add.html", **locals())
  128. @app.route("/screen/<int:sid>/graph", methods=["GET", "POST"])
  129. def dash_graph_add(sid):
  130. all_screens = DashboardScreen.gets_all()
  131. top_screens = [x for x in all_screens if x.pid == '0']
  132. children = []
  133. for t in top_screens:
  134. children.append([x for x in all_screens if x.pid == t.id])
  135. screen = DashboardScreen.get(sid)
  136. if not screen:
  137. abort(404, "no screen")
  138. pscreen = DashboardScreen.get(screen.pid)
  139. if request.method == "POST":
  140. title = request.form.get("title").strip()
  141. hosts = request.form.get("hosts", "").strip()
  142. hosts = hosts and hosts.split("\n") or []
  143. hosts = [x.strip() for x in hosts]
  144. counters = request.form.get("counters", "").strip()
  145. counters = counters and counters.split("\n") or []
  146. counters = [x.strip() for x in counters]
  147. timespan = int(request.form.get("timespan", 3600))
  148. graph_type = request.form.get("graph_type", 'h')
  149. method = request.form.get("method", '').upper()
  150. position = int(request.form.get("position", 0))
  151. graph = DashboardGraph.add(title, hosts, counters, sid,
  152. timespan, graph_type, method, position)
  153. return redirect("/screen/%s" % sid)
  154. else:
  155. gid = request.args.get("gid")
  156. graph = gid and DashboardGraph.get(gid)
  157. return render_template("screen/graph_add.html", config=config, **locals())
  158. @app.route("/graph/<int:gid>/edit", methods=["GET", "POST"])
  159. def dash_graph_edit(gid):
  160. error = ""
  161. graph = DashboardGraph.get(gid)
  162. if not graph:
  163. abort(404, "no graph")
  164. all_screens = DashboardScreen.gets_all()
  165. top_screens = [x for x in all_screens if x.pid == '0']
  166. children = []
  167. for t in top_screens:
  168. children.append([x for x in all_screens if x.pid == t.id])
  169. screen = DashboardScreen.get(graph.screen_id)
  170. if not screen:
  171. abort(404, "no screen")
  172. pscreen = DashboardScreen.get(screen.pid)
  173. if request.method == "POST":
  174. ajax = request.form.get("ajax", "")
  175. screen_id = request.form.get("screen_id")
  176. title = request.form.get("title", "").strip()
  177. hosts = request.form.get("hosts", "").strip()
  178. hosts = hosts and hosts.split("\n") or []
  179. hosts = [x.strip() for x in hosts]
  180. counters = request.form.get("counters", "").strip()
  181. counters = counters and counters.split("\n") or []
  182. counters = [x.strip() for x in counters]
  183. timespan = request.form.get("timespan", 3600)
  184. graph_type = request.form.get("graph_type", 'h')
  185. method = request.form.get("method", '').upper()
  186. position = request.form.get("position", 0)
  187. graph = graph.update(title, hosts, counters, screen_id,
  188. timespan, graph_type, method, position)
  189. error = gettext("edit successful")
  190. if not ajax:
  191. return render_template("screen/graph_edit.html", config=config, **locals())
  192. else:
  193. return "ok"
  194. else:
  195. ajax = request.args.get("ajax", "")
  196. return render_template("screen/graph_edit.html", **locals())
  197. @app.route("/graph/multi_edit", methods=["GET", "POST"])
  198. def dash_graph_multi_edit():
  199. ret = {
  200. "ok": False,
  201. "msg": "",
  202. "data": [],
  203. }
  204. if request.method == "POST":
  205. d = request.data
  206. try:
  207. jdata = json.loads(d)
  208. except ValueError:
  209. jdata = None
  210. if not jdata:
  211. return json.dumps({
  212. "ok": False,
  213. "msg": "no_data_post",
  214. })
  215. rows = []
  216. for x in jdata:
  217. rows.append({"id": x["id"], "hosts": x["endpoints"], "counters": x["counters"]})
  218. DashboardGraph.update_multi(rows)
  219. return json.dumps({
  220. "ok": True,
  221. "msg": "",
  222. })
  223. elif request.method == "GET":
  224. sid = request.args.get("sid")
  225. if not sid or not DashboardScreen.get(sid):
  226. ret["msg"] = "no_screen"
  227. return json.dumps(ret)
  228. ret["ok"] = True
  229. graphs = DashboardGraph.gets_by_screen_id(sid)
  230. ret['data'] = [{"id": x.id, "title": x.title, "endpoints":x.hosts, "counters":x.counters} for x in graphs]
  231. return json.dumps(ret)