limit.py 9.0 KB

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