Skip to content

Commit

Permalink
Merge pull request #45 from Andrei-Dolgolev/BUG-397-reporting-to-humb…
Browse files Browse the repository at this point in the history
…ug-reports

Bug 397 reporting to humbug reports
  • Loading branch information
zomglings committed Apr 19, 2021
2 parents 64437de + 5302e49 commit 5982739
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 41 deletions.
59 changes: 40 additions & 19 deletions python/humbug/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import List, Optional
import uuid

from bugout.app import Bugout
import requests

from .consent import HumbugConsent
from .system_information import (
Expand All @@ -26,6 +26,15 @@
)


DEFAULT_URL = "https://spire.bugout.dev"


class BugoutUnexpectedStatusResponse(Exception):
"""
Raised when Bugout server response return incorrect status.
"""


@dataclass
class Report:
title: str
Expand All @@ -38,19 +47,22 @@ class Modes(Enum):
SYNCHRONOUS = 1


class Reporter:
class HumbugReporter:
def __init__(
self,
name: str,
consent: HumbugConsent,
url: Optional[str] = None,
client_id: Optional[str] = None,
session_id: Optional[str] = None,
system_information: Optional[SystemInformation] = None,
bugout_token: Optional[str] = None,
bugout_journal_id: Optional[str] = None,
timeout_seconds: int = 10,
mode: Modes = Modes.DEFAULT,
):
if url is None:
url = DEFAULT_URL
self.url = url
self.name = name
self.consent = consent
self.client_id = client_id
Expand All @@ -61,9 +73,7 @@ def __init__(
if system_information is None:
system_information = generate_system_information()
self.system_information = system_information
self.bugout = Bugout()
self.bugout_token = bugout_token
self.bugout_journal_id = bugout_journal_id
self.timeout_seconds = timeout_seconds

self.report_futures: List[concurrent.futures.Future] = []
Expand Down Expand Up @@ -107,28 +117,27 @@ def system_tags(self) -> List[str]:
def publish(self, report: Report, wait: bool = False) -> None:
if not self.consent.check():
return
if self.bugout_token is None or self.bugout_journal_id is None:
if self.bugout_token is None:
return

json = {"title": report.title, "content": report.content, "tags": report.tags}
headers = {
"Authorization": "Bearer {}".format(self.bugout_token),
}
url = "{}/humbug/reports".format(self.url.rstrip("/"))

try:
report.tags = list(set(report.tags))
if wait or self.executor is None:
self.bugout.create_entry(
token=self.bugout_token,
journal_id=self.bugout_journal_id,
title=report.title,
content=report.content,
tags=report.tags,
timeout=self.timeout_seconds,
requests.post(
url=url, headers=headers, json=json, timeout=self.timeout_seconds
)
else:
report_future = self.executor.submit(
self.bugout.create_entry,
token=self.bugout_token,
journal_id=self.bugout_journal_id,
title=report.title,
content=report.content,
tags=report.tags,
requests.post,
url=url,
headers=headers,
json=json,
timeout=self.timeout_seconds,
)
self.report_futures.append(report_future)
Expand Down Expand Up @@ -405,3 +414,15 @@ def showtraceback(*args, **kwargs):

ipython_shell.showtraceback = showtraceback
self.setup_excepthook(publish=True, tags=tags)


class Reporter(HumbugReporter):

"""
Deprecated.
Old class name.
"""

def __init__(self, bugout_journal_id: Optional[str] = None, *args, **kw):
super().__init__(*args, **kw)
self.bugout_journal_id = bugout_journal_id
2 changes: 1 addition & 1 deletion python/humbug/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class TestReporter(unittest.TestCase):
def setUp(self):
self.consent = consent.HumbugConsent(True)
self.reporter = report.Reporter("TestReporter", self.consent)
self.reporter = report.Reporter(name="TestReporter", consent=self.consent)

def test_system_report_successful(self):
self.reporter.system_report(publish=False)
Expand Down
10 changes: 2 additions & 8 deletions python/recipes/error_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
from humbug.report import Reporter

DEMO_BUGOUT_ACCESS_TOKEN = "a390de7a-c64f-482c-b3f4-b9322ffaadef"
DEMO_BUGOUT_JOURNAL_ID = "1bf50549-a8ef-430f-b788-fb2ac864d42a"

"""
LEND ME YOUR EYES, FRIEND:
Please replace the bugout_access_token and bugout_journal_id with the values you generate in the
Please replace the bugout_access_token with the value you generate in the
process of setting up a "Usage Reports" integration at https://bugout.dev.
Full instructions can be found here: https://github.com/bugout-dev/humbug/blob/main/README.md
Expand All @@ -24,12 +23,8 @@
If you would like access to the demo knowledge base, email me at neeraj@bugout.dev.
"""
bugout_access_token: str = DEMO_BUGOUT_ACCESS_TOKEN
bugout_journal_id: str = DEMO_BUGOUT_JOURNAL_ID

if (
bugout_access_token == DEMO_BUGOUT_ACCESS_TOKEN
or bugout_journal_id == DEMO_BUGOUT_JOURNAL_ID
):
if bugout_access_token == DEMO_BUGOUT_ACCESS_TOKEN:
print(
"It seems you are running this script with the demo Bugout access token and journal id."
)
Expand Down Expand Up @@ -66,7 +61,6 @@
consent,
session_id=session_id,
bugout_token=bugout_access_token,
bugout_journal_id=bugout_journal_id,
)

"""
Expand Down
14 changes: 3 additions & 11 deletions python/recipes/system_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
from humbug.report import Reporter

DEMO_BUGOUT_ACCESS_TOKEN = "a390de7a-c64f-482c-b3f4-b9322ffaadef"
DEMO_BUGOUT_JOURNAL_ID = "1bf50549-a8ef-430f-b788-fb2ac864d42a"

"""
LEND ME YOUR EYES, FRIEND:
Please replace the bugout_access_token and bugout_journal_id with the values you generate in the
Please replace the bugout_access_token with the value you generate in the
process of setting up a "Usage Reports" integration at https://bugout.dev.
Full instructions can be found here: https://github.com/bugout-dev/humbug/blob/main/README.md
Expand All @@ -24,15 +23,9 @@
If you would like access to the demo knowledge base, email me at neeraj@bugout.dev.
"""
bugout_access_token: str = DEMO_BUGOUT_ACCESS_TOKEN
bugout_journal_id: str = DEMO_BUGOUT_JOURNAL_ID

if (
bugout_access_token == DEMO_BUGOUT_ACCESS_TOKEN
or bugout_journal_id == DEMO_BUGOUT_JOURNAL_ID
):
print(
"It seems you are running this script with the demo Bugout access token and journal id."
)
if bugout_access_token == DEMO_BUGOUT_ACCESS_TOKEN:
print("It seems you are running this script with the demo Bugout access token.")
print(
"If you continue, this script will publish reports to a Bugout.dev knowledge base that you do not have access to."
)
Expand Down Expand Up @@ -66,7 +59,6 @@
consent,
session_id=session_id,
bugout_token=bugout_access_token,
bugout_journal_id=bugout_journal_id,
)

"""
Expand Down
4 changes: 2 additions & 2 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

setup(
name="humbug",
version="0.1.16",
version="0.2.0",
packages=find_packages(),
install_requires=["bugout"],
install_requires=["requests"],
extras_require={
"dev": ["black", "mypy", "wheel"],
"distribute": ["setuptools", "twine", "wheel"],
Expand Down

0 comments on commit 5982739

Please sign in to comment.