OLD | NEW |
1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import datetime | 5 import datetime |
6 import json | 6 import json |
7 import math | 7 import math |
8 import random | 8 import random |
9 import uuid | 9 import uuid |
10 | 10 |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 class HistogramRef(object): | 489 class HistogramRef(object): |
490 | 490 |
491 def __init__(self, guid): | 491 def __init__(self, guid): |
492 self._guid = guid | 492 self._guid = guid |
493 | 493 |
494 @property | 494 @property |
495 def guid(self): | 495 def guid(self): |
496 return self._guid | 496 return self._guid |
497 | 497 |
498 | 498 |
499 class RelatedHistogramMap(diagnostic.Diagnostic): | |
500 | |
501 def __init__(self): | |
502 super(RelatedHistogramMap, self).__init__() | |
503 self._histograms_by_name = {} | |
504 | |
505 def Get(self, name): | |
506 return self._histograms_by_name.get(name) | |
507 | |
508 def Set(self, name, hist): | |
509 assert isinstance(hist, (Histogram, HistogramRef)) | |
510 self._histograms_by_name[name] = hist | |
511 | |
512 def Add(self, hist): | |
513 self.Set(hist.name, hist) | |
514 | |
515 def __len__(self): | |
516 return len(self._histograms_by_name) | |
517 | |
518 def __iter__(self): | |
519 for name, hist in self._histograms_by_name.iteritems(): | |
520 yield name, hist | |
521 | |
522 def Resolve(self, histograms, required=False): | |
523 for name, hist in self: | |
524 if not isinstance(hist, HistogramRef): | |
525 continue | |
526 | |
527 guid = hist.guid | |
528 hist = histograms.LookupHistogram(guid) | |
529 if isinstance(hist, Histogram): | |
530 self._histograms_by_name[name] = hist | |
531 else: | |
532 assert not required, guid | |
533 | |
534 def _AsDictInto(self, d): | |
535 d['values'] = {} | |
536 for name, hist in self: | |
537 d['values'][name] = hist.guid | |
538 | |
539 @staticmethod | |
540 def FromDict(d): | |
541 result = RelatedHistogramMap() | |
542 for name, guid in d['values'].iteritems(): | |
543 result.Set(name, HistogramRef(guid)) | |
544 return result | |
545 | |
546 | |
547 class RelatedHistogramBreakdown(RelatedHistogramMap): | |
548 | |
549 def __init__(self): | |
550 super(RelatedHistogramBreakdown, self).__init__() | |
551 self._color_scheme = None | |
552 | |
553 def Set(self, name, hist): | |
554 if not isinstance(hist, HistogramRef): | |
555 assert isinstance(hist, Histogram) | |
556 # All Histograms must have the same unit. | |
557 for _, other_hist in self: | |
558 expected_unit = other_hist.unit | |
559 assert expected_unit == hist.unit, ( | |
560 'Units mismatch ' + expected_unit + ' != ' + hist.unit) | |
561 break # Only the first Histogram needs to be checked. | |
562 super(RelatedHistogramBreakdown, self).Set(name, hist) | |
563 | |
564 def _AsDictInto(self, d): | |
565 RelatedHistogramMap._AsDictInto(self, d) | |
566 if self._color_scheme: | |
567 d['colorScheme'] = self._color_scheme | |
568 | |
569 @staticmethod | |
570 def FromDict(d): | |
571 result = RelatedHistogramBreakdown() | |
572 for name, guid in d['values'].iteritems(): | |
573 result.Set(name, HistogramRef(guid)) | |
574 if 'colorScheme' in d: | |
575 result._color_scheme = d['colorScheme'] | |
576 return result | |
577 | |
578 | |
579 class TagMap(diagnostic.Diagnostic): | 499 class TagMap(diagnostic.Diagnostic): |
580 | 500 |
581 def __init__(self, info): | 501 def __init__(self, info): |
582 super(TagMap, self).__init__() | 502 super(TagMap, self).__init__() |
583 self._tags_to_story_names = dict( | 503 self._tags_to_story_names = dict( |
584 (k, set(v)) for k, v in info.get( | 504 (k, set(v)) for k, v in info.get( |
585 'tagsToStoryNames', {}).iteritems()) | 505 'tagsToStoryNames', {}).iteritems()) |
586 | 506 |
587 def _AsDictInto(self, d): | 507 def _AsDictInto(self, d): |
588 d['tagsToStoryNames'] = dict( | 508 d['tagsToStoryNames'] = dict( |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 elif required: | 655 elif required: |
736 raise ValueError('Unable to find shared Diagnostic ' + guid) | 656 raise ValueError('Unable to find shared Diagnostic ' + guid) |
737 | 657 |
738 def AsDict(self): | 658 def AsDict(self): |
739 dct = {} | 659 dct = {} |
740 for name, diag in self.iteritems(): | 660 for name, diag in self.iteritems(): |
741 dct[name] = diag.AsDictOrReference() | 661 dct[name] = diag.AsDictOrReference() |
742 return dct | 662 return dct |
743 | 663 |
744 def Merge(self, other, parent_hist, other_parent_hist): | 664 def Merge(self, other, parent_hist, other_parent_hist): |
745 merged_from = self.get(reserved_infos.MERGED_FROM.name) | |
746 if merged_from is None: | |
747 merged_from = RelatedHistogramMap() | |
748 self[reserved_infos.MERGED_FROM.name] = merged_from | |
749 merged_from.Set(len(merged_from), other_parent_hist) | |
750 | |
751 for name, other_diagnostic in other.iteritems(): | 665 for name, other_diagnostic in other.iteritems(): |
752 if name not in self: | 666 if name not in self: |
753 self[name] = other_diagnostic | 667 self[name] = other_diagnostic |
754 continue | 668 continue |
755 my_diagnostic = self[name] | 669 my_diagnostic = self[name] |
756 if my_diagnostic.CanAddDiagnostic( | 670 if my_diagnostic.CanAddDiagnostic( |
757 other_diagnostic, name, parent_hist, other_parent_hist): | 671 other_diagnostic, name, parent_hist, other_parent_hist): |
758 my_diagnostic.AddDiagnostic( | 672 my_diagnostic.AddDiagnostic( |
759 other_diagnostic, name, parent_hist, other_parent_hist) | 673 other_diagnostic, name, parent_hist, other_parent_hist) |
760 continue | 674 continue |
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1371 } | 1285 } |
1372 | 1286 |
1373 | 1287 |
1374 all_diagnostics.DIAGNOSTICS_BY_NAME.update({ | 1288 all_diagnostics.DIAGNOSTICS_BY_NAME.update({ |
1375 'Breakdown': Breakdown, | 1289 'Breakdown': Breakdown, |
1376 'GenericSet': GenericSet, | 1290 'GenericSet': GenericSet, |
1377 'UnmergeableDiagnosticSet': UnmergeableDiagnosticSet, | 1291 'UnmergeableDiagnosticSet': UnmergeableDiagnosticSet, |
1378 'RelatedEventSet': RelatedEventSet, | 1292 'RelatedEventSet': RelatedEventSet, |
1379 'DateRange': DateRange, | 1293 'DateRange': DateRange, |
1380 'TagMap': TagMap, | 1294 'TagMap': TagMap, |
1381 'RelatedHistogramBreakdown': RelatedHistogramBreakdown, | |
1382 'RelatedHistogramMap': RelatedHistogramMap, | |
1383 }) | 1295 }) |
OLD | NEW |