Changeset View
Changeset View
Standalone View
Standalone View
swh/graph/http_naive_client.py
- This file was moved from swh/graph/naive_client.py.
Show All 16 Lines | from typing import ( | ||||
Set, | Set, | ||||
Tuple, | Tuple, | ||||
TypeVar, | TypeVar, | ||||
Union, | Union, | ||||
) | ) | ||||
from swh.model.swhids import CoreSWHID, ExtendedSWHID, ValidationError | from swh.model.swhids import CoreSWHID, ExtendedSWHID, ValidationError | ||||
from .client import GraphArgumentException | from .http_client import GraphArgumentException | ||||
_NODE_TYPES = "ori|snp|rel|rev|dir|cnt" | _NODE_TYPES = "ori|snp|rel|rev|dir|cnt" | ||||
NODES_RE = re.compile(rf"(\*|{_NODE_TYPES})") | NODES_RE = re.compile(rf"(\*|{_NODE_TYPES})") | ||||
EDGES_RE = re.compile(rf"(\*|{_NODE_TYPES}):(\*|{_NODE_TYPES})") | EDGES_RE = re.compile(rf"(\*|{_NODE_TYPES}):(\*|{_NODE_TYPES})") | ||||
T = TypeVar("T", bound=Callable) | T = TypeVar("T", bound=Callable) | ||||
SWHIDlike = Union[CoreSWHID, ExtendedSWHID, str] | SWHIDlike = Union[CoreSWHID, ExtendedSWHID, str] | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | def filter_node_types(node_types: str, nodes: Iterable[str]) -> Iterator[str]: | ||||
else: | else: | ||||
prefixes = tuple(f"swh:1:{type_}:" for type_ in node_types.split(",")) | prefixes = tuple(f"swh:1:{type_}:" for type_ in node_types.split(",")) | ||||
for node in nodes: | for node in nodes: | ||||
if node.startswith(prefixes): | if node.startswith(prefixes): | ||||
yield node | yield node | ||||
class NaiveClient: | class NaiveClient: | ||||
"""An alternative implementation of :class:`swh.graph.backend.Backend`, | """An alternative implementation of the graph server, written in | ||||
written in pure-python and meant for simulating it in other components' test | pure-python and meant for simulating it in other components' test cases; | ||||
cases; constructed from a list of nodes and (directed) edges, both | constructed from a list of nodes and (directed) edges, both represented as | ||||
represented as SWHIDs. | SWHIDs. | ||||
It is NOT meant to be efficient in any way; only to be a very simple | It is NOT meant to be efficient in any way; only to be a very simple | ||||
implementation that provides the same behavior. | implementation that provides the same behavior. | ||||
>>> nodes = [ | >>> nodes = [ | ||||
... "swh:1:rev:1111111111111111111111111111111111111111", | ... "swh:1:rev:1111111111111111111111111111111111111111", | ||||
... "swh:1:rev:2222222222222222222222222222222222222222", | ... "swh:1:rev:2222222222222222222222222222222222222222", | ||||
... "swh:1:rev:3333333333333333333333333333333333333333", | ... "swh:1:rev:3333333333333333333333333333333333333333", | ||||
Show All 23 Lines | def _check_swhid(self, swhid): | ||||
ExtendedSWHID.from_string(swhid) | ExtendedSWHID.from_string(swhid) | ||||
except ValidationError as e: | except ValidationError as e: | ||||
raise GraphArgumentException(*e.args) from None | raise GraphArgumentException(*e.args) from None | ||||
if swhid not in self.graph.nodes: | if swhid not in self.graph.nodes: | ||||
raise GraphArgumentException(f"SWHID not found: {swhid}") | raise GraphArgumentException(f"SWHID not found: {swhid}") | ||||
def stats(self) -> Dict: | def stats(self) -> Dict: | ||||
return { | return { | ||||
"counts": { | "num_nodes": len(self.graph.nodes), | ||||
"nodes": len(self.graph.nodes), | "num_edges": sum(map(len, self.graph.forward_edges.values())), | ||||
"edges": sum(map(len, self.graph.forward_edges.values())), | "compression_ratio": 1.0, | ||||
}, | |||||
"ratios": { | |||||
"compression": 1.0, | |||||
"bits_per_edge": 100.0, | "bits_per_edge": 100.0, | ||||
"bits_per_node": 100.0, | "bits_per_node": 100.0, | ||||
"avg_locality": 0.0, | "avg_locality": 0.0, | ||||
}, | "indegree_min": min(map(len, self.graph.backward_edges.values())), | ||||
"indegree": { | "indegree_max": max(map(len, self.graph.backward_edges.values())), | ||||
"min": min(map(len, self.graph.backward_edges.values())), | "indegree_avg": statistics.mean( | ||||
"max": max(map(len, self.graph.backward_edges.values())), | map(len, self.graph.backward_edges.values()) | ||||
"avg": statistics.mean(map(len, self.graph.backward_edges.values())), | ), | ||||
}, | "outdegree_min": min(map(len, self.graph.forward_edges.values())), | ||||
"outdegree": { | "outdegree_max": max(map(len, self.graph.forward_edges.values())), | ||||
"min": min(map(len, self.graph.forward_edges.values())), | "outdegree_avg": statistics.mean( | ||||
"max": max(map(len, self.graph.forward_edges.values())), | map(len, self.graph.forward_edges.values()) | ||||
"avg": statistics.mean(map(len, self.graph.forward_edges.values())), | ), | ||||
}, | |||||
} | } | ||||
@check_arguments | @check_arguments | ||||
def leaves( | def leaves( | ||||
self, | self, | ||||
src: str, | src: str, | ||||
edges: str = "*", | edges: str = "*", | ||||
direction: str = "forward", | direction: str = "forward", | ||||
▲ Show 20 Lines • Show All 245 Lines • Show Last 20 Lines |