Skip to main content

Generate State Workflow

#

Generate State Documentation

Overview

## Overview

**User Code:** `com.finmars.standard-workflow:generate-state`state

This script is designed to generate state files for state managers. It processes input data to create a structured state file that includes information about workers and their respective states.

##

Workflow Function

The main `workflow`workflow function is a task that generates the state file based on the provided payload.

###

Parameters:

Parameters:
-
    `payload`
  • payload: A dictionary containing the necessary information for state generation.

  • ###

Process:
1.

  1. Validates the presence of a payload
    2.
  2. Retrieves the input data from the specified path
    3.
  3. Generates a new state file using the `generate_state_file`generate_state_file function
    4.
  4. Saves the generated state file
    5.
  5. Updates the status of the operation in the state file

  6. ##

State File Generation

The `generate_state_file`generate_state_file function creates the structure of the state file.

###

State File Structure:
```json

{
  "user_code": "string",
  "configuration_code": "string",
  "name": "string",
  "status": "string",
  "schedule": "object",
  "workers": [
  {
  "order": "integer",
  "configuration_code": "string",
  "name": "string",
  "user_code": "string",
  "status": "string",
  "state_type": "string",
  "data_options": "object",
  "import_options": "object",
  "calculation_options": "object",
  "download_options": "object",
  "state_options": "object",
  "state": "object"
  }
  ]
}
```

###

Worker State Types:

1.

    **Fixed**
  1. Fixed: A single state for the worker.
    2.
  2. **Files**
  3. Files: Multiple states based on files in a specified directory.
    3.
  4. **Period**
  5. Period: Multiple states based on date ranges and portfolios.

  6. ##

Field Values

###

General Fields:
-

`status`
  • status: "to-do", "in-progress", "done", "error"
    -
  • `state_type`
  • state_type: "fixed", "files", "period"

  • ###

Period-specific Fields:
-

`periodicity`
  • periodicity: "daily", "weekly", "monthly"
    -
  • `date_from`
  • date_from: A date string ("YYYY-MM-DD") or "first_transaction_date"
    -
  • `date_to`
  • date_to: A date string ("YYYY-MM-DD") or "now"

  • ###

Download Options:
-

`type`
  • type: "day" or other (assumed to be a period)

  • ##

Helper Functions

1.

    `get_first_transaction_date`
  1. get_first_transaction_date: Retrieves the earliest transaction date from the API.
    2.
  2. `get_next_date`
  3. get_next_date: Calculates the next date based on the given periodicity.
    3.
  4. `get_period_end_date`
  5. get_period_end_date: Calculates the end date of a period based on the start date and periodicity.
    4.
  6. `build_url`
  7. build_url: Constructs the full API URL for a given endpoint.
    5.
  8. `get_headers`
  9. get_headers: Generates headers with authorization token for HTTP requests.
    6.
  10. `log_message`
  11. log_message: Logs a message with a timestamp.
    7.
  12. `save_file`
  13. save_file: Saves data to a specified file path in JSON format.
    8.
  14. `get_files`
  15. get_files: Lists files in the specified directory.
    9.
  16. `get_folders`
  17. get_folders: Lists directories in the specified path.
    10.
  18. `get_data`
  19. get_data: Reads data from a JSON file.

  20. ##

Error Handling

The script includes error handling for various scenarios, such as missing payloads, file read/write errors, and API communication issues. Errors are logged and, where appropriate, propagated to the state file.

##

Notes

Notes

    -

  • The script uses the `finmars`finmars module for storage operations and API communication.
    -
  • Date handling is done using the `pandas`pandas library for consistency and ease of manipulation.
    -
  • The script is designed to be flexible, handling different worker state types and periodicities.