Added anonymous telemetry helpers. (#340)

This commit is contained in:
Alek Petuskey 2023-01-25 14:57:29 -08:00 committed by GitHub
parent bfe9ad807e
commit cb61579f53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 225 additions and 510 deletions

647
poetry.lock generated

File diff suppressed because it is too large Load Diff

38
pynecone/telemetry.py Normal file
View File

@ -0,0 +1,38 @@
"""Anonymous telemetry for Pynecone."""
import platform
import psutil
import multiprocessing
from pynecone import constants
from pynecone.base import Base
class Telemetry(Base):
"""Anonymous telemetry for Pynecone."""
user_os: str = ""
cpu_count: int = 0
memory: int = 0
pynecone_version: str = ""
python_version: str = ""
def get_os(self) -> None:
"""Get the operating system."""
self.user_os = platform.system()
def get_python_version(self) -> None:
"""Get the Python version."""
self.python_version = platform.python_version()
def get_pynecone_version(self) -> None:
"""Get the Pynecone version."""
self.pynecone_version = constants.VERSION
def get_cpu_count(self) -> None:
"""Get the number of CPUs."""
self.cpu_count = multiprocessing.cpu_count()
def get_memory(self) -> None:
"""Get the total memory in MB."""
self.memory = psutil.virtual_memory().total >> 20

View File

@ -1,6 +1,4 @@
"""General utility functions."""
from __future__ import annotations
import contextlib

View File

@ -35,6 +35,7 @@ rich = "^12.6.0"
redis = "^4.3.5"
httpx = "^0.23.1"
websockets = "^10.4"
psutil = "^5.9.4"
[tool.poetry.dev-dependencies]
pytest = "^7.1.2"

47
tests/test_telemetry.py Normal file
View File

@ -0,0 +1,47 @@
import pytest
from pynecone import telemetry
import json
def versiontuple(v):
return tuple(map(int, (v.split("."))))
def test_telemetry():
"""Test that telemetry is sent correctly."""
tel = telemetry.Telemetry()
# Check that the user OS is one of the supported operating systems.
tel.get_os()
assert tel.user_os != None
assert tel.user_os in ["Linux", "Darwin", "Java", "Windows"]
# Check that the CPU count and memory are greater than 0.
tel.get_cpu_count()
assert tel.cpu_count > 0
# Check that the available memory is greater than 0
tel.get_memory()
assert tel.memory > 0
# Check that the Pynecone version is not None.
tel.get_python_version()
assert tel.pynecone_version != None
# Check that the Python version is greater than 3.7.
tel.get_pynecone_version()
assert tel.python_version != None
assert versiontuple(tel.python_version) >= versiontuple("3.7")
# Check the json method.
tel_json = json.loads(tel.json())
assert tel_json["user_os"] == tel.user_os
assert tel_json["cpu_count"] == tel.cpu_count
assert tel_json["memory"] == tel.memory
assert tel_json["pynecone_version"] == tel.pynecone_version
assert tel_json["python_version"] == tel.python_version