1
0

rrdgraph.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. from rrd.config import API_ADDR
  16. import json
  17. from rrd import corelib
  18. def graph_history(endpoints, counters, cf, start, end):
  19. #TODO:step
  20. params = {
  21. "start_time": start,
  22. "end_time": end,
  23. "consol_fun": cf,
  24. "hostnames": endpoints,
  25. "counters": counters,
  26. }
  27. h = {"Content-type": "application/json"}
  28. r = corelib.auth_requests("POST", "%s/graph/history" %API_ADDR, headers=h, data=json.dumps(params))
  29. if r.status_code != 200:
  30. raise Exception("%s : %s" %(r.status_code, r.text))
  31. return r.json()
  32. def merge_list(a, b):
  33. sum = []
  34. a_len = len(a)
  35. b_len = len(b)
  36. l1 = min(a_len, b_len)
  37. l2 = max(a_len, b_len)
  38. if a_len < b_len:
  39. a, b = b, a
  40. for i in range(0, l1):
  41. if a[i] is None and b[i] is None:
  42. sum.append(None)
  43. elif a[i] is None:
  44. sum.append(b[i])
  45. elif b[i] is None:
  46. sum.append(a[i])
  47. else:
  48. sum.append(a[i] + b[i])
  49. for i in range(l1, l2):
  50. sum.append(a[i])
  51. return sum
  52. def CF(cf, values):
  53. if cf == 'AVERAGE':
  54. return float(sum(values))/len(values)
  55. elif cf == 'MAX':
  56. return max(values)
  57. elif cf == 'MIN':
  58. return min(values)
  59. elif cf == 'LAST':
  60. return values[-1]