Skip to content

Commit

Permalink
Merge branch 'fix/entity-bot' into 'master'
Browse files Browse the repository at this point in the history
Make sure we are only collecting data for those entities that we were selected on

See merge request grafolean/grafolean-collector!1
  • Loading branch information
grafolean committed Nov 30, 2019
2 parents 3870a5a + 6a5cc2a commit 209f0ac
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions grafoleancollector/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,21 @@ def _run_job_error(self, job_id, exc, traceback=None):


class Collector(object):
__slots__ = 'backend_url', 'bot_token', 'scheduler', 'known_jobs', 'jobs_refresh_interval'
__slots__ = 'backend_url', 'bot_token', 'scheduler', 'known_jobs', 'jobs_refresh_interval', 'user_id'

def __init__(self, backend_url, bot_token, jobs_refresh_interval):
self.backend_url = backend_url
self.bot_token = bot_token
self.jobs_refresh_interval = jobs_refresh_interval
self.known_jobs = {}
self._fetch_user_id()

def _fetch_user_id(self):
r = requests.get('{}/profile/?b={}'.format(self.backend_url, self.bot_token))
if r.status_code != 200:
raise Exception("Invalid bot token or network error, got status {} while retrieving {}/profile".format(r.status_code, self.backend_url))
j = r.json()
self.user_id = j["user_id"]

@abstractmethod
def jobs(self):
Expand All @@ -204,8 +212,8 @@ def fetch_job_configs(self, protocol):
The data is cleaned up as much as possible, so that it only contains the things necessary for collectors
to do their job.
"""
# find all the accounts we have access to:
requests_session = requests.Session()
# find all the accounts we have access to:
r = requests_session.get('{}/accounts/?b={}'.format(self.backend_url, self.bot_token))
if r.status_code != 200:
raise Exception("Invalid bot token or network error, got status {} while retrieving {}/accounts".format(r.status_code, self.backend_url))
Expand All @@ -229,6 +237,9 @@ def fetch_job_configs(self, protocol):
# make sure that the protocol is enabled on the entity:
if protocol not in entity_info["protocols"]:
continue
# and that the bot id is our own, or not set: (indicating that it predates the time when this option became available - which should not matter after ~2020-01-01)
if entity_info["protocols"][protocol].get("bot", None) is not None and entity_info["protocols"][protocol]["bot"] != self.user_id:
continue
# and that credential is set:
if not entity_info["protocols"][protocol]["credential"]:
continue
Expand Down

0 comments on commit 209f0ac

Please sign in to comment.