1
0

screen.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 DashboardScreen(object):
  19. def __init__(self, id, pid, name):
  20. self.id = str(id)
  21. self.pid = str(pid)
  22. self.name = name
  23. def __repr__(self):
  24. return "<DashboardScreen id=%s, name=%s, pid=%s>" %(self.id, self.name, self.pid)
  25. __str__ = __repr__
  26. @classmethod
  27. def get(cls, id):
  28. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screen/%s" %(id,))
  29. if r.status_code != 200:
  30. raise Exception(r.text)
  31. j = r.json()
  32. if j:
  33. row = [j["id"], j["pid"], j["name"]]
  34. return cls(*row)
  35. @classmethod
  36. def gets_by_pid(cls, pid):
  37. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screens/pid/%s" %(pid,))
  38. if r.status_code != 200:
  39. raise Exception(r.text)
  40. j = r.json() or []
  41. return [cls(*[x["id"], x["pid"], x["name"]]) for x in j]
  42. @classmethod
  43. def gets_all(cls, limit=500):
  44. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screens?limit=%s" %(limit,))
  45. if r.status_code != 200:
  46. raise Exception(r.text)
  47. j = r.json() or []
  48. return [cls(*[x["id"], x["pid"], x["name"]]) for x in j]
  49. @classmethod
  50. def add(cls, pid, name):
  51. d = {"pid": pid, "name": name}
  52. r = corelib.auth_requests("POST", API_ADDR + "/dashboard/screen", data = d)
  53. if r.status_code != 200:
  54. raise Exception(r.text)
  55. j = r.json()
  56. return cls(*[j["id"], j["pid"], j["name"]])
  57. @classmethod
  58. def remove(cls, id):
  59. h = {"Content-type": "application/json"}
  60. r = corelib.auth_requests("DELETE", API_ADDR + "/dashboard/screen/%s" %(id,), headers=h)
  61. if r.status_code != 200:
  62. raise Exception(r.text)
  63. return r.json()
  64. def update(self, pid=None, name=None):
  65. d = {}
  66. if pid:
  67. d["pid"] = pid
  68. if name:
  69. d["name"] = name
  70. r = corelib.auth_requests("PUT", API_ADDR + "/dashboard/screen/%s" %self.id, data = d)
  71. if r.status_code != 200:
  72. raise Exception(r.text)
  73. return r.json()