limit.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import logging
  20. import mpp.api
  21. import mpp.utils
  22. import mpp.cout
  23. class Plugin(mpp.api.Plugin, mpp.api.IConfigurable, mpp.api.IRunable):
  24. def declare_configuration(self, parser):
  25. parser.add_option("--hotspots", "--hs", default=None, help="If not set (none), all exceeded limits are printed."
  26. " If set, exceeded limits are sorted (the worst is the first) and only first HOTSPOTS limits are printed."
  27. " [default: %default]", type=int)
  28. parser.add_option("--disable-suppressions", "--ds", action="store_true", default=False,
  29. help = "If not set (none), all suppressions are ignored"
  30. " and associated warnings are printed. [default: %default]")
  31. def configure(self, options):
  32. self.hotspots = options.__dict__['hotspots']
  33. self.no_suppress = options.__dict__['disable_suppressions']
  34. def run(self, args):
  35. return main(self, args)
  36. def main(plugin, args):
  37. exit_code = 0
  38. loader_prev = plugin.get_plugin_loader().get_plugin('mpp.dbf').get_loader_prev()
  39. loader = plugin.get_plugin_loader().get_plugin('mpp.dbf').get_loader()
  40. warn_plugin = plugin.get_plugin_loader().get_plugin('mpp.warn')
  41. paths = None
  42. if len(args) == 0:
  43. paths = [""]
  44. else:
  45. paths = args
  46. # Try to optimise iterative change scans
  47. modified_file_ids = None
  48. if warn_plugin.mode != warn_plugin.MODE_ALL:
  49. modified_file_ids = get_list_of_modified_files(loader, loader_prev)
  50. for path in paths:
  51. path = mpp.utils.preprocess_path(path)
  52. for limit in warn_plugin.iterate_limits():
  53. logging.info("Applying limit: " + str(limit))
  54. filters = [limit.filter]
  55. if modified_file_ids != None:
  56. filters.append(('file_id', 'IN', modified_file_ids))
  57. sort_by = None
  58. limit_by = None
  59. if plugin.hotspots != None:
  60. sort_by = limit.field
  61. if limit.type == "max":
  62. sort_by = "-" + sort_by
  63. limit_by = plugin.hotspots
  64. selected_data = loader.load_selected_data(limit.namespace,
  65. fields = [limit.field],
  66. path=path,
  67. filters = filters,
  68. sort_by=sort_by,
  69. limit_by=limit_by)
  70. if selected_data == None:
  71. mpp.utils.report_bad_path(path)
  72. exit_code += 1
  73. continue
  74. for select_data in selected_data:
  75. is_modified = None
  76. diff = None
  77. file_data = loader.load_file_data(select_data.get_path())
  78. file_data_prev = loader_prev.load_file_data(select_data.get_path())
  79. if file_data_prev != None:
  80. if file_data.get_checksum() == file_data_prev.get_checksum():
  81. diff = 0
  82. is_modified = False
  83. else:
  84. matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
  85. prev_id = matcher.get_prev_id(select_data.get_region().get_id())
  86. if matcher.is_matched(select_data.get_region().get_id()):
  87. if matcher.is_modified(select_data.get_region().get_id()):
  88. is_modified = True
  89. else:
  90. is_modified = False
  91. diff = mpp.api.DiffData(select_data,
  92. file_data_prev.get_region(prev_id)).get_data(limit.namespace, limit.field)
  93. if (warn_plugin.is_mode_matched(limit.limit,
  94. select_data.get_data(limit.namespace, limit.field),
  95. diff,
  96. is_modified) == False):
  97. continue
  98. is_sup = is_metric_suppressed(limit.namespace, limit.field, loader, select_data)
  99. if is_sup == True and plugin.no_suppress == False:
  100. continue
  101. exit_code += 1
  102. region_cursor = 0
  103. region_name = None
  104. if select_data.get_region() != None:
  105. region_cursor = select_data.get_region().cursor
  106. region_name = select_data.get_region().name
  107. report_limit_exceeded(select_data.get_path(),
  108. region_cursor,
  109. limit.namespace,
  110. limit.field,
  111. region_name,
  112. select_data.get_data(limit.namespace, limit.field),
  113. diff,
  114. limit.limit,
  115. is_modified,
  116. is_sup)
  117. return exit_code
  118. def get_list_of_modified_files(loader, loader_prev):
  119. logging.info("Identifying changed files...")
  120. old_files_map = {}
  121. for each in loader_prev.iterate_file_data():
  122. old_files_map[each.get_path()] = each.get_checksum()
  123. if len(old_files_map) == 0:
  124. return None
  125. modified_file_ids = []
  126. for each in loader.iterate_file_data():
  127. if len(modified_file_ids) > 1000: # If more than 1000 files changed, skip optimisation
  128. return None
  129. if (each.get_path() not in old_files_map.keys()) or old_files_map[each.get_path()] != each.get_checksum():
  130. modified_file_ids.append(str(each.get_id()))
  131. old_files_map = None
  132. if len(modified_file_ids) != 0:
  133. modified_file_ids = " , ".join(modified_file_ids)
  134. modified_file_ids = "(" + modified_file_ids + ")"
  135. return modified_file_ids
  136. return None
  137. def is_metric_suppressed(metric_namespace, metric_field, loader, select_data):
  138. data = loader.load_file_data(select_data.get_path())
  139. if select_data.get_region() != None:
  140. data = data.get_region(select_data.get_region().get_id())
  141. sup_data = data.get_data('std.suppress', 'list')
  142. else:
  143. sup_data = data.get_data('std.suppress.file', 'list')
  144. if sup_data != None and sup_data.find('[' + metric_namespace + ':' + metric_field + ']') != -1:
  145. return True
  146. return False
  147. def report_limit_exceeded(path, cursor, namespace, field, region_name,
  148. stat_level, trend_value, stat_limit,
  149. is_modified, is_suppressed):
  150. if region_name != None:
  151. message = "Metric '" + namespace + ":" + field + "' for region '" + region_name + "' exceeds the limit."
  152. else:
  153. message = "Metric '" + namespace + ":" + field + "' exceeds the limit."
  154. details = [("Metric name", namespace + ":" + field),
  155. ("Region name", region_name),
  156. ("Metric value", stat_level),
  157. ("Modified", is_modified),
  158. ("Change trend", '{0:{1}}'.format(trend_value, '+' if trend_value else '')),
  159. ("Limit", stat_limit),
  160. ("Suppressed", is_suppressed)]
  161. mpp.cout.notify(path, cursor, mpp.cout.SEVERITY_WARNING, message, details)