diff --git a/.env-template b/.env-template index 800f1fd..7ad731b 100644 --- a/.env-template +++ b/.env-template @@ -5,4 +5,5 @@ ERR_TABLE_COLLECTION=error LOG_LEVEL=DEBUG MONGO_URL=mongodb://root-user:root-password@mongo:27017/ RECON_DB_NAME=testdb -ERR_THRESHOLD_COUNT=1 \ No newline at end of file +ERR_THRESHOLD_COUNT=1 +DF_FORM_INVENTORY_API_URL=http://host.docker.internal:5000/api/v1/forms/statistics \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7963faa..e6f0a10 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,6 @@ prometheus-client==0.16.0 pymongo==4.3.3 Werkzeug==2.3.1 zipp==3.15.0 +numpy==1.24.3 pandas==2.0.1 +requests==2.31.0 diff --git a/src/dfmetricsfuncs.py b/src/dfmetricsfuncs.py new file mode 100644 index 0000000..be25d56 --- /dev/null +++ b/src/dfmetricsfuncs.py @@ -0,0 +1,46 @@ +import os +import requests + +class dfmetrics: + def __init__(self, logging): + self.logging = logging + self.form_inventory_api_url = os.getenv('DF_FORM_INVENTORY_API_URL', 'http://localhost:5000/api/v1/forms/statistics') + + def genFormInventoryMetrics(self): + self.logging.info("Generating metrics for form inventory") + try: + response = requests.get(self.form_inventory_api_url) + data = response.json() + return data + except Exception as e: + self.logging.error("Error in generating metrics for form inventory") + self.logging.info(e) + return [] + + def genAvailableFormsMetric(self, form_type): + inventory = self.genFormInventoryMetrics() + for item in inventory: + if item['form_type'] == form_type: + return item['available_forms'] + return 0 + + def genLeasedFormsMetric(self, form_type): + inventory = self.genFormInventoryMetrics() + for item in inventory: + if item['form_type'] == form_type: + return item['leased_forms'] + return 0 + + def genTotalFormsMetric(self, form_type): + inventory = self.genFormInventoryMetrics() + for item in inventory: + if item['form_type'] == form_type: + return item['total_forms'] + return 0 + + def genTotalUsedFormsMetric(self, form_type): + inventory = self.genFormInventoryMetrics() + for item in inventory: + if item['form_type'] == form_type: + return item['total_used_forms'] + return 0 \ No newline at end of file diff --git a/src/main.py b/src/main.py index 5251e8b..35aeb74 100644 --- a/src/main.py +++ b/src/main.py @@ -7,6 +7,7 @@ from prometheus_client import Gauge, generate_latest import logging from metricsfuncs import reconmetrics +from dfmetricsfuncs import dfmetrics numeric_level = getattr(logging, os.getenv('LOG_LEVEL').upper(), 10) # Set up logging @@ -29,6 +30,7 @@ metricsobj=reconmetrics(db,logging) +dfmetricsobj = dfmetrics(logging) err_metric = Gauge("msgs_err_count", "Count of errored messages",['count_type']) err_metric.labels("err_staging").set_function(metricsobj.genStageErrMetric) @@ -46,6 +48,21 @@ source_type_count_metric = Gauge("ride_msgs_by_source_counts", "Count of messages by data source",['source_type']) +# New metrics for form inventory +form_types = ['12Hour', '24Hour', 'IRP', 'VI'] + +available_forms_metric = Gauge("available_forms", "Number of available forms", ['form_type']) +leased_forms_metric = Gauge("leased_forms", "Number of leased forms", ['form_type']) +total_forms_metric = Gauge("total_forms", "Total number of forms", ['form_type']) +total_used_forms_metric = Gauge("total_used_forms", "Total number of used forms", ['form_type']) + +for form_type in form_types: + available_forms_metric.labels(form_type).set_function(lambda ft=form_type: dfmetricsobj.genAvailableFormsMetric(ft)) + leased_forms_metric.labels(form_type).set_function(lambda ft=form_type: dfmetricsobj.genLeasedFormsMetric(ft)) + total_forms_metric.labels(form_type).set_function(lambda ft=form_type: dfmetricsobj.genTotalFormsMetric(ft)) + total_used_forms_metric.labels(form_type).set_function(lambda ft=form_type: dfmetricsobj.genTotalUsedFormsMetric(ft)) + + @app.route('/ping') def pingroute(): resp="working" @@ -72,6 +89,16 @@ def genDetailedmetrics(): logging.info(e) return make_response(jsonify(status=respstatus),statuscode) +@app.route('/formsinventory') +def formsinventory(): + try: + inventory = dfmetricsobj.genFormInventoryMetrics() + return jsonify(inventory), 200 + except Exception as e: + logging.error("Error in retrieving form inventory") + logging.info(e) + return jsonify({"error": "Failed to retrieve form inventory"}), 500 + if __name__ == '__main__': diff --git a/src/requirements-build.txt b/src/requirements-build.txt index 7963faa..e6f0a10 100644 --- a/src/requirements-build.txt +++ b/src/requirements-build.txt @@ -12,4 +12,6 @@ prometheus-client==0.16.0 pymongo==4.3.3 Werkzeug==2.3.1 zipp==3.15.0 +numpy==1.24.3 pandas==2.0.1 +requests==2.31.0