view.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. MODE_NEW = 0x01
  25. MODE_TOUCHED = 0x03
  26. MODE_ALL = 0x07
  27. def declare_configuration(self, parser):
  28. self.parser = parser
  29. parser.add_option("--format", "--ft", default='txt', choices=['txt', 'xml', 'python'],
  30. help="Format of the output data. "
  31. "Possible values are 'xml', 'txt' or 'python' [default: %default]")
  32. parser.add_option("--nest-regions", "--nr", action="store_true", default=False,
  33. help="If the option is set (True), data for regions is exported in the form of a tree. "
  34. "Otherwise, all regions are exported in plain list. [default: %default]")
  35. parser.add_option("--max-distribution-rows", "--mdr", type=int, default=20,
  36. help="Maximum number of rows in distribution tables. "
  37. "If it is set to 0, the tool does not optimize the size of distribution tables [default: %default]")
  38. parser.add_option("--scope-mode", "--sm", default='all', choices=['new', 'touched', 'all'],
  39. help="Defines the analysis scope mode. "
  40. "'all' - all available regions and files are taken into account, "
  41. "'new' - only new regions and files are taken into account, "
  42. "'touched' - only new and modified regions and files are taken into account. "
  43. "Modes 'new' and 'touched' may require more time for processing than mode 'all' "
  44. "[default: %default]")
  45. def configure(self, options):
  46. self.out_format = options.__dict__['format']
  47. self.nest_regions = options.__dict__['nest_regions']
  48. self.dist_columns = options.__dict__['max_distribution_rows']
  49. if options.__dict__['scope_mode'] == 'new':
  50. self.mode = self.MODE_NEW
  51. elif options.__dict__['scope_mode'] == 'touched':
  52. self.mode = self.MODE_TOUCHED
  53. elif options.__dict__['scope_mode'] == 'all':
  54. self.mode = self.MODE_ALL
  55. if self.mode != self.MODE_ALL and options.__dict__['db_file_prev'] == None:
  56. self.parser.error("option --scope-mode: The mode '" + options.__dict__['scope_mode'] + "' requires '--db-file-prev' option set")
  57. def run(self, args):
  58. loader_prev = self.get_plugin_loader().get_plugin('mpp.dbf').get_loader_prev()
  59. loader = self.get_plugin_loader().get_plugin('mpp.dbf').get_loader()
  60. paths = None
  61. if len(args) == 0:
  62. paths = [""]
  63. else:
  64. paths = args
  65. (result, exit_code) = export_to_str(self.out_format,
  66. paths,
  67. loader,
  68. loader_prev,
  69. self.nest_regions,
  70. self.dist_columns,
  71. self.mode)
  72. print result
  73. return exit_code
  74. def export_to_str(out_format, paths, loader, loader_prev, nest_regions, dist_columns, mode):
  75. exit_code = 0
  76. result = ""
  77. if out_format == 'xml':
  78. result += "<export>\n"
  79. elif out_format == 'python':
  80. result += "{'export': ["
  81. for (ind, path) in enumerate(paths):
  82. path = mpp.utils.preprocess_path(path)
  83. aggregated_data, aggregated_data_prev = load_aggregated_data_with_mode(loader, loader_prev, path , mode)
  84. aggregated_data_tree = {}
  85. subdirs = []
  86. subfiles = []
  87. if aggregated_data != None:
  88. aggregated_data_tree = aggregated_data.get_data_tree()
  89. subdirs = aggregated_data.get_subdirs()
  90. subfiles = aggregated_data.get_subfiles()
  91. else:
  92. mpp.utils.report_bad_path(path)
  93. exit_code += 1
  94. if aggregated_data_prev != None:
  95. aggregated_data_tree = append_diff(aggregated_data_tree,
  96. aggregated_data_prev.get_data_tree())
  97. aggregated_data_tree = append_suppressions(path, aggregated_data_tree, loader)
  98. aggregated_data_tree = compress_dist(aggregated_data_tree, dist_columns)
  99. file_data = loader.load_file_data(path)
  100. file_data_tree = {}
  101. if file_data != None:
  102. file_data_tree = file_data.get_data_tree()
  103. file_data_prev = loader_prev.load_file_data(path)
  104. append_regions(file_data_tree, file_data, file_data_prev, nest_regions)
  105. data = {"info": {"path": path, "id": ind + 1},
  106. "aggregated-data": aggregated_data_tree,
  107. "file-data": file_data_tree,
  108. "subdirs": subdirs,
  109. "subfiles": subfiles}
  110. if out_format == 'txt':
  111. cout_txt(data, loader)
  112. elif out_format == 'xml':
  113. result += mpp.utils.serialize_to_xml(data, root_name = "data") + "\n"
  114. elif out_format == 'python':
  115. postfix = ""
  116. if ind < len(paths) - 1:
  117. postfix = ", "
  118. result += mpp.utils.serialize_to_python(data, root_name = "data") + postfix
  119. if out_format == 'xml':
  120. result += "</export>"
  121. elif out_format == 'python':
  122. result += "]}"
  123. return (result, exit_code)
  124. def load_aggregated_data_with_mode(loader, loader_prev, path, mode):
  125. if mode == Plugin.MODE_ALL:
  126. aggregated_data = loader.load_aggregated_data(path)
  127. aggregated_data_prev = loader_prev.load_aggregated_data(path)
  128. else:
  129. assert(mode == Plugin.MODE_NEW or mode == Plugin.MODE_TOUCHED)
  130. class AggregatedFilteredData(mpp.api.AggregatedData):
  131. def __init__(self, loader, path):
  132. super(AggregatedFilteredData, self).__init__(loader, path)
  133. self.in_processing_mode = True
  134. for name in loader.iterate_namespace_names():
  135. namespace = loader.get_namespace(name)
  136. for field in namespace.iterate_field_names():
  137. self.set_data(name, field, {
  138. 'count': 0,
  139. 'nonzero': namespace.get_field_packager(field).is_non_zero(),
  140. 'min': None,
  141. 'max': None,
  142. 'total': 0.0,
  143. 'avg': None,
  144. 'distribution-bars': {}
  145. })
  146. def get_data_tree(self, namespaces=None):
  147. self.in_processing_mode = False
  148. # need to convert distribution map to a list and calculate average
  149. for name in loader.iterate_namespace_names():
  150. namespace = loader.get_namespace(name)
  151. for field in namespace.iterate_field_names():
  152. data = self.get_data(name, field)
  153. bars_list = []
  154. for metric_value in sorted(data['distribution-bars'].keys()):
  155. bars_list.append({'metric': metric_value,
  156. 'count': data['distribution-bars'][metric_value],
  157. 'ratio': ((float(data['distribution-bars'][metric_value]) /
  158. float(data['count'])))})
  159. data['distribution-bars'] = bars_list
  160. if data['count'] != 0:
  161. data['avg'] = float(data['total']) / float(data['count'])
  162. self.set_data(name, field, data)
  163. return super(AggregatedFilteredData, self).get_data_tree(namespaces=namespaces)
  164. def _append_data(self, orig_data):
  165. # flag to protect ourselves from getting incomplete data
  166. # the workflow in this tool: append data first and after get it using get_data_tree()
  167. assert(self.in_processing_mode == True)
  168. data = orig_data.get_data_tree()
  169. for namespace in data.keys():
  170. for field in data[namespace].keys():
  171. aggr_data = self.get_data(namespace, field)
  172. metric_value = data[namespace][field]
  173. if aggr_data['min'] == None or aggr_data['min'] > metric_value:
  174. aggr_data['min'] = metric_value
  175. if aggr_data['max'] == None or aggr_data['max'] < metric_value:
  176. aggr_data['max'] = metric_value
  177. aggr_data['count'] += 1
  178. aggr_data['total'] += metric_value
  179. # average is calculated later on get_data_tree
  180. if metric_value not in aggr_data['distribution-bars'].keys():
  181. aggr_data['distribution-bars'][metric_value] = 0
  182. aggr_data['distribution-bars'][metric_value] += 1
  183. self.set_data(namespace, field, aggr_data)
  184. def _append_file_data(self, file_data):
  185. self._append_data(file_data)
  186. for region in file_data.iterate_regions():
  187. self._append_data(region)
  188. result = AggregatedFilteredData(loader, path)
  189. result_prev = AggregatedFilteredData(loader_prev, path)
  190. prev_file_ids = set()
  191. file_data_iterator = loader.iterate_file_data(path)
  192. if file_data_iterator != None:
  193. for file_data in file_data_iterator:
  194. file_data_prev = loader_prev.load_file_data(file_data.get_path())
  195. if file_data_prev != None:
  196. prev_file_ids.add(file_data_prev.get_id())
  197. if (file_data_prev == None and (mode == Plugin.MODE_NEW or mode == Plugin.MODE_TOUCHED)):
  198. # new file and required mode matched
  199. logging.info("Processing: " + file_data.get_path() + " [new]")
  200. result._append_file_data(file_data)
  201. elif (file_data.get_checksum() != file_data_prev.get_checksum()):
  202. # modified file and required mode matched
  203. logging.info("Processing: " + file_data.get_path() + " [modified]")
  204. # append file data without appending regions...
  205. if (mode == Plugin.MODE_TOUCHED):
  206. # if required mode matched
  207. result._append_data(file_data)
  208. result_prev._append_data(file_data_prev)
  209. # process regions separately
  210. matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
  211. prev_reg_ids = set()
  212. for region in file_data.iterate_regions():
  213. prev_id = matcher.get_prev_id(region.get_id())
  214. if prev_id != None:
  215. prev_reg_ids.add(prev_id)
  216. if (matcher.is_matched(region.get_id()) == False and
  217. (mode == Plugin.MODE_NEW or mode == Plugin.MODE_TOUCHED)):
  218. # new region
  219. logging.debug("Processing region: " + region.get_name() + " [new]")
  220. result._append_data(region)
  221. elif matcher.is_modified(region.get_id()) and mode == Plugin.MODE_TOUCHED:
  222. # modified region
  223. logging.debug("Processing region: " + region.get_name() + " [modified]")
  224. result._append_data(region)
  225. result_prev._append_data(file_data_prev.get_region(prev_id))
  226. if mode == Plugin.MODE_TOUCHED:
  227. for region_prev in file_data_prev.iterate_regions():
  228. if region_prev.get_id() not in prev_reg_ids:
  229. # deleted region
  230. logging.debug("Processing: " + region_prev.get_name() + " [deleted]")
  231. result_prev._append_data(region_prev)
  232. if mode == Plugin.MODE_TOUCHED:
  233. file_data_prev_iterator = loader_prev.iterate_file_data(path)
  234. if file_data_prev_iterator != None:
  235. for file_data_prev in file_data_prev_iterator:
  236. if file_data_prev.get_id() not in prev_file_ids:
  237. # deleted file and required mode matched
  238. logging.info("Processing: " + file_data.get_path() + " [deleted]")
  239. result_prev._append_file_data(file_data_prev)
  240. return (result, result_prev)
  241. return (aggregated_data, aggregated_data_prev)
  242. def append_regions(file_data_tree, file_data, file_data_prev, nest_regions):
  243. regions_matcher = None
  244. if file_data_prev != None:
  245. file_data_tree = append_diff(file_data_tree,
  246. file_data_prev.get_data_tree())
  247. regions_matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
  248. if nest_regions == False:
  249. regions = []
  250. for region in file_data.iterate_regions():
  251. region_data_tree = region.get_data_tree()
  252. if regions_matcher != None and regions_matcher.is_matched(region.get_id()):
  253. region_data_prev = file_data_prev.get_region(regions_matcher.get_prev_id(region.get_id()))
  254. region_data_tree = append_diff(region_data_tree,
  255. region_data_prev.get_data_tree())
  256. regions.append({"info": {"name" : region.name,
  257. 'type' : file_data.get_region_types()().to_str(region.get_type()),
  258. "cursor" : region.cursor,
  259. 'line_begin': region.line_begin,
  260. 'line_end': region.line_end,
  261. 'offset_begin': region.begin,
  262. 'offset_end': region.end},
  263. "data": region_data_tree})
  264. file_data_tree['regions'] = regions
  265. else:
  266. def append_rec(region_id, file_data_tree, file_data, file_data_prev):
  267. region = file_data.get_region(region_id)
  268. region_data_tree = region.get_data_tree()
  269. if regions_matcher != None and regions_matcher.is_matched(region.get_id()):
  270. region_data_prev = file_data_prev.get_region(regions_matcher.get_prev_id(region.get_id()))
  271. region_data_tree = append_diff(region_data_tree,
  272. region_data_prev.get_data_tree())
  273. result = {"info": {"name" : region.name,
  274. 'type' : file_data.get_region_types()().to_str(region.get_type()),
  275. "cursor" : region.cursor,
  276. 'line_begin': region.line_begin,
  277. 'line_end': region.line_end,
  278. 'offset_begin': region.begin,
  279. 'offset_end': region.end},
  280. "data": region_data_tree,
  281. "subregions": []}
  282. for sub_id in file_data.get_region(region_id).iterate_subregion_ids():
  283. result['subregions'].append(append_rec(sub_id, file_data_tree, file_data, file_data_prev))
  284. return result
  285. file_data_tree['regions'] = []
  286. file_data_tree['regions'].append(append_rec(1, file_data_tree, file_data, file_data_prev))
  287. def append_diff(main_tree, prev_tree):
  288. assert(main_tree != None)
  289. assert(prev_tree != None)
  290. for name in main_tree.keys():
  291. if name not in prev_tree.keys():
  292. continue
  293. for field in main_tree[name].keys():
  294. if field not in prev_tree[name].keys():
  295. continue
  296. if isinstance(main_tree[name][field], dict) and isinstance(prev_tree[name][field], dict):
  297. diff = {}
  298. for key in main_tree[name][field].keys():
  299. if key not in prev_tree[name][field].keys():
  300. continue
  301. main_val = main_tree[name][field][key]
  302. prev_val = prev_tree[name][field][key]
  303. if main_val == None:
  304. main_val = 0
  305. if prev_val == None:
  306. prev_val = 0
  307. if isinstance(main_val, list) and isinstance(prev_val, list):
  308. main_tree[name][field][key] = append_diff_list(main_val, prev_val)
  309. else:
  310. diff[key] = main_val - prev_val
  311. main_tree[name][field]['__diff__'] = diff
  312. elif (not isinstance(main_tree[name][field], dict)) and (not isinstance(prev_tree[name][field], dict)):
  313. if '__diff__' not in main_tree[name]:
  314. main_tree[name]['__diff__'] = {}
  315. main_tree[name]['__diff__'][field] = main_tree[name][field] - prev_tree[name][field]
  316. return main_tree
  317. def append_diff_list(main_list, prev_list):
  318. merged_list = {}
  319. for bar in main_list:
  320. merged_list[bar['metric']] = {'count': bar['count'], '__diff__':bar['count'], 'ratio': bar['ratio']}
  321. for bar in prev_list:
  322. if bar['metric'] in merged_list.keys():
  323. merged_list[bar['metric']]['__diff__'] = \
  324. merged_list[bar['metric']]['count'] - bar['count']
  325. else:
  326. merged_list[bar['metric']] = {'count': 0, '__diff__':-bar['count'], 'ratio': 0}
  327. result = []
  328. for metric in sorted(merged_list.keys()):
  329. result.append({'metric':metric,
  330. 'count':merged_list[metric]['count'],
  331. 'ratio':merged_list[metric]['ratio'],
  332. '__diff__':merged_list[metric]['__diff__']})
  333. return result
  334. def append_suppressions(path, data, loader):
  335. # TODO can not append right suppressions for mode != ALL, fix it
  336. for namespace in data.keys():
  337. for field in data[namespace].keys():
  338. selected_data = loader.load_selected_data('std.suppress',
  339. fields = ['list'],
  340. path=path,
  341. filters = [('list', 'LIKE', '%[{0}:{1}]%'.format(namespace, field))])
  342. if selected_data == None:
  343. data[namespace][field]['sup'] = 0
  344. else:
  345. count = 0
  346. for each in selected_data:
  347. each = each # used
  348. count += 1
  349. data[namespace][field]['sup'] = count
  350. return data
  351. def compress_dist(data, columns):
  352. if columns == 0:
  353. return data
  354. for namespace in data.keys():
  355. for field in data[namespace].keys():
  356. metric_data = data[namespace][field]
  357. distr = metric_data['distribution-bars']
  358. columns = float(columns) # to trigger floating calculations
  359. if metric_data['count'] == 0:
  360. continue
  361. new_dist = []
  362. remaining_count = metric_data['count']
  363. next_consume = None
  364. next_bar = None
  365. max_count = -(0xFFFFFFFF)
  366. min_count = 0xFFFFFFFF
  367. sum_ratio = 0
  368. for (ind, bar) in enumerate(distr):
  369. if next_bar == None:
  370. # start new bar
  371. next_bar = {'count': bar['count'],
  372. 'ratio': bar['ratio'],
  373. 'metric_s': bar['metric'],
  374. 'metric_f': bar['metric']}
  375. if '__diff__' in bar.keys():
  376. next_bar['__diff__'] = bar['__diff__']
  377. next_consume = int(round(remaining_count/ (columns - len(new_dist))))
  378. else:
  379. # merge to existing bar
  380. next_bar['count'] += bar['count']
  381. next_bar['ratio'] += bar['ratio']
  382. next_bar['metric_f'] = bar['metric']
  383. if '__diff__' in bar.keys():
  384. next_bar['__diff__'] += bar['__diff__']
  385. next_consume -= bar['count']
  386. if (next_consume <= 0 # consumed enough
  387. or (ind + 1) == len(distr)): # or the last bar
  388. # append to new distribution
  389. if isinstance(next_bar['metric_s'], float):
  390. next_bar['metric_s'] = "{0:.4f}".format(next_bar['metric_s'])
  391. next_bar['metric_f'] = "{0:.4f}".format(next_bar['metric_f'])
  392. else:
  393. next_bar['metric_s'] = str(next_bar['metric_s'])
  394. next_bar['metric_f'] = str(next_bar['metric_f'])
  395. if next_bar['metric_s'] == next_bar['metric_f']:
  396. next_bar['metric'] = next_bar['metric_s']
  397. else:
  398. next_bar['metric'] = next_bar['metric_s'] + "-" + next_bar['metric_f']
  399. del next_bar['metric_s']
  400. del next_bar['metric_f']
  401. new_dist.append(next_bar)
  402. sum_ratio += next_bar['ratio']
  403. if max_count < next_bar['count']:
  404. max_count = next_bar['count']
  405. if min_count > next_bar['count'] and next_bar['count'] != 0:
  406. min_count = next_bar['count']
  407. remaining_count -= next_bar['count']
  408. next_bar = None
  409. # check that consumed all
  410. assert((ind + 1) != len(distr) or remaining_count == 0)
  411. if remaining_count == 0:
  412. break
  413. if (float(max_count - min_count) / metric_data['count'] < 0.05 and
  414. metric_data['count'] > columns and
  415. len(new_dist) > 1):
  416. # trick here:
  417. # if all bars are even in the new distribution AND
  418. # there are many items in the distribution (> max distribution rows),
  419. # it is better to do linear compression instead
  420. new_dist = []
  421. step = int(round(float(metric_data['max'] - metric_data['min']) / columns))
  422. next_end_limit = metric_data['min']
  423. next_bar = None
  424. for (ind, bar) in enumerate(distr):
  425. if next_bar == None:
  426. # start new bar
  427. next_bar = {'count': bar['count'],
  428. 'ratio': bar['ratio'],
  429. 'metric_s': next_end_limit,
  430. 'metric_f': bar['metric']}
  431. if '__diff__' in bar.keys():
  432. next_bar['__diff__'] = bar['__diff__']
  433. next_end_limit += step
  434. else:
  435. # merge to existing bar
  436. next_bar['count'] += bar['count']
  437. next_bar['ratio'] += bar['ratio']
  438. next_bar['metric_f'] = bar['metric']
  439. if '__diff__' in bar.keys():
  440. next_bar['__diff__'] += bar['__diff__']
  441. if (next_bar['metric_f'] >= next_end_limit # consumed enough
  442. or (ind + 1) == len(distr)): # or the last bar
  443. if (ind + 1) != len(distr):
  444. next_bar['metric_f'] = next_end_limit
  445. # append to new distribution
  446. if isinstance(next_bar['metric_s'], float):
  447. next_bar['metric_s'] = "{0:.4f}".format(next_bar['metric_s'])
  448. next_bar['metric_f'] = "{0:.4f}".format(next_bar['metric_f'])
  449. else:
  450. next_bar['metric_s'] = str(next_bar['metric_s'])
  451. next_bar['metric_f'] = str(next_bar['metric_f'])
  452. next_bar['metric'] = next_bar['metric_s'] + "-" + next_bar['metric_f']
  453. del next_bar['metric_s']
  454. del next_bar['metric_f']
  455. new_dist.append(next_bar)
  456. next_bar = None
  457. data[namespace][field]['distribution-bars'] = new_dist
  458. return data
  459. def cout_txt_regions(path, regions, indent = 0):
  460. for region in regions:
  461. details = [
  462. ('Region name', region['info']['name']),
  463. ('Region type', region['info']['type']),
  464. ('Offsets', str(region['info']['offset_begin']) + "-" + str(region['info']['offset_end'])),
  465. ('Line numbers', str(region['info']['line_begin']) + "-" + str(region['info']['line_end']))
  466. ]
  467. for namespace in region['data'].keys():
  468. diff_data = {}
  469. if '__diff__' in region['data'][namespace].keys():
  470. diff_data = region['data'][namespace]['__diff__']
  471. for field in region['data'][namespace].keys():
  472. diff_str = ""
  473. if field == '__diff__':
  474. continue
  475. if field in diff_data.keys():
  476. diff_str = " [" + ("+" if diff_data[field] >= 0 else "") + str(diff_data[field]) + "]"
  477. details.append((namespace + ":" + field, str(region['data'][namespace][field]) + diff_str))
  478. mpp.cout.notify(path,
  479. region['info']['cursor'],
  480. mpp.cout.SEVERITY_INFO,
  481. "Metrics per '" + region['info']['name']+ "' region",
  482. details,
  483. indent=indent)
  484. if 'subregions' in region.keys():
  485. cout_txt_regions(path, region['subregions'], indent=indent+1)
  486. def cout_txt(data, loader):
  487. details = []
  488. for key in data['file-data'].keys():
  489. if key == 'regions':
  490. cout_txt_regions(data['info']['path'], data['file-data'][key])
  491. else:
  492. namespace = key
  493. diff_data = {}
  494. if '__diff__' in data['file-data'][namespace].keys():
  495. diff_data = data['file-data'][namespace]['__diff__']
  496. for field in data['file-data'][namespace].keys():
  497. diff_str = ""
  498. if field == '__diff__':
  499. continue
  500. if field in diff_data.keys():
  501. diff_str = " [" + ("+" if diff_data[field] >= 0 else "") + str(diff_data[field]) + "]"
  502. details.append((namespace + ":" + field, str(data['file-data'][namespace][field]) + diff_str))
  503. if len(details) > 0:
  504. mpp.cout.notify(data['info']['path'],
  505. 0,
  506. mpp.cout.SEVERITY_INFO,
  507. "Metrics per file",
  508. details)
  509. attr_map = {'total': 'Total',
  510. 'avg': 'Average',
  511. 'min': 'Minimum',
  512. 'max': 'Maximum',
  513. }
  514. for namespace in data['aggregated-data'].keys():
  515. for field in data['aggregated-data'][namespace].keys():
  516. details = []
  517. diff_data = {}
  518. if '__diff__' in data['aggregated-data'][namespace][field].keys():
  519. diff_data = data['aggregated-data'][namespace][field]['__diff__']
  520. for attr in ['avg', 'min', 'max', 'total']:
  521. diff_str = ""
  522. if attr in diff_data.keys():
  523. diff_str = " [" + ("+" if diff_data[attr] >= 0 else "") + str(diff_data[attr]) + "]"
  524. if attr == 'avg' and data['aggregated-data'][namespace][field]['nonzero'] == True:
  525. diff_str += " (excluding zero metric values)"
  526. details.append((attr_map[attr], str(data['aggregated-data'][namespace][field][attr]) + diff_str))
  527. measured = data['aggregated-data'][namespace][field]['count']
  528. if 'count' in diff_data.keys():
  529. diff_str = ' [{0:{1}}]'.format(diff_data['count'], '+' if diff_data['count'] >= 0 else '')
  530. count_str_len = len(str(measured))
  531. elem_name = 'regions'
  532. if loader.get_namespace(namespace).are_regions_supported() == False:
  533. elem_name = 'files'
  534. details.append(('Distribution',
  535. '{0}{1} {2} in total (including {3} suppressed)'.format(measured,
  536. diff_str,
  537. elem_name,
  538. data['aggregated-data'][namespace][field]['sup'])))
  539. details.append((' Metric value', 'Ratio : R-sum : Number of ' + elem_name))
  540. sum_ratio = 0
  541. for bar in data['aggregated-data'][namespace][field]['distribution-bars']:
  542. sum_ratio += bar['ratio']
  543. diff_str = ""
  544. if '__diff__' in bar.keys():
  545. if bar['__diff__'] >= 0:
  546. diff_str = ' [+{0:<{1}}]'.format(bar['__diff__'], count_str_len)
  547. else:
  548. diff_str = ' [{0:<{1}}]'.format(bar['__diff__'], count_str_len+1)
  549. if isinstance(bar['metric'], float):
  550. metric_str = "{0:.4f}".format(bar['metric'])
  551. else:
  552. metric_str = str(bar['metric'])
  553. metric_str = (" " * (mpp.cout.DETAILS_OFFSET - len(metric_str) - 1)) + metric_str
  554. count_str = str(bar['count'])
  555. count_str = ((" " * (count_str_len - len(count_str))) + count_str + diff_str + "\t")
  556. details.append((metric_str,
  557. "{0:.3f}".format(bar['ratio']) + " : " + "{0:.3f}".format(sum_ratio) + " : " +
  558. count_str + ('|' * int(round(bar['ratio']*100)))))
  559. mpp.cout.notify(data['info']['path'],
  560. '', # no line number
  561. mpp.cout.SEVERITY_INFO,
  562. "Overall metrics for '" + namespace + ":" + field + "' metric",
  563. details)
  564. details = []
  565. for each in data['subdirs']:
  566. details.append(('Directory', each))
  567. for each in data['subfiles']:
  568. details.append(('File', each))
  569. if len(details) > 0:
  570. mpp.cout.notify(data['info']['path'],
  571. '', # no line number
  572. mpp.cout.SEVERITY_INFO,
  573. "Directory content:",
  574. details)