| Conditions | 11 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 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.GlancesLogs.add() 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 -*- |
||
| 98 | def add(self, item_state, item_type, item_value, |
||
| 99 | proc_list=None, proc_desc="", peak_time=6): |
||
| 100 | """Add a new item to the logs list. |
||
| 101 | |||
| 102 | If 'item' is a 'new one', add the new item at the beginning of |
||
| 103 | the logs list. |
||
| 104 | If 'item' is not a 'new one', update the existing item. |
||
| 105 | If event < peak_time the the alert is not setoff. |
||
| 106 | """ |
||
| 107 | proc_list = proc_list or [] |
||
| 108 | |||
| 109 | # Add or update the log |
||
| 110 | item_index = self.__itemexist__(item_type) |
||
| 111 | if item_index < 0: |
||
| 112 | # Item did not exist, add if WARNING or CRITICAL |
||
| 113 | if item_state == "WARNING" or item_state == "CRITICAL": |
||
| 114 | # Define the automatic process sort key |
||
| 115 | self.set_process_sort(item_type) |
||
| 116 | |||
| 117 | # Create the new log item |
||
| 118 | # Time is stored in Epoch format |
||
| 119 | # Epoch -> DMYHMS = datetime.fromtimestamp(epoch) |
||
| 120 | item = [ |
||
| 121 | time.mktime(datetime.now().timetuple()), # START DATE |
||
| 122 | -1, # END DATE |
||
| 123 | item_state, # STATE: WARNING|CRITICAL |
||
| 124 | item_type, # TYPE: CPU, LOAD, MEM... |
||
| 125 | item_value, # MAX |
||
| 126 | item_value, # AVG |
||
| 127 | item_value, # MIN |
||
| 128 | item_value, # SUM |
||
| 129 | 1, # COUNT |
||
| 130 | # Process list is sorted automatically |
||
| 131 | # Overwrite the user choice |
||
| 132 | # topprocess = sorted(proc_list, key=lambda process: process[process_auto_by], |
||
| 133 | # reverse=True) |
||
| 134 | # topprocess[0:3], # TOP 3 PROCESS LIST |
||
| 135 | [], # TOP 3 PROCESS LIST |
||
| 136 | proc_desc] # MONITORED PROCESSES DESC |
||
| 137 | |||
| 138 | # Add the item to the list |
||
| 139 | self.logs_list.insert(0, item) |
||
| 140 | if self.len() > self.logs_max: |
||
| 141 | self.logs_list.pop() |
||
| 142 | else: |
||
| 143 | # Item exist, update |
||
| 144 | if item_state == "OK" or item_state == "CAREFUL": |
||
| 145 | # Reset the automatic process sort key |
||
| 146 | self.reset_process_sort() |
||
| 147 | |||
| 148 | endtime = time.mktime(datetime.now().timetuple()) |
||
| 149 | if endtime - self.logs_list[item_index][0] > peak_time: |
||
| 150 | # If event is > peak_time seconds |
||
| 151 | self.logs_list[item_index][1] = endtime |
||
| 152 | else: |
||
| 153 | # If event <= peak_time seconds, ignore |
||
| 154 | self.logs_list.remove(self.logs_list[item_index]) |
||
| 155 | else: |
||
| 156 | # Update the item |
||
| 157 | # State |
||
| 158 | if item_state == "CRITICAL": |
||
| 159 | self.logs_list[item_index][2] = item_state |
||
| 160 | # Value |
||
| 161 | if item_value > self.logs_list[item_index][4]: |
||
| 162 | # MAX |
||
| 163 | self.logs_list[item_index][4] = item_value |
||
| 164 | elif item_value < self.logs_list[item_index][6]: |
||
| 165 | # MIN |
||
| 166 | self.logs_list[item_index][6] = item_value |
||
| 167 | # AVG |
||
| 168 | self.logs_list[item_index][7] += item_value |
||
| 169 | self.logs_list[item_index][8] += 1 |
||
| 170 | self.logs_list[item_index][5] = (self.logs_list[item_index][7] / |
||
| 171 | self.logs_list[item_index][8]) |
||
| 172 | # TOP PROCESS LIST |
||
| 173 | # # Process list is sorted automaticaly |
||
| 174 | # # Overwrite the user choise |
||
| 175 | # topprocess = sorted(proc_list, key=lambda process: process[process_auto_by], |
||
| 176 | # reverse=True) |
||
| 177 | # # TOP PROCESS LIST |
||
| 178 | # self.logs_list[item_index][9] = topprocess[0:3] |
||
| 179 | self.logs_list[item_index][9] = [] |
||
| 180 | # MONITORED PROCESSES DESC |
||
| 181 | self.logs_list[item_index][10] = proc_desc |
||
| 182 | |||
| 183 | return self.len() |
||
| 184 | |||
| 202 |