limit.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://swi.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 time
  21. import core.log
  22. import core.db.loader
  23. import core.db.post
  24. import core.db.utils
  25. import core.export.cout
  26. import core.warn
  27. import core.cmdparser
  28. def main():
  29. exit_code = 0
  30. log_plugin = core.log.Plugin()
  31. db_plugin = core.db.post.Plugin()
  32. warn_plugin = core.warn.Plugin()
  33. parser = core.cmdparser.MultiOptionParser(usage="Usage: %prog [options] -- <path 1> ... <path N>")
  34. log_plugin.declare_configuration(parser)
  35. db_plugin.declare_configuration(parser)
  36. warn_plugin.declare_configuration(parser)
  37. (options, args) = parser.parse_args()
  38. log_plugin.configure(options)
  39. db_plugin.configure(options)
  40. warn_plugin.configure(options)
  41. loader_prev = core.db.loader.Loader()
  42. if db_plugin.dbfile_prev != None:
  43. loader_prev.open_database(db_plugin.dbfile_prev)
  44. loader = core.db.loader.Loader()
  45. loader.open_database(db_plugin.dbfile)
  46. warn_plugin.verify_namespaces(loader.iterate_namespace_names())
  47. for each in loader.iterate_namespace_names():
  48. warn_plugin.verify_fields(each, loader.get_namespace(each).iterate_field_names())
  49. paths = None
  50. if len(args) == 0:
  51. paths = [""]
  52. else:
  53. paths = args
  54. # Try to optimise iterative change scans
  55. modified_file_ids = None
  56. if warn_plugin.mode != warn_plugin.MODE_ALL:
  57. modified_file_ids = get_list_of_modified_files(loader, loader_prev)
  58. for path in paths:
  59. logging.info("Processing: " + path)
  60. for limit in warn_plugin.iterate_limits():
  61. logging.info("Applying limit: " + str(limit))
  62. filters = [limit.filter]
  63. if modified_file_ids != None:
  64. filters.append(('file_id', 'IN', modified_file_ids))
  65. selected_data = loader.load_selected_data(limit.namespace,
  66. fields = [limit.field],
  67. path=path,
  68. filters = filters)
  69. if selected_data == None:
  70. logging.error("Specified path '" + path + "' is invalid (not found in the database records)")
  71. exit_code += 1
  72. continue
  73. for select_data in selected_data:
  74. is_modified = None
  75. diff = None
  76. file_data = loader.load_file_data(select_data.get_path())
  77. file_data_prev = loader_prev.load_file_data(select_data.get_path())
  78. if file_data_prev != None:
  79. if file_data.get_checksum() == file_data_prev.get_checksum():
  80. diff = 0
  81. is_modified = False
  82. else:
  83. matcher = core.db.utils.FileRegionsMatcher(file_data, file_data_prev)
  84. prev_id = matcher.get_prev_id(select_data.get_region().get_id())
  85. if matcher.is_matched(select_data.get_region().get_id()):
  86. if matcher.is_modified(select_data.get_region().get_id()):
  87. is_modified = True
  88. else:
  89. is_modified = False
  90. diff = core.db.loader.DiffData(select_data,
  91. file_data_prev.get_region(prev_id)).get_data(limit.namespace, limit.field)
  92. if warn_plugin.is_mode_matched(limit.limit, select_data.get_data(limit.namespace, limit.field), diff, is_modified):
  93. exit_code += 1
  94. region_cursor = 0
  95. region_name = ""
  96. if select_data.get_region() != None:
  97. region_cursor = select_data.get_region().cursor
  98. region_name = select_data.get_region().name
  99. report_limit_exceeded(select_data.get_path(),
  100. region_cursor,
  101. limit.namespace,
  102. limit.field,
  103. region_name,
  104. select_data.get_data(limit.namespace, limit.field),
  105. diff,
  106. limit.limit,
  107. is_modified)
  108. return exit_code
  109. def get_list_of_modified_files(loader, loader_prev):
  110. modified_file_ids = []
  111. logging.info("Identifying changed files...")
  112. old_files_map = {}
  113. for each in loader_prev.iterate_file_data():
  114. old_files_map[each.get_path()] = each.get_checksum()
  115. if len(old_files_map) == 0:
  116. return None
  117. for each in loader.iterate_file_data():
  118. if len(modified_file_ids) > 1000: # If more than 1000 files changed, skip optimisation
  119. modified_file_ids = None
  120. break
  121. if (each.get_path() not in old_files_map.keys()) or old_files_map[each.get_path()] != each.get_checksum():
  122. modified_file_ids.append(each.get_id())
  123. if modified_file_ids != None:
  124. modified_file_ids = " , ".join(modified_file_ids)
  125. modified_file_ids = "(" + modified_file_ids + ")"
  126. old_files_map = None
  127. return modified_file_ids
  128. def report_limit_exceeded(path, cursor, namespace, field, region_name, stat_level, trend_value, stat_limit, is_modified):
  129. message = "Metric '" + namespace + "/" + field + "' for region '" + region_name + "' exceeds the limit."
  130. details = [("Metric name", namespace + "/" + field),
  131. ("Region name", region_name),
  132. ("Metric value", stat_level),
  133. ("Modified", is_modified),
  134. ("Change trend", '{0:{1}}'.format(trend_value, '+' if trend_value else '')),
  135. ("Limit", stat_limit)]
  136. core.export.cout.cout(path, cursor, core.export.cout.SEVERITY_WARNING, message, details)
  137. if __name__ == '__main__':
  138. ts = time.time()
  139. core.log.set_default_format()
  140. exit_code = main()
  141. logging.warning("Exit code: " + str(exit_code) + ". Time spent: " + str(round((time.time() - ts), 2)) + " seconds. Done")
  142. exit(exit_code)