| Conditions | 11 | 
| Total Lines | 54 | 
| 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 st2common.cmd.purge_executions() 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 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more | 
            ||
| 74 | def purge_executions(timestamp=None, action_ref=None, purge_incomplete=False):  | 
            ||
| 75 | if not timestamp:  | 
            ||
| 76 |         LOG.error('Specify a valid timestamp to purge.') | 
            ||
| 77 | return 1  | 
            ||
| 78 | |||
| 79 |     LOG.info('Purging executions older than timestamp: %s' % | 
            ||
| 80 |              timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) | 
            ||
| 81 | |||
| 82 |     filters = {} | 
            ||
| 83 | |||
| 84 | if purge_incomplete:  | 
            ||
| 85 | filters['start_timestamp__lt'] = isotime.parse(timestamp)  | 
            ||
| 86 | else:  | 
            ||
| 87 | filters['end_timestamp__lt'] = isotime.parse(timestamp)  | 
            ||
| 88 | filters['start_timestamp__lt'] = isotime.parse(timestamp)  | 
            ||
| 89 |         filters['status'] = {"$in": DONE_STATES} | 
            ||
| 90 | |||
| 91 | exec_filters = copy.copy(filters)  | 
            ||
| 92 | if action_ref:  | 
            ||
| 93 | exec_filters['action__ref'] = action_ref  | 
            ||
| 94 | |||
| 95 | liveaction_filters = copy.copy(filters)  | 
            ||
| 96 | if action_ref:  | 
            ||
| 97 | liveaction_filters['action'] = action_ref  | 
            ||
| 98 | |||
| 99 | try:  | 
            ||
| 100 | ActionExecution.delete_by_query(**exec_filters)  | 
            ||
| 101 | except InvalidQueryError:  | 
            ||
| 102 |         LOG.exception('Bad query (%s) used to delete execution instances. ' + | 
            ||
| 103 | 'Please contact support.', exec_filters)  | 
            ||
| 104 | return 2  | 
            ||
| 105 | except:  | 
            ||
| 106 |         LOG.exception('Deletion of execution models failed for query with filters: %s.', | 
            ||
| 107 | exec_filters)  | 
            ||
| 108 | |||
| 109 | try:  | 
            ||
| 110 | LiveAction.delete_by_query(**liveaction_filters)  | 
            ||
| 111 | except InvalidQueryError:  | 
            ||
| 112 |         LOG.exception('Bad query (%s) used to delete liveaction instances. ' + | 
            ||
| 113 | 'Please contact support.', liveaction_filters)  | 
            ||
| 114 | return 3  | 
            ||
| 115 | except:  | 
            ||
| 116 |         LOG.exception('Deletion of liveaction models failed for query with filters: %s.', | 
            ||
| 117 | liveaction_filters)  | 
            ||
| 118 | |||
| 119 | zombie_execution_instances = len(ActionExecution.query(**exec_filters))  | 
            ||
| 120 | zombie_liveaction_instances = len(LiveAction.query(**liveaction_filters))  | 
            ||
| 121 | |||
| 122 | if (zombie_execution_instances > 0) or (zombie_liveaction_instances > 0):  | 
            ||
| 123 |         LOG.error('Zombie execution instances left: %d.', zombie_execution_instances) | 
            ||
| 124 |         LOG.error('Zombie liveaction instances left: %s.', zombie_liveaction_instances) | 
            ||
| 125 | else:  | 
            ||
| 126 | # Print stats  | 
            ||
| 127 |         LOG.info('#### All execution models less than timestamp %s were deleted.', timestamp) | 
            ||
| 128 | |||
| 152 |