limit.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.log
  21. import mpp.db.post
  22. import mpp.utils
  23. import mpp.cout
  24. import mpp.warn
  25. import mpp.cmdparser
  26. import mpp.utils
  27. import mpp.api
  28. class Tool(mpp.api.ITool):
  29. def run(self, tool_args):
  30. return main(tool_args)
  31. def main(tool_args):
  32. exit_code = 0
  33. log_plugin = mpp.log.Plugin()
  34. db_plugin = mpp.db.post.Plugin()
  35. warn_plugin = mpp.warn.Plugin()
  36. parser = mpp.cmdparser.MultiOptionParser(usage="Usage: %prog limit [options] -- [path 1] ... [path N]")
  37. log_plugin.declare_configuration(parser)
  38. db_plugin.declare_configuration(parser)
  39. warn_plugin.declare_configuration(parser)
  40. parser.add_option("--hotspots", "--hs", default=None, help="If not set (none), all exceeded limits are printed."
  41. " If set, exceeded limits are sorted (the worst is the first) and only first HOTSPOTS limits are printed."
  42. " [default: %default]", type=int)
  43. parser.add_option("--disable-suppressions", "--ds", action="store_true", default=False,
  44. help = "If not set (none), all suppressions are ignored"
  45. " and associated warnings are printed. [default: %default]")
  46. (options, args) = parser.parse_args(tool_args)
  47. log_plugin.configure(options)
  48. db_plugin.configure(options)
  49. warn_plugin.configure(options)
  50. hotspots = options.__dict__['hotspots']
  51. no_suppress = options.__dict__['disable_suppressions']
  52. log_plugin.initialize()
  53. db_plugin.initialize()
  54. loader_prev = db_plugin.get_loader_prev()
  55. loader = db_plugin.get_loader()
  56. warn_plugin.verify_namespaces(loader.iterate_namespace_names())
  57. for each in loader.iterate_namespace_names():
  58. warn_plugin.verify_fields(each, loader.get_namespace(each).iterate_field_names())
  59. # Check for versions consistency
  60. if db_plugin.dbfile_prev != None:
  61. mpp.utils.check_db_metadata(loader, loader_prev)
  62. paths = None
  63. if len(args) == 0:
  64. paths = [""]
  65. else:
  66. paths = args
  67. # Try to optimise iterative change scans
  68. modified_file_ids = None
  69. if warn_plugin.mode != warn_plugin.MODE_ALL:
  70. modified_file_ids = get_list_of_modified_files(loader, loader_prev)
  71. for path in paths:
  72. path = mpp.utils.preprocess_path(path)
  73. for limit in warn_plugin.iterate_limits():
  74. logging.info("Applying limit: " + str(limit))
  75. filters = [limit.filter]
  76. if modified_file_ids != None:
  77. filters.append(('file_id', 'IN', modified_file_ids))
  78. sort_by = None
  79. limit_by = None
  80. if hotspots != None:
  81. sort_by = limit.field
  82. if limit.type == "max":
  83. sort_by = "-" + sort_by
  84. limit_by = hotspots
  85. selected_data = loader.load_selected_data(limit.namespace,
  86. fields = [limit.field],
  87. path=path,
  88. filters = filters,
  89. sort_by=sort_by,
  90. limit_by=limit_by)
  91. if selected_data == None:
  92. mpp.utils.report_bad_path(path)
  93. exit_code += 1
  94. continue
  95. for select_data in selected_data:
  96. is_modified = None
  97. diff = None
  98. file_data = loader.load_file_data(select_data.get_path())
  99. file_data_prev = loader_prev.load_file_data(select_data.get_path())
  100. if file_data_prev != None:
  101. if file_data.get_checksum() == file_data_prev.get_checksum():
  102. diff = 0
  103. is_modified = False
  104. else:
  105. matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
  106. prev_id = matcher.get_prev_id(select_data.get_region().get_id())
  107. if matcher.is_matched(select_data.get_region().get_id()):
  108. if matcher.is_modified(select_data.get_region().get_id()):
  109. is_modified = True
  110. else:
  111. is_modified = False
  112. diff = mpp.api.DiffData(select_data,
  113. file_data_prev.get_region(prev_id)).get_data(limit.namespace, limit.field)
  114. if (warn_plugin.is_mode_matched(limit.limit,
  115. select_data.get_data(limit.namespace, limit.field),
  116. diff,
  117. is_modified) == False):
  118. continue
  119. is_sup = is_metric_suppressed(limit.namespace, limit.field, loader, select_data)
  120. if is_sup == True and no_suppress == False:
  121. continue
  122. exit_code += 1
  123. region_cursor = 0
  124. region_name = None
  125. if select_data.get_region() != None:
  126. region_cursor = select_data.get_region().cursor
  127. region_name = select_data.get_region().name
  128. report_limit_exceeded(select_data.get_path(),
  129. region_cursor,
  130. limit.namespace,
  131. limit.field,
  132. region_name,
  133. select_data.get_data(limit.namespace, limit.field),
  134. diff,
  135. limit.limit,
  136. is_modified,
  137. is_sup)
  138. return exit_code
  139. def get_list_of_modified_files(loader, loader_prev):
  140. logging.info("Identifying changed files...")
  141. old_files_map = {}
  142. for each in loader_prev.iterate_file_data():
  143. old_files_map[each.get_path()] = each.get_checksum()
  144. if len(old_files_map) == 0:
  145. return None
  146. modified_file_ids = []
  147. for each in loader.iterate_file_data():
  148. if len(modified_file_ids) > 1000: # If more than 1000 files changed, skip optimisation
  149. return None
  150. if (each.get_path() not in old_files_map.keys()) or old_files_map[each.get_path()] != each.get_checksum():
  151. modified_file_ids.append(str(each.get_id()))
  152. old_files_map = None
  153. if len(modified_file_ids) != 0:
  154. modified_file_ids = " , ".join(modified_file_ids)
  155. modified_file_ids = "(" + modified_file_ids + ")"
  156. return modified_file_ids
  157. return None
  158. def is_metric_suppressed(metric_namespace, metric_field, loader, select_data):
  159. data = loader.load_file_data(select_data.get_path())
  160. if select_data.get_region() != None:
  161. data = data.get_region(select_data.get_region().get_id())
  162. sup_data = data.get_data('std.suppress', 'list')
  163. else:
  164. sup_data = data.get_data('std.suppress.file', 'list')
  165. if sup_data != None and sup_data.find('[' + metric_namespace + ':' + metric_field + ']') != -1:
  166. return True
  167. return False
  168. def report_limit_exceeded(path, cursor, namespace, field, region_name,
  169. stat_level, trend_value, stat_limit,
  170. is_modified, is_suppressed):
  171. if region_name != None:
  172. message = "Metric '" + namespace + ":" + field + "' for region '" + region_name + "' exceeds the limit."
  173. else:
  174. message = "Metric '" + namespace + ":" + field + "' exceeds the limit."
  175. details = [("Metric name", namespace + ":" + field),
  176. ("Region name", region_name),
  177. ("Metric value", stat_level),
  178. ("Modified", is_modified),
  179. ("Change trend", '{0:{1}}'.format(trend_value, '+' if trend_value else '')),
  180. ("Limit", stat_limit),
  181. ("Suppressed", is_suppressed)]
  182. mpp.cout.notify(path, cursor, mpp.cout.SEVERITY_WARNING, message, details)