Last active
September 16, 2021 08:32
-
-
Save Nilsty/d2a6a1a1dedcde392672e2158c953bfe to your computer and use it in GitHub Desktop.
Demo Humanitec metrics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import sys | |
humanitec_url = "api.humanitec.io" | |
def printUsage(): | |
print(f"usage: python3 {sys.argv[0]} your-humanitec-api-token") | |
exit() | |
if len(sys.argv) != 2: | |
printUsage() | |
else: | |
humanitec_token = sys.argv[1] | |
headers = { | |
'Authorization': f'Bearer {humanitec_token}', | |
'Content-Type': 'application/json' | |
} | |
###################################################### | |
# Get your user name and organization from Humanitec # | |
###################################################### | |
url = f"https://{humanitec_url}/current-user" | |
response = requests.request("GET", url, headers=headers) | |
if response.status_code == 200: | |
user = response.json()['name'] | |
roles = response.json()['roles'].keys() | |
for key in roles: | |
role = key | |
org = role.split("/")[-1] | |
print(f"Username: {user}") | |
print(f"Organization: {org}") | |
else: | |
sys.exit(f"Unable to obtain Humanitec user data. GET {url} returned status code {response.status_code}.") | |
###################################################### | |
# Number of applications # | |
###################################################### | |
url = f"https://{humanitec_url}/orgs/{org}/apps" | |
response = requests.request("GET", url, headers=headers) | |
if response.status_code == 200: | |
apps = response.json() | |
num_apps = len(apps) | |
print(f"Number of apps in organization {org}: {num_apps}") | |
else: | |
sys.exit(f"Unable to obtain Humanitec app data. GET {url} returned status code {response.status_code}.") | |
###################################################### | |
# Number of environments # | |
###################################################### | |
num_envs = 0 | |
for app in apps: | |
for env in app['envs']: | |
num_envs = num_envs + 1 | |
print(f"Number of application environments in organization {org}: {num_envs}") | |
###################################################### | |
# Number of deployments # | |
###################################################### | |
num_deploys = 0 | |
for app in apps: | |
for env in app["envs"]: | |
url = f"https://{humanitec_url}/orgs/{org}/apps/{app['id']}/envs/{env['id']}/deploys" | |
response = requests.request("GET", url, headers=headers) | |
if response.status_code == 200: | |
env_deploys = response.json() | |
for deploy in env_deploys: | |
num_deploys = num_deploys + 1 | |
print(f"Number of deployments in organization {org}: {num_deploys}") | |
###################################################### | |
# Number of images # | |
###################################################### | |
url = f"https://{humanitec_url}/orgs/{org}/images" | |
response = requests.request("GET", url, headers=headers) | |
if response.status_code == 200: | |
images = response.json() | |
num_image_builds = 0 | |
for image in images: | |
for build in image["builds"]: | |
num_image_builds = num_image_builds + 1 | |
print(f"Number of images in organization {org}: {num_image_builds}") | |
else: | |
sys.exit(f"Unable to obtain Humanitec images data. GET {url} returned status code {response.status_code}.") | |
###################################################### | |
# Number of users # | |
###################################################### | |
url = f"https://{humanitec_url}/orgs/{org}/users" | |
response = requests.request("GET", url, headers=headers) | |
if response.status_code == 200: | |
users = response.json() | |
num_users = len(users) | |
print(f"Number of users in organization {org}: {num_users}") | |
else: | |
sys.exit(f"Unable to obtain Humanitec organization users data. GET {url} returned status code {response.status_code}.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to create a Humanitec API token is described in our API docs: https://api-docs.humanitec.com/#section/Introduction/Authentication