Conditions | 13 |
Total Lines | 68 |
Lines | 68 |
Ratio | 100 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Netstat.check() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
68 | View Code Duplication | def check(self): |
|
1 ignored issue
–
show
|
|||
69 | """Return detailed network statitistics proc file net/netstat. |
||
70 | |||
71 | Note: ECT1Pkts and ECT0Pkts relate to ECT congestion notifications. |
||
72 | |||
73 | :rtype: plumd.Result |
||
74 | """ |
||
75 | # what metrics do we want to record? |
||
76 | result = plumd.Result("netstat") |
||
77 | |||
78 | # read the proc file |
||
79 | dat = None |
||
80 | with open(self.proc_file, 'r') as f: |
||
81 | dat = f.read().strip().split("\n") |
||
82 | |||
83 | # timestamp for Differential calculations |
||
84 | ts = time.time() |
||
85 | |||
86 | # should always have a header row and value row, ie. divisible by 2 |
||
87 | if len(dat) % 2 != 0: |
||
88 | self.log.error("netstat: cannot parse {0}".format(self.proc_file)) |
||
89 | return result |
||
90 | |||
91 | # split values into lists |
||
92 | dlist = deque() |
||
93 | for entry in dat: |
||
94 | dlist.append(entry.split()) |
||
95 | |||
96 | # put lists into key: value dict |
||
97 | metrics = {} |
||
98 | while dlist: |
||
99 | headers = dlist.popleft() |
||
100 | values = dlist.popleft() |
||
101 | # { 'IpExt': {'InNoRoutes': 0, ...} } - [:-1] on IpExt: removes : |
||
102 | metrics[headers[0][:-1]] = dict(zip(headers, values)) |
||
103 | |||
104 | # record gauges |
||
105 | for ext, mnames in self.gauges.items(): |
||
106 | if ext not in metrics: |
||
107 | self.log.warn("netstat: unknown extension: {0}".format(ext)) |
||
108 | del(self.gauges[ext]) |
||
109 | continue |
||
110 | values = metrics[ext] |
||
111 | for mname in mnames: |
||
112 | if mname in values: |
||
113 | mstr = "{0}.{1}".format(ext, mname) |
||
114 | result.add(plumd.Int(mstr, values[mname])) |
||
115 | else: |
||
116 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
||
117 | del(self.gauges[ext][mname]) |
||
118 | |||
119 | # record rates |
||
120 | for ext, mnames in self.rates.items(): |
||
121 | if ext not in metrics: |
||
122 | self.log.warn("netstat: unknown extension: {0}".format(ext)) |
||
123 | del(self.rates[ext]) |
||
124 | continue |
||
125 | values = metrics[ext] |
||
126 | for mname in mnames: |
||
127 | if mname in values: |
||
128 | mstr = "{0}.{1}".format(ext, mname) |
||
129 | mval = self.calc.per_second(mstr, int(values[mname]), ts) |
||
130 | result.add(plumd.Float(mstr, mval)) |
||
131 | else: |
||
132 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
||
133 | del(self.rates[ext][mname]) |
||
134 | |||
135 | return [result] |
||
136 |