Test Failed
Push — master ( 7e7379...128504 )
by Nicolas
03:31
created

glances.programs   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 6

1 Function

Rating   Name   Duplication   Size   Complexity  
B processes_to_programs() 0 44 6
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2022 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
# from glances.logger import logger
21
22
# This constant defines the list of available processes sort key
23
sort_programs_key_list = ['cpu_percent', 'memory_percent', 'cpu_times', 'io_counters', 'name']
24
25
26
def processes_to_programs(processes):
27
    """Convert a list of processes to a list of programs."""
28
    # Start to build a dict of programs (key is program name)
29
    programs_dict = {}
30
    key = 'name'
31
    for p in processes:
32
        if p[key] not in programs_dict:
33
            # Create a new entry in the dict (new program)
34
            programs_dict[p[key]] = {
35
                'time_since_update': p['time_since_update'],
36
                'num_threads': p['num_threads'],
37
                'cpu_percent': p['cpu_percent'],
38
                'memory_percent': p['memory_percent'],
39
                'cpu_times': p['cpu_times'],
40
                'memory_info': p['memory_info'],
41
                'io_counters': p['io_counters'],
42
                'childrens': [p['pid']],
43
                # Others keys are not used
44
                # but should be set to be compliant with the existing process_list
45
                'name': p['name'],
46
                'cmdline': [p['name']],
47
                'pid': '_',
48
                'username': p['username'],
49
                'nice': p['nice'],
50
                'status': p['status'],
51
            }
52
        else:
53
            # Update a existing entry in the dict (existing program)
54
            programs_dict[p[key]]['num_threads'] += p['num_threads']
55
            programs_dict[p[key]]['cpu_percent'] += p['cpu_percent']
56
            programs_dict[p[key]]['memory_percent'] += p['memory_percent']
57
            programs_dict[p[key]]['cpu_times'] += p['cpu_times']
58
            programs_dict[p[key]]['memory_info'] += p['memory_info']
59
            programs_dict[p[key]]['io_counters'] += p['io_counters']
60
            programs_dict[p[key]]['childrens'].append(p['pid'])
61
            # If all the subprocess has the same value, display it
62
            programs_dict[p[key]]['username'] = (
63
                p['username'] if p['username'] == programs_dict[p[key]]['username'] else '_'
64
            )
65
            programs_dict[p[key]]['nice'] = p['nice'] if p['nice'] == programs_dict[p[key]]['nice'] else '_'
66
            programs_dict[p[key]]['status'] = p['status'] if p['status'] == programs_dict[p[key]]['status'] else '_'
67
68
    # Convert the dict to a list of programs
69
    return [programs_dict[p] for p in programs_dict]
70