1
0

endpoint.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 Endpoint(object):
  19. def __init__(self, id, endpoint, ts):
  20. self.id = str(id)
  21. self.endpoint = endpoint
  22. self.ts = ts
  23. def __repr__(self):
  24. return "<Endpoint id=%s, endpoint=%s>" %(self.id, self.id)
  25. __str__ = __repr__
  26. @classmethod
  27. def gets_by_endpoint(cls, endpoints, deadline=0):
  28. if not endpoints:
  29. return []
  30. h = {"Content-type": "application/json"}
  31. qs = "deadline=%d" %deadline
  32. for x in endpoints:
  33. qs += "&endpoints=%s" %x
  34. r = corelib.auth_requests("GET", API_ADDR + "/graph/endpointobj?%s" %qs, headers=h)
  35. if r.status_code != 200:
  36. raise Exception(r.text)
  37. j = r.json() or []
  38. return [cls(*[x["id"], x["endpoint"], x["ts"]]) for x in j]
  39. class EndpointCounter(object):
  40. def __init__(self, endpoint_id, counter, step, type_):
  41. self.endpoint_id = str(endpoint_id)
  42. self.counter = counter
  43. self.step = step
  44. self.type_ = type_
  45. def __repr__(self):
  46. return "<EndpointCounter endpoint_id=%s, counter=%s>" %(self.endpoint_id, self.counter)
  47. __str__ = __repr__
  48. @classmethod
  49. def search_in_endpoint_ids(cls, qs, endpoint_ids, limit=100):
  50. if not endpoint_ids:
  51. return []
  52. eid_str = ",".join(endpoint_ids)
  53. r = corelib.auth_requests("GET", API_ADDR + "/graph/endpoint_counter?eid=%s&metricQuery=%s&limit=%d" %(eid_str, " ".join(qs), limit))
  54. if r.status_code != 200:
  55. raise Exception(r.text)
  56. j = r.json() or []
  57. return [cls(*[x["endpoint_id"], x["counter"], x["step"], x["type"]]) for x in j]