Skip to content

Commit

Permalink
Merge pull request #2886 from StackStorm/fix_traces_sort_order
Browse files Browse the repository at this point in the history
Various traces API and CLI improvements
  • Loading branch information
Kami committed Sep 12, 2016
2 parents a5da121 + 5b2de63 commit b0ff90b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ In development
* Fix ``st2 execution get`` command so now ``--attr`` argument correctly works with child
properties of the ``result`` and ``trigger_instance`` dictionary (e.g. ``--attr
result.stdout result.stderr``). (bug fix)
* Update traces list API endpoint and ``st2 trace list`` so the traces are sorted by
``start_timestamp`` in descending order by default. This way it's consistent with executions
list and ``-n`` CLI parameter works as expected. (improvement)
* Allow users to specify sort order when listing traces using the API endpoint by specifying
``?sort_desc=True|False`` query parameters and by passing ``--sort=asc|desc`` parameter to
the ``st2 trace list`` CLI command. (improvement)

2.0.0 - August 31, 2016
-----------------------
Expand Down
16 changes: 15 additions & 1 deletion st2api/st2api/controllers/v1/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from st2api.controllers.resource import ResourceController
from st2common.models.api.trace import TraceAPI
from st2common.persistence.trace import Trace
from st2common.models.api.base import jsexpose

__all__ = [
'TracesController'
Expand All @@ -33,5 +34,18 @@ class TracesController(ResourceController):
}

query_options = {
'sort': ['trace_tag']
'sort': ['-start_timestamp', 'trace_tag']
}

@jsexpose()
def get_all(self, **kwargs):
# Use a custom sort order when filtering on a timestamp so we return a correct result as
# expected by the user
if 'sort_desc' in kwargs:
query_options = {'sort': ['-start_timestamp', 'action.ref']}
kwargs['query_options'] = query_options
elif 'sort_asc' in kwargs:
query_options = {'sort': ['+start_timestamp', 'action.ref']}
kwargs['query_options'] = query_options

return self._get_all(**kwargs)
3 changes: 2 additions & 1 deletion st2api/tests/unit/controllers/v1/test_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ def test_get_all(self):
self.assertEqual(resp.status_int, 200)
self.assertEqual(len(resp.json), 3, '/v1/traces did not return all traces.')

# Note: traces are returned sorted by start_timestamp in descending order by default
retrieved_trace_tags = [trace['trace_tag'] for trace in resp.json]

self.assertEqual(retrieved_trace_tags,
[self.trace1.trace_tag, self.trace2.trace_tag, self.trace3.trace_tag],
[self.trace3.trace_tag, self.trace2.trace_tag, self.trace1.trace_tag],
'Incorrect traces retrieved.')

def test_get_by_id(self):
Expand Down
12 changes: 12 additions & 0 deletions st2client/st2client/commands/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def __init__(self, resource, *args, **kwargs):
default=50,
help=('List N most recent %s.' %
resource.get_plural_display_name().lower()))
self.parser.add_argument('-s', '--sort', type=str, dest='sort_order',
default='descending',
help=('Sort %s by start timestamp, '
'asc|ascending (earliest first) '
'or desc|descending (latest first)' %
resource.get_plural_display_name().lower()))

# Filter options
self.group.add_argument('-c', '--trace-tag', help='Trace-tag to filter the list.')
Expand Down Expand Up @@ -152,6 +158,12 @@ def run(self, args, **kwargs):
if args.rule:
kwargs['rule'] = args.rule

if args.sort_order:
if args.sort_order in ['asc', 'ascending']:
kwargs['sort_asc'] = True
elif args.sort_order in ['desc', 'descending']:
kwargs['sort_desc'] = True

return self.manager.query(limit=args.last, **kwargs)

def run_and_print(self, args, **kwargs):
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/models/db/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class TraceDB(stormbase.StormFoundationDB):
{'fields': ['start_timestamp']},
{'fields': ['action_executions.object_id']},
{'fields': ['trigger_instances.object_id']},
{'fields': ['rules.object_id']}
{'fields': ['rules.object_id']},
{'fields': ['-start_timestamp', 'trace_tag']},
]
}

Expand Down

0 comments on commit b0ff90b

Please sign in to comment.