Return ouput instead of using a hard true or false | Watcher

Hi all,

Is there a way I can return my new array "list" instead of hard coding true?
I know they both will come to the same outcome, however, I'm trying my best to not hard code anything which isn't necessary

// Watcher condition thus far

Thank you :slight_smile:

hey,

slight workaround: you can just set a variable in the payload like ctx.payload.foo = new_list and then return true or false for the condition to match.

--Alex

Hi @spinscale
Can you please show me with an example?
I still don't completely understand this

Thanks,
Nhung

"condition": {
    "script": {
      "source": "ctx.payload.foo = 'bar' ; return true"
   }
}

Hi @spinscale

I have that already - it's just truncated in my gist on github.
ctx.payload.array_new = list; return true;

My question was more if I could just return "list" instead of "true". I don't think my code currently does that, however I believe you can and was hoping for advice on how I can achieve this.

I found this example online for reference from A Brief Painless Walkthrough

then I do not understand your question. A condition requires a boolean to be returned in order to trigger the actions or not, and you can access the newly created data structure in your actions.

The above example is something completely different than a watcher condition and therefore returns something completely different - that is relevant in that context.

What else is needed here?

Sorry about that

Question: Is there anyway I can access the list without a ctx.payload?

Thank you,
Nhung

you need to store it somewhere that it can be passed around the execution, and that place is the payload or ctx.vars in the so called watch execution context.

Again, what is the use-case, that cannot be covered at the moment? If I understand this, I may be able to help further instead of asking around :slight_smile:

Use-case: Create an alert to notify me of which customer has not received anything. If doc_count is 0, this means nothing has been sent to the client. The action should call upon the client name.

// Console + Output thus far:

I feel like I am missing something in my condition, however I don't know what. When I play around in the "action", by doing {{ctx.payload.array_new}}, for example, I always get error messages. I have put in a simple text action for the time being.

Thanks for always helping by the way! I really appreciate it

can you include {{ctx.payload}} in the logging output instead of a non templated text and rerun? This way you can see if that array is part of your payload.

PUT _watcher/watch/testing
{
  "trigger": {
    "schedule": {
      "interval": "5m"
    }
  },
  "input": {
    "search": {
      "request": {
        "indices": "edmetricdata*",
        "body": {
          "query": {
            "bool": {
              "must": [
                {
                  "range": {
                    "metricdate": {
                      "gte": "now-1h"
                    }
                  }
                },
                {
                  "exists": {
                    "field": "managedobjectref.keyword"
                  }
                }
              ]
            }
          },
          "aggs": {
            "managedobjectref": {
              "terms": {
                "field": "managedobjectref.keyword"
              },
              "aggs": {
                "timerange": {
                  "range": {
                    "field": "metricdate",
                    "ranges": [
                      {
                        "from": "now-30m"
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": """
def list = []; 
for (int j=0;j<ctx.payload.aggregations.managedobjectref.buckets.size();j++){
  
 if(ctx.payload.aggregations.managedobjectref.buckets[j].timerange.buckets[0].doc_count == 0){
list.add(ctx.payload.aggregations.managedobjectref.buckets[j]);
}
}
ctx.payload.array_new = list;

return ctx.payload.array_new.size() == 0;
"""
    }
  },
  "actions": {
    "log": {
      "logging": {
        "text": "{{ctx.payload.array_new}}"
      }
    }
  }
}

// Executed output

Thanks in advanced :slight_smile:

I asked for {{ctx.payload}} as this would allow us if there is an empty object.

For debugging, please remove the if statement and just write something into that array. Just try def list = [1,2,3,4] ; ctx.payload.array_new = list

Also your condition logic is flawed. You return true, if the list is empty and then have an empty list in your logging statement

Got the solution I was looking for

// For future people reading this:

  "condition": {
    "script": {
      "source": """
def list = []; 
for (int j=0;j<ctx.payload.aggregations.forward_to.buckets.size();j++){
  
 if(ctx.payload.aggregations.forward_to.buckets[j].time_range.buckets[0].doc_count == 0){
list.add(ctx.payload.aggregations.forward_to.buckets[j]);
}
}
ctx.payload.array_new = list;
return ctx.payload.array_new.size() == 0;
"""
    }
  }
1 Like

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