| Conditions | 16 |
| Total Lines | 78 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 24 |
| CRAP Score | 30.5635 |
| 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 build.main.Main._validate_payload() 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 | """Main module of kytos/pathfinder Kytos Network Application.""" |
||
| 111 | 1 | paths = self.graph.constrained_k_shortest_paths( |
|
| 112 | data["source"], |
||
| 113 | data["destination"], |
||
| 114 | weight=self.graph.spf_edge_data_cbs[spf_attr], |
||
| 115 | k=spf_max_paths, |
||
| 116 | graph=graph, |
||
| 117 | minimum_hits=minimum_hits, |
||
| 118 | mandatory_metrics=mandatory_metrics, |
||
| 119 | flexible_metrics=flexible_metrics, |
||
| 120 | ) |
||
| 121 | else: |
||
| 122 | 1 | paths = self.graph.k_shortest_paths( |
|
| 123 | data["source"], |
||
| 124 | data["destination"], |
||
| 125 | weight=self.graph.spf_edge_data_cbs[spf_attr], |
||
| 126 | k=spf_max_paths, |
||
| 127 | graph=graph, |
||
| 128 | ) |
||
| 129 | |||
| 130 | 1 | paths = self.graph.path_cost_builder( |
|
| 131 | paths, |
||
| 132 | weight=spf_attr, |
||
| 133 | ) |
||
| 134 | 1 | log.debug(f"Found paths: {paths}") |
|
| 135 | 1 | except TypeError as err: |
|
| 136 | 1 | raise HTTPException(400, str(err)) |
|
| 137 | |||
| 138 | 1 | paths = self._filter_paths_le_cost(paths, max_cost=spf_max_path_cost) |
|
| 139 | 1 | log.debug(f"Filtered paths: {paths}") |
|
| 140 | 1 | return JSONResponse({"paths": paths}) |
|
| 141 | |||
| 142 | 1 | @listen_to( |
|
| 143 | "kytos.topology.updated", |
||
| 144 | "kytos/topology.current", |
||
| 145 | "kytos/topology.topology_loaded", |
||
| 146 | ) |
||
| 147 | 1 | def on_topology_updated(self, event): |
|
| 148 | """Update the graph when the network topology is updated.""" |
||
| 149 | self.update_topology(event) |
||
| 150 | |||
| 151 | 1 | def update_topology(self, event): |
|
| 152 | """Update the graph when the network topology is updated.""" |
||
| 153 | 1 | if "topology" not in event.content: |
|
| 154 | 1 | return |
|
| 155 | 1 | topology = event.content["topology"] |
|
| 156 | 1 | with self._lock: |
|
| 157 | 1 | if ( |
|
| 158 | self._topology_updated_at |
||
| 159 | and self._topology_updated_at > event.timestamp |
||
| 160 | ): |
||
| 161 | 1 | return |
|
| 162 | 1 | self._topology = topology |
|
| 163 | 1 | self._topology_updated_at = event.timestamp |
|
| 164 | 1 | self.graph.update_topology(topology) |
|
| 165 | 1 | switches = list(topology.switches.keys()) |
|
| 166 | 1 | links = list(topology.links.keys()) |
|
| 167 | 1 | log.debug(f"Topology graph updated with switches: {switches}, links: {links}.") |
|
| 168 | |||
| 169 | 1 | def update_links_metadata_changed(self, event) -> None: |
|
| 170 | """Update the graph when links' metadata are added or removed.""" |
||
| 171 | 1 | link = event.content["link"] |
|
| 172 | 1 | try: |
|
| 173 | 1 | with self._lock: |
|
| 174 | 1 | if ( |
|
| 175 | link.id in self._links_updated_at |
||
| 176 | and self._links_updated_at[link.id] > event.timestamp |
||
| 177 | ): |
||
| 178 | 1 | return |
|
| 179 | 1 | self.graph.update_link_metadata(link) |
|
| 180 | 1 | self._links_updated_at[link.id] = event.timestamp |
|
| 181 | 1 | metadata = event.content["metadata"] |
|
| 182 | 1 | log.debug(f"Topology graph updated link id: {link.id} metadata: {metadata}") |
|
| 183 | 1 | except KeyError as exc: |
|
| 184 | 1 | log.warning( |
|
| 185 | f"Unexpected KeyError {str(exc)} on event {event}." |
||
| 186 | " pathfinder will reconciliate the topology" |
||
| 187 | ) |
||
| 188 | 1 | self.controller.buffers.app.put(KytosEvent(name="kytos/topology.get")) |
|
| 189 | |||
| 194 |