Skip to content

fix(cost-insight): reconcile Tencent monthly real cost only - #668

Merged
dillon-zheng merged 3 commits into
PingCAP-QE:mainfrom
dillon-zheng:fix/tencent-month-real-cost-reconciliation
Sep 17, 2026
Merged

dillon-zheng merged 3 commits into
PingCAP-QE:mainfrom
dillon-zheng:fix/tencent-month-real-cost-reconciliation

Conversation

@dillon-zheng

Copy link
Copy Markdown
Contributor

Summary

  • reconcile Tencent monthly billing against RealTotalCost only
  • retain TotalCost as an observed, non-comparable value
  • document the validated organization detail API semantics

Validation

  • cd cost-insight && python -m pytest -q
  • cd cost-insight && python -m ruff check .

Production evidence

  • August component RealCost exactly matched organization RealTotalCost.
  • The detail API did not expose a detail-level TotalCost, and component Cost differed from monthly TotalCost.

No production fact rewrite is needed.

@ti-chi-bot

ti-chi-bot Bot commented Sep 17, 2026

Copy link
Copy Markdown

I Skip it since the diff size(227362 bytes > 80000 bytes) is too large

@ti-chi-bot ti-chi-bot Bot added the size/XXL label Sep 17, 2026
@dillon-zheng
dillon-zheng force-pushed the fix/tencent-month-real-cost-reconciliation branch from 41c17e6 to 85929c6 Compare September 17, 2026 09:59

@ti-chi-bot ti-chi-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 unreliable TotalCost and focuses on RealTotalCost.

Code Improvements

  • _reconcile_closed_months function (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:
      # if summary.total_cost is not None and imported_list != summary.total_cost:
      #     mismatches.append(f"list {imported_list} != {summary.total_cost}")
      Suggestion: Delete these lines entirely as the PR no longer uses TotalCost for reconciliation.
  • Clarify status "matched-real-cost-only" usage
    The current logic sets status "matched-real-cost-only" if the net cost matches and TotalCost is None. This might be confusing if TotalCost is sometimes present but ignored. Consider explicitly documenting or asserting that TotalCost presence does not affect reconciliation or always set "matched-real-cost-only" when matched on RealTotalCost.


Best Practices

  • Documentation clarity (docs/tencent-billing-import-design.md)

    • The added explanation on the difference between TotalCost and RealTotalCost is very helpful.
    • Suggest adding a short summary or note near the top of the document explaining that reconciliation now only considers RealTotalCost to help future maintainers quickly understand the change.
  • Test naming and parameterization (tests/test_sync_tencent_billing_summary.py)

    • The test test_scheduled_run_reconciles_closed_month_real_cost_with_single_summary_request was renamed appropriately.
    • The parametrize arguments removed expected_status and hard-coded "matched-real-cost-only". This is fine but consider also adding a test case where TotalCost is present but mismatches, to ensure reconciliation ignores it, to document behavior explicitly.
  • 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.

Summary of actionable changes:

  • Remove commented-out TotalCost reconciliation code in sync_tencent_billing_summary.py.
  • Add a brief summary note about RealTotalCost reconciliation near the top of tencent-billing-import-design.md.
  • Consider adding a test case where TotalCost is present but ignored to explicitly verify behavior.
  • Optionally, convert explanatory comments about Tencent API in sync_tencent_billing_summary.py into a docstring or link to official API docs if possible.

These changes will improve maintainability, clarity, and robustness of the reconciliation logic.

@ti-chi-bot ti-chi-bot Bot added size/M and removed size/XXL labels Sep 17, 2026

@ti-chi-bot ti-chi-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_totals and 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_totals prematurely
    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 around TotalCost vs RealTotalCost are 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_request now only asserts matched-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_status but left in the call signature. Clean up unused parameters for clarity.
  • 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 to TotalCost differences.

  • 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.

@ti-chi-bot ti-chi-bot Bot added size/L and removed size/M labels Sep 17, 2026

@ti-chi-bot ti-chi-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 RealTotalCost and removes comparison against TotalCost as 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 whether summary.total_cost is None or not. Since TotalCost is 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_totals function 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 why TotalCost is 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 TotalCost present but mismatched (file: test_sync_tencent_billing_summary.py)
    The tests cover cases where TotalCost is None or differs from component cost, but adding an explicit test case to confirm that when TotalCost is present but mismatches, reconciliation still passes if RealTotalCost matches 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.

@dillon-zheng

Copy link
Copy Markdown
Contributor Author

/approve

@ti-chi-bot

ti-chi-bot Bot commented Sep 17, 2026

Copy link
Copy Markdown

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added the approved label Sep 17, 2026
@dillon-zheng
dillon-zheng merged commit 825d500 into PingCAP-QE:main Sep 17, 2026
3 checks passed
@dillon-zheng
dillon-zheng deleted the fix/tencent-month-real-cost-reconciliation branch September 17, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant