Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions backend/routers/v1/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
update_snack,
create_bulk_items
)
from prometheus_client import generate_latest
from utils.db import snack_gauge, purchase_count

router = APIRouter()

Expand Down Expand Up @@ -76,3 +78,10 @@ async def create_bulk_route(request:BulkSnackCreate):
items=None,
error=f"Error Processing bulk request:{str(e)}"
)

@router.get("/metrics")
def get_metrics():
return PlainTextResponse(
media_type='text/plain',
content=generate_latest()
)
28 changes: 27 additions & 1 deletion backend/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
import sqlite3
from models.snack import Snack, SnackCreateSchema, SnackUpdateSchema,BulkSnackCreate
from typing import List
import prometheus_client

# Prometheus metrics
# track count of different types of snacks
snack_gauge = prometheus_client.Gauge(
"snack_gauge",
"Amount of snack types in inventory",
["sku"],
)

# track count of purchases
purchase_count = prometheus_client.Counter(
"purchase_count",
"Total number of snacks bought from inventory",
["sku"],
)

def get_db_connection(db_file_path:str="data/db.sqlite3"):
"""Creates and returns a SQLite database connection"""
Expand Down Expand Up @@ -79,6 +95,7 @@ def delete_snack(sku: str) -> Snack:
RETURNING *
""", (sku,))
record = cursor.fetchone()
snack_gauge.remove(sku)
return Snack(**record)


Expand All @@ -102,6 +119,8 @@ def create_snack(snack: SnackCreateSchema) -> Snack:
snack.photo_url
))
record = cursor.fetchone()
snack = Snack(**record)
snack_gauge.labels(sku=snack.sku).set(snack.quantity)
return Snack(**record)


Expand All @@ -124,6 +143,9 @@ def update_snack(sku: str, updates: SnackUpdateSchema) -> Snack:
description = updates.description if updates.description is not None else current_snack['description']

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic assumes that a decrease in quantity means a purchase was made, but this may not always be true (e.g., returns, adjustments). Consider revising the logic to accurately reflect purchase events.

category = updates.category if updates.category is not None else current_snack['category']

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calculation of quantity_change as current_snack['quantity'] - quantity can be negative if quantity is greater than current_snack['quantity'], which may lead to incorrect logic downstream. This needs validation or adjustment to handle such cases properly.

photo_url = updates.photo_url if updates.photo_url is not None else current_snack['photo_url']

purchase_made = quantity < current_snack['quantity']
quantity_change = current_snack['quantity'] - quantity

cursor.execute("""
UPDATE snacks
Expand All @@ -146,7 +168,11 @@ def update_snack(sku: str, updates: SnackUpdateSchema) -> Snack:
sku

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In update_snack, purchase_count.labels(sku).inc(quantity_change) is called without checking if quantity_change is positive. Incrementing with negative or zero values can cause incorrect metric reporting. Add validation to ensure only positive increments.

))
record = cursor.fetchone()
return Snack(**record)
snack = Snack(**record)
if purchase_made:
purchase_count.labels(sku).inc(quantity_change)
snack_gauge.labels(sku).set(quantity)
return snack


# Bulk Processing function
Expand Down