def _get_children(self, client: DgraphClient, eg=True) -> Optional[List[Process]]: if eg: q_filter = '@filter(eq(engagement_key, "{}"))'.format( self.engagement_key) else: q_filter = '' proc_res = json.loads( client.query("""{{ q0(func: eq(node_key, "{}")) {} {{ children {{ uid, node_key, image_name, image_path, }} }} }}""".format(self.node_key, q_filter)).json)['q0'] if not proc_res: return None return [self.from_dict(child) for child in proc_res[0]['children']]
def _get_parent(self, client: DgraphClient, eg=True) -> Optional[Process]: if eg: q_filter = '@filter(eq(engagement_key, "{}"))'.format( self.engagement_key) else: q_filter = '' proc_res = json.loads( client.query("""{{ q0(func: eq(node_key, "{}")) {} {{ ~children {{ uid, node_key, image_name, image_path, }} }} }}""".format(self.node_key, q_filter)).json)['q0'] if not proc_res: return None return Process( proc_res[0]['~children'][0]['node_key'], proc_res[0]['~children'][0]['uid'], self.engagement_key, proc_res[0]['~children'][0].get('image_name', None), proc_res[0]['~children'][0].get('image_path', None), self.engagement, )
def analyze_by_node_key( client: DgraphClient, keys: Union[Iterable[str], Subgraph], signature_fn: Callable[[str], str]) -> List[Dict[str, Any]]: if isinstance(keys, Subgraph): keys = [node_key for node_key in keys.subgraph.nodes] queries = [signature_fn(node_key) for node_key in keys] batched = batch_queries(queries) response = json.loads(client.query(batched).json) return list(itertools.chain.from_iterable(response.values()))
def should_throttle(engagement_key: str, dgraph_client: DgraphClient) -> bool: query = """ query q0($a: string) { q0(func: eq(engagement_key, $a), first: 1) { uid, } } """ res = json.loads(dgraph_client.query(query, variables={'$a': engagement_key}).json) if res['q0']: return True return False