Report file fetching/storing between container and local filesystem and report decoding fixes.#18
Conversation
…tainer and local filesystem. Also fixed report file parsing.
Reviewer's Guide by SourceryThis pull request introduces several key improvements and fixes to the MT5 report parsing process. It allows the Sequence diagram for MT5 report processingsequenceDiagram
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
Updated class diagram for parse_mt_report.pyclassDiagram
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"
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Warning
|
There was a problem hiding this comment.
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.pyscript 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
argparseimport to enable standalone execution. - Modified
write_optfunction 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_optfunction, 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.
- Added
- 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
lookupcalls to use the temporary file/tmp/mt5_report.htmlinstead of the original report file path. - Changed the extension of the Header report from CSV to JSON.
- Added
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
-
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. ↩
There was a problem hiding this comment.
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
slurpaction. - 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.
There was a problem hiding this comment.
Hey @nseam - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding error handling or logging within the
write_optfunction to better diagnose issues with malformed input files. - The
mainfunction inparse_mt_report.pyreturns 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
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
802ee1d to
61b6018
Compare
61b6018 to
b4ca883
Compare
1fbf521 to
9abfc2d
Compare
6af1b33 to
e8208ff
Compare
e8208ff to
1865858
Compare
Summary by Sourcery
Improve MetaTrader 5 report parsing and file handling in the Ansible lookup plugin
New Features:
Bug Fixes:
Enhancements:
Chores: