Merge pull request #20 from jsawatzky/master

Return metrics on connection error
This commit is contained in:
Esteban Sánchez 2023-05-09 15:52:59 +02:00 committed by GitHub
commit 940b5c6242
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,7 +29,6 @@ class QbittorrentMetricsCollector():
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
self.torrents = None
self.client = Client( self.client = Client(
host=config["host"], host=config["host"],
port=config["port"], port=config["port"],
@ -38,12 +37,6 @@ class QbittorrentMetricsCollector():
) )
def collect(self): def collect(self):
try:
self.torrents = self.client.torrents.info()
except Exception as e:
logger.error(f"Couldn't get server info: {e}")
return None
metrics = self.get_qbittorrent_metrics() metrics = self.get_qbittorrent_metrics()
for metric in metrics: for metric in metrics:
@ -75,11 +68,8 @@ class QbittorrentMetricsCollector():
try: try:
response = self.client.transfer.info response = self.client.transfer.info
version = self.client.app.version version = self.client.app.version
self.torrents = self.client.torrents.info() except Exception as e:
except APIConnectionError as e: logger.error(f"Couldn't get server info: {e}")
logger.error(f"Couldn't get server info: {e.error_message}")
except Exception:
logger.error(f"Couldn't get server info")
return [ return [
{ {
@ -120,17 +110,15 @@ class QbittorrentMetricsCollector():
def get_qbittorrent_torrent_tags_metrics(self): def get_qbittorrent_torrent_tags_metrics(self):
try: try:
categories = self.client.torrent_categories.categories categories = self.client.torrent_categories.categories
torrents = self.client.torrents.info()
except Exception as e: except Exception as e:
logger.error(f"Couldn't fetch categories: {e}") logger.error(f"Couldn't fetch torrent info: {e}")
return []
if not self.torrents:
return [] return []
metrics = [] metrics = []
categories.Uncategorized = AttrDict({'name': 'Uncategorized', 'savePath': ''}) categories.Uncategorized = AttrDict({'name': 'Uncategorized', 'savePath': ''})
for category in categories: for category in categories:
category_torrents = [t for t in self.torrents if t['category'] == category or (category == "Uncategorized" and t['category'] == "")] category_torrents = [t for t in torrents if t['category'] == category or (category == "Uncategorized" and t['category'] == "")]
for status in self.TORRENT_STATUSES: for status in self.TORRENT_STATUSES:
status_prop = f"is_{status}" status_prop = f"is_{status}"
@ -211,7 +199,7 @@ def main():
logger.error("No host specified, please set QBITTORRENT_HOST environment variable") logger.error("No host specified, please set QBITTORRENT_HOST environment variable")
sys.exit(1) sys.exit(1)
if not config["port"]: if not config["port"]:
logger.error("No post specified, please set QBITTORRENT_PORT environment variable") logger.error("No port specified, please set QBITTORRENT_PORT environment variable")
sys.exit(1) sys.exit(1)
# Register our custom collector # Register our custom collector