limit.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. import logging
  8. import re
  9. import mpp.api
  10. import mpp.utils
  11. import mpp.cout
  12. class Plugin(mpp.api.Plugin, mpp.api.IRunable):
  13. def print_warnings(self, args):
  14. exit_code = 0
  15. warnings = []
  16. limit_backend = self.get_plugin('std.tools.limit_backend')
  17. paths = None
  18. if len(args) == 0:
  19. paths = [""]
  20. else:
  21. paths = args
  22. for path in paths:
  23. path = mpp.utils.preprocess_path(path)
  24. for limit in limit_backend.iterate_limits():
  25. warns_count = 0
  26. logging.info("Applying limit: " + str(limit))
  27. warnings = limit_backend.get_warnings(path, limit)
  28. if warnings == None:
  29. exit_code += 1
  30. else:
  31. for warning in warnings:
  32. report_limit_exceeded(warning.path,
  33. warning.cursor,
  34. warning.namespace,
  35. warning.field,
  36. warning.region_name,
  37. warning.stat_level,
  38. warning.trend_value,
  39. warning.stat_limit,
  40. warning.is_modified,
  41. warning.is_suppressed)
  42. exit_code += len(warnings)
  43. mpp.cout.notify(path, None, mpp.cout.SEVERITY_INFO, "{0} regions exceeded the limit {1}".format(len(warnings), str(limit)))
  44. return exit_code
  45. def run(self, args):
  46. return self.print_warnings(args)
  47. def report_limit_exceeded(path, cursor, namespace, field, region_name,
  48. stat_level, trend_value, stat_limit,
  49. is_modified, is_suppressed):
  50. if region_name != None:
  51. message = "Metric '" + namespace + ":" + field + "' for region '" + region_name + "' exceeds the limit."
  52. else:
  53. message = "Metric '" + namespace + ":" + field + "' exceeds the limit."
  54. details = [("Metric name", namespace + ":" + field),
  55. ("Region name", region_name),
  56. ("Metric value", stat_level),
  57. ("Modified", is_modified),
  58. ("Change trend", '{0:{1}}'.format(trend_value, '+' if trend_value else '')),
  59. ("Limit", stat_limit),
  60. ("Suppressed", is_suppressed)]
  61. mpp.cout.notify(path, cursor, mpp.cout.SEVERITY_WARNING, message, details)