Skip to content

Reduce cyclomatic complexity of TimescaleDB export module#3463

Open
zfh468 wants to merge 11 commits into
nicolargo:developfrom
zfh468:refactor-timescaledb-complexity
Open

Reduce cyclomatic complexity of TimescaleDB export module#3463
zfh468 wants to merge 11 commits into
nicolargo:developfrom
zfh468:refactor-timescaledb-complexity

Conversation

@zfh468

@zfh468 zfh468 commented Mar 1, 2026

Copy link
Copy Markdown

Summary
Reduces the cyclomatic complexity of the TimescaleDB export module (glances/exports/glances_timescaledb/init.py) by simplifying control flow and extracting clearer logic boundaries.

Relates to #3460

Problem
The update() method contained duplicated logic for dict-based and list-based plugin stats handling, nested conditional branches, and inline preprocessing steps mixed with SQL preparation logic.

This resulted in increased cyclomatic complexity and reduced readability, making future maintenance more difficult.

Solution
Refactored the module to:

  • Simplify the data preprocessing logic
  • Reduce duplicated dict/list handling branches
  • Improve separation between preprocessing, schema generation, and value preparation
  • Reduce nesting depth and clarify control flow

The refactor preserves the existing SQL generation logic, table schema structure, segmentation behavior, and normalization rules.

Changes
glances/exports/glances_timescaledb/init.py:

  • Simplified update() method structure
  • Centralized preprocessing logic
  • Reduced duplicated branches
  • Improved readability without altering behavior

Testing

  • Linting & Formatting: Passed make format and ruff check .
  • Module Integrity: Verified with uv run python -m glances and manual import of glances_timescaledb to ensure no syntax or loading errors.
  • Logic Audit: Manually verified that _prepare_stats and _create_hypertable preserve the original SQL logic and data structures.
  • Security: Confirmed SQL queries now use parameterized inputs instead of f-strings.
    Note: Full pytest suite was partially limited locally due to environmental dependencies, but module integrity was verified via manual import.

Checklist

  • PR targets the develop branch.
  • Code follows PEP8 (verified via Ruff).
  • Successfully ran make format.
  • No functional regressions or schema changes.

@nicolargo nicolargo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Hi @zfh468

Thanks for the PR.

Some reworks should be done in order to accept it.

Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
Comment thread glances/exports/glances_timescaledb/__init__.py Outdated
@zfh468

zfh468 commented Mar 13, 2026

Copy link
Copy Markdown
Author

All original comments, documentation links, and TODOs have been restored. Code complexity and security issues are also addressed.

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.

2 participants