Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ascending/Descending sort order GET parameter in RequestsView #95

Merged
merged 1 commit into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Ascending/Descending sort order GET parameter in RequestsView
This closes #84
  • Loading branch information
trik committed Jan 19, 2016
commit 25f0e0612d786fc1e1cd281a12abfb439706d07b
20 changes: 17 additions & 3 deletions silk/templates/silk/requests.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,22 @@
<label>Order:
<select name="order_by" form="filter-form" onchange="this.form.submit();">
{% for option in options_order_by %}
<option value="{{ option }}"
{% if option == order_by %}selected{% endif %}>{{ option }}</option>
<option value="{{ option.value }}"
{% if option.value == order_by %}selected{% endif %}>{{ option.label }}</option>
{% endfor %}
</select>
</label>
</div>
</div>
</div>
<div class="menu-item">
<div class="menu-item-outer">
<div class="menu-item-inner">
<label>Order:
<select name="order_dir" form="filter-form" onchange="this.form.submit();">
{% for option in options_order_dir %}
<option value="{{ option.value }}"
{% if option.value == order_dir %}selected{% endif %}>{{ option.label }}</option>
{% endfor %}
</select>
</label>
Expand Down Expand Up @@ -197,7 +211,7 @@ <h4>View</h4>
{% block data %}
{% if results %}
{% for silk_request in results %}
<a href="{% url "silk:request_detail" request_id=silk_request.pk %}">
<a href="{% url 'silk:request_detail' request_id=silk_request.pk %}">
{% request_summary silk_request %}
</a>
{% endfor %}
Expand Down
69 changes: 50 additions & 19 deletions silk/views/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,93 @@ class RequestsView(View):
show = [5, 10, 25, 100, 250]
default_show = 25

order_by = ['Recent', 'Path', 'Num. Queries', 'Time', 'Time on queries']
defualt_order_by = 'Recent'
order_by = {
'start_time': {
'label': 'Recent',
'additional_query_filter': None
},
'path': {
'label': 'Path',
'additional_query_filter': None
},
'num_sql_queries': {
'label': 'Num. Queries',
'additional_query_filter': None
},
'time_taken': {
'label': 'Time',
'additional_query_filter': lambda x: x.filter(time_taken__gte=0)
},
'db_time': {
'label': 'Time on queries',
'additional_query_filter': lambda x: x.annotate(db_time=Sum('queries__time_taken'))\
.filter(db_time__gte=0)
},
}
order_dir = {
'ASC': {
'label': 'Ascending'
},
'DESC': {
'label': 'Descending'
}
}
default_order_by = 'start_time'
default_order_dir = 'DESC'

session_key_request_filters = 'request_filters'

def _get_paths(self):
return [''] + [x['path'] for x in Request.objects.values('path').distinct()]

def _get_objects(self, show=None, order_by=None, path=None, filters=None):
def _get_objects(self, show=None, order_by=None, order_dir=None, path=None, filters=None):
if not filters:
filters = []
if not show:
show = self.default_show
query_set = Request.objects.all()
if not order_by:
order_by = self.defualt_order_by
if order_by == 'Recent':
query_set = query_set.order_by('-start_time')
elif order_by == 'Path':
query_set = query_set.order_by('-path')
elif order_by == 'Num. Queries':
query_set = query_set.order_by('-num_sql_queries')
elif order_by == 'Time':
query_set = query_set.order_by('-time_taken')
elif order_by == 'Time on queries':
query_set = query_set.annotate(db_time=Sum('queries__time_taken')).order_by('-db_time')
else:
order_by = self.default_order_by
if not order_dir:
order_dir = self.default_order_dir
if order_by not in self.order_by.keys():
raise RuntimeError('Unknown order_by: "%s"' % order_by)
ob = self.order_by[order_by]
if ob['additional_query_filter'] is not None:
query_set = ob['additional_query_filter'](query_set)
query_set = query_set.order_by('%s%s' % ('-' if order_dir == 'DESC' else '', order_by))
if path:
query_set = query_set.filter(path=path)
for f in filters:
query_set = f.contribute_to_query_set(query_set)
query_set = query_set.filter(f)
return list(query_set[:show])
return query_set[:show]

def _create_context(self, request):
show = request.GET.get('show', self.default_show)
order_by = request.GET.get('order_by', self.defualt_order_by)
order_by = request.GET.get('order_by', self.default_order_by)
order_dir = request.GET.get('order_dir', self.default_order_dir)
if show:
show = int(show)
path = request.GET.get('path', None)
raw_filters = request.session.get(self.session_key_request_filters, {})
context = {
'show': show,
'order_by': order_by,
'order_dir': order_dir,
'request': request,
'options_show': self.show,
'options_order_by': self.order_by,
'options_order_by': [{'value': x, 'label': self.order_by[x]['label']} for x in self.order_by.keys()],
'options_order_dir': [{'value': x, 'label': self.order_dir[x]['label']} for x in self.order_dir.keys()],
'options_paths': self._get_paths(),
'view_names': [x[0] for x in Request.objects.values_list('view_name').distinct()],
'filters': raw_filters
}
context.update(csrf(request))
if path:
context['path'] = path
context['results'] = self._get_objects(show, order_by, path, filters=[BaseFilter.from_dict(x) for _, x in raw_filters.items()])
context['results'] = self._get_objects(show, order_by, order_dir, path,
filters=[BaseFilter.from_dict(x) for _, x in raw_filters.items()])
return context

@method_decorator(login_possibly_required)
Expand Down