Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

from mtools.util import OrderedDict 

from mtools.util.log2code import Log2CodeConverter 

from mtools.util.grouping import Grouping 

 

import re 

from datetime import MINYEAR, MAXYEAR, datetime, timedelta 

import types 

 

try: 

    from matplotlib import cm 

except ImportError: 

    raise ImportError("Can't import matplotlib. See https://github.com/rueckstiess/mtools/blob/master/INSTALL.md for instructions how to install matplotlib or try mlogvis instead, which is a simplified version of mplotqueries that visualizes the logfile in a web browser.") 

 

class BasePlotType(object): 

 

    # 14 most distinguishable colors, according to  

    # http://stackoverflow.com/questions/309149/generate-distinctly-different-rgb-colors-in-graphs 

    colors = ['#000000','#00FF00','#0000FF','#FF0000','#01FFFE','#FFA6FE','#FFDB66','#006401', \ 

              '#010067','#95003A','#007DB5','#FF00F6','#FFEEE8','#774D00'] 

    color_index = 0 

    markers = ['o', 's', '<', 'D'] 

    marker_index = 0 

 

    sort_order = 0 

    plot_type_str = 'base' 

    default_group_by = None 

    date_range = (datetime(MAXYEAR, 12, 31), datetime(MINYEAR, 1, 1)) 

 

 

    def __init__(self, args=None, unknown_args=None): 

        self.args = args 

        self.unknown_args = unknown_args 

        self.groups = OrderedDict() 

        self.empty = True 

        self.limits = None 

 

        if self.args['optime_start']: 

            self.xlabel = 'time (start of ops)' 

        else: 

            self.xlabel = 'time (end of ops)' 

 

 

    def accept_line(self, logevent): 

        """ return True if this PlotType can plot this line. """ 

        return True 

 

    def add_line(self, logevent): 

        """ append log line to this plot type. """ 

        key = None 

        self.empty = False 

        self.groups.setdefault(key, list()).append(logevent) 

 

    @property 

    def logevents(self): 

        """ iterator yielding all logevents from groups dictionary. """ 

        for key in self.groups: 

            for logevent in self.groups[key]: 

                yield logevent 

 

    @classmethod 

    def color_map(cls, group): 

        color = cls.colors[cls.color_index] 

        cls.color_index += 1 

 

        marker = cls.markers[cls.marker_index] 

        if cls.color_index >= len(cls.colors): 

            cls.marker_index += 1 

            cls.marker_index %= len(cls.markers) 

            cls.color_index %= cls.color_index 

 

        return color, marker 

 

 

    def group(self): 

        """ (re-)group all logevents by the given group. """ 

        if hasattr(self, 'group_by'): 

            group_by = self.group_by 

        else: 

            group_by = self.default_group_by 

            if self.args['group'] != None: 

                group_by = self.args['group'] 

 

        self.groups = Grouping(self.logevents, group_by) 

        self.groups.move_items(None, 'others') 

        self.groups.sort_by_size(group_limit=self.args['group_limit'], discard_others=self.args['no_others']) 

 

    def plot_group(self, group, idx, axis): 

        raise NotImplementedError("BasePlotType can't plot. Use a derived class instead") 

 

 

    def clicked(self, event): 

        """ this is called if an element of this plottype was clicked. Implement in sub class. """ 

        pass 

 

 

    def plot(self, axis, ith_plot, total_plots, limits): 

        self.limits = limits 

 

        artists = [] 

        print self.plot_type_str.upper(), "plot" 

        print "%5s %9s  %s"%("id", " #points", "group") 

 

        for idx, group in enumerate(self.groups): 

            print "%5s %9s  %s"%(idx+1, len(self.groups[group]), group) 

            group_artists = self.plot_group(group, idx+ith_plot, axis) 

            if isinstance(group_artists, list): 

                artists.extend(group_artists) 

            else: 

                artists.append(group_artists) 

 

        print 

 

        return artists