Skip to content

Commit 209f0ac

Browse files
committed
Merge branch 'fix/entity-bot' into 'master'
Make sure we are only collecting data for those entities that we were selected on See merge request grafolean/grafolean-collector!1
2 parents 3870a5a + 6a5cc2a commit 209f0ac

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

grafoleancollector/collector.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,21 @@ def _run_job_error(self, job_id, exc, traceback=None):
182182

183183

184184
class Collector(object):
185-
__slots__ = 'backend_url', 'bot_token', 'scheduler', 'known_jobs', 'jobs_refresh_interval'
185+
__slots__ = 'backend_url', 'bot_token', 'scheduler', 'known_jobs', 'jobs_refresh_interval', 'user_id'
186186

187187
def __init__(self, backend_url, bot_token, jobs_refresh_interval):
188188
self.backend_url = backend_url
189189
self.bot_token = bot_token
190190
self.jobs_refresh_interval = jobs_refresh_interval
191191
self.known_jobs = {}
192+
self._fetch_user_id()
193+
194+
def _fetch_user_id(self):
195+
r = requests.get('{}/profile/?b={}'.format(self.backend_url, self.bot_token))
196+
if r.status_code != 200:
197+
raise Exception("Invalid bot token or network error, got status {} while retrieving {}/profile".format(r.status_code, self.backend_url))
198+
j = r.json()
199+
self.user_id = j["user_id"]
192200

193201
@abstractmethod
194202
def jobs(self):
@@ -204,8 +212,8 @@ def fetch_job_configs(self, protocol):
204212
The data is cleaned up as much as possible, so that it only contains the things necessary for collectors
205213
to do their job.
206214
"""
207-
# find all the accounts we have access to:
208215
requests_session = requests.Session()
216+
# find all the accounts we have access to:
209217
r = requests_session.get('{}/accounts/?b={}'.format(self.backend_url, self.bot_token))
210218
if r.status_code != 200:
211219
raise Exception("Invalid bot token or network error, got status {} while retrieving {}/accounts".format(r.status_code, self.backend_url))
@@ -229,6 +237,9 @@ def fetch_job_configs(self, protocol):
229237
# make sure that the protocol is enabled on the entity:
230238
if protocol not in entity_info["protocols"]:
231239
continue
240+
# 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)
241+
if entity_info["protocols"][protocol].get("bot", None) is not None and entity_info["protocols"][protocol]["bot"] != self.user_id:
242+
continue
232243
# and that credential is set:
233244
if not entity_info["protocols"][protocol]["credential"]:
234245
continue

0 commit comments

Comments
 (0)