46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import time
|
|
from flask import Flask, jsonify, request
|
|
from prometheus_client import Counter, Summary
|
|
from timber import axe, LumberJack
|
|
from config import _CLIENT_CONFIG
|
|
|
|
_VERSION = '0.1.2'
|
|
lumber = LumberJack()
|
|
lumber.set_log_level('i')
|
|
app = Flask(__name__)
|
|
|
|
# Create metrics
|
|
REQUEST_COUNT = Counter('api_requests_total', 'Total API Requests', ['method', 'endpoint'])
|
|
REQUEST_LATENCY = Summary('api_request_latency_seconds', 'API Request Latency', ['method', 'endpoint'])
|
|
|
|
@app.before_request
|
|
def before_request():
|
|
request.start_time = time.time()
|
|
|
|
@app.after_request
|
|
def after_request(response):
|
|
latency = time.time() - request.start_time
|
|
REQUEST_COUNT.labels(method=request.method, endpoint=request.path).inc()
|
|
REQUEST_LATENCY.labels(method=request.method, endpoint=request.path).observe(latency)
|
|
return response
|
|
|
|
@app.route('/metrics')
|
|
def metrics():
|
|
return str(REQUEST_COUNT.collect()), 200, {'Content-Type': 'text/plain'}
|
|
|
|
@app.route('/power/<hostname>', methods=['GET'])
|
|
def get_status(hostname):
|
|
axe.info(f'Sending power 200 to {hostname}')
|
|
return hostname, 200
|
|
|
|
@app.route('/power/_VERSION', methods=['GET'])
|
|
def get_version():
|
|
return _VERSION, 200
|
|
|
|
@app.route('/power/_CONFIG/client/<hostname>', methods=['GET'])
|
|
def get_client_config(hostname):
|
|
axe.info(f'Config request from {hostname}')
|
|
return jsonify(_CLIENT_CONFIG), 200
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, host='0.0.0.0') |