limit.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. limit_warnings = None
  60. if plugin.hotspots != None:
  61. sort_by = limit.field
  62. if limit.type == "max":
  63. sort_by = "-" + sort_by
  64. if warn_plugin.mode == warn_plugin.MODE_ALL:
  65. # if it is not ALL mode, the tool counts number of printed warnings below
  66. limit_by = plugin.hotspots
  67. limit_warnings = plugin.hotspots
  68. selected_data = loader.load_selected_data(limit.namespace,
  69. fields = [limit.field],
  70. path=path,
  71. filters = filters,
  72. sort_by=sort_by,
  73. limit_by=limit_by)
  74. if selected_data == None:
  75. mpp.utils.report_bad_path(path)
  76. exit_code += 1
  77. continue
  78. for select_data in selected_data:
  79. if limit_warnings != None and limit_warnings <= 0:
  80. break
  81. is_modified = None
  82. diff = None
  83. file_data = loader.load_file_data(select_data.get_path())
  84. file_data_prev = loader_prev.load_file_data(select_data.get_path())
  85. if file_data_prev != None:
  86. if file_data.get_checksum() == file_data_prev.get_checksum():
  87. diff = 0
  88. is_modified = False
  89. else:
  90. matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
  91. prev_id = matcher.get_prev_id(select_data.get_region().get_id())
  92. if matcher.is_matched(select_data.get_region().get_id()):
  93. if matcher.is_modified(select_data.get_region().get_id()):
  94. is_modified = True
  95. else:
  96. is_modified = False
  97. diff = mpp.api.DiffData(select_data,
  98. file_data_prev.get_region(prev_id)).get_data(limit.namespace, limit.field)
  99. if (warn_plugin.is_mode_matched(limit.limit,
  100. select_data.get_data(limit.namespace, limit.field),
  101. diff,
  102. is_modified) == False):
  103. continue
  104. is_sup = is_metric_suppressed(limit.namespace, limit.field, loader, select_data)
  105. if is_sup == True and plugin.no_suppress == False:
  106. continue
  107. exit_code += 1
  108. region_cursor = 0
  109. region_name = None
  110. if select_data.get_region() != None:
  111. region_cursor = select_data.get_region().cursor
  112. region_name = select_data.get_region().name
  113. report_limit_exceeded(select_data.get_path(),
  114. region_cursor,
  115. limit.namespace,
  116. limit.field,
  117. region_name,
  118. select_data.get_data(limit.namespace, limit.field),
  119. diff,
  120. limit.limit,
  121. is_modified,
  122. is_sup)
  123. if limit_warnings != None:
  124. limit_warnings -= 1
  125. return exit_code
  126. def get_list_of_modified_files(loader, loader_prev):
  127. logging.info("Identifying changed files...")
  128. old_files_map = {}
  129. for each in loader_prev.iterate_file_data():
  130. old_files_map[each.get_path()] = each.get_checksum()
  131. if len(old_files_map) == 0:
  132. return None
  133. modified_file_ids = []
  134. for each in loader.iterate_file_data():
  135. if len(modified_file_ids) > 1000: # If more than 1000 files changed, skip optimisation
  136. return None
  137. if (each.get_path() not in old_files_map.keys()) or old_files_map[each.get_path()] != each.get_checksum():
  138. modified_file_ids.append(str(each.get_id()))
  139. old_files_map = None
  140. if len(modified_file_ids) != 0:
  141. modified_file_ids = " , ".join(modified_file_ids)
  142. modified_file_ids = "(" + modified_file_ids + ")"
  143. return modified_file_ids
  144. return None
  145. def is_metric_suppressed(metric_namespace, metric_field, loader, select_data):
  146. data = loader.load_file_data(select_data.get_path())
  147. if select_data.get_region() != None:
  148. data = data.get_region(select_data.get_region().get_id())
  149. sup_data = data.get_data('std.suppress', 'list')
  150. else:
  151. sup_data = data.get_data('std.suppress.file', 'list')
  152. if sup_data != None and sup_data.find('[' + metric_namespace + ':' + metric_field + ']') != -1:
  153. return True
  154. return False
  155. def report_limit_exceeded(path, cursor, namespace, field, region_name,
  156. stat_level, trend_value, stat_limit,
  157. is_modified, is_suppressed):
  158. if region_name != None:
  159. message = "Metric '" + namespace + ":" + field + "' for region '" + region_name + "' exceeds the limit."
  160. else:
  161. message = "Metric '" + namespace + ":" + field + "' exceeds the limit."
  162. details = [("Metric name", namespace + ":" + field),
  163. ("Region name", region_name),
  164. ("Metric value", stat_level),
  165. ("Modified", is_modified),
  166. ("Change trend", '{0:{1}}'.format(trend_value, '+' if trend_value else '')),
  167. ("Limit", stat_limit),
  168. ("Suppressed", is_suppressed)]
  169. mpp.cout.notify(path, cursor, mpp.cout.SEVERITY_WARNING, message, details)