Passed
Push — master ( f9226a...a128c6 )
by Dave
01:14
created

backend.doflow_handlemessages.dispatchmessages()   C

Complexity

Conditions 11

Size

Total Lines 44
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 34
nop 2
dl 0
loc 44
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like backend.doflow_handlemessages.dispatchmessages() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'''handles messages in a loop'''
2
from helpers.queuehelper import QueueName
3
from backend.when_discovered import dodiscovered
4
from backend.when_alert import doalert
5
from backend.when_provision import doprovision
6
from backend.when_monitorminer import domonitorminer
7
from backend.when_runrules import dorules
8
from backend.when_offline import dooffline
9
from backend.when_online import doonline
10
from backend.when_restart import dorestart
11
12
def is_queue_match(queue_name, queue_enum):
13
    return queue_name == queue_enum or queue_name == QueueName.value(queue_enum)
14
15
def dispatchmessages(mainapp, entries):
16
    '''process the messages'''
17
    if entries is None:
18
        return
19
    for entry in entries.entries:
20
21
        if is_queue_match(entry.queuename, QueueName.Q_DISCOVERED):
22
            miner = mainapp.messagedecodeminer(entry.message)
23
            more = dodiscovered(miner)
24
            dispatchmessages(mainapp, more)
25
26
        elif is_queue_match(entry.queuename, QueueName.Q_ALERT):
27
            doalert(entry.message)
28
29
        elif is_queue_match(entry.queuename, QueueName.Q_PROVISION):
30
            miner = mainapp.messagedecodeminer(entry.message)
31
            more = doprovision(miner)
32
            dispatchmessages(mainapp, more)
33
34
        elif is_queue_match(entry.queuename, QueueName.Q_MONITORMINER):
35
            miner = mainapp.messagedecodeminer(entry.message)
36
            more = domonitorminer(miner)
37
            dispatchmessages(mainapp, more)
38
39
        elif is_queue_match(entry.queuename, QueueName.Q_STATISTICSUPDATED):
40
            msg = mainapp.messagedecodeminerstats(entry.message)
41
            more = dorules(msg.miner, msg.minerstats, msg.minerpool)
42
            dispatchmessages(mainapp, more)
43
44
        elif is_queue_match(entry.queuename, QueueName.Q_OFFLINE):
45
            miner = mainapp.messagedecodeminer(entry.message)
46
            more = dooffline(miner)
47
            dispatchmessages(mainapp, more)
48
49
        elif is_queue_match(entry.queuename, QueueName.Q_ONLINE):
50
            miner = mainapp.messagedecodeminer(entry.message)
51
            more = doonline(miner)
52
            dispatchmessages(mainapp, more)
53
54
        elif is_queue_match(entry.queuename, QueueName.Q_RESTART):
55
            minermsg = mainapp.messagedecodeminercommand(entry.message)
56
            dorestart(minermsg.miner, minermsg.command)
57
        else:
58
            print('{0} is not handled'.format(entry.queuename))
59