def aggregation_parser(out_dict, agg_dict): """ Parses a part of the aggregation tree recursively :param out_dict: :param agg_dict: """ # Go over the items in the es_response for key, value_dict in agg_dict.items(): if type(value_dict) == dict: # whenever we have some more aggregations of term if "buckets" in value_dict.keys(): agg_list = extract_value(value_dict, "buckets") if agg_list: # Go over the entire list for i in range(len(agg_list)): agg_dict = agg_list[i] value = extract_value(agg_dict, "key") if not value: continue # Check if we are at the last aggregation # Recursion stops when we reach the last aggregation if len(agg_dict.keys()) <= 2: # If we have more to go if len(agg_list) > 1: ESAdapter._add_to_dict(out_dict, key, value, value_as_list=True) else: ESAdapter._add_to_dict( out_dict, key, value) else: ESAdapter._add_to_dict(out_dict, key, value, value_as_dict=True) # Continue the recursion ESAdapter.aggregation_parser( out_dict[key][value], agg_dict) # Parse value aggregations else: value = extract_value(value_dict, "value") ESAdapter._add_to_dict(out_dict, key, value) return out_dict
def parse_es_put_operation_response(es_response): """ This method parses the response received when creating an ES index :param dictionary es_response: :return bool: """ if extract_value(es_response, "acknowledged"): return True else: return False
def parse_es_insert_response(es_response): """ This method parses the returned response after inserting data to ES :param dict es_response: :return bool: returned status """ created_status = extract_value(es_response, "created") if created_status: return True else: return False
def get_field_func(self, field_type): """ Return the parsing function :param field_type: :return func: :raise ValueError: When received a wrong field_type """ field_func = extract_value(self.function_mapper[field_type]) # When we got an invalid field_type if not field_func: raise ValueError("Wrong field type received %s" % field_type) return field_func
def get_single_param_value_from_object(self, serial_number, param_name): """ Returns the profile name for a given AP :param str serial_number: :param str param_name: The name of the parameter to take from the list :return str: The value of the parameter """ param_list = [param_name] params_dict = self.get_param_value_from_object(serial_number, param_list) name_list = extract_value(params_dict, param_list[0]) # The returned value is a list of values if name_list and type(name_list) is list: return name_list[0] else: return ""
def parse_aggregation_response_to_dict(self, es_response): """ Parses an ES response coming from a query returning a list of aggregated values The response is parse to {NAME: VALUE} dictionary :param dict es_response: Response from ES :return dict object_dict: {Name: Value} """ object_dict = {} agg_dict = extract_value(es_response, "aggregations") if not agg_dict: self.logger.error( "Error receiving aggregation dictionary from es query in %s, %s" % (class_name(self), es_response)) return object_dict out_dict = {} self.aggregation_parser(out_dict, agg_dict) return out_dict
def parse_es_aggregation_list(self, es_response, agg_name): """ This method parses an ES aggregation into a list of values :param dictionary es_response: The response from ES :param string agg_name: The name of the aggregation to make a list :return list : """ try: item_list_doc = extract_value(es_response, 'aggregations', agg_name, "buckets") item_list = [] for items in item_list_doc: item_list.append(items['key']) return item_list except KeyError as exc: raise ESAdapterException( "Error while trying to create a list in %s: %s" % (class_name(self), exc))
def convert_kafka_topic_to_rest_api(kafka_topic): return extract_value(KAFKA_TOPIC_TO_RW_API_MAPPER, kafka_topic)