{
    "Comment": "Workflow for Publishing Compositions Changelog Report generation",
    "StartAt": "ReportPopulator",
    "States": {
      "ReportPopulator": {
        "Type": "Task",
        "Resource": "${report_populator_arn}",
        "ResultPath": "$.compositionCount",
        "Retry": [
          {
            "ErrorEquals": [
              "States.Timeout",
              "Lambda.ServiceException",
              "Lambda.AWSLambdaException",
              "Lambda.SdkClientException",
              "Lambda.Unknown",
              "Lambda.TooManyRequestsException"
            ],
            "BackoffRate": 2,
            "IntervalSeconds": 60,
            "MaxAttempts": 3,
            "Comment": "Retry on timeout"
          }
        ],
        "Catch": [
          {
            "ErrorEquals": [
              "States.ALL"
            ],
            "Next": "ReportCompiler",
            "ResultPath": "$.reportErrors",
            "Comment": "Error after retry"
          }
        ],
        "Next": "RowBuilderDecider"
      },
      "RowBuilderDecider": {
      "Comment": "Skip the row builder if no compositions to generate a report for",
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.compositionCount",
          "NumericEquals": 0,
          "Next": "ReportCompiler"
        }
      ],
      "Default": "RowBuilderFanout"
    },
      "RowBuilderFanout": {
        "Type": "Map",
        "MaxConcurrency": 50,
        "Label": "RowBuilderFanout",
        "ToleratedFailurePercentage": 100,
        "Next": "ReportCompiler",
        "ItemReader": {
          "Resource": "arn:aws:states:::s3:getObject",
          "ReaderConfig": {
            "InputType": "JSON"
          },
          "Parameters": {
            "Bucket": "${bucket_name}",
            "Key.$": "States.Format('changelog-reports/{}/compositions.json', $$.Execution.Input.reportId)"
          }
        },
        "ItemBatcher": {
          "MaxItemsPerBatch": 20,
          "BatchInput": {
            "reportId.$": "$$.Execution.Input.reportId",
            "reportType.$": "$$.Execution.Input.reportType",
            "createdBy.$": "$$.Execution.Input.createdBy",
            "reportStartDate.$": "$$.Execution.Input.reportStartDate",
            "reportEndDate.$": "$$.Execution.Input.reportEndDate"
          }
        },
        "Iterator": {
          "StartAt": "RowBuilder",
          "ProcessorConfig": {
            "Mode": "DISTRIBUTED",
            "ExecutionType": "STANDARD"
          },
          "States": {
              "RowBuilder": {
                "Type": "Task",
                "Resource": "${row_builder_arn}",
                "Parameters": {
                  "reportId.$": "$.BatchInput.reportId",
                  "reportType.$": "$.BatchInput.reportType",
                  "reportStartDate.$": "$.BatchInput.reportStartDate",
                  "reportEndDate.$": "$.BatchInput.reportEndDate",
                  "createdBy.$": "$.BatchInput.createdBy",
                  "compositionIds.$": "$.Items"
                },
                "Retry": [
                  {
                    "ErrorEquals": [
                      "States.ALL"
                    ],
                    "IntervalSeconds": 2,
                    "MaxAttempts": 6,
                    "BackoffRate": 2
                  }
                ],
                "End": true
              }
          }
        },
        "ResultPath": null,
        "Catch": [
          {
            "ErrorEquals": [
              "States.ALL"
            ],
            "Next": "ReportCompiler",
            "ResultPath": "$.reportErrors",
            "Comment": "For all report errors"
          }
        ]
      },
      "ReportCompiler": {
        "Type": "Task",
        "Resource": "${report_compiler_arn}",
        "Retry": [
          {
            "ErrorEquals": [
              "States.Timeout",
              "Lambda.ServiceException",
              "Lambda.AWSLambdaException",
              "Lambda.SdkClientException",
              "Lambda.Unknown",
              "Lambda.TooManyRequestsException"
            ],
            "BackoffRate": 2,
            "IntervalSeconds": 60,
            "MaxAttempts": 3,
            "Comment": "Retry on timeout"
          }
        ],
        "End": true
        }
    }
}
