Skip to content

Commit

Permalink
remove is_online
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Aug 11, 2023
1 parent f0d346a commit 948f1e2
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 60 deletions.
15 changes: 1 addition & 14 deletions src/ploomber_core/telemetry/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,6 @@ def python_version():
return f"{py_version.major}.{py_version.minor}.{py_version.micro}"


def is_online():
"""Check if host is online"""
conn = httplib.HTTPSConnection("www.google.com", timeout=1)

try:
conn.request("HEAD", "/")
return True
except Exception:
return False
finally:
conn.close()


def is_docker():
"""Will output if the code is within a container"""
try:
Expand Down Expand Up @@ -542,7 +529,7 @@ def log_api(self, action, client_time=None, total_runtime=None, metadata=None):
os = get_os()
environment = get_env()

if telemetry_enabled and is_online():
if telemetry_enabled:
(event_id, uid, action, client_time, elapsed_time) = validate_entries(
event_id, uid, action, client_time, total_runtime
)
Expand Down
46 changes: 0 additions & 46 deletions tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,52 +308,6 @@ def test_python_version():
assert isinstance(version, str)


def test_is_online():
assert telemetry.is_online()


def test_is_online_timeout():
# Check the total run time is less than 1.5 secs
start_time = datetime.datetime.now()
telemetry.is_online()
end_time = datetime.datetime.now()
total_runtime = end_time - start_time
assert total_runtime < datetime.timedelta(milliseconds=1500)


def test_stats_off(monkeypatch):
mock = Mock()
posthog_mock = Mock()
mock.patch(telemetry, "_get_telemetry_info", (False, "TestUID"))

# when stats are off, we should not check is_online, as it's inefficient
monkeypatch.setattr(telemetry, "is_online", Mock(side_effect=ValueError))

_telemetry = telemetry.Telemetry(MOCK_API_KEY, "ploomber", "0.14.0")
_telemetry.log_api("test_action")

assert posthog_mock.call_count == 0


def test_offline_stats(monkeypatch):
mock = Mock()
posthog_mock = Mock()
mock.patch(telemetry, "is_online", False)

_telemetry = telemetry.Telemetry(MOCK_API_KEY, "ploomber", "0.14.0")
_telemetry.log_api("test_action")

assert posthog_mock.call_count == 0


def test_is_not_online(monkeypatch):
mock_httplib = Mock()
mock_httplib.HTTPSConnection().request.side_effect = Exception
monkeypatch.setattr(telemetry, "httplib", mock_httplib)

assert not telemetry.is_online()


def test_validate_entries(monkeypatch):
event_id = "event_id"
uid = "uid"
Expand Down

0 comments on commit 948f1e2

Please sign in to comment.