How do I use a Condition to Match Against a Value in a Loop from ctx.payload

On the bottom of this example, in the actions section, I sort through all the snapshots in an S3 bucket, end goal is to send an alert to victorops if I have a failed snapshot. I finally figured out how to write out every state of each snapshot, but can't figure out how to do a compare to show only the ones that have failed. Here is an example of what I'm trying to do that currently does not work.

{
	"trigger": {
		"schedule": {
			"interval": "60s"
		}
	},
	"input": {
		"http": {
			"request": {
				"scheme": "http",
				"host": "analytics.devc.openwhere.net",
				"port": 9200,
				"method": "get",
				"path": "/_snapshot/s3_repository/_all",
				"params": {},
				"headers": {}
			}
		}
	},
	"condition": {
		"compare": {
			"{{#ctx.payload.snapshots}}{{state}}{{/ctx.payload.snapshots}}": {
				"eq": "FAILED"
			}
		},
	"actions": {
			"logging": {
				"logging": {
					"level": "info",
					"text": " Snapshot Sate: {{#ctx.payload.snapshots}}{{state}}{{/ctx.payload.snapshots}}"
				}
			}
		}
	}
}

The other questions is even when I get the compare to work would it print every snapshot state or just the ones that matched the condition?

CC @spinscale

The conditions needs to check if any snapshots are marked as failed.

Then you need an script transform in your action, that filters for those that are failed, something like this (written on top of my head)

return [ 'snapshots' : ctx.payload.snapshots.stream().filter(s -> s.state == "FAILED").collect(Collectors.toList()) ]

hope that helps.

--Alex

Yes and thank you. I used it the following way in an action:

"actions": {
    "logging": {
      "transform": {
        "script": {
          "source": "return ['failedones' : ctx.payload.snapshots.stream().filter(s -> s.state == 'FAILED').collect(Collectors.toList()) ]",
          "lang": "painless"
        }
      },
      "logging": {
        "level": "info",
        "text": "The following snapshot failed: {{#ctx.payload}}{{failedones}}{{/ctx.payload}}"
      }
    }
  }
1 Like

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