Skip to content

Commit

Permalink
Merge pull request #5 from bcgov/release/add-available-form-statistics
Browse files Browse the repository at this point in the history
Added Form Statistics to Prometheus monitoring
  • Loading branch information
adimar-aot authored Nov 12, 2024
2 parents d6b6565 + be434ed commit 7c48445
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .env-template
Original file line number Diff line number Diff line change
Expand Up @@ -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
ERR_THRESHOLD_COUNT=1
DF_FORM_INVENTORY_API_URL=http://host.docker.internal:5000/api/v1/forms/statistics
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 46 additions & 0 deletions src/dfmetricsfuncs.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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__':
Expand Down
2 changes: 2 additions & 0 deletions src/requirements-build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7c48445

Please sign in to comment.