tmpgraph.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 TmpGraph(object):
  19. def __init__(self, id, endpoints, counters):
  20. self.id = str(id)
  21. self.endpoints = endpoints or []
  22. self.endpoints = filter(None, [x.strip() for x in self.endpoints])
  23. self.counters = counters or []
  24. self.counters = filter(None, [x.strip() for x in self.counters])
  25. def __repr__(self):
  26. return "<TmpGraph id=%s, endpoints=%s, counters=%s>" %(self.id, self.endpoints, self.counters)
  27. __str__ = __repr__
  28. @classmethod
  29. def get(cls, id):
  30. h = {"Content-type": "application/json"}
  31. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/tmpgraph/%s" %(id,), headers=h)
  32. if r.status_code != 200:
  33. raise Exception(r.text)
  34. j = r.json()
  35. return j and cls(*[id, j["endpoints"], j["counters"]])
  36. @classmethod
  37. def add(cls, endpoints, counters):
  38. d = {
  39. "endpoints": endpoints,
  40. "counters": counters,
  41. }
  42. h = {'Content-type': 'application/json'}
  43. r = corelib.auth_requests("POST", API_ADDR + "/dashboard/tmpgraph", headers=h, data=json.dumps(d))
  44. if r.status_code != 200:
  45. raise Exception(r.text)
  46. j = r.json()
  47. return j and j.get('id')