limit.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 core.log
  21. import core.db.post
  22. import core.utils
  23. import core.cout
  24. import core.warn
  25. import core.cmdparser
  26. import core.utils
  27. import core.api
  28. class Tool(core.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 = core.log.Plugin()
  34. db_plugin = core.db.post.Plugin()
  35. warn_plugin = core.warn.Plugin()
  36. parser = core.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. loader_prev = core.api.Loader()
  53. if db_plugin.dbfile_prev != None:
  54. if loader_prev.open_database(db_plugin.dbfile_prev) == False:
  55. parser.error("Can not open file: " + db_plugin.dbfile_prev)
  56. loader = core.api.Loader()
  57. if loader.open_database(db_plugin.dbfile) == False:
  58. parser.error("Can not open file: " + db_plugin.dbfile)
  59. warn_plugin.verify_namespaces(loader.iterate_namespace_names())
  60. for each in loader.iterate_namespace_names():
  61. warn_plugin.verify_fields(each, loader.get_namespace(each).iterate_field_names())
  62. # Check for versions consistency
  63. if db_plugin.dbfile_prev != None:
  64. core.utils.check_db_metadata(loader, loader_prev)
  65. paths = None
  66. if len(args) == 0:
  67. paths = [""]
  68. else:
  69. paths = args
  70. # Try to optimise iterative change scans
  71. modified_file_ids = None
  72. if warn_plugin.mode != warn_plugin.MODE_ALL:
  73. modified_file_ids = get_list_of_modified_files(loader, loader_prev)
  74. for path in paths:
  75. path = core.utils.preprocess_path(path)
  76. for limit in warn_plugin.iterate_limits():
  77. logging.info("Applying limit: " + str(limit))
  78. filters = [limit.filter]
  79. if modified_file_ids != None:
  80. filters.append(('file_id', 'IN', modified_file_ids))
  81. sort_by = None
  82. limit_by = None
  83. if hotspots != None:
  84. sort_by = limit.field
  85. if limit.type == "max":
  86. sort_by = "-" + sort_by
  87. limit_by = hotspots
  88. selected_data = loader.load_selected_data(limit.namespace,
  89. fields = [limit.field],
  90. path=path,
  91. filters = filters,
  92. sort_by=sort_by,
  93. limit_by=limit_by)
  94. if selected_data == None:
  95. core.utils.report_bad_path(path)
  96. exit_code += 1
  97. continue
  98. for select_data in selected_data:
  99. is_modified = None
  100. diff = None
  101. file_data = loader.load_file_data(select_data.get_path())
  102. file_data_prev = loader_prev.load_file_data(select_data.get_path())
  103. if file_data_prev != None:
  104. if file_data.get_checksum() == file_data_prev.get_checksum():
  105. diff = 0
  106. is_modified = False
  107. else:
  108. matcher = core.utils.FileRegionsMatcher(file_data, file_data_prev)
  109. prev_id = matcher.get_prev_id(select_data.get_region().get_id())
  110. if matcher.is_matched(select_data.get_region().get_id()):
  111. if matcher.is_modified(select_data.get_region().get_id()):
  112. is_modified = True
  113. else:
  114. is_modified = False
  115. diff = core.api.DiffData(select_data,
  116. file_data_prev.get_region(prev_id)).get_data(limit.namespace, limit.field)
  117. if (warn_plugin.is_mode_matched(limit.limit,
  118. select_data.get_data(limit.namespace, limit.field),
  119. diff,
  120. is_modified) == False):
  121. continue
  122. is_sup = is_metric_suppressed(limit.namespace, limit.field, loader, select_data)
  123. if is_sup == True and no_suppress == False:
  124. continue
  125. exit_code += 1
  126. region_cursor = 0
  127. region_name = None
  128. if select_data.get_region() != None:
  129. region_cursor = select_data.get_region().cursor
  130. region_name = select_data.get_region().name
  131. report_limit_exceeded(select_data.get_path(),
  132. region_cursor,
  133. limit.namespace,
  134. limit.field,
  135. region_name,
  136. select_data.get_data(limit.namespace, limit.field),
  137. diff,
  138. limit.limit,
  139. is_modified,
  140. is_sup)
  141. return exit_code
  142. def get_list_of_modified_files(loader, loader_prev):
  143. logging.info("Identifying changed files...")
  144. old_files_map = {}
  145. for each in loader_prev.iterate_file_data():
  146. old_files_map[each.get_path()] = each.get_checksum()
  147. if len(old_files_map) == 0:
  148. return None
  149. modified_file_ids = []
  150. for each in loader.iterate_file_data():
  151. if len(modified_file_ids) > 1000: # If more than 1000 files changed, skip optimisation
  152. return None
  153. if (each.get_path() not in old_files_map.keys()) or old_files_map[each.get_path()] != each.get_checksum():
  154. modified_file_ids.append(str(each.get_id()))
  155. old_files_map = None
  156. if len(modified_file_ids) != 0:
  157. modified_file_ids = " , ".join(modified_file_ids)
  158. modified_file_ids = "(" + modified_file_ids + ")"
  159. return modified_file_ids
  160. return None
  161. def is_metric_suppressed(metric_namespace, metric_field, loader, select_data):
  162. data = loader.load_file_data(select_data.get_path())
  163. if select_data.get_region() != None:
  164. data = data.get_region(select_data.get_region().get_id())
  165. sup_data = data.get_data('std.suppress', 'list')
  166. else:
  167. sup_data = data.get_data('std.suppress.file', 'list')
  168. if sup_data != None and sup_data.find('[' + metric_namespace + ':' + metric_field + ']') != -1:
  169. return True
  170. return False
  171. def report_limit_exceeded(path, cursor, namespace, field, region_name,
  172. stat_level, trend_value, stat_limit,
  173. is_modified, is_suppressed):
  174. if region_name != None:
  175. message = "Metric '" + namespace + ":" + field + "' for region '" + region_name + "' exceeds the limit."
  176. else:
  177. message = "Metric '" + namespace + ":" + field + "' exceeds the limit."
  178. details = [("Metric name", namespace + ":" + field),
  179. ("Region name", region_name),
  180. ("Metric value", stat_level),
  181. ("Modified", is_modified),
  182. ("Change trend", '{0:{1}}'.format(trend_value, '+' if trend_value else '')),
  183. ("Limit", stat_limit),
  184. ("Suppressed", is_suppressed)]
  185. core.cout.notify(path, cursor, core.cout.SEVERITY_WARNING, message, details)