Skip to content

Report file fetching/storing between container and local filesystem and report decoding fixes.#18

Open
nseam wants to merge 6 commits intoEA31337:dev-mt-report-parsingfrom
nseam:dev-mt-report-parsing-slurp-fix
Open

Report file fetching/storing between container and local filesystem and report decoding fixes.#18
nseam wants to merge 6 commits intoEA31337:dev-mt-report-parsingfrom
nseam:dev-mt-report-parsing-slurp-fix

Conversation

@nseam
Copy link
Contributor

@nseam nseam commented Mar 26, 2025

Summary by Sourcery

Improve MetaTrader 5 report parsing and file handling in the Ansible lookup plugin

New Features:

  • Add command-line interface for the parse_mt_report.py script
  • Implement file fetching and temporary file creation for MT5 report processing

Bug Fixes:

  • Fix report parsing to handle cases with missing table data
  • Ensure consistent return types for different report parsing methods

Enhancements:

  • Modify report parsing to handle UTF-16LE encoded files with BeautifulSoup preprocessing
  • Add robust error handling and file reading mechanisms for MT5 report files

Chores:

  • Update Ansible tasks to use new file handling approach for MT5 reports

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 26, 2025

Reviewer's Guide by Sourcery

This pull request introduces several key improvements and fixes to the MT5 report parsing process. It allows the parse_mt_report.py script to be run as a standalone program, improving its usability and testability. It also addresses file handling by reading the MT5 report directly from the container's filesystem, decoding the content, and using a temporary file for processing. Additionally, it fixes decoding issues with OPT report files and handles cases where the table is not present, ensuring more robust and reliable report parsing.

Sequence diagram for MT5 report processing

sequenceDiagram
    participant Ansible Task
    participant MT5 Container
    participant parse_mt_report.py

    Ansible Task->>MT5 Container: Slurp report.html
    MT5 Container-->>Ansible Task: Returns base64 encoded content
    Ansible Task->>Ansible Task: Decode base64 content
    Ansible Task->>Ansible Task: Create temporary file /tmp/mt5_report.html
    Ansible Task->>Ansible Task: Fetch /tmp/mt5_report.html to control node
    Ansible Task->>parse_mt_report.py: Parse /tmp/mt5_report.html
    parse_mt_report.py-->>Ansible Task: Returns parsed data
    Ansible Task->>Ansible Task: Create CSV/JSON files
Loading

Updated class diagram for parse_mt_report.py

classDiagram
    class main{
        +input_file_path: str
        +output_file_path: str
        +include_titles: bool
        +type: str
        +return_string: bool
        +main(input_file_path: str, output_file_path: str, include_titles: bool, type: str, return_string: bool) : list
    }
    class write_opt{
        +input_file: str
        +output_file: str
        +include_titles: bool
        +return_string: bool
        +write_opt(input_file: str, output_file: str, include_titles: bool, return_string: bool) : list
    }
    class write_to_csv{
        +html_content: str
        +output_file: str
        +include_titles: bool
        +type: str
        +return_string: bool
        +write_to_csv(html_content: str, output_file: str, include_titles: bool, type: str, return_string: bool) : list
    }
    class write_to_json{
        +html_content: str
        +output_file: str
        +type: str
        +return_string: bool
        +write_to_json(html_content: str, output_file: str, type: str, return_string: bool) : list
    }
    class LookupModule{
        +run(terms: list, variables: dict, **kwargs) : list
    }
    note for main "Main function to parse MT report and extract data"
    note for write_opt "Function to write OPT report to CSV"
    note for write_to_csv "Function to write Orders/Deals report to CSV"
    note for write_to_json "Function to write Header report to JSON"
    note for LookupModule "Lookup module for Ansible"
Loading

File-Level Changes

Change Details Files
Added the ability to execute the script as a standalone program using argparse.
  • Added argparse import.
  • Implemented an argument parser to handle command-line arguments.
  • Added a main execution block to parse arguments and call the main function.
lookup_plugins/parse_mt_report.py
Improved handling of the MT5 report file by reading it directly from the container's filesystem, decoding it, and using a temporary file for processing.
  • Added a task to slurp the MT5 report file from the container.
  • Added a task to create a temporary file with the decoded content of the MT5 report.
  • Modified the lookup calls to use the temporary file as input.
tasks/main.yml
Fixed decoding issues with the OPT report files and handled cases where the table is not present.
  • Read the OPT file with 'utf-16le' encoding and parsed it using BeautifulSoup to handle decoding issues.
  • Handled the case where the table element is not found in the OPT report, providing default values.
lookup_plugins/parse_mt_report.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link

coderabbitai bot commented Mar 26, 2025

Warning

.coderabbit.yaml has a parsing error

The CodeRabbit configuration file in this repository has a parsing error and default settings were used instead. Please fix the error(s) in the configuration file. You can initialize chat with CodeRabbit to get help with the configuration file.

