| Conditions | 19 |
| Total Lines | 80 |
| 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 traceroutedb.receive_traces() 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 | #!/usr/bin/env python |
||
| 66 | @app.route("/trace", methods=["POST"]) |
||
| 67 | def receive_traces(): |
||
| 68 | config = app.config["trdb"] |
||
| 69 | if config.get("mmdb", False): |
||
| 70 | reader = config["mmdb"] |
||
| 71 | |||
| 72 | if request.method == "POST": |
||
| 73 | cur = conn.cursor() |
||
| 74 | data = request.get_json(force=True) |
||
| 75 | |||
| 76 | trace = data["data"] |
||
| 77 | reporter = data["reporter"] |
||
| 78 | kvs = {"note": data.get("note"), "ext_ip": data.get("ext_ip")} |
||
| 79 | |||
| 80 | if config.debug: |
||
| 81 | print("SELECT nextval('traceroute_id_seq');") |
||
| 82 | trace_id = "DEBUG_ID" |
||
| 83 | else: |
||
| 84 | try: |
||
| 85 | cur.execute("SELECT nextval('traceroute_id_seq');") |
||
| 86 | except psycopg2.ProgrammingError as e: |
||
| 87 | logging.error(str(e)) |
||
| 88 | conn.rollback() |
||
| 89 | abort(503) |
||
| 90 | trace_id = cur.fetchone()[0] |
||
| 91 | |||
| 92 | trace_sql = "INSERT INTO traceroute VALUES ({0}, '{1}', '{2}', now(), '{3}', {4}::HSTORE);".format(trace_id, trace["src_ip"], trace["dst_ip"], reporter, dictToHstore(kvs)) |
||
| 93 | if config.debug: |
||
| 94 | print(trace_sql) |
||
| 95 | else: |
||
| 96 | try: |
||
| 97 | cur.execute(trace_sql) |
||
| 98 | except psycopg2.ProgrammingError as e: |
||
| 99 | logging.error(str(e)) |
||
| 100 | conn.rollback() |
||
| 101 | abort(503) |
||
| 102 | |||
| 103 | hops = trace["hops"] |
||
| 104 | for key in hops.keys(): |
||
| 105 | hop = hops[key] |
||
| 106 | for probe in hop: |
||
| 107 | if probe["ip"] is None or probe["rtt"] is None: |
||
| 108 | continue |
||
| 109 | else: |
||
| 110 | try: |
||
| 111 | probe["isp"] = reader.isp(probe["ip"]) |
||
| 112 | except: |
||
| 113 | pass |
||
| 114 | |||
| 115 | kvs = {} |
||
| 116 | time = probe.get("rtt", None) |
||
| 117 | if time is not None and time != "None": |
||
| 118 | kvs["time"] = time |
||
| 119 | |||
| 120 | anno = probe.get("anno", None) |
||
| 121 | if anno: |
||
| 122 | kvs["anno"] = anno |
||
| 123 | |||
| 124 | try: |
||
| 125 | asn = probe.get("isp").raw.get("autonomous_system_number", None) |
||
| 126 | if asn: |
||
| 127 | kvs["asn"] = asn |
||
| 128 | except: |
||
| 129 | pass |
||
| 130 | |||
| 131 | kvs = dictToHstore(kvs) |
||
| 132 | if config.debug: |
||
| 133 | print("INSERT INTO hop VALUES (nextval('probe_id_seq'), {0}, {1}, {2}, '{3}', now());".format(trace_id, key, kvs, probe["ip"])) |
||
| 134 | else: |
||
| 135 | try: |
||
| 136 | cur.execute("INSERT INTO hop VALUES (nextval('probe_id_seq'), {0}, {1}, {2}, '{3}', now());".format(trace_id, key, kvs, probe["ip"])) |
||
| 137 | except psycopg2.ProgrammingError as e: |
||
| 138 | logging.error(str(e)) |
||
| 139 | conn.rollback() |
||
| 140 | abort(503) |
||
| 141 | conn.commit() |
||
| 142 | cur.close() |
||
| 143 | conn.commit() |
||
| 144 | print("ip:", request.remote_addr, "submitted result for:", trace["src_ip"], ">", trace["dst_ip"], "trace_id:", trace_id) |
||
| 145 | return 'OK' |
||
| 146 | |||
| 180 |