Beispiel #1
0
def _get_agent_items(func, offset, limit, select, filters, search, sort, array=False, query=''):
    agents, result = Agent.get_agents_overview(select={'fields': ['id']})['items'], []

    total = 0

    for agent in agents:
        items = func(agent_id=agent['id'], select=select, filters=filters, limit=limit, offset=offset, search=search,
                     sort=sort, nested=False, q=query)
        if items == {}:
            continue

        total += 1 if not array else items['totalItems']
        items = [items] if not array else items['items']

        for item in items:
            if 0 < limit <= len(result):
                break
            item['agent_id'] = agent['id']
            result.append(item)

    if result:
        if sort and sort['fields']:
            result = sorted(result, key=itemgetter(sort['fields'][0]),
                            reverse=True if sort['order'] == "desc" else False)

        fields_to_nest, non_nested = get_fields_to_nest(result[0].keys(), '_')
    else:
        fields_to_nest, non_nested = None, None

    return {'items': list(map(lambda x: plain_dict_to_nested_dict(x, fields_to_nest, non_nested,
                                                                  WazuhDBQuerySyscollector.nested_fields, '_'),
                              result)),
            'totalItems': total}
Beispiel #2
0
    def _format_data_into_dictionary(self):
        if self.nested:
            fields_to_nest, non_nested = get_fields_to_nest(
                self.fields.keys(), self.nested_fields, '_')
            self._data = [
                plain_dict_to_nested_dict(d, fields_to_nest, non_nested,
                                          self.nested_fields, '_')
                for d in self._data
            ]

        return super()._format_data_into_dictionary() if self.array else next(
            iter(self._data), {})
Beispiel #3
0
def _nest_dictionary_by_keys(fields, dict, keys):
    fields_to_nest, non_nested = get_fields_to_nest(fields.values(), keys, '.')
    return [plain_dict_to_nested_dict(d, fields_to_nest, non_nested, keys, '.') for d in dict]