Skip to content

Commit

Permalink
Fix minor UI bugs for resources page (#324)
Browse files Browse the repository at this point in the history
* Fix pagination resets selection
* Load last view collection by default
  • Loading branch information
kofrezo committed Jul 25, 2023
1 parent 90ed8d8 commit f1b5735
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 52 deletions.
84 changes: 42 additions & 42 deletions serveradmin/resources/templates/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,53 +69,53 @@
<button type="submit" class="btn btn-sm btn-success">Apply</button>
</div>
</div>
</form>
<hr>
<div class="row">
<div class="col-md-12">
<table class="table table-sm table-borderless table-striped table-responsive">
<thead>
<tr>
<th>Hostname</th>
{% for column in columns %}
{% if column.visible %}
<th>{{ column.name }}</th>
{% endif %}
{% endfor %}
</tr>
</thead>
<tbody>
{% for obj in hosts %}
<tr>
<td>
<a href="{% url 'graphite_graph_table' %}?hostname={{ obj.hostname | urlencode }}">{{ obj.hostname }}</a>
</td>
{% for column in columns %}
{% if column.visible %}
<td>
{% if column.type == 'numeric' %}
{{ obj|dict_get:column.name }}
{% elif column.type == 'relation' %}
{% for rel in obj|dict_get:column.name %}
<a href="{% url 'graphite_graph_table' %}?hostname={{ rel }}" {% if rel in matched_hostnames %}style="color:#d00;"{% endif %}>{{ rel }}</a>
{% endfor %}
{% else %}
<a class="graph_sprite" style="background:url({{ STATIC_URL }}empty.png) -{{ column.sprite_offset }}px 0;" data-hostname="{{ obj.hostname }}" data-graph="{{ column.graph_index }}" data-image="{{ sprite_url }}/{{ obj.hostname }}.png"></a>
<hr>
<div class="row">
<div class="col-md-12">
<table class="table table-sm table-borderless table-striped table-responsive">
<thead>
<tr>
<th>Hostname</th>
{% for column in columns %}
{% if column.visible %}
<th>{{ column.name }}</th>
{% endif %}
{% endfor %}
</tr>
</thead>
<tbody>
{% for obj in hosts %}
<tr>
<td>
<a href="{% url 'graphite_graph_table' %}?hostname={{ obj.hostname | urlencode }}">{{ obj.hostname }}</a>
</td>
{% endif %}
{% for column in columns %}
{% if column.visible %}
<td>
{% if column.type == 'numeric' %}
{{ obj|dict_get:column.name }}
{% elif column.type == 'relation' %}
{% for rel in obj|dict_get:column.name %}
<a href="{% url 'graphite_graph_table' %}?hostname={{ rel }}" {% if rel in matched_hostnames %}style="color:#d00;"{% endif %}>{{ rel }}</a>
{% endfor %}
{% else %}
<a class="graph_sprite" style="background:url({{ STATIC_URL }}empty.png) -{{ column.sprite_offset }}px 0;" data-hostname="{{ obj.hostname }}" data-graph="{{ column.graph_index }}" data-image="{{ sprite_url }}/{{ obj.hostname }}.png"></a>
{% endif %}
</td>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
</tr>
</tbody>
</table>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
{% include "pagination.html" with page=hosts %}
<div class="row">
<div class="col-md-12">
{% include "pagination.html" with page=hosts form=True %}
</div>
</div>
</div>
</form>
{% endblock %}
{% block additional_scripts %}
<script type="text/javascript" src="{{ STATIC_URL }}js/graphite.js"></script>
Expand Down
26 changes: 16 additions & 10 deletions serveradmin/resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@
def index(request):
"""The hardware resources page"""
term = request.GET.get('term', request.session.get('term', ''))
current_collection_id = request.GET.get('current_collection', request.session.get('current_collection'))
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
# If a graph collection was specified, use it. Otherwise, use the first one
if not current_collection_id:
current_collection = collections[0]
else:
return HttpResponseBadRequest('No matching current collection')
for collection in collections:
if collection.id != int(current_collection_id):
continue
current_collection = collection
break
else:
return HttpResponseBadRequest(f"Collection {current_collection_id} does not exist!")

# Save latest choice for collection
request.session['current_collection'] = current_collection.id

template_info = {
'search_term': term,
Expand Down Expand Up @@ -102,7 +108,7 @@ def index(request):
attribute_ids.append(relation.attribute_id)

hosts = OrderedDict()
filters = {GRAPHITE_ATTRIBUTE_ID: collection.name}
filters = {GRAPHITE_ATTRIBUTE_ID: current_collection.name}
if len(hostnames) > 0:
filters['hostname'] = Any(*hostnames)
for server in Query(filters, attribute_ids):
Expand All @@ -126,7 +132,7 @@ def index(request):
except (PageNotAnInteger, EmptyPage):
raise SuspiciousOperation('{} is not a valid!'.format(page))

sprite_url = settings.MEDIA_URL + 'graph_sprite/' + collection.name
sprite_url = settings.MEDIA_URL + 'graph_sprite/' + current_collection.name
template_info.update({
'columns': columns,
'hosts': hosts_page,
Expand Down

0 comments on commit f1b5735

Please sign in to comment.