fix(cost-insight): reconcile Tencent monthly real cost only - #668
dillon-zheng merged 3 commits into
Conversation
|
I Skip it since the diff size(227362 bytes > 80000 bytes) is too large |
41c17e6 to
85929c6
Compare
There was a problem hiding this comment.
I have already done a preliminary review for you, and I hope to help you do a better job.
Summary
This PR updates the Tencent billing reconciliation logic and documentation to reconcile monthly billed cost only against RealTotalCost, treating TotalCost as observational. The approach focuses on changing the reconciliation check and clarifying the API semantics, along with updating tests accordingly. The changes are well-scoped and supported by production evidence and tests, improving correctness and clarity.
Critical Issues
- No critical bugs or regressions found.
The logic change is straightforward and well-covered by tests. The PR correctly avoids comparing the unreliableTotalCostand focuses onRealTotalCost.
Code Improvements
-
_reconcile_closed_monthsfunction (src/cost_insight/jobs/sync_tencent_billing_summary.py, lines 841-860)- The commented-out code on line 847-849 should be removed rather than left commented to keep code clean:
Suggestion: Delete these lines entirely as the PR no longer uses
# if summary.total_cost is not None and imported_list != summary.total_cost: # mismatches.append(f"list {imported_list} != {summary.total_cost}")
TotalCostfor reconciliation.
- The commented-out code on line 847-849 should be removed rather than left commented to keep code clean:
-
Clarify status
"matched-real-cost-only"usage
The current logic sets status"matched-real-cost-only"if the net cost matches andTotalCostis None. This might be confusing ifTotalCostis sometimes present but ignored. Consider explicitly documenting or asserting thatTotalCostpresence does not affect reconciliation or always set"matched-real-cost-only"when matched onRealTotalCost.
Best Practices
-
Documentation clarity (docs/tencent-billing-import-design.md)
- The added explanation on the difference between
TotalCostandRealTotalCostis very helpful. - Suggest adding a short summary or note near the top of the document explaining that reconciliation now only considers
RealTotalCostto help future maintainers quickly understand the change.
- The added explanation on the difference between
-
Test naming and parameterization (tests/test_sync_tencent_billing_summary.py)
- The test
test_scheduled_run_reconciles_closed_month_real_cost_with_single_summary_requestwas renamed appropriately. - The parametrize arguments removed
expected_statusand hard-coded"matched-real-cost-only". This is fine but consider also adding a test case whereTotalCostis present but mismatches, to ensure reconciliation ignores it, to document behavior explicitly.
- The test
-
Code style and comments
- In
sync_tencent_billing_summary.py, the new comment explaining the API behavior is useful. Consider formatting it as a docstring or adding references to the relevant API docs if available for easier traceability.
- In
Summary of actionable changes:
- Remove commented-out
TotalCostreconciliation code insync_tencent_billing_summary.py. - Add a brief summary note about
RealTotalCostreconciliation near the top oftencent-billing-import-design.md. - Consider adding a test case where
TotalCostis present but ignored to explicitly verify behavior. - Optionally, convert explanatory comments about Tencent API in
sync_tencent_billing_summary.pyinto a docstring or link to official API docs if possible.
These changes will improve maintainability, clarity, and robustness of the reconciliation logic.
There was a problem hiding this comment.
I have already done a preliminary review for you, and I hope to help you do a better job.
Summary
This PR updates Tencent monthly billing reconciliation to verify only against the RealTotalCost (billed amount) instead of TotalCost, which is now treated as observational and non-comparable. It modifies the reconciliation logic, status flags, and documentation to reflect this approach. The change is well contained and accompanied by aligned tests and detailed doc updates explaining the rationale. Overall, the PR improves correctness by aligning with observed Tencent API semantics and maintains good quality with clear validation and coverage.
Critical Issues
- No critical issues found. The logic change is straightforward and well tested.
Code Improvements
-
Reconciliation Logic (sync_tencent_billing_summary.py, lines ~813-871)
The removal of_same_imported_month_totalsand direct comparison of net costs is correct. However, consider extracting the reconciliation condition to a clearly named function for clarity and future maintainability, e.g.:def _is_reconciled_to_real_cost(record: dict, imported_net: Decimal) -> bool: return record.get("imported_net_cost") == _decimal_text(imported_net)
This would improve readability at the call site and isolate reconciliation criteria.
-
Error message consistency (sync_tencent_billing_summary.py, line ~867)
The error message only mentions the net cost mismatch now, which matches the new logic. To improve debuggability if other cost fields evolve later, consider including the full record or relevant fields in the error logs. -
Avoid removing
_same_imported_month_totalsprematurely
The helper function was removed entirely. If there is any chance you may reintroduce list cost checks or multiple cost fields, consider keeping it or refactoring it as above.
Best Practices
-
Documentation Clarity (docs/tencent-billing-import-design.md)
The added explanations aroundTotalCostvsRealTotalCostare clear and helpful. Consider adding a short summary or a table early in the doc summarizing which Tencent API fields are authoritative for what purpose. This would help future maintainers quickly understand the data semantics. -
Test Naming and Parametrization (test_sync_tencent_billing_summary.py)
- The test
test_scheduled_run_reconciles_closed_month_real_cost_with_single_summary_requestnow only assertsmatched-real-cost-only. Consider parameterizing or adding separate tests if in future you support other reconciliation statuses. - The parametrize input was changed by removing
expected_statusbut left in the call signature. Clean up unused parameters for clarity.
- The test
-
Logging and Alerting (docs/tencent-billing-import-design.md)
The doc mentions alerting on "month-close net total does not reconcile" (corrected from previous "list or net"). Ensure that production monitoring/logging systems are updated accordingly to avoid alert noise due toTotalCostdifferences. -
Code Style
Minor: In the test file, ensure consistent blank lines before and after test functions for readability.
Summary:
This PR makes a necessary and well-executed correction to Tencent cost reconciliation logic by focusing on RealTotalCost. It improves correctness and documentation, has good test coverage, and removes now-incorrect handling of TotalCost. Minor improvements around helper functions, error logging, and documentation clarity would enhance maintainability and debuggability going forward.
There was a problem hiding this comment.
I have already done a preliminary review for you, and I hope to help you do a better job.
Summary
This PR modifies the Tencent billing reconciliation logic to rely solely on RealTotalCost for monthly reconciliation, treating TotalCost as an observational value without reconciliation impact. It updates documentation to clarify API semantics, adjusts reconciliation logic to compare only RealTotalCost, and adapts tests to reflect the new behavior. The changes improve correctness by aligning with Tencent's actual billing data semantics. The code changes are focused, well-documented, and accompanied by meaningful tests.
Critical Issues
- None found. The logic correctly changes the reconciliation gating to
RealTotalCostand removes comparison againstTotalCostas intended.
Code Improvements
-
Simplify reconciliation status logic (file:
sync_tencent_billing_summary.py, lines ~837-857)
The status is set to"matched-real-cost-only"regardless of whethersummary.total_costis None or not. SinceTotalCostis now purely observational, the code could remove the conditional status assignment and always use"matched-real-cost-only"when matched. This reduces complexity and potential confusion.Suggested refactor:
reconciled[month_key] = { "status": "matched-real-cost-only" if matched else "mismatch", "source_total_cost": ( _decimal_text(summary.total_cost) if summary.total_cost is not None else None ), "imported_list_cost": _decimal_text(imported_list), "imported_net_cost": _decimal_text(imported_net), }
-
Remove dead code (file:
sync_tencent_billing_summary.py, lines ~940-947)
The_same_imported_month_totalsfunction is deleted but still present in the diff. Ensure it is fully removed from the codebase. If referenced elsewhere, remove or refactor those calls.
Best Practices
-
Improve docstring or comment clarity (file:
sync_tencent_billing_summary.py, near reconciliation logic)
The comment explaining whyTotalCostis not used is helpful. Adding a brief summary in the function or module docstring about this reconciliation approach would help future maintainers understand this key design decision at a glance. -
Test coverage for edge cases with
TotalCostpresent but mismatched (file:test_sync_tencent_billing_summary.py)
The tests cover cases whereTotalCostis None or differs from component cost, but adding an explicit test case to confirm that whenTotalCostis present but mismatches, reconciliation still passes ifRealTotalCostmatches would reinforce the intended behavior. -
Consistent naming for reconciliation statuses
The new status"matched-real-cost-only"is clear, but consider defining constants or an Enum for reconciliation statuses to avoid magic strings and reduce typo risk.
Overall, the PR achieves its goal cleanly with good documentation and test updates. Addressing the minor refactoring and adding explicit test cases for edge cases would further improve maintainability and clarity.
|
/approve |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: dillon-zheng The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Summary
RealTotalCostonlyTotalCostas an observed, non-comparable valueValidation
cd cost-insight && python -m pytest -qcd cost-insight && python -m ruff check .Production evidence
RealCostexactly matched organizationRealTotalCost.TotalCost, and componentCostdiffered from monthlyTotalCost.No production fact rewrite is needed.