full server dashboard working

This commit is contained in:
2026-03-22 18:44:07 -07:00
parent 324eaff135
commit 97fdb3d5d8
19 changed files with 1441 additions and 169 deletions

View File

@ -1,12 +1,17 @@
from flask import Flask, jsonify, request
from flask import Flask, jsonify, request, Response
from flask_apscheduler import APScheduler
from typing import Dict, Union
import json, time, redis, yaml
import base64, hashlib
import requests
from requests import RequestException, Response
from Components import *
from Cosmos_Settings import *
from Cosmostat import *
# declare flask apps
app = Flask(__name__)
scheduler = APScheduler()
@ -16,7 +21,7 @@ scheduler = APScheduler()
#######################################################################
# Redis client - will publish updates
r = redis.Redis(host=service_gateway_ip(), port=6379)
r = redis.Redis(host=redis_gateway_ip(), port=6379)
def update_redis_channel(redis_channel, data):
# Publish to the specified Redis channel
@ -25,14 +30,14 @@ def update_redis_channel(redis_channel, data):
def update_redis_server():
# Client Redis Tree
if not run_cosmostat_server():
if cosmostat_client.check_system_timer():
if cosmostat_client.check_system_timer():
update_redis_channel("host_metrics", get_client_redis_data(human_readable = False))
if run_cosmostat_server():
update_redis_channel("client_summary", get_server_redis_data())
update_redis_channel("client_hostnames", get_server_hostnames())
# Server Redis Tree
# History Redis Tree
# Update history_stats Redis Channel
# update_redis_channel("history_stats", get_component_list())
@ -48,13 +53,17 @@ def get_server_redis_data():
result = []
for client in cosmostat_server.systems:
this_client_key = {
"uuid": client["uuid"],
"short_id": client["short_id"],
"redis_data": client["redis_data"]
"hostname": client.hostname,
"uuid": client.uuid,
"short_id": client.name,
"redis_data": client.redis_data
}
result.append(this_client_key)
return result
def get_server_hostnames():
return cosmostat_server.get_client_hostnames()
#######################################################################
### Client Flask Routes
#######################################################################
@ -175,30 +184,56 @@ def generate_state_definition():
#######################################################################
# update client on server
@app.route('/update_client', methods=['GET'])
@app.route('/update_client', methods=['POST'])
def update_client():
result = {}
# check the request and return payload if all good
# check the request and return payload dict {} if all good
payload = client_submit_check(request = request, dict_name = "redis_data")
this_client = cosmostat_server.get_system(uuid = payload["uuid"])
result = run_update_client(this_client)
result = run_update_client(payload)
return jsonify(result), 200
# create client on server
@app.route('/create_client', methods=['GET'])
@app.route('/create_client', methods=['POST'])
def create_client():
result = {}
# check the request and return payload if all good
# check the request and return payload dict {} if all good
payload = client_submit_check(request = request, dict_name = "client_properties")
this_client = cosmostat_server.get_system(uuid = payload["uuid"])
result = run_create_client(this_client)
if not cosmostat_server.check_uuid(payload["uuid"]):
result = run_create_client(payload)
else:
result = {"message": "object already exists, skipping creation"}
return jsonify(result), 200
# api to validate Cosmostat Class
@app.route('/client_summary', methods=['GET'])
def client_summary():
client_summary = get_client_summary()
return jsonify()
result = []
if run_cosmostat_server():
result = get_client_summary()
else:
result = {"message": "server not running on this endpoint"}
return jsonify(result)
# api to pull all data
@app.route('/client_details', methods=['GET'])
def client_details():
result = []
if run_cosmostat_server():
result = get_client_details()
else:
result = {"message": "server not running on this endpoint"}
return jsonify(result)
# api to get all hostnames
@app.route('/client_hostnames', methods=['GET'])
def client_hostnames():
result = []
if run_cosmostat_server():
result = cosmostat_server.get_client_hostnames()
else:
result = {"message": "server not running on this endpoint"}
return jsonify(result)
#######################################################################
### Server Flask Helpers
@ -206,29 +241,34 @@ def client_summary():
# update client on server
def run_update_client(this_client):
if this_client == {}:
if not cosmostat_server.check_uuid(this_client["uuid"]):
return { "message": "client not found" }
update_status = f"updated client {this_client.short_id}"
timestamp_update = cosmostat_server.update_system(system_state = payload, system_uuid = payload["uuid"])
return {
"status": update_status,
"uuid": payload["uuid"],
"timestamp": timestamp_update
}
else:
timestamp_update = cosmostat_server.update_system(system_dictionary = this_client["redis_data"], system_uuid = this_client["uuid"])
update_status = f'updated client {this_client["short_id"]}'
return {
"status": update_status,
"uuid": this_client["uuid"],
"redis_data": this_client,
"timestamp_update": timestamp_update
}
# create client on server
def run_create_client(this_client):
update_status = f"created client {this_client.short_id}"
timestamp_update = cosmostat_server.create_system(system_state = payload, system_uuid = payload["uuid"])
timestamp_update = cosmostat_server.add_system(system_dictionary = this_client)
update_status = f'created client {this_client["short_id"]}'
return {
"status": update_status,
"uuid": payload["uuid"],
"timestamp": timestamp_update
"uuid": this_client["uuid"],
"client_properties": this_client,
"timestamp_update": timestamp_update
}
# flask submission check fucntion
# flask submission check function
def client_submit_check(request, dict_name: str):
required_keys = {"uuid", "short_id", "data_timestamp", dict_name}
payload = {}
required_keys = {"uuid", "short_id", "hostname", dict_name}
if not request.is_json:
logging.warning("Received non-JSON request")
return jsonify({"error": "Content-type must be application/json"}), 400
@ -239,97 +279,98 @@ def client_submit_check(request, dict_name: str):
missing = required_keys - payload.keys()
if missing:
raise ValueError(f"Missing required keys: {', '.join(sorted(missing))}")
return payload
# generate cosmostat server summary
def get_client_summary():
result = []
for client in cosmostat_server.systems:
this_client_properties = client.get_system_properties(human_readable = True)
this_client_components = []
for component in client.get_components():
this_component = {
"component_name": component.name,
"info_strings": component.get_properties_strings(return_simple = True)
}
this_client_components.append(this_component)
data_age = time.time() - client.data_timestamp
this_client = {
"client_properties": this_client_properties,
"client_components": this_client_components
"uuid": client.uuid,
"short_id": client.name,
"data_age": data_age,
"hostname": client.hostname
}
result.append(this_client)
if result == []:
result = {"message": "no clients reporting"}
return result
# no redis data needed here
def get_client_details():
result = []
for client in cosmostat_server.systems:
data_age = time.time() - client.data_timestamp
this_client = {
"uuid": client.uuid,
"short_id": client.name,
"client_properties": client.client_properties,
# "redis_data": client.redis_data,
"hostname": client.hostname
}
result.append(this_client)
if result == []:
result = {"message": "no clients reporting"}
return result
#######################################################################
### Cosmostat Client Subroutines
#######################################################################
# since the API isn't running
# def local_client_update():
# Cosmostat Client Reporter
def client_update(this_client: dict, api_endpoint = "update_client"):
# set variables for API call
this_uuid = cosmostat_client.uuid
this_short_id = cosmostat_client.short_id
this_timestamp = time.time()
api_url = f"{cosmostat_server_api()}{api_endpoint}"
# generate payload
payload = {
"uuid": this_uuid,
"short_id": this_short_id,
"data_timestamp": this_timestamp, # Unix epoch float
"redis_data": get_client_redis_data(human_readable = False),
}
def client_update():
api_url = f"{cosmostat_server_api()}update_client"
payload = get_client_payload(get_client_redis_data(human_readable = False), "redis_data")
log_data(log_output = "client_update - redis data from local client:", log_level = "noisy_test")
log_data(log_output = payload, log_level = "noisy_test")
# execute API call
result = client_submission_handler()
if (
isinstance(result, dict)
and result.get("message", "").lower() == "client not found"
):
# if client not found, create client
if api_endpoint == "update_client":
client_initialize()
raise RuntimeError("Client not found - initializing")
result = client_submission_handler(api_url, payload)
client_initialize()
return result
# Cosmostat Client Initializer
def client_initialize():
# set variables for API call
this_uuid = cosmostat_client.uuid
this_short_id = cosmostat_client.short_id
this_timestamp = time.time()
api_url = f"{cosmostat_server_api()}create_client"
# generate payload
payload = {
"uuid": this_uuid,
"short_id": this_short_id,
"data_timestamp": this_timestamp, # Unix epoch float
"client_properties": get_php_summary(),
}
payload = get_client_payload(get_php_summary(), "client_properties")
# execute API call
result = client_submission_handler()
result = client_submission_handler(api_url, payload)
return result
# Cosmostat Client API Reporting Handler
def client_submission_handler():
def client_submission_handler(api_url: str, payload: dict):
result = None
try:
# `json=` automatically sets Content-Type to application/json
response: Response = requests.post(api_url, json=payload, timeout=timeout)
response: Response = requests.post(api_url, json=payload, timeout=4)
response.raise_for_status() # raise HTTPError for 4xx/5xx
except RequestException as exc:
# Wrap the low-level exception in a more descriptive one
raise RuntimeError(
f"Failed to POST to {url!r}: {exc}"
) from exc
log_data(log_output = f"Failed to POST to {api_url!r}: {exc}", log_level = "log_output")
# process reply from API
try:
result = response.json()
except ValueError as exc:
raise RuntimeError(
f"Server responded with non-JSON payload: {response.text!r}"
) from exc
log_data(log_output = "Server responded with non-JSON payload: {response.text!r}", log_level = "log_output")
return result
def get_client_payload(system_dictionary: dict, dictionary_name: str):
this_uuid = cosmostat_client.uuid
this_short_id = cosmostat_client.short_id
this_hostname = cosmostat_client.name
payload = {
"uuid": this_uuid,
"short_id": this_short_id,
"hostname": this_hostname,
dictionary_name: system_dictionary
}
return payload
#######################################################################
#######################################################################
@ -353,9 +394,9 @@ if __name__ == '__main__':
return new_client
# instantiate and return the Cosmoserver System object
def new_cosmos_server():
new_server = Cosmoserver(cosmostat_client.uuid)
log_data(log_output = f"New Cosmostat object name: {new_server.name}", log_level = "log_output")
def new_cosmostat_server():
new_server = CosmostatServer(cosmostat_client.uuid)
log_data(log_output = f"New Cosmostat serverobject name: {new_server.name}", log_level = "log_output")
return new_server
# Background Loop Function
@ -366,15 +407,26 @@ if __name__ == '__main__':
if app_settings["push_redis"]:
update_redis_server()
if app_settings["cosmostat_server_reporter"]:
if run_cosmostat_reporter():
if int(time.time()) % 5 == 0 and not cosmostat_client.check_system_timer():
cosmostat_client.update_system_state()
client_update()
if run_cosmostat_server():
this_client = get_client_payload(get_client_redis_data(human_readable = False), "redis_data")
run_update_client(this_client)
time.sleep(0.5)
######################################
# instantiate client
######################################
cosmostat_client = new_cosmos_client()
if app_settings["cosmostat_server_reporter"]:
if app_settings["cosmostat_server_reporter"] and not app_settings["cosmostat_server"]:
client_initialize()
######################################
@ -383,7 +435,9 @@ if __name__ == '__main__':
cosmostat_server = None
if run_cosmostat_server():
cosmostat_server = new_cosmos_server()
cosmostat_server = new_cosmostat_server()
this_client = get_client_payload(get_php_summary(), "client_properties")
timestamp_update = cosmostat_server.add_system(system_dictionary = this_client)
######################################
# send initial stats update to redis