50 lines
2 KiB
Python
50 lines
2 KiB
Python
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
from flask import Flask, request, jsonify
|
||
|
from prometheus_client import Counter, generate_latest, multiprocess, REGISTRY, CollectorRegistry
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
|
||
|
# Create a new CollectorRegistry to avoid duplicate timeseries error
|
||
|
custom_registry = CollectorRegistry()
|
||
|
|
||
|
# Define Prometheus Counters for each input device
|
||
|
c3woc_b1_counter = Counter('c3woc_buzzer1_pressed_total', 'Total number the buzzer 1 from C3WOC is pressed', registry=custom_registry)
|
||
|
c3woc_b2_counter = Counter('c3woc_buzzer2_pressed_total', 'Total number the buzzer 2 from C3WOC is pressed', registry=custom_registry)
|
||
|
c3woc_b3_counter = Counter('c3woc_buzzer3_pressed_total', 'Total number the buzzer 3 from C3WOC is pressed', registry=custom_registry)
|
||
|
c3woc_b4_counter = Counter('c3woc_buzzer4_pressed_total', 'Total number the buzzer 4 from C3WOC is pressed', registry=custom_registry)
|
||
|
# Define more counters as needed for other devices
|
||
|
# TODO: dynamic counter creation
|
||
|
|
||
|
@app.route('/metrics')
|
||
|
def metrics():
|
||
|
# Generate and return Prometheus metrics format using custom registry
|
||
|
return generate_latest(registry=custom_registry)
|
||
|
|
||
|
@app.route('/buzzed', methods=['POST'])
|
||
|
def add_data():
|
||
|
data = request.get_json()
|
||
|
device = data.get('buzzer')
|
||
|
waffles_counted = data.get('waffle_ready')
|
||
|
|
||
|
match device:
|
||
|
case 'buzzer1':
|
||
|
c3woc_b1_counter.inc(waffles_counted)
|
||
|
return jsonify({'message': f'Data added for {device}'}), 200
|
||
|
case 'buzzer2':
|
||
|
c3woc_b2_counter.inc(waffles_counted)
|
||
|
return jsonify({'message': f'Data added for {device}'}), 200
|
||
|
case 'buzzer3':
|
||
|
c3woc_b3_counter.inc(waffles_counted)
|
||
|
return jsonify({'message': f'Data added for {device}'}), 200
|
||
|
case 'buzzer4':
|
||
|
c3woc_b4_counter.inc(waffles_counted)
|
||
|
return jsonify({'message': f'Data added for {device}'}), 200
|
||
|
case _:
|
||
|
return jsonify({'error': 'Invalid device or buzzer specified'}), 400
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(host='0.0.0.0', port=8042)
|
||
|
|