Conditions | 13 |
Total Lines | 73 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like backend.when_monitorminer.domonitorminer() 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 | '''#David Foderick, Skylake Software Inc. |
||
37 | def domonitorminer(miner): |
||
38 | '''get statistics from miner''' |
||
39 | entries = QueueEntries() |
||
40 | savedminer = APPMONITOR.app.getminer(miner) |
||
41 | if savedminer is None: |
||
42 | savedminer = miner |
||
43 | try: |
||
44 | #individual miner can be monitored even if manually disabled |
||
45 | #todo:savedminer and knownminer out of sync. this will be fixed in refactoring redis |
||
46 | if not savedminer.should_monitor() and not miner.should_monitor(): |
||
47 | print('skipped monitoring {0}'.format(miner.name)) |
||
48 | return entries |
||
49 | mineroriginalstatus = savedminer.status |
||
50 | minerstats, minerinfo, apicall, minerpool = stats(savedminer) |
||
51 | #minerlcd = antminerhelper.getminerlcd(miner) |
||
52 | if minerstats is None: |
||
53 | print('could not monitor {0}({1})'.format(savedminer.name, savedminer.ipaddress)) |
||
54 | else: |
||
55 | #what to do if monitored miner type conflicts with saved miner type??? |
||
56 | #should probably provision? |
||
57 | foundpool = APPMONITOR.app.findpool(minerpool) |
||
58 | if foundpool is not None: |
||
59 | minerpool.poolname = foundpool.name |
||
60 | savedminer.monitored(minerstats, minerpool, minerinfo, apicall.elapsed()) |
||
61 | if mineroriginalstatus == '': |
||
62 | #first time monitoring since bootup |
||
63 | print(Fore.GREEN + APPMONITOR.app.now(), savedminer.name, 'first time monitoring') |
||
64 | elif savedminer.status == mining.MinerStatus.Online and (mineroriginalstatus == mining.MinerStatus.Disabled or mineroriginalstatus == mining.MinerStatus.Offline): |
||
65 | #changing status from offline to online so raise event |
||
66 | entries.add(QueueName.Q_ONLINE, APPMONITOR.app.messageencode(savedminer)) |
||
67 | print(Fore.GREEN + APPMONITOR.app.now(), savedminer.name, 'back online!') |
||
68 | #TODO: if stats.elapsed < previous.elapsed then raise provision or online events |
||
69 | |||
70 | APPMONITOR.app.putminerandstats(savedminer, minerstats, minerpool) |
||
71 | #show name of current pool instead of worker |
||
72 | print('{0} mining at {1}'.format(savedminer.name, getpoolname(minerpool))) |
||
73 | check_miner_should_provision(entries, savedminer, minerpool) |
||
74 | |||
75 | print(Fore.CYAN+str(APPMONITOR.app.now()), savedminer.name, savedminer.status, |
||
76 | 'h='+str(minerstats.currenthash), str(minerstats.minercount), |
||
77 | '{0}/{1}/{2}'.format(str(minerstats.tempboard1), |
||
78 | str(minerstats.tempboard2), |
||
79 | str(minerstats.tempboard3)), |
||
80 | savedminer.uptime(minerstats.elapsed), |
||
81 | '{0:d}ms'.format(int(savedminer.monitorresponsetime() * 1000))) |
||
82 | msg = APPMONITOR.app.createmessagestats(savedminer, minerstats, minerpool) |
||
83 | entries.addbroadcast(QueueName.Q_STATISTICSUPDATED, msg) |
||
84 | |||
85 | except pika.exceptions.ConnectionClosed as qex: |
||
86 | #could not enqueue a message |
||
87 | print(Fore.RED + '{0} Queue Error: {1}'.format(savedminer.name, |
||
88 | APPMONITOR.app.exceptionmessage(qex))) |
||
89 | APPMONITOR.app.logexception(qex) |
||
90 | except MinerMonitorException as monitorex: |
||
91 | print(Fore.RED + '{0} Miner Error: {1}'.format(savedminer.name, |
||
92 | APPMONITOR.app.exceptionmessage(monitorex))) |
||
93 | savedminer.lastmonitor = datetime.datetime.utcnow() |
||
94 | #TODO: this should be a rule. publish miner offline event |
||
95 | #and let event handler decide how to handle it |
||
96 | savedminer.offline_now() |
||
97 | print(Fore.RED + APPMONITOR.app.now(), savedminer.name, savedminer.status) |
||
98 | entries.add(QueueName.Q_OFFLINE, APPMONITOR.app.messageencode(savedminer)) |
||
99 | |||
100 | except BaseException as ex: |
||
101 | print(Fore.RED+'{0} Unexpected Error in monitorminer: {1}'.format(savedminer.name, |
||
102 | APPMONITOR.app.exceptionmessage(ex))) |
||
103 | # we have to consider any exception to be a miner error. sets status to offline |
||
104 | #if str(e) == "timed out": #(timeout('timed out',),) |
||
105 | APPMONITOR.app.logexception(ex) |
||
106 | #TODO: review usage of savedminer and knownminer. should only go with one |
||
107 | APPMONITOR.app.putminer(savedminer) |
||
108 | APPMONITOR.app.updateknownminer(savedminer) |
||
109 | return entries |
||
110 | |||
141 |