💥 Parsing errors (1)
Validation error: Unrecognized key(s) in object: 'version'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @nseam, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request focuses on improving the handling and decoding of MetaTrader 5 (MT5) report files within the Ansible automation. The primary goal is to correctly process and convert MT5 reports (specifically the 'opt' type) into usable CSV and JSON formats. The changes address encoding issues, handle cases where the report table is missing, and ensure the report file is accessible to the parsing script. Additionally, the pull request introduces an argument parser to allow the script to be run as a standalone.

Highlights

  • Encoding Fix for OPT Reports: The code now reads the 'opt' report file with 'utf-16le' encoding and uses BeautifulSoup to prettify the content before parsing it as XML. This resolves issues caused by incorrect encoding.
  • Handling Missing Tables in OPT Reports: The code now checks if the table element exists in the 'opt' report. If the table is missing, it creates a default CSV with headers and an empty data row, preventing errors.
  • File Access and Storage: The Ansible tasks now slurp the MT5 report file from the container, store it in a temporary file, and fetch it to the control node. This ensures the parsing script has access to the report file, regardless of its location.
  • Report Conversion: The Ansible tasks convert the MT5 report into CSV (Orders, Deals, Optimization) and JSON (Header) formats.
  • Standalone Execution: The parse_mt_report.py script can now be executed as a standalone script with arguments for input file, output file, report type, include titles, and return string.

Changelog

Click here to see the changelog
  • lookup_plugins/parse_mt_report.py
    • Added argparse import to enable standalone execution.
    • Modified write_opt function to read the input file with 'utf-16le' encoding and prettify the content using BeautifulSoup.
    • Added a check for a missing table element in the write_opt function, creating a default CSV if the table is not found.
    • Modified the return value of the main function to return a list.
    • Added a main block to allow the script to be run as a standalone script with arguments.
  • tasks/main.yml
    • Added WINEDLLOVERRIDES: "mshtml=" to the environment variables for the MT5 terminal command.
    • Added tasks to slurp the MT5 report file from the container, store it in a temporary file, and fetch it to the control node.
    • Modified the lookup calls to use the temporary file /tmp/mt5_report.html instead of the original report file path.
    • Changed the extension of the Header report from CSV to JSON.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Trivia time!

What is the primary use case for the BeautifulSoup library?

Click here for the answer
BeautifulSoup is primarily used for web scraping purposes, to parse HTML and XML documents.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces fixes for reporting file handling and decoding, specifically addressing issues related to storing and retrieving files between the container and the local filesystem, as well as decoding problems. The changes include modifications to the parse_mt_report.py file and the tasks/main.yml file. The addition of argparse in parse_mt_report.py and the if __name__ == "__main__" block allows the script to be run as a standalone program. The changes in tasks/main.yml involve adding a WINEDLLOVERRIDES environment variable and modifying the report processing logic to use a temporary file.

Summary of Findings

  • File Handling in Ansible Task: The Ansible task now involves reading the MT5 report file, creating a temporary file, and then fetching it to the control node. This approach introduces additional steps and potential overhead. Consider if the intermediate steps are necessary or if the report can be directly processed from the initial slurp action.
  • Error Handling in Standalone Script: The standalone script (if __name__ == "__main__") includes basic error handling, but it could be improved by providing more specific error messages and potentially logging the errors to a file. This would aid in debugging and troubleshooting.
  • Redundant File Fetch: The task fetches the report file to the control node after creating a temporary file. This step might be redundant if the report processing can be done directly on the target node. Removing the fetch step could simplify the task and reduce unnecessary file transfers.

Merge Readiness

The pull request introduces important fixes related to file handling and report decoding. However, there are some areas that could be improved, such as the file handling logic in the Ansible task and the error handling in the standalone script. I recommend addressing these points before merging. I am unable to approve this pull request, and other reviewers should review and approve this code before merging.

@nseam nseam changed the title Report file fetinch/storing between container and local filesystem and report decoding fixes. Report file fetching/storing between container and local filesystem and report decoding fixes. Mar 26, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @nseam - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding error handling or logging within the write_opt function to better diagnose issues with malformed input files.
  • The main function in parse_mt_report.py returns a list, but the return value is only used when calling the function from the command line, so the return type should be changed to a string.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@nseam nseam force-pushed the dev-mt-report-parsing-slurp-fix branch 2 times, most recently from 802ee1d to 61b6018 Compare March 27, 2025 17:38
@nseam nseam force-pushed the dev-mt-report-parsing-slurp-fix branch from 61b6018 to b4ca883 Compare March 27, 2025 18:58
@nseam nseam force-pushed the dev-mt-report-parsing-slurp-fix branch 3 times, most recently from 1fbf521 to 9abfc2d Compare April 8, 2025 15:19
@nseam nseam force-pushed the dev-mt-report-parsing-slurp-fix branch 2 times, most recently from 6af1b33 to e8208ff Compare April 11, 2025 13:16
@nseam nseam force-pushed the dev-mt-report-parsing-slurp-fix branch from e8208ff to 1865858 Compare April 11, 2025 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants