|
| 1 | +import json |
| 2 | +from typing import Any, List, Optional |
| 3 | + |
| 4 | +from pandas import DataFrame |
| 5 | + |
| 6 | +from ...arrow_client.authenticated_flight_client import AuthenticatedArrowClient |
| 7 | +from ...arrow_client.v2.data_mapper_utils import deserialize_single |
| 8 | +from ...arrow_client.v2.job_client import JobClient |
| 9 | +from ...arrow_client.v2.mutation_client import MutationClient |
| 10 | +from ...arrow_client.v2.write_back_client import WriteBackClient |
| 11 | +from ...graph.graph_object import Graph |
| 12 | +from ..api.estimation_result import EstimationResult |
| 13 | +from ..api.wcc_endpoints import WccEndpoints, WccMutateResult, WccStatsResult, WccWriteResult |
| 14 | +from ..utils.config_converter import ConfigConverter |
| 15 | + |
| 16 | +WCC_ENDPOINT = "v2/community.wcc" |
| 17 | + |
| 18 | + |
| 19 | +class WccArrowEndpoints(WccEndpoints): |
| 20 | + def __init__(self, arrow_client: AuthenticatedArrowClient, write_back_client: Optional[WriteBackClient] = None): |
| 21 | + self._arrow_client = arrow_client |
| 22 | + self._write_back_client = write_back_client |
| 23 | + |
| 24 | + def mutate( |
| 25 | + self, |
| 26 | + G: Graph, |
| 27 | + mutate_property: str, |
| 28 | + threshold: Optional[float] = None, |
| 29 | + relationship_types: Optional[List[str]] = None, |
| 30 | + node_labels: Optional[List[str]] = None, |
| 31 | + sudo: Optional[bool] = None, |
| 32 | + log_progress: Optional[bool] = None, |
| 33 | + username: Optional[str] = None, |
| 34 | + concurrency: Optional[int] = None, |
| 35 | + job_id: Optional[str] = None, |
| 36 | + seed_property: Optional[str] = None, |
| 37 | + consecutive_ids: Optional[bool] = None, |
| 38 | + relationship_weight_property: Optional[str] = None, |
| 39 | + ) -> WccMutateResult: |
| 40 | + config = ConfigConverter.convert_to_gds_config( |
| 41 | + graph_name=G.name(), |
| 42 | + concurrency=concurrency, |
| 43 | + consecutive_ids=consecutive_ids, |
| 44 | + job_id=job_id, |
| 45 | + log_progress=log_progress, |
| 46 | + node_labels=node_labels, |
| 47 | + relationship_types=relationship_types, |
| 48 | + relationship_weight_property=relationship_weight_property, |
| 49 | + seed_property=seed_property, |
| 50 | + sudo=sudo, |
| 51 | + threshold=threshold, |
| 52 | + ) |
| 53 | + |
| 54 | + job_id = JobClient.run_job_and_wait(self._arrow_client, WCC_ENDPOINT, config) |
| 55 | + |
| 56 | + mutate_result = MutationClient.mutate_node_property(self._arrow_client, job_id, mutate_property) |
| 57 | + computation_result = JobClient.get_summary(self._arrow_client, job_id) |
| 58 | + |
| 59 | + computation_result["nodePropertiesWritten"] = mutate_result.node_properties_written |
| 60 | + computation_result["mutateMillis"] = 0 |
| 61 | + |
| 62 | + return WccMutateResult(**computation_result) |
| 63 | + |
| 64 | + def stats( |
| 65 | + self, |
| 66 | + G: Graph, |
| 67 | + threshold: Optional[float] = None, |
| 68 | + relationship_types: Optional[List[str]] = None, |
| 69 | + node_labels: Optional[List[str]] = None, |
| 70 | + sudo: Optional[bool] = None, |
| 71 | + log_progress: Optional[bool] = None, |
| 72 | + username: Optional[str] = None, |
| 73 | + concurrency: Optional[int] = None, |
| 74 | + job_id: Optional[str] = None, |
| 75 | + seed_property: Optional[str] = None, |
| 76 | + consecutive_ids: Optional[bool] = None, |
| 77 | + relationship_weight_property: Optional[str] = None, |
| 78 | + ) -> WccStatsResult: |
| 79 | + config = ConfigConverter.convert_to_gds_config( |
| 80 | + graph_name=G.name(), |
| 81 | + concurrency=concurrency, |
| 82 | + consecutive_ids=consecutive_ids, |
| 83 | + job_id=job_id, |
| 84 | + log_progress=log_progress, |
| 85 | + node_labels=node_labels, |
| 86 | + relationship_types=relationship_types, |
| 87 | + relationship_weight_property=relationship_weight_property, |
| 88 | + seed_property=seed_property, |
| 89 | + sudo=sudo, |
| 90 | + threshold=threshold, |
| 91 | + ) |
| 92 | + |
| 93 | + job_id = JobClient.run_job_and_wait(self._arrow_client, WCC_ENDPOINT, config) |
| 94 | + computation_result = JobClient.get_summary(self._arrow_client, job_id) |
| 95 | + |
| 96 | + return WccStatsResult(**computation_result) |
| 97 | + |
| 98 | + def stream( |
| 99 | + self, |
| 100 | + G: Graph, |
| 101 | + min_component_size: Optional[int] = None, |
| 102 | + threshold: Optional[float] = None, |
| 103 | + relationship_types: Optional[List[str]] = None, |
| 104 | + node_labels: Optional[List[str]] = None, |
| 105 | + sudo: Optional[bool] = None, |
| 106 | + log_progress: Optional[bool] = None, |
| 107 | + username: Optional[str] = None, |
| 108 | + concurrency: Optional[int] = None, |
| 109 | + job_id: Optional[str] = None, |
| 110 | + seed_property: Optional[str] = None, |
| 111 | + consecutive_ids: Optional[bool] = None, |
| 112 | + relationship_weight_property: Optional[str] = None, |
| 113 | + ) -> DataFrame: |
| 114 | + config = ConfigConverter.convert_to_gds_config( |
| 115 | + graph_name=G.name(), |
| 116 | + concurrency=concurrency, |
| 117 | + consecutive_ids=consecutive_ids, |
| 118 | + job_id=job_id, |
| 119 | + log_progress=log_progress, |
| 120 | + min_component_size=min_component_size, |
| 121 | + node_labels=node_labels, |
| 122 | + relationship_types=relationship_types, |
| 123 | + relationship_weight_property=relationship_weight_property, |
| 124 | + seed_property=seed_property, |
| 125 | + sudo=sudo, |
| 126 | + threshold=threshold, |
| 127 | + ) |
| 128 | + |
| 129 | + job_id = JobClient.run_job_and_wait(self._arrow_client, WCC_ENDPOINT, config) |
| 130 | + return JobClient.stream_results(self._arrow_client, G.name(), job_id) |
| 131 | + |
| 132 | + def write( |
| 133 | + self, |
| 134 | + G: Graph, |
| 135 | + write_property: str, |
| 136 | + min_component_size: Optional[int] = None, |
| 137 | + threshold: Optional[float] = None, |
| 138 | + relationship_types: Optional[List[str]] = None, |
| 139 | + node_labels: Optional[List[str]] = None, |
| 140 | + sudo: Optional[bool] = None, |
| 141 | + log_progress: Optional[bool] = None, |
| 142 | + username: Optional[str] = None, |
| 143 | + concurrency: Optional[int] = None, |
| 144 | + job_id: Optional[str] = None, |
| 145 | + seed_property: Optional[str] = None, |
| 146 | + consecutive_ids: Optional[bool] = None, |
| 147 | + relationship_weight_property: Optional[str] = None, |
| 148 | + write_concurrency: Optional[int] = None, |
| 149 | + ) -> WccWriteResult: |
| 150 | + config = ConfigConverter.convert_to_gds_config( |
| 151 | + graph_name=G.name(), |
| 152 | + concurrency=concurrency, |
| 153 | + consecutive_ids=consecutive_ids, |
| 154 | + job_id=job_id, |
| 155 | + log_progress=log_progress, |
| 156 | + min_component_size=min_component_size, |
| 157 | + node_labels=node_labels, |
| 158 | + relationship_types=relationship_types, |
| 159 | + relationship_weight_property=relationship_weight_property, |
| 160 | + seed_property=seed_property, |
| 161 | + sudo=sudo, |
| 162 | + threshold=threshold, |
| 163 | + ) |
| 164 | + |
| 165 | + job_id = JobClient.run_job_and_wait(self._arrow_client, WCC_ENDPOINT, config) |
| 166 | + computation_result = JobClient.get_summary(self._arrow_client, job_id) |
| 167 | + |
| 168 | + if self._write_back_client is None: |
| 169 | + raise Exception("Write back client is not initialized") |
| 170 | + |
| 171 | + write_millis = self._write_back_client.write( |
| 172 | + G.name(), job_id, write_concurrency if write_concurrency is not None else concurrency |
| 173 | + ) |
| 174 | + |
| 175 | + computation_result["writeMillis"] = write_millis |
| 176 | + |
| 177 | + return WccWriteResult(**computation_result) |
| 178 | + |
| 179 | + def estimate( |
| 180 | + self, G: Optional[Graph] = None, projection_config: Optional[dict[str, Any]] = None |
| 181 | + ) -> EstimationResult: |
| 182 | + if G is not None: |
| 183 | + payload = {"graphName": G.name()} |
| 184 | + elif projection_config is not None: |
| 185 | + payload = projection_config |
| 186 | + else: |
| 187 | + raise ValueError("Either graph_name or projection_config must be provided.") |
| 188 | + |
| 189 | + res = self._arrow_client.do_action_with_retry("v2/community.wcc.estimate", json.dumps(payload).encode("utf-8")) |
| 190 | + |
| 191 | + return EstimationResult(**deserialize_single(res)) |
0 commit comments