new classes based on json descriptor
This commit is contained in:
205
files/api/app.py
205
files/api/app.py
@ -16,11 +16,12 @@ scheduler = APScheduler()
|
||||
# default application setting variables
|
||||
app_settings = {
|
||||
"noisy_test" : False,
|
||||
"debug_output" : False,
|
||||
"log_output" : False,
|
||||
"debug_output" : True,
|
||||
"log_output" : True,
|
||||
"secure_api" : True,
|
||||
"push_redis" : False,
|
||||
"run_background" : True
|
||||
"run_background" : True,
|
||||
"update_frequency": 1
|
||||
}
|
||||
|
||||
with open('cosmostat_settings.yaml', 'r') as f:
|
||||
@ -75,93 +76,29 @@ def update_redis_channel(redis_channel, data):
|
||||
|
||||
def update_redis_server():
|
||||
# Update Stats Redis Channel
|
||||
update_redis_channel("host_stats", get_full_summary())
|
||||
update_redis_channel("host_metrics", get_redis_data())
|
||||
# Update history_stats Redis Channel
|
||||
update_redis_channel("history_stats", get_component_list())
|
||||
|
||||
#######################################################################
|
||||
### Other Functions
|
||||
#######################################################################
|
||||
|
||||
def get_component_summary():
|
||||
result = []
|
||||
for component in cosmostat_system.components:
|
||||
result.append(component.get_summary_key())
|
||||
return result
|
||||
|
||||
def get_full_summary():
|
||||
result = []
|
||||
|
||||
for component in cosmostat_system.components:
|
||||
result.append(component.get_summary_key())
|
||||
|
||||
for sysvar in cosmostat_system.get_sysvars_summary_keys():
|
||||
result.append(sysvar)
|
||||
|
||||
return result
|
||||
|
||||
# This will instantiate a System object
|
||||
def new_cosmos_system():
|
||||
new_system = System(f"{jenkins_hostname_settings()}")
|
||||
if app_settings["log_output"]:
|
||||
print(f"New system object name: {new_system.name}")
|
||||
for component in new_system.components:
|
||||
print(component)
|
||||
return new_system
|
||||
|
||||
def get_component_list(history_count = None):
|
||||
result = []
|
||||
for component in cosmostat_system.components:
|
||||
if history_count is not None:
|
||||
history = component.get_history(history_count)
|
||||
else:
|
||||
history = component.get_history()
|
||||
result.append(
|
||||
{
|
||||
"info": component.get_info_key(),
|
||||
"history": history
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
def get_info():
|
||||
device_summary = []
|
||||
for component in cosmostat_system.components:
|
||||
device_summary.append(
|
||||
{
|
||||
"info": component.get_info_key(),
|
||||
}
|
||||
)
|
||||
result = {
|
||||
"system_info":
|
||||
{
|
||||
"user": jenkins_user_settings(),
|
||||
"hostname": jenkins_hostname_settings(),
|
||||
"timestamp": jenkins_inventory_generation_timestamp_settings(),
|
||||
"component_count:": len(cosmostat_system.components),
|
||||
"object_name": cosmostat_system.name,
|
||||
"docker_gateway": docker_gateway_settings()
|
||||
},
|
||||
"device_summary": device_summary
|
||||
}
|
||||
return result
|
||||
|
||||
#def get_history_summary():
|
||||
# update_redis_channel("history_stats", get_component_list())
|
||||
|
||||
#######################################################################
|
||||
### Flask Routes
|
||||
#######################################################################
|
||||
|
||||
# full component list
|
||||
@app.route('/component_list', methods=['GET'])
|
||||
def component_list():
|
||||
count = request.args.get('count', type=int)
|
||||
return jsonify(get_component_list(count))
|
||||
# dynamic data
|
||||
# this will go to the redis server
|
||||
@app.route('/dynamic_data', methods=['GET'])
|
||||
def dynamic_data():
|
||||
return jsonify(get_dynamic_data())
|
||||
|
||||
# component summary
|
||||
@app.route('/component_summary', methods=['GET'])
|
||||
def component_summary():
|
||||
return jsonify(get_component_summary())
|
||||
# static data
|
||||
@app.route('/static_data', methods=['GET'])
|
||||
def static_data():
|
||||
return jsonify(get_static_data())
|
||||
|
||||
# redis data
|
||||
@app.route('/redis_data', methods=['GET'])
|
||||
def redis_data():
|
||||
return jsonify(get_redis_data())
|
||||
|
||||
# full summary
|
||||
@app.route('/full_summary', methods=['GET'])
|
||||
@ -180,27 +117,103 @@ def test():
|
||||
{
|
||||
"component_count:": len(cosmostat_system.components),
|
||||
"user": jenkins_user_settings(),
|
||||
"hostname": jenkins_hostname_settings()
|
||||
"hostname": jenkins_hostname_settings(),
|
||||
"cpu_model": cosmostat_system.get_components(component_type="CPU").description
|
||||
}
|
||||
)
|
||||
|
||||
#######################################################################
|
||||
### Flask Helpers
|
||||
#######################################################################
|
||||
|
||||
# needs to return array of {name: name, type: type, metrics: metrics}
|
||||
# for redis table generation, includes system and component metrics
|
||||
def get_dynamic_data():
|
||||
return cosmostat_system.get_live_metrics()
|
||||
|
||||
def get_static_data():
|
||||
result = []
|
||||
for metric in cosmostat_system.get_system_properties():
|
||||
result.append(metric)
|
||||
for metric in cosmostat_system.get_component_properties():
|
||||
result.append(metric)
|
||||
return result
|
||||
|
||||
def get_redis_data():
|
||||
result = []
|
||||
for metric in get_dynamic_data():
|
||||
result.append(metric)
|
||||
for metric in get_static_data():
|
||||
result.append(metric)
|
||||
return result
|
||||
|
||||
def get_full_summary():
|
||||
live_metrics = cosmostat_system.get_live_metrics()
|
||||
system_components = cosmostat_system.get_component_strings()
|
||||
system_info = get_info()
|
||||
result = {
|
||||
"system_settings":
|
||||
{
|
||||
"user": jenkins_user_settings(),
|
||||
"hostname": jenkins_hostname_settings(),
|
||||
"timestamp": jenkins_inventory_generation_timestamp_settings(),
|
||||
"component_count:": len(cosmostat_system.components),
|
||||
"object_name": cosmostat_system.name,
|
||||
"docker_gateway": docker_gateway_settings()
|
||||
},
|
||||
"live_metrics": live_metrics,
|
||||
"system_components": system_components,
|
||||
"system_info": system_info
|
||||
}
|
||||
return result
|
||||
|
||||
def get_info():
|
||||
component_strings = []
|
||||
for component in cosmostat_system.get_components():
|
||||
component_strings.append({"name": component.name, "description": component.description})
|
||||
system_strings = []
|
||||
|
||||
result = {
|
||||
"hostname": jenkins_hostname_settings(),
|
||||
"component_strings": component_strings
|
||||
}
|
||||
#for component_string in component_strings:
|
||||
# for name, description in component_string.items():
|
||||
# result[name] = description
|
||||
return result
|
||||
|
||||
#######################################################################
|
||||
### Other Functions
|
||||
#######################################################################
|
||||
|
||||
# instantiate and return the System object
|
||||
def new_cosmos_system():
|
||||
new_system = System(f"{jenkins_hostname_settings()}")
|
||||
if app_settings["log_output"]:
|
||||
print(f"New system object name: {new_system.name} - {new_system.get_component_count()} components:")
|
||||
for component in new_system.components:
|
||||
print(component.description)
|
||||
return new_system
|
||||
|
||||
|
||||
# Background Loop Function
|
||||
def background_loop():
|
||||
# Update all data on the System object
|
||||
cosmostat_system.update_system_state()
|
||||
|
||||
if app_settings["push_redis"]:
|
||||
update_redis_server()
|
||||
|
||||
if app_settings["noisy_test"]:
|
||||
print("Sorry about the mess...")
|
||||
print(f"Blame {jenkins_user_settings()}")
|
||||
|
||||
#######################################################################
|
||||
### Main Subroutine
|
||||
#######################################################################
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
# Background Loop Function
|
||||
def background_loop():
|
||||
# Update all data on the System object
|
||||
cosmostat_system.update_values()
|
||||
|
||||
if app_settings["push_redis"]:
|
||||
update_redis_server()
|
||||
|
||||
if app_settings["noisy_test"]:
|
||||
print("Sorry about the mess...")
|
||||
print(f"Blame {jenkins_user_settings()}")
|
||||
|
||||
# instantiate system
|
||||
cosmostat_system = new_cosmos_system()
|
||||
@ -217,7 +230,7 @@ if __name__ == '__main__':
|
||||
scheduler.add_job(id='background_loop',
|
||||
func=background_loop,
|
||||
trigger='interval',
|
||||
seconds=1)
|
||||
seconds=app_settings["update_frequency"])
|
||||
scheduler.init_app(app)
|
||||
scheduler.start()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user