Passed
Push — develop ( df8237...b0ff90 )
by Plexxi
06:18 queued 03:16
created

TracesController.get_all()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from st2api.controllers.resource import ResourceController
17
from st2common.models.api.trace import TraceAPI
18
from st2common.persistence.trace import Trace
19
from st2common.models.api.base import jsexpose
20
21
__all__ = [
22
    'TracesController'
23
]
24
25
26
class TracesController(ResourceController):
27
    model = TraceAPI
28
    access = Trace
29
    supported_filters = {
30
        'trace_tag': 'trace_tag',
31
        'execution': 'action_executions.object_id',
32
        'rule': 'rules.object_id',
33
        'trigger_instance': 'trigger_instances.object_id',
34
    }
35
36
    query_options = {
37
        'sort': ['-start_timestamp', 'trace_tag']
38
    }
39
40
    @jsexpose()
41
    def get_all(self, **kwargs):
42
        # Use a custom sort order when filtering on a timestamp so we return a correct result as
43
        # expected by the user
44
        if 'sort_desc' in kwargs:
45
            query_options = {'sort': ['-start_timestamp', 'action.ref']}
46
            kwargs['query_options'] = query_options
47
        elif 'sort_asc' in kwargs:
48
            query_options = {'sort': ['+start_timestamp', 'action.ref']}
49
            kwargs['query_options'] = query_options
50
51
        return self._get_all(**kwargs)
52