tmpgraph.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #-*- coding:utf-8 -*-
  2. import json
  3. from rrd.config import API_ADDR
  4. from rrd import corelib
  5. class TmpGraph(object):
  6. def __init__(self, id, endpoints, counters):
  7. self.id = str(id)
  8. self.endpoints = endpoints or []
  9. self.endpoints = filter(None, [x.strip() for x in self.endpoints])
  10. self.counters = counters or []
  11. self.counters = filter(None, [x.strip() for x in self.counters])
  12. def __repr__(self):
  13. return "<TmpGraph id=%s, endpoints=%s, counters=%s>" %(self.id, self.endpoints, self.counters)
  14. __str__ = __repr__
  15. @classmethod
  16. def get(cls, id):
  17. h = {"Content-type": "application/json"}
  18. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/tmpgraph/%s" %(id,), headers=h)
  19. if r.status_code != 200:
  20. raise Exception(r.text)
  21. j = r.json()
  22. return j and cls(*[id, j["endpoints"], j["counters"]])
  23. @classmethod
  24. def add(cls, endpoints, counters):
  25. d = {
  26. "endpoints": endpoints,
  27. "counters": counters,
  28. }
  29. h = {'Content-type': 'application/json'}
  30. r = corelib.auth_requests("POST", API_ADDR + "/dashboard/tmpgraph", headers=h, data=json.dumps(d))
  31. if r.status_code != 200:
  32. raise Exception(r.text)
  33. j = r.json()
  34. return j and j.get('id')