Passed
Push — master ( 69eaf4...82ce9d )
by Alexander
02:23
created

tcms.telemetry.api._map_query_set()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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