Multiple chain inputs and foreach

I have following basic watcher.

{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "chain": {
      "inputs": [
        {
          "first_input": {
            "http": {
              "request": {
                "url": "http://example.com/first_input",
                "params": {},
                "headers": {}
              }
            }
          }
        },
        {
          "second_input": {
            "http": {
              "request": {
                "url": "http://example.com/second_input",
                "params": {},
                "headers": {}
              }
            }
          }
        }
      ]
    }
  },
  "condition": {
    "script": {
      "source": "for (def item : ctx.payload.first_input) { if (item.some_field > 10) { return true; } } return false;",
      "lang": "painless"
    }
  },
  "actions": {
    "send_email": {
      "email": {
        "to": "email@example.com",
        "subject": "Watcher Triggered",
        "body": {
          "text": "The watcher was triggered."
        }
      }
    }
  }
}


I want to run second_input in foreach for the total count of first_input.

So i changed the code like this for second_input

second_input": {
           "foreach": "ctx.payload.first_input.hits.hits",
           "max_iterations": 50,
            "http": {
              "request": {
                "url": "http://example.com/second_input",
                "params": {},
                "headers": {}
              }
            }
          }

As soon i try to save it i get this error.

could not parse input for watch [chain_example]. expected an object representing input [foreach], but found [VALUE_STRING] instead

But if i remove that for each code and print in log following it , it shows total count.

Total {{ctx.payload.first_input.hits.total}} shows # i.e 12

How can i fix this ?

The foreach is only for actions, not inputs

Maybe describe the use case of what you're trying to do - perhaps there's a different way of doing it.

ok let me explain . Please let me know if its unclear.

{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "chain": {
      "inputs": [
        {
          "first_input": {
            // search query gets resutls from heartbeat index and
// aggregates on monitor.name whose status is down for last 15 mins
        },
        {
          "second_input": {
           
           //for each document found in above query second input fires
// a get request to by passing  monitor.name as query param to restapi
// and fetchs data for given monitor.name ,so if 2 documents were found 
//in first query second input will fire 2 times rest query with respective
 //monitor.name as query param.
          }
        }.
        {
           " third input" : {
            // transform the payload from first and second and create an array
// object  from first payload get monitor.name and second payload get value of 
// flag 
           }
         }
        
      ]
    }
  },
  "condition": {
    "script": {
      // for loop to iterate through third payload array
      // if payload has flag = True value then take action. 
      // may be this condition can be in actions i think instead of here. 
    }
  },
  "actions": {
    "send_email": {
    "condition": {
            "script": {
            //  if payload has flag = True value then take action.
            }
          },
      "foreach": "ctx.payload._value",
      "email": {
        "to": "email@example.com",
        "subject": "Watcher Triggered",
        "body": {
          "text": "The watcher was triggered."
        }
      }
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.