[PR #465] [CLOSED] Improve and fix on Add cost tracking for spools (#415) #1048

Closed
opened 2026-05-06 12:34:41 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/maziggy/bambuddy/pull/465
Author: @Keybored02
Created: 2/20/2026
Status: Closed

Base: 0.2.1b2Head: feature/cost_tracker


📝 Commits (10+)

  • 04ffca2 Add cost tracking for spools and usage history
  • 0e50285 Merge branch '0.2.1b' into feature/cost_tracker
  • c72366c Merge branch '0.2.1' into feature/cost_tracker
  • 5cda70a Merge branch '0.2.1b2' into feature/cost_tracker
  • 8043bb1 Refactor cost calculation logic to prioritize spool usage history and update schema validation for cost_per_kg
  • 380edfc Merge branch '0.2.1b2' into feature/cost_tracker
  • 071c2c7 Refactor cost calculation logic to utilize pre-fetched spool usage costs and remove redundant imports; update InventoryPage to adjust cost display format
  • 9f19e8a Add currency symbol, hide projected cost for no costs configured, add cost rounding to decimal point, fixed isExternal logic
  • 1dd266b Refactor functions in cost statistics integration tests
  • 427b08f Add archive_id to SpoolUsageHistory model and update cost calculations in usage_tracker

📊 Changes

26 files changed (+1314 additions, -22 deletions)

View changed files

📝 backend/app/api/routes/archives.py (+59 -17)
📝 backend/app/core/database.py (+21 -0)
📝 backend/app/main.py (+1 -0)
📝 backend/app/models/spool.py (+4 -0)
📝 backend/app/models/spool_usage_history.py (+2 -0)
📝 backend/app/schemas/spool.py (+2 -0)
📝 backend/app/schemas/spool_usage.py (+1 -0)
📝 backend/app/services/usage_tracker.py (+70 -0)
📝 backend/tests/conftest.py (+3 -0)
backend/tests/integration/test_cost_statistics.py (+426 -0)
backend/tests/unit/test_cost_tracking.py (+592 -0)
📝 frontend/src/api/client.ts (+2 -0)
📝 frontend/src/components/PrintModal/FilamentMapping.tsx (+48 -2)
📝 frontend/src/components/PrintModal/index.tsx (+6 -0)
📝 frontend/src/components/PrintModal/types.ts (+2 -0)
📝 frontend/src/components/SpoolFormModal.tsx (+5 -1)
📝 frontend/src/components/spool-form/AdditionalSection.tsx (+24 -0)
📝 frontend/src/components/spool-form/types.ts (+3 -0)
📝 frontend/src/i18n/locales/de.ts (+2 -0)
📝 frontend/src/i18n/locales/en.ts (+2 -0)

...and 6 more files

📄 Description

Description

Follow up to PR #454

This PR implements per-spool cost tracking, with localization and test coverage. The objective is to allow the user to set a cost per Kg when adding a new spool to the integrated Inventory. This field is then used to calculate print cost based on weight used for each spool. The Statistics page shows the filament cost based on this calculation. If no cost is present, the default cost specified in Settings is used instead. This PR also introduces changes to the Archive object card to show last print total cost and to the Print card to show projected total cost based on the Filament Mapping the user chooses. In the Filament page, an optional cost column has been added. All locales have been updated with the required fields. A new set of tests has been added and verified.

Fixes #452

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • [] Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code refactoring
  • Performance improvement
  • Test addition or update

Changes Made

  • Added: cost_per_kg and spool_usage_history to database fields
  • Add cost field to archive.py
  • Add cost calculation logic in usage_tracker.py with fallback to defaults (uses weight_grams to be able to handle failed prints)
  • Saved cost updates in main.py by summing usage cost
  • Update archives.py with total cost calculation for Statistics page usage
  • Updated UI elements:
    • Add Cost field to Add Spool form
    • Added total cost to Print form (calculated on the Filament Mapping)
    • Added total cost to Archive object card
    • Added cost as a column option in the Filaments page
  • Added Unit Tests backend/tests/unit/test_cost_tracking.py
    • test_cost_with_spool_specific_cost_per_kg
    • test_cost_with_default_fallback
    • test_cost_zero_when_default_cost_is_zero
    • test_cost_for_failed_print_uses_actual_usage
    • test_cost_with_ams_fallback_tracking
    • test_multi_filament_cost_aggregation
    • test_costs_summed_in_archive
    • test_null_costs_handled_in_aggregation
  • Added Integration Tests backend/tests/integration/test_cost_statistics.py
    • test_archive_has_cost_field
    • test_statistics_includes_total_cost
    • test_statistics_aggregates_costs_correctly
    • test_statistics_handles_null_costs
    • test_statistics_includes_failed_print_costs
    • test_spool_cost_fields_persist
    • test_spool_update_cost_fields
    • test_cost_with_different_currencies

Screenshots

Screenshot 2026-02-19 144329 Screenshot 2026-02-19 144404 Screenshot 2026-02-19 144423

Testing

  • I have tested this on my local machine
  • I have tested with my printer model: H2C

Checklist

  • My code follows the project's coding style
  • I have commented my code where necessary
  • I have updated the documentation (if needed)
  • My changes generate no new warnings
  • I have tested my changes thoroughly

Additional Notes


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/maziggy/bambuddy/pull/465 **Author:** [@Keybored02](https://github.com/Keybored02) **Created:** 2/20/2026 **Status:** ❌ Closed **Base:** `0.2.1b2` ← **Head:** `feature/cost_tracker` --- ### 📝 Commits (10+) - [`04ffca2`](https://github.com/maziggy/bambuddy/commit/04ffca204f4f5ad06290befbdd05ea3def208880) Add cost tracking for spools and usage history - [`0e50285`](https://github.com/maziggy/bambuddy/commit/0e502858604dec31a99ab63d6aeacc3ed710e18d) Merge branch '0.2.1b' into feature/cost_tracker - [`c72366c`](https://github.com/maziggy/bambuddy/commit/c72366c9d38b7aa187543c7ff8b0252a0b0e61fe) Merge branch '0.2.1' into feature/cost_tracker - [`5cda70a`](https://github.com/maziggy/bambuddy/commit/5cda70a62c66af463281811e7ebbb508ba631717) Merge branch '0.2.1b2' into feature/cost_tracker - [`8043bb1`](https://github.com/maziggy/bambuddy/commit/8043bb18bad1109fff707ae91e9b225e8d359e13) Refactor cost calculation logic to prioritize spool usage history and update schema validation for cost_per_kg - [`380edfc`](https://github.com/maziggy/bambuddy/commit/380edfc249f66460d165dcc1e32a90fb6b060ff5) Merge branch '0.2.1b2' into feature/cost_tracker - [`071c2c7`](https://github.com/maziggy/bambuddy/commit/071c2c702a196fff24c2667bc02d06e7fb842a3c) Refactor cost calculation logic to utilize pre-fetched spool usage costs and remove redundant imports; update InventoryPage to adjust cost display format - [`9f19e8a`](https://github.com/maziggy/bambuddy/commit/9f19e8a2ffaa1880c051ce01592335e30a116db1) Add currency symbol, hide projected cost for no costs configured, add cost rounding to decimal point, fixed isExternal logic - [`1dd266b`](https://github.com/maziggy/bambuddy/commit/1dd266b66ea7f162b0ae05b9dfe254db464a8bf4) Refactor functions in cost statistics integration tests - [`427b08f`](https://github.com/maziggy/bambuddy/commit/427b08f9f63c9b7c5a141a8bc20dff8f47ec2d92) Add archive_id to SpoolUsageHistory model and update cost calculations in usage_tracker ### 📊 Changes **26 files changed** (+1314 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `backend/app/api/routes/archives.py` (+59 -17) 📝 `backend/app/core/database.py` (+21 -0) 📝 `backend/app/main.py` (+1 -0) 📝 `backend/app/models/spool.py` (+4 -0) 📝 `backend/app/models/spool_usage_history.py` (+2 -0) 📝 `backend/app/schemas/spool.py` (+2 -0) 📝 `backend/app/schemas/spool_usage.py` (+1 -0) 📝 `backend/app/services/usage_tracker.py` (+70 -0) 📝 `backend/tests/conftest.py` (+3 -0) ➕ `backend/tests/integration/test_cost_statistics.py` (+426 -0) ➕ `backend/tests/unit/test_cost_tracking.py` (+592 -0) 📝 `frontend/src/api/client.ts` (+2 -0) 📝 `frontend/src/components/PrintModal/FilamentMapping.tsx` (+48 -2) 📝 `frontend/src/components/PrintModal/index.tsx` (+6 -0) 📝 `frontend/src/components/PrintModal/types.ts` (+2 -0) 📝 `frontend/src/components/SpoolFormModal.tsx` (+5 -1) 📝 `frontend/src/components/spool-form/AdditionalSection.tsx` (+24 -0) 📝 `frontend/src/components/spool-form/types.ts` (+3 -0) 📝 `frontend/src/i18n/locales/de.ts` (+2 -0) 📝 `frontend/src/i18n/locales/en.ts` (+2 -0) _...and 6 more files_ </details> ### 📄 Description ## Description Follow up to PR #454 This PR implements per-spool cost tracking, with localization and test coverage. The objective is to allow the user to set a cost per Kg when adding a new spool to the integrated Inventory. This field is then used to calculate print cost based on weight used for each spool. The Statistics page shows the filament cost based on this calculation. If no cost is present, the default cost specified in Settings is used instead. This PR also introduces changes to the Archive object card to show last print total cost and to the Print card to show projected total cost based on the Filament Mapping the user chooses. In the Filament page, an optional cost column has been added. All locales have been updated with the required fields. A new set of tests has been added and verified. ## Related Issue <!-- Link to the issue this PR addresses (if applicable) --> Fixes #452 ## Type of Change <!-- Mark the relevant option with an "x" --> - [ ] Bug fix (non-breaking change that fixes an issue) - [x] New feature (non-breaking change that adds functionality) - [] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Code refactoring - [ ] Performance improvement - [x] Test addition or update ## Changes Made - Added: `cost_per_kg` and `spool_usage_history` to database fields - Add `cost` field to `archive.py` - Add cost calculation logic in `usage_tracker.py` with fallback to defaults (uses `weight_grams` to be able to handle failed prints) - Saved cost updates in `main.py` by summing usage cost - Update `archives.py` with total cost calculation for Statistics page usage - Updated UI elements: - Add Cost field to Add Spool form - Added total cost to Print form (calculated on the Filament Mapping) - Added total cost to Archive object card - Added cost as a column option in the Filaments page - Added Unit Tests `backend/tests/unit/test_cost_tracking.py` - `test_cost_with_spool_specific_cost_per_kg` - `test_cost_with_default_fallback` - `test_cost_zero_when_default_cost_is_zero` - `test_cost_for_failed_print_uses_actual_usage` - `test_cost_with_ams_fallback_tracking` - `test_multi_filament_cost_aggregation` - `test_costs_summed_in_archive` - `test_null_costs_handled_in_aggregation` - Added Integration Tests `backend/tests/integration/test_cost_statistics.py` - `test_archive_has_cost_field` - `test_statistics_includes_total_cost` - `test_statistics_aggregates_costs_correctly` - `test_statistics_handles_null_costs` - `test_statistics_includes_failed_print_costs` - `test_spool_cost_fields_persist` - `test_spool_update_cost_fields` - `test_cost_with_different_currencies` ## Screenshots <img width="479" height="210" alt="Screenshot 2026-02-19 144329" src="https://github.com/user-attachments/assets/677e45d0-978b-45ed-9c1c-f4b1592115fb" /> <img width="492" height="351" alt="Screenshot 2026-02-19 144404" src="https://github.com/user-attachments/assets/b3330fad-c66f-4ffa-9e49-48f0d9903aba" /> <img width="400" height="531" alt="Screenshot 2026-02-19 144423" src="https://github.com/user-attachments/assets/59dfc5a7-539c-4b46-92dc-1caa7779c1e6" /> ## Testing <!-- Describe how you tested your changes --> - [x] I have tested this on my local machine - [x] I have tested with my printer model: H2C ## Checklist - [x] My code follows the project's coding style - [x] I have commented my code where necessary - [ ] I have updated the documentation (if needed) - [x] My changes generate no new warnings - [x] I have tested my changes thoroughly ## Additional Notes --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 12:34:41 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/bambuddy#1048
No description provided.