| Conditions | 1 |
| Total Lines | 68 |
| 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:
| 1 | #!/usr/bin/env python |
||
| 61 | def build_payload(context): |
||
| 62 | """ |
||
| 63 | Check_MK Contextual Data Example: |
||
| 64 | CONTACTNAME=hirni |
||
| 65 | [email protected] |
||
| 66 | CONTACTPAGER= |
||
| 67 | |||
| 68 | DATE=2012-11-08 |
||
| 69 | SHORTDATETIME=2012-11-08 14:07:1 |
||
| 70 | LONGDATETIME=Thu Nov 8 14:07:12 CET 2012 |
||
| 71 | |||
| 72 | NOTIFICATIONTYPE=PROBLEM |
||
| 73 | LOGDIR=/omd/sites/hirn/var/check_mk/notify |
||
| 74 | |||
| 75 | PARAMETERS=0199399485 Foo/Bar |
||
| 76 | PARAMETER_1=0199399485 |
||
| 77 | PARAMETER_2=Foo/Bar |
||
| 78 | |||
| 79 | HOSTNAME=localhost |
||
| 80 | HOSTALIAS=localhost |
||
| 81 | HOSTADDRESS=127.0.0.1 |
||
| 82 | HOSTCHECKCOMMAND=check-mk-ping |
||
| 83 | HOSTNOTIFICATIONNUMBER=0 |
||
| 84 | HOSTOUTPUT=OK - 127.0.0.1: rta 0.054ms, lost 0% |
||
| 85 | HOSTPERFDATA=rta=0.054ms;200.000;500.000;0; pl=0%;40;80;; |
||
| 86 | HOSTSTATE=UP |
||
| 87 | LASTHOSTSTATE=UP |
||
| 88 | LONGHOSTOUTPUT= |
||
| 89 | |||
| 90 | SERVICEDESC=fs_/ |
||
| 91 | SERVICECHECKCOMMAND=check_mk-df |
||
| 92 | SERVICENOTIFICATIONNUMBER=7 |
||
| 93 | SERVICEOUTPUT=CRIT - 90.0% used (18.27 of 20.3 GB), (level |
||
| 94 | SERVICEPERFDATA=/=18712.1132812MB;16630;18709;0;20788.5820 |
||
| 95 | SERVICESTATE=CRITICAL |
||
| 96 | LASTSERVICESTATE=WARNING |
||
| 97 | LONGSERVICEOUTPUT= |
||
| 98 | """ |
||
| 99 | return { |
||
| 100 | "notification_type": context.get("NOTIFICATIONTYPE"), |
||
| 101 | "timestamp": to_epoch(context.get("SHORTDATETIME")), |
||
| 102 | "parameters": context.get("PARAMETERS").split(), |
||
| 103 | "contact": { |
||
| 104 | "name": context.get("CONTACTNAME"), |
||
| 105 | "email": context.get("CONTACTEMAIL"), |
||
| 106 | "pager": context.get("CONTACTPAGER"), |
||
| 107 | }, |
||
| 108 | "host": { |
||
| 109 | "name": context.get("HOSTNAME"), |
||
| 110 | "alias": context.get("HOSTALIAS"), |
||
| 111 | "address": context.get("HOSTADDRESS"), |
||
| 112 | "state": context.get("HOSTSTATE"), |
||
| 113 | "last_state": context.get("LASTHOSTSTATE"), |
||
| 114 | "output": context.get("HOSTOUTPUT"), |
||
| 115 | "perf_data": context.get("HOSTPERFDATA"), |
||
| 116 | "check_command": context.get("HOSTCHECKCOMMAND"), |
||
| 117 | "notification_number": context.get("HOSTNOTIFICATIONNUMBER"), |
||
| 118 | "long_output": context.get("LONGHOSTOUTPUT"), |
||
| 119 | }, |
||
| 120 | "service": { |
||
| 121 | "desc": context.get("SERVICEDESC"), |
||
| 122 | "state": context.get("SERVICESTATE"), |
||
| 123 | "last_state": context.get("LASTSERVICESTATE"), |
||
| 124 | "output": context.get("SERVICEOUTPUT"), |
||
| 125 | "perf_data": context.get("SERVICEPERFDATA"), |
||
| 126 | "check_command": context.get("SERVICECHECKCOMMAND"), |
||
| 127 | "notification_number": context.get("SERVICENOTIFICATIONNUMBER"), |
||
| 128 | "long_output": context.get("LONGSERVICEOUTPUT"), |
||
| 129 | } |
||
| 154 |