Repository: RocketMap/RocketMap

Issue:
Active workers are not really active
In the overseer message we are showing active workers but it really only shows the number of workers defined and not the ones that are actively scanning. 

## Expected Behavior
Don't count workers waiting for accounts

## Current Behavior
Just shows the number of configured workers

## Possible Solution
<!--- Not obligatory, but suggest a fix/reason for the bug, -->
<!--- or ideas how to implement the addition or change -->

## Steps to Reproduce (for bugs)
<!--- Provide a link to a live example, or an unambiguous set of steps to -->
<!--- reproduce this bug. Include code to reproduce, if relevant -->
1.
2.
3.
4.

## Context
<!--- How has this issue affected you? What are you trying to accomplish? -->
<!--- Providing context helps us come up with a solution that is most useful in the real world -->

## Your Environment
<!--- Include as many relevant details about the environment you experienced the bug in -->
* Version used:
* Environment name and version (e.g. Python 2.7):
* Operating System and version (desktop or mobile):



Discussion:
Agreed, found this bit I posted in #pr a while back.  

> I noticed something.  Worker counts (such as in /get_stats) include workers that are 'down'.  This could be workers waiting on a new account (because banned, errors of expired hash key, etc) but there are not any, or due to API change, or other reasons.   IMHO, worker counts should not include workers that are inactive.  I noticed this because I have a script that gets the total worker counts in /get_stats and let's me know if an instance goes down (because worker count drops).  However, I see cases where things have gone down and the worker counts don't change because the instances are still up but they include 'down' workers as active ones.
It's just coded as ``args.workers``. I guess we can fix that, @tomballgithub what are the columns you check with your script?
I check "Working:" field at http://arengopokemap.xyz/get_stats


Resolving patch:
diff --git a/pogom/search.py b/pogom/search.py
--- a/pogom/search.py
+++ b/pogom/search.py
@@ -660,11 +660,13 @@ def update_total_stats(threadStatus, last_account_status):
     overseer = threadStatus['Overseer']
 
     # Calculate totals.
-    usercount = 0
+    active_count = 0
     current_accounts = Set()
     for tstatus in threadStatus.itervalues():
         if tstatus.get('type', '') == 'Worker':
-            usercount += 1
+            if tstatus.get('active', False):
+                active_count += 1
+
             username = tstatus.get('username', '')
             current_accounts.add(username)
             last_status = last_account_status.get(username, {})
@@ -678,7 +680,7 @@ def update_total_stats(threadStatus, last_account_status):
                 'success_total'] += stat_delta(tstatus, last_status, 'success')
             last_account_status[username] = copy.deepcopy(tstatus)
 
-    overseer['active_accounts'] = usercount
+    overseer['active_accounts'] = active_count
 
     # Remove last status for accounts that workers
     # are not using anymore
@@ -768,6 +770,7 @@ def search_worker_thread(args, account_queue, account_sets,
                 dbq.put((WorkerStatus, {0: WorkerStatus.db_format(status)}))
 
             status['starttime'] = now()
+            status['active'] = False
 
             # Track per loop.
             first_login = True
@@ -809,7 +812,7 @@ def search_worker_thread(args, account_queue, account_sets,
 
             # The forever loop for the searches.
             while True:
-
+                status['active'] = True
                 while is_paused(control_flags):
                     status['message'] = 'Scanning paused.'
                     time.sleep(2)
@@ -1164,6 +1167,7 @@ def search_worker_thread(args, account_queue, account_sets,
             log.error((
                 'Exception in search_worker under account {} Exception ' +
                 'message: {}.').format(account['username'], repr(e)))
+            status['active'] = False
             status['message'] = (
                 'Exception in search_worker using account {}. Restarting ' +
                 'with fresh account. See logs for details.').format(


Test patch: