cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Looping through items in a collection of records - Workflows

What I need is to loop through all of the records from a DQL output and have it formatted in a human-readable format. 

Here is my DQL query

timeseries percent = avg(dt.host.disk.used.percent), by: { dt.entity.host, dt.entity.disk }, filter: { in(dt.entity.host, classicEntitySelector("type(host),tag(\"Business Unit:FINANCE\")")) }
| fields util = abs(arrayAvg(percent)), entityName(dt.entity.host)
| fieldsRename dt.entity.host.name, Host

 

...which if I use get_disk_stats.records, equivalent to {{ result("get_disk_stats")["records"] }}, I get the following output:

 

[
  {
    "Host": "WIN11023.corporate.org",
    "util": 73.14564947430772
  },
  {
    "Host": "WIN19876.corporate.org",
    "util": 49.68959838117046
  },
  {
    "Host": "WIN11234.corporate.org",
    "util": 37.80735823445836
  },
  {
    "Host": "WIN15678.corporate.org",
    "util": 69.33072939353247
  },
  {
    "Host": "WIN19987.corporate.org",
    "util": 37.659657944489574
  },
  {
    "Host": "WIN12045.corporate.org",
    "util": 42.12913605995685
  },
  {
    "Host": "WIN13456.corporate.org",
    "util": 0.5479891637632675
  }
]

 

How do I code the for loop within the input itself or in the 'Options' menu?

 

2 REPLIES 2

ChristopherHejl
Dynatrace Advisor
Dynatrace Advisor

You can use a for loop in any input (with the exception of javascript task, there you would natively loop) in order to generate the message. Here would be an example with the data you provided:

Here is the email.

You can use for expressions as well as if/else or any combination.

Host utilization summary:
{% for record in result("get_disk_stats")["records"] %}
	- {{ record["Host"] }}: {{ record["util"] }}
{% endfor %}

You can also use other expressions at any place.
Eg scanned bytes: {{ result("get_disk_stats")["metadata"]["grail"]["scannedBytes"] }}

 

 

ChristopherHejl_1-1719578336007.png

 

absolute genius, thank you so much! Have a good weekend! 🙂

Featured Posts