Conditions | 12 |
Total Lines | 56 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 glances.plugins.glances_connections.Plugin.update() 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 -*- |
||
73 | @GlancesPlugin._check_decorator |
||
74 | @GlancesPlugin._log_result_decorator |
||
75 | def update(self): |
||
76 | """Update connections stats using the input method. |
||
77 | |||
78 | Stats is a dict |
||
79 | """ |
||
80 | # Init new stats |
||
81 | stats = self.get_init_value() |
||
82 | |||
83 | if self.input_method == 'local': |
||
84 | # Update stats using the PSUtils lib |
||
85 | |||
86 | # Grab network interface stat using the psutil net_connections method |
||
87 | if self.net_connections_enabled: |
||
88 | try: |
||
89 | net_connections = psutil.net_connections(kind="tcp") |
||
90 | except Exception as e: |
||
91 | logger.debug('Can not get network connections stats ({})'.format(e)) |
||
92 | self.net_connections_enabled = False |
||
93 | self.stats = stats |
||
94 | return self.stats |
||
95 | |||
96 | for s in self.status_list: |
||
97 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
98 | initiated = 0 |
||
99 | for s in self.initiated_states: |
||
100 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
101 | initiated += stats[s] |
||
102 | stats['initiated'] = initiated |
||
103 | terminated = 0 |
||
104 | for s in self.initiated_states: |
||
105 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
106 | terminated += stats[s] |
||
107 | stats['terminated'] = terminated |
||
108 | |||
109 | if self.nf_conntrack_enabled: |
||
110 | # Grab connections track directly from the /proc file |
||
111 | for i in self.conntrack: |
||
112 | try: |
||
113 | with open(self.conntrack[i], 'r') as f: |
||
114 | stats[i] = float(f.readline().rstrip("\n")) |
||
115 | except IOError as e: |
||
116 | logger.debug('Can not get network connections track ({})'.format(e)) |
||
117 | self.nf_conntrack_enabled = False |
||
118 | self.stats = stats |
||
119 | return self.stats |
||
120 | stats['nf_conntrack_percent'] = stats['nf_conntrack_count'] * 100 / stats['nf_conntrack_max'] |
||
121 | |||
122 | elif self.input_method == 'snmp': |
||
123 | # Update stats using SNMP |
||
124 | pass |
||
125 | |||
126 | # Update the stats |
||
127 | self.stats = stats |
||
128 | return self.stats |
||
129 | |||
179 |