Skip to content

Commit

Permalink
starting to add telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
bendhouseart committed Jan 14, 2025
1 parent e047a59 commit c72ee19
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions petdeface/petdeface.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bids import BIDSLayout
import importlib
import glob
import sys
from platform import system

# import shutil
Expand Down Expand Up @@ -36,6 +37,37 @@
from .mideface import Mideface
from .pet import WeightedAverage

telemetry_data = {
"freesurfer_license": None,
"docker_installed": None,
"in_docker": None,
"version": None,
"subjects": None,
"pet_niftis": None,
"anat_niftis": None,
"exit_status": None,
}
def sendstatsoncrash(type, value, tb):
"""
Sends telemetry data to openneuropet.org on crash
"""
import traceback
import requests
#url = "http://openneuropet.org/petdeface"
url = "http://127.0.0.1:8000/petdeface/"
no_track = os.environ.get("PETDEFACE_NO_TRACK", "False")
trace_back_text = ''.join(traceback.format_exception(type, value, tb))
print(trace_back_text)
if not no_track or no_track == "False":
try:
requests.post(url, json=telemetry_data)

except:
pass
elif no_track == "True" or no_track == True:
pass

sys.excepthook = sendstatsoncrash

# collect version from pyproject.toml
places_to_look = [
Expand All @@ -53,6 +85,9 @@
# we try to load the version using import lib
try:
__version__ = version(__package__)
# if version is not of the form x.y.z we try to load it from the pyproject.toml
if not all([x.isdigit() for x in __version__.split(".")]):
raise ValueError
except ValueError:
# if we can't load the version using importlib we try to load it from the pyproject.toml
for place in places_to_look:
Expand All @@ -65,6 +100,7 @@
except FileNotFoundError:
pass

telemetry_data['version'] = __version__

def locate_freesurfer_license():
"""
Expand All @@ -81,26 +117,31 @@ def locate_freesurfer_license():
if os.environ.get("FREESURFER_LICENSE", ""):
fs_license_env_var = pathlib.Path(os.environ.get("FREESURFER_LICENSE", ""))
if not fs_license_env_var.exists():
telemetry_data['freesurfer_license'] = False
raise ValueError(
f"Freesurfer license file does not exist at {fs_license_env_var}, but is set under $FREESURFER_LICENSE variable."
f"Update or unset this varible to use the license.txt at $FREESURFER_HOME"
)
else:
telemetry_data['freesurfer_license'] = True
return fs_license_env_var
else:
# collect freesurfer home environment variable and look there instead
fs_home = pathlib.Path(os.environ.get("FREESURFER_HOME", ""))
if not fs_home:
telemetry_data['freesurfer_license'] = False
raise ValueError(
"FREESURFER_HOME environment variable is not set, unable to determine location of license file"
)
else:
fs_license = fs_home / pathlib.Path("license.txt")
if not fs_license.exists():
telemetry_data['freesurfer_license'] = False
raise ValueError(
"Freesurfer license file does not exist at {}".format(fs_license)
)
else:
telemetry_data['freesurfer_license'] = True
return fs_license


Expand All @@ -120,7 +161,9 @@ def check_docker_installed():
check=True,
)
docker_installed = True
telemetry_data["docker_installed"] = docker_installed
except subprocess.CalledProcessError:
telemetry_data["docker_installed"] = False
raise Exception("Could not detect docker installation, exiting")
return docker_installed

Expand Down Expand Up @@ -148,6 +191,7 @@ def determine_in_docker():
for line in lines:
if "bash" in line:
in_docker = True
telemetry_data["in_docker"] = True
return in_docker


Expand Down

0 comments on commit c72ee19

Please sign in to comment.