1
0

graph.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. from rrd.config import API_ADDR
  17. from rrd import corelib
  18. class DashboardGraph(object):
  19. def __init__(self, id, title, hosts, counters, screen_id,
  20. timespan=3600, graph_type='h', method='', position=0):
  21. self.id = str(id)
  22. self.title = title
  23. self.hosts = hosts or []
  24. self.counters = counters or []
  25. self.screen_id = str(screen_id)
  26. self.timespan = timespan
  27. self.graph_type = graph_type
  28. self.method = method.upper() # method can be ["", "sum", "average", "max", "min", "last"]
  29. self.position = position or int(self.id) # init as self.id
  30. def __repr__(self):
  31. return "<DashboardGraph id=%s, title=%s, screen_id=%s>" %(self.id, self.title, self.screen_id)
  32. __str__ = __repr__
  33. @classmethod
  34. def gets_by_screen_id(cls, screen_id):
  35. h = {"Content-type": "application/json"}
  36. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/graphs/screen/%s" %(screen_id,), headers=h)
  37. if r.status_code != 200:
  38. raise Exception(r.text)
  39. j = r.json()
  40. return [cls(*[x["graph_id"], x["title"], x["endpoints"], x["counters"], \
  41. x["screen_id"], x["timespan"], x["graph_type"], x["method"], x["position"]]) for x in j]
  42. @classmethod
  43. def get(cls, id):
  44. h = {"Content-type": "application/json"}
  45. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/graph/%s" %(id,), headers=h)
  46. if r.status_code != 200:
  47. raise Exception(r.text)
  48. x = r.json()
  49. return x and cls(*[x["graph_id"], x["title"], x["endpoints"], x["counters"], \
  50. x["screen_id"], x["timespan"], x["graph_type"], x["method"], x["position"]])
  51. @classmethod
  52. def add(cls, title, hosts, counters, screen_id,
  53. timespan=3600, graph_type='h', method='', position=0):
  54. d = {
  55. "screen_id": int(screen_id),
  56. "title": title,
  57. "endpoints": hosts,
  58. "counters": counters,
  59. "timespan": int(timespan),
  60. "graph_type": graph_type,
  61. "method": method,
  62. "position": int(position),
  63. "falcon_tags": "",
  64. }
  65. h = {"Content-type": "application/json"}
  66. r = corelib.auth_requests("POST", API_ADDR + "/dashboard/graph", data = json.dumps(d), headers =h )
  67. if r.status_code != 200:
  68. raise Exception(r.text)
  69. j = r.json()
  70. graph_id = j and j.get("id")
  71. return graph_id and cls.get(graph_id)
  72. @classmethod
  73. def remove(cls, id):
  74. h = {"Content-type": "application/json"}
  75. r = corelib.auth_requests("DELETE", API_ADDR + "/dashboard/graph/%s" %(id,), headers=h)
  76. if r.status_code != 200:
  77. raise Exception(r.text)
  78. return r.json()
  79. def update(self, title=None, hosts=None, counters=None, screen_id=None,
  80. timespan=None, graph_type=None, method=None, position=None):
  81. title = self.title if title is None else title
  82. hosts = self.hosts if hosts is None else hosts
  83. counters = self.counters if counters is None else counters
  84. screen_id = screen_id or self.screen_id
  85. timespan = timespan or self.timespan
  86. graph_type = graph_type or self.graph_type
  87. method = method if method is not None else self.method
  88. position = position or self.position
  89. d = {
  90. "screen_id": int(screen_id),
  91. "title": title,
  92. "endpoints": hosts,
  93. "counters": counters,
  94. "timespan": int(timespan),
  95. "graph_type": graph_type,
  96. "method": method,
  97. "position": int(position),
  98. "falcon_tags": "",
  99. }
  100. h = {"Content-type": "application/json"}
  101. r = corelib.auth_requests("PUT", API_ADDR + "/dashboard/graph/%s" %(self.id,), data = json.dumps(d), headers =h )
  102. if r.status_code != 200:
  103. raise Exception(r.text)
  104. j = r.json()
  105. graph_id = j and j.get("id")
  106. return graph_id and DashboardGraph.get(graph_id)
  107. @classmethod
  108. def update_multi(cls, rows):
  109. for x in rows:
  110. id = x["id"]
  111. hosts = x["hosts"] or []
  112. counters = x["counters"] or []
  113. grh = cls.get(id)
  114. grh and grh.update(hosts=hosts, counters=counters)