Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/unity sds client update health service #43

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libs/unity-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.2] - 2024-09-16
### Added
### Fixed
### Changed
* Changed Health Service to report information from updated Health API endpoint.
* Minor changes to the Health Service Status report.
* Updated health JSON schema definition to match updated output from Health API endpoint.
### Removed
### Security
### Deprecated


## [0.6.1] - 2024-09-05
### Added
* Added sphinx doc gen
Expand Down
2 changes: 1 addition & 1 deletion libs/unity-py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "unity-sds-client"
version = "0.6.1"
version = "0.6.2"
description = "Unity-Py is a Python client to simplify interactions with NASA's Unity Platform."
authors = ["Anil Natha, Mike Gangl"]
readme = "README.md"
Expand Down
30 changes: 18 additions & 12 deletions libs/unity-py/tests/test_unity_health_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
Unity Health Service is functional.
"""

import json
import pytest

from unity_sds_client.unity import Unity
from unity_sds_client.unity_environments import UnityEnvironments
from unity_sds_client.unity_services import UnityServices
import json
from jsonschema import validate


Expand All @@ -25,11 +26,12 @@ def test_health_status_retrieval():
Test that health statuses can be retrieved using the health service.
"""
print("Example health status check")
s = Unity()
s = Unity(environment=UnityEnvironments.DEV)
s.set_project("unity")
s.set_venue("dev")
health_service = s.client(UnityServices.HEALTH_SERVICE)
health_statuses = health_service.get_health_status()

f = open('../../schemas/health-service/health-services.schema.json')
f = open('../../schemas/health-service/health-services.schema.json', encoding='utf-8')
schema = json.load(f)

validate(instance=health_statuses, schema=schema)
Expand All @@ -41,18 +43,22 @@ def test_health_status_printing():
"""
Test that health statuses can be printed using the health service.
"""
print("Example health status check")
s = Unity()
s = Unity(environment=UnityEnvironments.DEV)
s.set_project("unity")
s.set_venue("dev")
health_service = s.client(UnityServices.HEALTH_SERVICE)

print("Example health status output using health service object:")
health_service.print_health_status()

@pytest.mark.regression
def test_health_service_printing():
"""
Test that when the health service client is printed, it outputs
the health status information
Test that health statuses can be printed using the unity object
"""
print("Example health status printing of health service object.")
s = Unity()
health_service = s.client(UnityServices.HEALTH_SERVICE)
print(health_service)
s = Unity(environment=UnityEnvironments.DEV)
s.set_project("unity")
s.set_venue("dev")

print("Example health status output using unity object:")
print(s)
71 changes: 28 additions & 43 deletions libs/unity-py/unity_sds_client/services/health_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
"""
The HealthService module contains those utilities related to the
Unity Health Services Infrastructure.
"""

import requests
from unity_sds_client.unity_session import UnitySession
from unity_sds_client.utils.http import get_headers

class HealthService(object):
"""
Expand All @@ -7,7 +14,8 @@ class HealthService(object):

def __init__(
self,
session:UnitySession
session:UnitySession,
endpoint: str = None
):
"""
Initialize the HealthService class.
Expand All @@ -23,7 +31,9 @@ def __init__(
List of applications and their health statses
"""

self._health_statuses = None
self._session = session
if endpoint is None:
self.endpoint = self._session.get_unity_href()

def __str__(self):
return self.generate_health_status_report()
Expand All @@ -33,57 +43,32 @@ def get_health_status(self):
Returns a list of services and their respective health status
"""

# Get Health Information
# Stubbed in health data until Health API endpoint is available
self._health_statuses = [
{
"service": "airflow",
"landingPage":"https://unity.jpl.nasa.gov/project/venue/processing/ui",
"healthChecks": [
{
"status": "HEALTHY",
"date": "2024-04-09T18:01:08Z"
}
]
},
{
"service": "jupyter",
"landingPage":"https://unity.jpl.nasa.gov/project/venue/ads/jupyter",
"healthChecks": [
{
"status": "HEALTHY",
"date": "2024-04-09T18:01:08Z"
}
]
},
{
"service": "other_service",
"landingPage":"https://unity.jpl.nasa.gov/project/venue/other_service",
"healthChecks": [
{
"status": "UNHEALTHY",
"date": "2024-04-09T18:01:08Z"
}
]
}
]
token = self._session.get_auth().get_token()
project = self._session.get_project()
venue = self._session.get_venue()
url = self.endpoint + f"{project}/{venue}/management/api/health_checks"

headers = get_headers(token, {
'Content-type': 'application/json'
})
response = requests.get(url, headers=headers, timeout=60)

return response.json()

return self._health_statuses

def generate_health_status_report(self):
"""
Return a generated report of health status information
"""

if self._health_statuses is None:
self.get_health_status()
health_statuses = self.get_health_status()

health_status_title = "HEALTH STATUS REPORT"
report = f"\n\n{health_status_title}\n"
report = report + len(health_status_title) * "-" + "\n\n"
for service in self._health_statuses:
service_name = service["service"]
report = report + f"{service_name}\n"
for service in health_statuses["services"]:
service_name = service["componentName"]
landing_page_url = service["landingPageUrl"]
report = report + f"{service_name} ({landing_page_url})\n"
for status in service["healthChecks"]:
service_status = status["status"]
service_status_date = status["date"]
Expand Down
14 changes: 14 additions & 0 deletions libs/unity-py/unity_sds_client/unity_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ def get_config(self):
"""
return self._config

def get_project(self):
if self._project is None:
raise UnityException("session variables project and venue or venue_id are required to interact with a "
"processing service.")
else:
return self._project

def get_venue(self):
if self._venue is None:
raise UnityException("session variables project and venue or venue_id are required to interact with a "
"processing service.")
else:
return self._venue

def get_venue_id(self):
if self._venue_id is None:
if self._project is None or self._venue is None:
Expand Down
86 changes: 53 additions & 33 deletions schemas/health-service/health-services.schema.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"service": {
"type": "string"
},
"landingPage": {
"type": "string"
},
"healthChecks": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["HEALTHY", "UNHEALTHY"]
},
"date": {
"type": "string",
"format": "date-time"
"type": "object",
"properties": {
"services": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"componentName": {
"type": "string"
},
"ssmKey": {
"type": "string"
},
"healthCheckUrl": {
"type": "string"
},
"landingPageUrl": {
"type": "string"
},
"healthChecks": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["HEALTHY", "UNHEALTHY"]
},
"httpResponseCode": {
"type": "string"
},
"date": {
"type": "string",
"format": "date-time"
}
},
"required": [
"status",
"httpResponseCode",
"date"
]
}
},
"required": [
"status",
"date"
]
}
},
"required": [
"componentName",
"ssmKey",
"healthCheckUrl",
"landingPageUrl",
"healthChecks"
]
}
},
"required": [
"service",
"landingPage",
"healthChecks"
]
}
},
"required": [
"services"
]
}
Loading