Passed
Push — master ( dac537...326ab7 )
by Alexander
02:16
created

tcms.telemetry.api.breakdown()   A

Complexity

Conditions 2

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 33
rs 9.55
c 0
b 0
f 0
cc 2
nop 1
1
from django.db.models import Count
2
from modernrpc.core import rpc_method
3
4
from tcms.testcases.models import TestCase
5
6
7
@rpc_method(name='Testing.breakdown')
8
def breakdown(query=None):
9
    """
10
    .. function:: XML-RPC Testing.breakdown(query)
11
12
        Perform a search and return the statistics for the selected test cases
13
14
        :param query: Field lookups for :class:`tcms.testcases.models.TestCase`
15
        :type query: dict
16
        :return: Object, containing the statistics for the selected test cases
17
        :rtype: dict
18
    """
19
20
    if query is None:
21
        query = {}
22
23
    test_cases = TestCase.objects.filter(**query).filter()
24
25
    manual_count = test_cases.filter(is_automated=False).count()
26
    automated_count = test_cases.filter(is_automated=True).count()
27
    count = {
28
        'manual': manual_count,
29
        'automated': automated_count,
30
        'all': manual_count + automated_count
31
    }
32
33
    priorities = _get_field_count_map(test_cases, 'priority', 'priority__value')
34
    categories = _get_field_count_map(test_cases, 'category', 'category__name')
35
36
    return {
37
        'count': count,
38
        'priorities': priorities,
39
        'categories': categories,
40
    }
41
42
43
def _get_field_count_map(test_cases, expression, field):
44
    query_set = test_cases.values(field).annotate(count=Count(expression))
45
    return [[entry[field], entry['count']] for entry in query_set]
46