def main(): args = parse_args(sys.argv[1:]) attribute_ids_to_print = args.attr if args.attr else ['hostname'] attribute_ids_to_fetch = list(attribute_ids_to_print) if args.reset: attribute_ids_to_fetch.extend(args.reset) if args.update: attribute_ids_to_fetch.extend(u[0] for u in args.update) # TODO: Avoid .join() filters = parse_query(' '.join(args.query)) query = Query(filters, attribute_ids_to_fetch, args.order) if args.one and len(query) > 1: raise Exception( 'Expecting exactly one server, found {} servers' .format(len(query)) ) for server in query: if args.reset: apply_resets(server, args.reset) if args.update: apply_updates(server, args.update) print_server(server, attribute_ids_to_print) if args.reset or args.update: query.commit()
def get_results(request): term = request.GET.get('term', '') shown_attributes = request.GET.get('shown_attributes').split(',') # We need servertypes to return the attribute properties. if 'servertype' not in shown_attributes: shown_attributes.append('servertype') try: offset = int(request.GET.get('offset', '0')) limit = int(request.GET.get('limit', '0')) except ValueError: offset = 0 limit = NUM_SERVERS_DEFAULT if 'order_by' in request.GET: order_by = [request.GET['order_by']] else: order_by = None try: query = Query(parse_query(term), shown_attributes, order_by) num_servers = len(query) except (DatatypeError, ObjectDoesNotExist, ValidationError) as error: return HttpResponse( json.dumps({ 'status': 'error', 'message': str(error) })) servers = list(islice(query, offset, offset + limit)) request.session['term'] = term request.session['per_page'] = limit # Add information about available, editable attributes on servertypes servertype_ids = {s['servertype'] for s in servers} editable_attributes = dict() for servertype_id in servertype_ids: editable_attributes[servertype_id] = list(Attribute.specials) for sa in ServertypeAttribute.objects.filter( servertype_id__in=servertype_ids, attribute_id__in=shown_attributes, related_via_attribute_id__isnull=True, attribute__readonly=False, ): editable_attributes[sa.servertype_id].append(sa.attribute_id) return HttpResponse(json.dumps( { 'status': 'success', 'understood': repr(query), 'servers': servers, 'num_servers': num_servers, 'editable_attributes': editable_attributes, }, default=json_encode_extra), content_type='application/x-json')
def export(request): term = request.GET.get('term', '') try: query = Query(parse_query(term), ['hostname']) except (DatatypeError, ObjectDoesNotExist, ValidationError) as error: return HttpResponse(str(error), status=400) hostnames = ' '.join(server['hostname'] for server in query) return HttpResponse(hostnames, content_type='text/plain')
def index(request): """The hardware resources page""" term = request.GET.get('term', request.session.get('term', '')) collections = list(Collection.objects.filter(overview=True)) # If a graph collection was specified, use it. Otherwise use the first # one. for collection in collections: if request.GET.get('current_collection'): if str(collection.id) != request.GET['current_collection']: continue current_collection = collection break else: return HttpResponseBadRequest('No matching current collection') template_info = { 'search_term': term, 'collections': collections, 'current_collection': current_collection.id, } # TODO: Generalize this part using the relations hostnames = [] matched_hostnames = [] if term: try: query_args = parse_query(term) host_query = Query(query_args, ['hostname', 'hypervisor']) for host in host_query: matched_hostnames.append(host['hostname']) if host.get('hypervisor'): hostnames.append(host['hypervisor']) else: # If it's not guest, it might be a server, so we add it hostnames.append(host['hostname']) understood = repr(host_query) request.session['term'] = term if len(hostnames) == 0: template_info.update({ 'understood': understood, }) return TemplateResponse(request, 'resources/index.html', template_info) except (DatatypeError, ValidationError) as error: template_info.update({'error': str(error)}) return TemplateResponse(request, 'resources/index.html', template_info) else: understood = repr(Query({})) variations = list(current_collection.variation_set.all()) columns = [] attribute_ids = ['hostname', 'servertype'] graph_index = 0 sprite_width = settings.GRAPHITE_SPRITE_WIDTH for template in current_collection.template_set.all(): for variation in variations: columns.append({ 'name': str(template) + ' ' + str(variation), 'type': 'graph', 'graph_index': graph_index, 'sprite_offset': graph_index * sprite_width, }) graph_index += 1 for numeric in current_collection.numeric_set.all(): columns.append({ 'name': str(numeric), 'type': 'numeric', }) attribute_ids.append(numeric.attribute_id) for relation in current_collection.relation_set.all(): columns.append({ 'name': str(relation), 'type': 'relation', }) attribute_ids.append(relation.attribute_id) hosts = OrderedDict() filters = {GRAPHITE_ATTRIBUTE_ID: collection.name} if len(hostnames) > 0: filters['hostname'] = Any(*hostnames) for server in Query(filters, attribute_ids): hosts[server['hostname']] = dict(server) sprite_url = settings.MEDIA_URL + 'graph_sprite/' + collection.name template_info.update({ 'columns': columns, 'hosts': hosts.values(), 'matched_hostnames': matched_hostnames, 'understood': understood, 'error': None, 'sprite_url': sprite_url, }) return TemplateResponse(request, 'resources/index.html', template_info)
def get_results(request): term = request.GET.get('term', '') shown_attributes = request.GET.getlist('shown_attributes[]') deep_link = bool(strtobool(request.GET.get('deep_link', 'false'))) if request.session.get('save_attributes') and not deep_link: request.session['shown_attributes'] = shown_attributes try: offset = int(request.GET.get('offset', '0')) limit = int(request.GET.get('limit', '0')) request.session['limit'] = limit except ValueError: offset = 0 limit = NUM_SERVERS_DEFAULT if 'order_by' in request.GET: order_by = [request.GET['order_by']] else: order_by = None try: # Query manipulates shown_attributes by adding object_id we want to # keep the original value to save settings ... restrict = shown_attributes.copy() if 'servertype' not in restrict: restrict.append('servertype') query = Query(parse_query(term), restrict, order_by) # TODO: Using len is terribly slow for large datasets because it has # to query all objects but we cannot use count which is available on # Django QuerySet num_servers = len(query) except (DatatypeError, ObjectDoesNotExist, ValidationError) as error: return HttpResponse( json.dumps({ 'status': 'error', 'message': str(error) })) # Query successful term must be valid here, so we can save it safely now. request.session['term'] = term servers = list(islice(query, offset, offset + limit)) # Add information about available, editable attributes on servertypes servertype_ids = {s['servertype'] for s in servers} # We do not support editing of all attributes default_editable = list(Attribute.specials) default_editable.remove('object_id') default_editable.remove('servertype') editable_attributes = dict() for servertype_id in servertype_ids: editable_attributes[servertype_id] = default_editable.copy() for sa in ServertypeAttribute.objects.filter( servertype_id__in=servertype_ids, attribute_id__in=shown_attributes, related_via_attribute_id__isnull=True, attribute__readonly=False, ): editable_attributes[sa.servertype_id].append(sa.attribute_id) return HttpResponse(json.dumps( { 'status': 'success', 'understood': repr(query), 'servers': servers, 'num_servers': num_servers, 'editable_attributes': editable_attributes, }, default=json_encode_extra), content_type='application/x-json')
def get_filters(self): if self._filters is None: self._filters = parse_query(self.query) return self._filters
def index(request): """The hardware resources page""" term = request.GET.get('term', request.session.get('term', '')) collections = list(Collection.objects.filter(overview=True)) # If a graph collection was specified, use it. Otherwise use the first one for collection in collections: if request.GET.get('current_collection'): if str(collection.id) != request.GET['current_collection']: continue current_collection = collection break else: return HttpResponseBadRequest('No matching current collection') template_info = { 'search_term': term, 'collections': collections, 'current_collection': current_collection.id, } # TODO: Generalize this part using the relations hostnames = [] matched_hostnames = [] if term: query_args = parse_query(term) # @TODO: This is the slowest part here unfortunately the Query object # does not support pagination yet so there is nothing to speed this # up right now. host_query = Query(query_args, ['hostname', 'hypervisor']) for host in host_query: matched_hostnames.append(host['hostname']) if host.get('hypervisor'): hostnames.append(host['hypervisor']) else: # If it's not guest, it might be a server, so we add it hostnames.append(host['hostname']) understood = repr(host_query) request.session['term'] = term else: understood = repr(Query({})) variations = list(current_collection.variation_set.all()) columns = [] columns_selected = request.GET.getlist( 'columns', request.session.get('resources_columns', [])) request.session['resources_columns'] = columns_selected attribute_ids = ['hostname', 'servertype'] graph_index = 0 sprite_width = settings.GRAPHITE_SPRITE_WIDTH for template in current_collection.template_set.all(): for variation in variations: name = str(template) + ' ' + str(variation) columns.append({ 'name': name, 'type': 'graph', 'graph_index': graph_index, 'sprite_offset': graph_index * sprite_width, 'visible': slugify(name) in columns_selected, }) graph_index += 1 for numeric in current_collection.numeric_set.all(): columns.append({ 'name': str(numeric), 'type': 'numeric', 'visible': slugify(numeric) in columns_selected, }) attribute_ids.append(numeric.attribute_id) for relation in current_collection.relation_set.all(): columns.append({ 'name': str(relation), 'type': 'relation', 'visible': slugify(relation) in columns_selected, }) attribute_ids.append(relation.attribute_id) hosts = OrderedDict() filters = {GRAPHITE_ATTRIBUTE_ID: collection.name} if len(hostnames) > 0: filters['hostname'] = Any(*hostnames) for server in Query(filters, attribute_ids): hosts[server['hostname']] = dict(server) page = abs(int(request.GET.get('page', 1))) per_page = int(request.GET.get( 'per_page', request.session.get('resources_per_page', 8))) # Save settings in session request.session['resources_per_page'] = per_page try: hosts_pager = Paginator(list(hosts.values()), per_page) # Term or data in DB has changed if page > hosts_pager.num_pages: page = 1 hosts_pager = hosts_pager.page(page) except (PageNotAnInteger, EmptyPage): raise SuspiciousOperation('{} is not a valid!'.format(page)) sprite_url = settings.MEDIA_URL + 'graph_sprite/' + collection.name template_info.update({ 'columns': columns, 'hosts': hosts_pager, 'page': page, 'per_page': per_page, 'matched_hostnames': matched_hostnames, 'understood': understood, 'error': None, 'sprite_url': sprite_url, }) return TemplateResponse(request, 'resources/index.html', template_info)